Debian Codegraph @

tools/translatedoc.pl

raw
1 #!/usr/bin/perl -w
2 #
3 # Bacula Systems - Philippe Chauvat
4 # 27 jul 2012
5 # 24-apr-2019: Adding font awesome css file
6 #
7 # This script is designed to translate Bacula enterprise LaTeX2HTML
8 # documentation files to something more "tunable" / "adaptable" from a CSS
9 # point of view.
10 #
11 # $1 is an HTML file to analyze and translate
12 # The output is automatically send to $1.out
13 #
14 # - add some ids, class
15 # - re-order some piece of HTML code
16 #
17 # This script is based on HTML::Parser module
18 #
19 # args:
20 # -i: HTML input file
21 # -o: HTML output file
22 # -j: javascript directory
23 # -c: css directory
24 # -p: images (pictures) directory
25 # -n: Manual name
26 # -e: Request a menu extraction
27 # -r: Source directory (ako part of -i arg)
28 # -?: help / usage
29 # -d: debug requested
30 use HTML::Parser ;
31 use HTML::TreeBuilder ;
32 use HTML::PullParser ;
33 use Getopt::Long ;
34 use File::Basename ;
35 use Data::Dumper ;
36 use open qw/:std :utf8/;
37 $edition = 'Bacula Edition' ;
38 sub usage {
39 print "translatedoc.pl -i | --input html-source-file
40 [ -o | --output html-destination-file ]
41 [ -j | --javascript javascript-diretory ]
42 [ -c | --css css-directory ]
43 [ -p | --pictures pictures-directory ]
44 [ -n | --name manual_name ]
45 [ -e | --extract ]
46 [ -d | --debug ]
47 [ -r | --source-directory the_original_root_directory ]
48 [ --help | -? ]\n" ;
49 exit 1 ;
50 }
51 #
52 # Send message to output in case of debug only
53 # ======================
54 sub debugdump {
55 my ($what,$msg) = @_ ;
56 if ($debug) {
57 print "\n===============================\nBegin of $msg\n" ;
58 $what->dump ;
59 print "\n===============================\nEnd of $msg\n\n" ;
60 }
61 }
62 #
63 # Args to Vars
64 our($inputfile,$outputfile,$help,$debug,$mytree,$extractmenu,$picturesdir,
65 $cssdir,$javascriptdir,$manualname,$sourcedir) ;
66 #
67 # Usage in case of missing arguments
68 usage() unless($#ARGV > -1) ;
69 #
70 # Input file / Output file
71 GetOptions("input|i=s" => \$inputfile,
72 "output|o=s" => \$outputfile,
73 "extract|e" => \$extractmenu,
74 "pictures|p=s" => \$picturesdir,
75 "css|c=s" => \$cssdir,
76 "source-directory|r=s" => \$sourcedir,
77 "javascript|j=s" => \$javascriptdir,
78 "name|n=s" => \$manualname,
79 "debug|d" => \$debug,
80 "help|?" => \$help) or usage() ;
81 usage() if ($help) ;
82 usage() unless (defined $inputfile) ;
83
84 die "$inputfile does not exists.\n" unless -e $inputfile ;
85
86 print "Converting file $inputfile\n" ;
87 $outputfile = "./" . basename($inputfile) . ".out" unless defined($outputfile) ;
88 $picturesdir = "../images" unless defined $picturesdir ;
89 $cssdir = "../css" unless defined($cssdir) ;
90 $javascriptdir = "../js" unless defined($javascriptdir) ;
91 $manualname = "main" unless defined($manualname) ;
92
93 my $MENUFILE="./wholemenu_" . $manualname . ".htm" ;
94 #
95 # Build HTML Tree of existing page
96 $mytree = HTML::TreeBuilder->new ;
97 $mytree->parse_file($inputfile) ;
98 debugdump($mytree,"Read tree") ;
99 #
100 # Find the beginning of the content
101 # Which is also a point where to put
102 # the menu
103 $beginning_of_content = $mytree->look_down('_tag','h1') ;
104 debugdump($beginning_of_content,"Beginning of content H1") ;
105 if (! $beginning_of_content) {
106 $beginning_of_content = $mytree->look_down('_tag','h2') ;
107 debugdump($beginning_of_content,"Beginning of content H2") ;
108 }
109 die "The only thing we could test is a <H1> / <H2> tags, which does not exist there...:$!\n" unless($beginning_of_content) ;
110
111 #
112 # Look for the table of contents
113 # ==============================
114 my $thetableofcontents ;
115 my $dummy ;
116 if ($thetableofcontents = $mytree->look_down('_tag', 'div', 'id','table-of-contents')) {
117 $dummy = $thetableofcontents->detach() ;
118 debugdump($thetableofcontents,"Table of Contents") ;
119 debugdump($dummy,"Dummy when ToC") ;
120 debugdump($mytree,"MyTree when ToC") ;
121 #
122 # Clean up the content of table of contents
123 while ($d = $thetableofcontents->look_down('_tag','br')) {
124 $d->detach() ;
125 $d->delete() ;
126 }
127 debugdump($thetableofcontents,"ToC after cleaning <br>:") ;
128 }
129 #
130 # The following will appear in case of "subsections"
131 if ($dummy = $mytree->look_down('_tag','a','name','CHILD_LINKS')) {
132 debugdump($dummy,"cleaning subsections") ;
133 $dummy->detach() ;
134 $dummy->delete() ;
135 debugdump($mytree,"MyTree after cleaning subsections") ;
136 }
137 #
138 # Remove the navigation part.... (next, up, previous, and so on)
139 if ($dummy = $mytree->look_down('_tag', 'div', 'class', 'navigation')) {
140 debugdump(0,"cleaning navigation") ;
141 $dummy->detach_content() ;
142 $dummy->detach() ;
143 $dummy->delete_content() ;
144 $dummy->delete() ;
145 debugdump($mytree,"MyTree after cleaning navigation") ;
146 }
147 # End removing navigation
148 #
149 #
150 # Remove every 'dirty' lines
151 # between <body> and <h1> tag
152 # What is "before" the <h1> tag (between <body> and <h1>) is just dropped
153 my @lefts = $beginning_of_content->left() ;
154 foreach my $l (@lefts) {
155 # print $l . "\n" ;
156 debugdump($l,"lefts") ;
157 $l->detach_content() ;
158 $l->delete_content() ;
159 $l->detach() ;
160 $l->delete() ;
161 }
162 debugdump($mytree,"MyTree after removing 'lefts'") ;
163 #
164 # Remove Bacula logo
165 if ($dummy = $beginning_of_content->look_down('_tag','img','alt','\\includegraphics{bacula-logo.eps}')) {
166 $dummy->detach() ;
167 $dummy->delete() ;
168 debugdump($beginning_of_content,"Content after removing logo") ;
169 debugdump($mytree,"MyTree after removing logo") ;
170 }
171 # End remove Bacula logo
172 #
173 # Remove 'address' tag
174 if ($dummy = $mytree->look_down('_tag','address')) {
175 $dummy->detach() ;
176 $dummy->delete() ;
177 debugdump($mytree,"MyTree after removing address") ;
178 }
179 # End remove address
180 #
181 # Identifying the body part
182 my $thebody = $mytree->look_down('_tag','body') ;
183 # ... to add a javascript "onload" action
184 $thebody->attr('onload','menuonload(this);') ;
185 debugdump($thebody,"TheBody after adding the onload js action") ;
186 my $d ;
187 if ($d = $thebody->look_down('_tag','div','class','bsys_topclass')) {
188 $d->detach() ;
189 $d->delete() ;
190 }
191 if ($d = $thebody->look_down('_tag','div','class','bsys_breadnsearchclass')) {
192 $d->detach() ;
193 $d->delete() ;
194 }
195 #
196 # Getting the content of the body
197 my @content = $thebody->detach_content() ;
198 # End remove dirty lines
199 #
200 # What do we do with the menu?
201 # If the menu file exists then just use it
202 if (-e $MENUFILE) {
203 #
204 # Build the menu file
205 $thetableofcontents = HTML::TreeBuilder->new ;
206 $thetableofcontents->parse_file($MENUFILE) ;
207 debugdump($thetableofcontents,"ToC read from $MENUFILE") ;
208 $dummy = $thetableofcontents->look_down('_tag','body') ;
209 $thetableofcontents = $dummy->detach_content() ;
210 debugdump($thetableofcontents,"ToC content read from $MENUFILE") ;
211 }
212 else {
213 debugdump($thetableofcontents,"ToC when building the menu") ;
214 }
215
216 #
217 #
218 # Create a div to manage the whole page
219 my $mainpage = HTML::Element->new_from_lol(
220 ['div', { 'class' => "b_mainpageclass", 'id' => "b_mainpageid" },
221 [ 'div', {'class' => 'b_topclass', 'id' => 'b_topid'},
222 [ 'img', { 'src' => $picturesdir . '/blogo-header.png', 'id' => 'b_logo','alt' => 'BaculaSystems Logo' }],
223 [ 'img', { 'src' => $picturesdir . '/btitle-header.png', 'id' => 'b_doctitle', 'alt' => join(' ',$edition,'Documentation text image transdoc')}]
224 ],
225 [ 'div', {'id' => 'b_breadnsearchid', 'class' => 'b_breadnsearchclass'},
226 ['div', { 'class' => 'b_searchclass', 'id' => 'b_searchid'},
227 ['span','Search' , {'class' => 'b_searchtitleclass','id' => 'b_searchtitleid'}],
228 [ 'input', { 'class' => 'b_searchfieldclass', 'id' => 'b_searchfieldid', 'type' => 'text', 'value' => 'Type your text here' }]
229 ],
230 [ 'div', { 'class' => 'b_breadcrumbsclass', 'id' => 'b_breadcrumbsid'},
231 [ 'p', { 'class' => 'b_breadcrumbscontentclass', 'id' => 'b_breadcrumbscontentid' }, 'Main' ],
232 ]
233 ],
234 [ 'div', { 'class' => "b_pageclass", 'id' => "b_pageid"},
235 [ 'div', { 'class' => "b_leftnavigationclass", 'id' => "b_leftnavigationid" },
236 $thetableofcontents,
237 ],
238 [ 'div', { 'class ' => 'b_contentclass', 'id' => 'b_contentid' },
239 [ map (('div', {'class' => 'b_truecontent' }), $_ ), @content ]
240 ]
241 ]
242 ]
243 ) ;
244 debugdump($mainpage,"Main page build") ;
245 $beginning_of_content = $thebody->push_content($mainpage) ;
246 # Remove "Contents" links
247 if ($dummy = $mytree->look_down('_tag','a','href','Contents.html')) {
248 $dummy = $dummy->parent() ;
249 $dummy->delete() ;
250 }
251 debugdump($beginning_of_content,"Content after pushing mainpage") ;
252 #
253 # Now begins the modification for navigation
254 # ==========================================
255 # We must analyze what is below <ul class="Child_Links">
256 # At first level, we consider each <li> as part of the main menu
257 # At other levels, we consider each <li> as sub(sub | ...)menus
258 if (my $firstul = $thetableofcontents->look_down('_tag','ul')) {
259 my $firsta = $firstul->look_down('_tag','a') ;
260 debugdump($firsta,'first <a> tag');
261 $dummy = $firsta->parent();
262 debugdump($dummy,'parent') ;
263 my $topul = $dummy->parent();
264 debugdump($topul,"grand parent") ;
265 $topul->attr('id','childlinksid') ;
266 #
267 # This counter is for generating unique identifiers
268 my $ulcounter = 1 ;
269 my $ullevelcounter = 0 ;
270 my $ulpreviousdepth = 0 ;
271 #
272 # Browse all the <ul name="ChildLinks"> node
273 # ------------------------------------------
274 foreach my $d ($dummy->descendants()) {
275 #
276 # Which tag are we checking ?
277 my $tag = $d->tag() ;
278 #
279 # Nothing to do with <a> tags
280 if ($tag =~ /a/) {
281 debugdump($d,'a tag') ;
282 $d->attr('onclick',"menuonclick(this);") ;
283 }
284 #
285 # <ul>s represent "openable" menus
286 elsif ($tag =~ /ul/) {
287 debugdump($d,'ul tag') ;
288 if ($d->depth() > $ulpreviousdepth) {
289 $ullevelcounter++ ;
290 }
291 else {
292 $ullevelcounter-- ;
293 }
294 $ulpreviousdepth=$d->depth() ;
295 #
296 # We need to identify uniquely this <ul> start tag to be able to "open" or "close" it
297 my $ullevel= 'level' . $ullevelcounter ;
298 my $idf = 'b_ul_' . $ulcounter++ ;
299 $d->attr('class',$ullevel . ' expandingMenu expandingMenuNotSelected') ;
300 $d->attr('id', $idf) ;
301 # $d->attr('style','display: none;') ;
302 #
303 # We now are knowing the previous <li> tag is a (sub)menu header too
304 # Adding the "onclick" behavior
305 my $previoustagli = $d->look_up('_tag','li') ; # <li> just above
306 $previoustagli->attr('pct_onmouseover',"over_expandingMenuHeader(this,\'" . $idf . "\')") ;
307 $previoustagli->attr('pct_onmouseout',"out_expandingMenuHeader(this,\'" . $idf . "\')") ;
308 my $previoustaga = $d->left('_tag','a') ; # <a> just above
309 $previoustaga->attr('onclick',"menuonclick(this);") ;
310 #
311 # Do not forgot what we defined earlier...
312 my $class = $previoustagli->attr('class') ;
313 $class = $class . ' expandingMenuHeader' ;
314 $previoustagli->attr('class', $class) ;
315 }
316 #
317 # <li>s represent at least menu items
318 # and sometimes menu headers (see <ul> treatment)
319 elsif ($tag =~ /li/) {
320 debugdump($d,'li tag') ;
321 #
322 # At this stage we only know <li> is a menu item.
323 # nothing more...
324 $d->attr('class', 'expandingMenuItem') ;
325 }
326 }
327 $firstul->replace_with($topul) ;
328 debugdump($thetableofcontents,"Table of contents after ul/li management") ;
329 }
330 #
331 # <head> treatment
332 # Add some stuff
333 my $themanual = "nothing" ;
334 if ($manualname eq "main") {
335 $themanual = "Main Reference Manual" ;
336 }
337 elsif ($manualname eq "developers") {
338 $themanual = "Developer's Guide" ;
339 }
340 elsif ($manualname eq "console") {
341 $themanual = 'Command Console and Operators Guide' ;
342 }
343 elsif ($manualname eq "utility") {
344 $themanual = 'Utility Programs' ;
345 }
346 elsif ($manualname eq "problems") {
347 $themanual = 'Problem Resolution Guide' ;
348 }
349 elsif ($manualname eq "misc") {
350 $themanual = 'Miscellaneous Guide' ;
351 }
352
353 if ($dummy = $mytree->look_down('_tag','meta','name','description')) {
354 $dummy->attr('content',join(' ',$edition,$themanual)) ;
355 }
356 if ($dummy = $mytree->look_down('_tag','meta','name','keywords')) {
357 $dummy->attr('content',join(',','BaculaSystems',$edition,$themanual)) ;
358 }
359 if ($dummy = $mytree->look_down('_tag','link','href',join('.',$manualname,'css'))) {
360 $dummy->attr('href',join('',$cssdir,'/',$manualname,'.','css')) ;
361 $dummy->postinsert(
362 HTML::Element->new_from_lol(
363 ['link',{ 'href' => join('',$cssdir,'/','bmanual.css'), 'rel' => 'stylesheet' } ],
364 ['link',{ 'href' => join('',$cssdir,'/','font-awesome.css'), 'rel' => 'stylesheet' } ],
365 ['script',{ 'type' => 'text/javascript', 'src' => join('',$javascriptdir,'/','bmanual.js') } ],
366 ['meta',{ 'charset' => 'utf-8' } ]
367 )
368 ) ;
369 }
370 debugdump($mytree,"MyTree after adding header information") ;
371 #
372 # Replace textregistered images with the HTML special char
373 my @images = $mytree->look_down('_tag','img') ;
374 foreach $dummy (@images) {
375 my $alttext = $dummy->attr('alt') ;
376 next unless $alttext ;
377 if ($alttext =~ /.*registe.*/) {
378 $dummy->preinsert(HTML::Element->new_from_lol(['span', {'class' => 'expochar' }, '&reg;'])) ;
379 $dummy->detach() ;
380 $dummy->delete() ;
381 }
382 if ($alttext =~ /.*bacula.*-logo.*/) {
383 $dummy->detach() ;
384 $dummy->delete() ;
385 }
386 }
387 debugdump($mytree,"MyTree after changing registered characters") ;
388
389 @images = $mytree->look_down('_tag','img') ;
390 foreach $dummy (@images) {
391 my $img = $dummy->attr('src') ;
392 $img =~ s:^\./:$picturesdir/: ;
393 $dummy->attr('src',$img) ;
394 }
395 debugdump($mytree,"MyTree after defining images path") ;
396 #
397 # Handling double quotes
398 my @quotes = $mytree->look_down('class','bleftquote') ;
399 foreach $dummy (@quotes) {
400 $dummy->preinsert(HTML::Element->new_from_lol(['span', '&ldquo;'])) ;
401 $dummy->detach() ;
402 $dummy->delete() ;
403 }
404 @quotes = $mytree->look_down('class','brightquote') ;
405 foreach $dummy (@quotes) {
406 $dummy->preinsert(HTML::Element->new_from_lol(['span', '&rdquo;'])) ;
407 $dummy->detach() ;
408 $dummy->delete() ;
409 }
410
411 debugdump($mytree,"MyTree after handling quotes") ;
412 #
413 # Removing <hr />
414 while ($dummy = $mytree->look_down('_tag','hr')) {
415 debugdump($dummy,"HR block to be deleted") ;
416 $dummy->detach() ;
417 $dummy->delete() ;
418 }
419 debugdump($mytree,'MyTree after removing <hr />') ;
420
421 #
422 # I am not sure the following is still requested
423 # ==============================================
424 #
425 # Locate all <a name="whatever_but_SECTION...">
426 my @atags = $mytree->look_down('_tag','a') ;
427 local *AFH ;
428 open AFH, ">> list-of-anchors" or die "Unable to append to list-of-anchors file\n";
429 foreach $dummy (@atags) {
430 my $atagname ;
431 if ($atagname = $dummy->attr('name')) {
432 print AFH $manualname . "\t" . basename($inputfile) . "\t" . $atagname . "\n" ;
433 }
434 }
435 close AFH ;
436 # This li is at first level
437 if ($outputfile) {
438 local *FH ;
439 open FH, ">" . $outputfile or die "Unable to create $outputfile: $!\n" ;
440 print FH $mytree->as_HTML("<>","\t",{}) ;
441 close FH ;
442 }
443 else {
444 print $mytree->as_HTML("","\t",{}) ;
445 }
446 if ($extractmenu) {
447 local *FH ;
448 open FH, ">" . $MENUFILE or die "Unable to create the menu file: $!\n" ;
449 debugdump($thetableofcontents,"Before pushing it to the menu file.") ;
450 print FH $thetableofcontents->as_HTML("","\t",{}) ;
451 close FH ;
452 }
453
454 1;