Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #!/usr/bin/perl
  2. #
  3. # Script to convert fw-data.ini file to html
  4. #
  5. # Takes filename of ini file as input or defaults to fw-data.ini
  6. # Writes (bare) html to stdout
  7. #
  8. # Copyright (C) 2009, Niall Walsh <niallwalsh@users.berlios.de>
  9. #
  10. # This program is free software; you can redistribute it and/or
  11. # modify it under the terms of the GNU General Public License
  12. # as published by the Free Software Foundation; version 2 of the
  13. # License.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. use strict;
  20. # TODO add some options properly
  21. # -t|--template
  22. # insert generated content into given file
  23. # default value stdin
  24. # -p|--pattern
  25. # use given value as string to replace with content in template
  26. # implies -t
  27. # disables -w
  28. # default off value <!-- fw-data -->
  29. # -w|--wrap
  30. # use given value as root path to header/footer
  31. # disables -t/-p
  32. # default on value fw-html.
  33. # -i|--indent
  34. # indent all content by this string
  35. # automatically taken from template's pattern indent if -t
  36. # default value ""
  37. # -s|--space
  38. # use this string to add a level of indenting
  39. # default value "\t"
  40. # -l|--lang
  41. # only output the given language
  42. # default on value en
  43. # -m|--multi
  44. # output all languages
  45. # can be inline or block
  46. # block repeats the entire structure for each lang
  47. # inline makes each translated element a set of siblings
  48. # default value ? inline ?
  49. # default off
  50. # -o|--order
  51. # list of fields to output in order
  52. # default value drivers url fw ok note
  53. # -h|--header
  54. # what tag to use for the links wrapper elements in the index/toc
  55. # possible values none li dd div span
  56. # default value li
  57. # -c|--class
  58. # what tag to use to wrap the classes
  59. # possible values are none li dt div span
  60. # default value dt
  61. # -d|--device
  62. # what tag to use to wrap the classes
  63. # possible values are none li dt div span
  64. # default value dt
  65. # -b|--blacklist
  66. # list of classes to exclude from output
  67. # You might want to set these!
  68. my $indent="\t\t\t\t";
  69. my $level="\t";
  70. my $pattern='\<\!--\s+fw-data\s+\-\-\>';
  71. my $langpatt='\<\!--\s+fw-lang\s+\-\-\>';
  72. my @blacklist=('usb');
  73. my $liwrap='h2';
  74. my $dtwrap='strong';
  75. # For now you can give the language as only current argument
  76. my $lang="en";
  77. if (($ARGV[0]=~/^[a-z]{2}$/i)||($ARGV[0]=~/^[a-z]{2}\_[a-z]{2}$/i))
  78. {
  79. $lang="$ARGV[0]";
  80. shift(@ARGV);
  81. print STDERR "NOTE: $lang selected as language\n";
  82. }
  83. my %trans;
  84. if (-e $lang.'.i18n')
  85. {
  86. open(LANG,$lang.'.i18n')||die "failed to open $lang.i18n for reading : $!\n";
  87. my $tstring;
  88. while($tstring=<LANG>)
  89. {
  90. chomp($tstring);
  91. my($tname)=$tstring;
  92. my($tval)=$tstring;
  93. $tname=~s/\=.*$//;
  94. $tval=~s/$tname\=//;
  95. $trans{$tname}=$tval;
  96. }
  97. close(LANG);
  98. }
  99. my $file=$ARGV[0];
  100. if (length($file)==0)
  101. {
  102. $file='fw-data.ini';
  103. }
  104. unless (-f $file)
  105. {
  106. die "you must supply a valid filename to load from if fw-data.ini is not in the working directory";
  107. }
  108. # Using Config::Any instead would allow to switch to anything it supports
  109. #use Config::Any;
  110. #my $cfg = Config::Any->load_files({files => [ $file ] });
  111. #my $cards = $$cfg[0]{$file};
  112. use Config::Tiny;
  113. my $cards = Config::Tiny->read($file);
  114. # name and num are used to loop over the cards and build up:
  115. # @names is the list of sorted ini section names
  116. # %names is a hash of the ini section names to their sorted number
  117. # %class holds an array of sections in a class by number
  118. my $name;
  119. my $num=0;
  120. my (@names, %names, %class);
  121. # these are the fields we loop over in this order for content of output
  122. my (@fieldnames)=(
  123. 'drivers',
  124. 'url',
  125. 'fw',
  126. 'ok',
  127. 'note'
  128. );
  129. # go through the ini sections and build up [@%]names and $class
  130. foreach $name (sort(keys(%$cards)))
  131. {
  132. my $field;
  133. foreach $field (@fieldnames,'class','title')
  134. {
  135. $$cards{$name}{$field}=~s/^\s*\"\s*(.*?)\s*\"\s*$/$1/;
  136. }
  137. push(@names,$name);
  138. $names{$name}=$num;
  139. $$cards{$name}{'class'}='other' unless (length($$cards{$name}{'class'}));
  140. my @class = split(/\s+/,$$cards{$name}{'class'});
  141. my $class;
  142. my $classCSS;
  143. foreach $class (@class)
  144. {
  145. $classCSS=$class;
  146. $classCSS=~s/\./\_/g;
  147. push(@{$$cards{$name}{'_class'}},$classCSS);
  148. if (!(defined($class{$class})))
  149. {
  150. $class{$class}=[$num];
  151. }
  152. else
  153. {
  154. push(@{$class{$class}},$num);
  155. }
  156. }
  157. $num++;
  158. }
  159. sub closetags () {
  160. my $count=shift();
  161. unless ($count)
  162. {
  163. unshift(@{$_[0]},$count);
  164. $count=@{$_[0]};
  165. }
  166. $count = @{$_[0]} - $count;
  167. while (@{$_[0]} > $count)
  168. {
  169. print $indent;
  170. my $loop;
  171. for ($loop=1;$loop<@{$_[0]};$loop++) {
  172. print $level;
  173. }
  174. print pop(@{$_[0]})."\n";
  175. }
  176. }
  177. sub writefile () {
  178. my $file=shift();
  179. if ( -r $file )
  180. {
  181. open(IN,$file)||die "failed to open $file : $!";
  182. my $line;
  183. while ($line=<IN>)
  184. {
  185. print "$line";
  186. }
  187. close(IN);
  188. }
  189. }
  190. &writefile('fw-html.header');
  191. my $buffer='';
  192. while ($buffer=<STDIN>)
  193. {
  194. $indent='';
  195. $buffer=~s/$langpatt/$lang/;
  196. if ($buffer=~/$pattern/)
  197. {
  198. $indent=$buffer;
  199. $indent=~s/$pattern.*$//;
  200. chomp($indent);
  201. $buffer=~s/$indent$pattern//;
  202. last;
  203. }
  204. else
  205. {
  206. print $indent.$buffer;
  207. }
  208. }
  209. # go through the classes to put them in their own section
  210. my $class;
  211. my $classCSS;
  212. # print $indent . '<ul id="fwul">'."\n";
  213. # my $liin='';
  214. # my $liout='';
  215. # if (length($liwrap))
  216. # {
  217. # $liin='<' . $liwrap . '>';
  218. # $liout='</' . $liwrap . '>';
  219. # }
  220. # CLASSLI: foreach $class (sort(keys(%class)))
  221. # {
  222. # my $test;
  223. # foreach $test (@blacklist)
  224. # {
  225. # next CLASSLI if ($class=~/^$test$/i);
  226. # }
  227. # $classCSS=$class;
  228. # $classCSS=~s/\./\_/g;
  229. # print $indent . $level . '<li class="fwindex">' . $liin . '<a href="#' . $classCSS . '">' . $class . '</a>' . $liout . '</li>' . "\n";
  230. # }
  231. # print $indent . '</ul>' . "\n";
  232. my @close;
  233. print $indent . '<dl class="firmware">' . "\n";
  234. push(@close,'</dl>');
  235. my $dtin='';
  236. my $dtout='';
  237. if (length($dtwrap))
  238. {
  239. $dtin='<' . $dtwrap . '>';
  240. $dtout='</' . $dtwrap . '>';
  241. }
  242. CLASSDD: foreach $class (sort(keys(%class)))
  243. {
  244. my $test;
  245. foreach $test (@blacklist)
  246. {
  247. next CLASSDD if ($class=~/^$test$/i);
  248. }
  249. $classCSS=$class;
  250. $classCSS=~s/\./\_/g;
  251. print $indent . $level . '<dt class="' . $classCSS . '" id="' . $classCSS . '">' . $dtin . '<a name="' . $class . '">' . $class . '</a>' . $dtout . '</dt>' . "\n" .
  252. $indent . $level . '<dd class="devices">' . "\n" .
  253. $indent . $level . $level . '<dl class="class">' . "\n";
  254. # does layer them out on close, and could elsewhere if required
  255. push(@close,'</dd>','</dl>');
  256. my $prefix=$level.$level.$level;
  257. # will have the name and number of the ini section
  258. my $dname;
  259. my $dnum;
  260. # the ini sections in the class
  261. foreach $dnum (@{$class{$class}})
  262. {
  263. my $dname = $names[$dnum];
  264. my $classes=$$cards{$dname}{'class'};
  265. $classes=~s/\./\_/;
  266. my $title=$$cards{$dname}{'title'};
  267. if ((length($lang))&&(defined($$cards{$dname}{join('_','title',$lang)})))
  268. {
  269. $title=$$cards{$dname}{join('_','title',$lang)};
  270. }
  271. $title.=' '.$trans{'class'};
  272. if ($classes=~/\s*usb\s*/)
  273. {
  274. $title.=' '.$trans{'class_usb'};
  275. }
  276. print $indent . $prefix . '<dt class="device ' . $classes . '" id="' . $dname .'">' . $title . '</dt>' . "\n" . $indent . $prefix . '<dd>' . "\n$indent" . $prefix . $level . '<dl class="dvals">' . "\n";
  277. push(@close,'</dd>','</dl>');
  278. my $field;
  279. # the main fields in the ini section
  280. foreach $field (@fieldnames)
  281. {
  282. if (defined($$cards{$dname}{$field})) {
  283. my @content=();
  284. my @terms=();
  285. my $content=$$cards{$dname}{$field};
  286. if ((length($lang))&&($field=~/^note$/))
  287. {
  288. if (defined($$cards{$dname}{join('_',$field,$lang)}))
  289. {
  290. $content=$$cards{$dname}{join('_',$field,$lang)};
  291. $content=~s/^\s*\"\s*(.+?)\s*\"\s*$/$1/;
  292. }
  293. }
  294. foreach $content (split(/\s+/,$content))
  295. {
  296. if (($content=~/\ /)||($content!~/^[fh]t+ps?\:\/\//))
  297. {
  298. push(@content,$content) if (length($content));
  299. }
  300. else
  301. {
  302. push(@content,'<a href="' . $content . '">' . $content . '</a>');
  303. }
  304. }
  305. if ($field=~/^fw$/)
  306. {
  307. if ((@content==2)&&($content[0]=~/^\<a\ href\=\"/)&&($content[1]!~/^\<a\ href\=\"/))
  308. {
  309. $content[1]=$trans{'fw_link_target_pre'} . ' <span class="target">' . $content[1] . '</span> ' . $trans{'fw_link_target_post'};
  310. $content[0]=$trans{'fw_link'}.' <span class="link">' . $content[0] .'<br />'. $content[1] . '</span>';
  311. $content[1]='';
  312. }
  313. elsif ($content[0]=~/^\<a\ href\=\"/)
  314. {
  315. $content[0]=$trans{'fw_link'}.' <span class="link">' . $content[0] . '</span>';
  316. }
  317. unshift(@terms,$trans{'fw'});
  318. }
  319. elsif (($field=~/^ok$/)&&(@content))
  320. {
  321. my $cc;
  322. my (@newc)=();
  323. my $fwdir='/lib/firmware';
  324. if ($content[0]=~/^\//)
  325. {
  326. $fwdir=shift(@content);
  327. }
  328. for ($cc=0; $cc<@content; $cc++)
  329. {
  330. push(@newc,'<li>' . $fwdir . '/' . $content[$cc] . '</li> ');
  331. }
  332. if (@newc)
  333. {
  334. $newc[0]='<ul>'.$newc[0];
  335. $newc[@newc-1].='</ul>';
  336. }
  337. @content=(@newc);
  338. unshift(@terms,$trans{'ok'});
  339. }
  340. elsif ($field=~/^drivers$/)
  341. {
  342. unshift(@terms,$trans{'drivers'});
  343. }
  344. elsif ($field=~/^url$/)
  345. {
  346. unshift(@terms,$trans{'url'});
  347. }
  348. elsif ($field=~/^note$/)
  349. {
  350. unshift(@terms,$trans{'note'});
  351. }
  352. print $indent . $prefix . $level . $level . '<dt class="' . $field . '">'. join(' ',@terms) .'</dt>' . "\n" .
  353. $indent . $prefix . $level . $level . '<dd>' . join(' ',@content) . '</dd>' . "\n" if (@content);
  354. }
  355. }
  356. &closetags(2, \@close);
  357. }
  358. &closetags((@close-1), \@close);
  359. }
  360. &closetags((@close*1),\@close);
  361. &writefile('fw-html.footer');
  362. print $buffer;
  363. while ($buffer=<STDIN>)
  364. {
  365. print $buffer;
  366. }