]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-texi2html.init
Remove unneccessary commented code
[lilypond.git] / lilypond-texi2html.init
1 #!/usr/bin/env perl
2
3 package Texi2HTML::Config;
4
5 my $lastfilename;
6 my $docnr = 0;
7 my $page_toc_depth = 2;
8 my @default_toc = [];
9
10 use Data::Dumper;
11 $Data::Dumper::Maxdepth = 2;
12
13 sub print_element_info($) 
14 {
15   my $element = shift;
16   print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
17   print "Element: $element\n";
18   print Dumper($element);
19 }
20
21
22 # Convert a given node name to its proper file name (normalization as explained
23 # in the texinfo manual:
24 # http://www.gnu.org/software/texinfo/manual/texinfo/html_node/HTML-Xref-Node-Name-Expansion.html
25 sub texinfo_file_name($)
26 {
27   my $str = shift;
28   # File name normalization by texinfo:
29   # 1/2: letters and numbers are left unchanged
30   # 3/4: multiple, leading and trailing whitespace is removed
31   $str = main::normalise_space($str);
32   # 5/6: all remaining spaces are converted to '-', all other 7- or 8-bit 
33   #      chars are replaced by _xxxx (xxxx=ascii character code)
34   my @chars = split(//, $str);
35   my $str = '';
36   foreach my $char (@chars) {
37     if ( $char eq ' ' ) { # space -> '-'
38       $str .= '-';
39     } elsif ( ('0' le $char and $char le '9' ) or
40               ('A' le $char and $char le 'Z' ) or
41               ('a' le $char and $char le 'z' ) ) { # number or letter
42       $str .= $char;
43     } else {
44       my $ccode = ord($char);
45       my $addstr;
46       if ( ord($char)<= 0xFFFF ) {
47         $addstr = sprintf("_%4x", $ccode);
48       } else {
49         $addstr = sprintf("__%6x", $ccode);
50       }
51       # padding is done by spaces, replace by '0'
52       $addstr =~ s/\ /0/g;
53       $str .= $addstr;
54     }
55   }
56   # 7: if name begins with number, prepend 't_g' (so it starts with a letter)
57   if ($str =~ /^[0-9]/) {
58     $str = 't_g' . $str;
59   }
60   # DONE
61   return $str
62 }
63
64
65
66 # This function makes sure that files are only generated for numbered sections,
67 # but not for unnumbered ones. It is called after texi2html has done its own
68 # splitting and simply returns the filename for the node given as first argument
69 # Nodes with the same filename will be printed out to the same filename, so 
70 # this really all we need. Also, make sure that the file names for sections
71 # are derived from the section title. We also might want to name the anchors
72 # according to node titles, which works by simply overriding the id element of 
73 # the $element hash.
74 sub split_at_numbered_sections($$$)
75 {
76   my $element = shift;
77   my $type = shift;
78   my $docu_name = shift;
79   my $docu_ext = $Texi2HTML::Config::EXTENSION;
80
81   # TOC, footer, about etc. are called with undefined $element and $type == "toc"|"stoc"|"foot"|"about"
82   if ($type eq "toc" or $type eq "stoc" or $type eq "foot" or $type eq "about") {
83     return;
84   } else {
85     # derive the name of the anchor (i.e. the part after # in the links!), 
86     # don't use texi2html's SECx.x default!
87     my $anchor = main::remove_texi($$element{texi});
88     if ($$element{translationof}) {
89       $anchor = main::remove_texi($$element{translationof});
90     }
91     # normalize to the same file name as texinfo
92     $anchor = texinfo_file_name($anchor);
93     $$element{id} = $anchor;
94     # Numbered sections will get a filename Section_1.1.2, unnumbered sections will use 
95     # the file name of the previous numbered section:
96     if ($$element{number}) {
97       my $filename = $anchor;
98       $filename .= ".$docu_ext" if (defined($docu_ext));
99       $docnr += 1;
100       $$element{doc_nr} = $docnr;
101       $lastfilename = $filename;
102       return $filename;
103     } else {
104       $$element{doc_nr} = $docnr;
105       return $lastfilename;
106     }
107   }
108
109   return;
110 }
111
112
113 # The default formatting of external refs returns e.g. 
114 # "(lilypond-internals)Timing_translator", while we simply want "Timing_translator".
115 # Solution: Remove all (...) from the file_and_node argument before calling
116 # the default handler!
117 sub lilypond_external_ref($$$$$$)
118 {
119   my $type = shift;
120   my $section = shift;
121   my $book = shift;
122   my $file_node = shift;
123   my $href = shift;
124   my $cross_ref = shift;
125
126   $file_node =~ s/\(.*\)//;
127   return t2h_default_external_ref($type, $section, $book, $file_node, $href, $cross_ref);
128 }
129
130
131 # recursively generate the TOC entries for the element and its children (which
132 # are only shown up to maxlevel. All ancestors of the current element are also 
133 # shown with their immediate children, irrespective of their level.
134 sub generate_ly_toc_entries($$$)
135 {
136   my $element = shift;
137   my $element_path = shift;
138   my $maxlevel = shift;
139   # Skip undefined sections, plus all sections generated by index splitting
140   return() if (not defined($element) or exists($element->{'index_page'}));
141   my @result = ();
142   my $level = $element->{'toc_level'};
143   my $is_parent_of_current = $element_path->{$element->{'number'}};
144   my $print_children = ( ($level < $maxlevel) or $is_parent_of_current );
145   my $ind = '  ' x $level;
146   my $this_css_class = $is_parent_of_current ? " class=\"toc_current\"" : "";
147
148   my $entry = "$ind<li$this_css_class>" . &$anchor ($element->{'tocid'}, "$element->{'file'}#$element->{'id'}",$element->{'text'});
149
150   my $children = $element->{'section_childs'};
151   if ( $print_children and defined($children) and (ref($children) eq "ARRAY") ) {
152     push (@result, $entry);
153     my @child_result = ();
154     foreach (@$children) {
155       push (@child_result, generate_ly_toc_entries($_, $element_path, $maxlevel));
156     }
157     # if no child nodes were generated, e.g. for the index, where expanded pages
158     # are ignored, don't generate a list at all...
159     if (@child_result) {
160       push (@result, "$ind<ul$NO_BULLET_LIST_ATTRIBUTE>");
161       push (@result, @child_result);
162       push (@result, "$ind</ul></li>\n");
163     }
164   } else {
165     push (@result, $entry . "</li>\n");
166   }
167   return @result;
168 }
169
170
171 # Print a customized TOC, containing only the first two levels plus the whole
172 # path to the current page
173 sub lilypond_generate_page_toc_body($)
174 {
175     my $element = shift;
176     my $current_element = $element;
177     my %parentelements;
178     $parentelements{$element->{'number'}} = 1;
179     # Find the path to the current element
180     while ( defined($current_element->{'sectionup'}) and 
181            ($current_element->{'sectionup'} ne $current_element) )
182     {
183       $parentelements{$current_element->{'sectionup'}->{'number'}} = 1
184               if ($current_element->{'sectionup'}->{'number'} ne '');
185       $current_element = $current_element->{'sectionup'};
186     }
187     return () if not defined($current_element);
188     # Create the toc entries recursively
189     my @toc_entries = ("<div class=\"contents\">", "<ul$NO_BULLET_LIST_ATTRIBUTE>");
190     my $children = $current_element->{'section_childs'};
191     foreach ( @$children ) {
192       push (@toc_entries, generate_ly_toc_entries($_, \%parentelements, $page_toc_depth));
193     }
194     push (@toc_entries, "</ul>");
195     push (@toc_entries, "</div>");
196     return @toc_entries;
197 }
198
199 my @this_page_toc = ();
200
201 sub lilypond_print_element_header
202 {
203   my $fh = shift;
204   my $first_in_page = shift;
205   my $previous_is_top = shift;
206   if ($first_in_page and not @this_page_toc) {
207     if (defined($Texi2HTML::THIS_ELEMENT)) {
208       # Create the TOC for this page
209       @this_page_toc = lilypond_generate_page_toc_body($Texi2HTML::THIS_ELEMENT);
210     }
211   }
212   return T2H_DEFAULT_print_element_header( $fh, $first_in_page, $previous_is_top);
213 }
214
215 sub lilypond_toc_body($)
216 {
217     my $elements_list = shift;
218     # Generate a default TOC for pages without THIS_ELEMENT
219     @default_toc = lilypond_generate_page_toc_body(@$elements_list[0]);
220     return T2H_GPL_toc_body($elements_list);
221 }
222
223
224 sub lilypond_css_lines ($$)
225 {
226     my $import_lines = shift;
227     my $rule_lines = shift;
228     return if (defined($CSS_LINES));
229 #     return if (!@$rule_lines and !@$import_lines and (! keys(%css_map)));
230     if (@$rule_lines or @$import_lines)
231     {
232         $CSS_LINES = "<style type=\"text/css\">\n<!--\n";
233         $CSS_LINES .= join('',@$import_lines) . "\n" if (@$import_lines);
234         $CSS_LINES .= join('',@$rule_lines) . "\n" if (@$rule_lines);
235         $CSS_LINES .= "-->\n</style>\n";
236     }
237     foreach my $ref (@CSS_REFS)
238     {
239         $CSS_LINES .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"$ref\">\n";
240     }
241     $CSS_LINES .= "<!--[if lte IE 7]>\n<link href=\"lilypond-ie-fixes.css\" rel=\"stylesheet\" type=\"text/css\">\n<![endif]-->\n";
242 }
243
244
245
246
247
248
249
250 # Print out the TOC in a <div> at the end of th page, which will be formatted as a
251 # sidebar mimicking a TOC frame
252 sub print_lilypond_page_foot($)
253 {
254   my $fh = shift;
255   my $program_string = &$program_string();
256   print $fh "<p><font size='-1'>$program_string</font><br>$PRE_BODY_CLOSE</p>\n";
257   
258   # Print the TOC frame:
259   my @lines = @this_page_toc;
260   # use default TOC if no custom lines have been generated
261   @lines = @default_toc if (not @lines);
262   if (@lines) {
263     print $fh "\n\n<div id=\"tocframe\">";
264     print $fh '<h4> ' . $Texi2HTML::NAME{'Contents'}  . "</h4>\n";
265     foreach my $line (@lines) {
266       print $fh $line;
267     }
268     print $fh "</div>";
269     @this_page_toc = ();
270   }
271   
272   # Close the page:
273   print $fh "</body>\n</html>\n";
274 }
275
276
277
278
279
280
281 sub get_navigation_text
282 {
283   my $button = shift;
284   my $text = $NAVIGATION_TEXT{$button};
285   if ( ($button eq 'Back') or ($button eq 'FastBack') ) {
286     $text = $text . $Texi2HTML::NODE{$button} . "&nbsp;";
287   } elsif ( ($button eq 'Forward') or ($button eq 'FastForward') ) {
288     $text = "&nbsp;" . $Texi2HTML::NODE{$button} . $text;
289   } elsif ( $button eq 'Up' ) {
290     $text = "&nbsp;".$text.":&nbsp;" . $Texi2HTML::NODE{$button} . "&nbsp;";
291   }
292   return $text;
293 }
294
295
296 # Don't automatically create left-aligned table cells for every link, but 
297 # instead create a <td> only on an appropriate '(left|right|center)-aligned-cell-n'
298 # button text. It's alignment as well as the colspan will be taken from the
299 # name of the button. Also, add 'newline' button text to create a new table
300 # row. The texts of the buttons are generated by get_navigation_text and 
301 # will contain the name of the next/previous section/chapter.
302 sub lilypond_print_navigation
303 {
304     my $fh = shift;
305     my $buttons = shift;
306     my $vertical = shift;
307     my $spacing = 1;
308 #     print $fh '<table cellpadding="', $spacing, '" cellspacing="', $spacing,
309 #       "\" border=\"0\" class=\"nav_table\">\n";
310     print $fh "<table class=\"nav_table\">\n";
311
312     print $fh "<tr>" unless $vertical;
313     my $beginofline = 1;
314     foreach my $button (@$buttons)
315     {
316         print $fh qq{<tr valign="top" align="left">\n} if $vertical;
317         # Allow (left|right|center)-aligned-cell and newline as buttons!
318         if ( $button =~ /^(.*)-aligned-cell-(.*)$/ ) 
319         {
320           print $fh qq{</td>} unless $beginofline;
321           print $fh qq{<td valign="middle" align="$1" colspan="$2">};
322           $beginofline = 0;
323         } 
324         elsif ( $button eq 'newline' ) 
325         {
326           print $fh qq{</td>} unless $beginofline;
327           print $fh qq{</tr>};
328           print $fh qq{<tr>};
329           $beginofline = 1;
330
331         } 
332         elsif (ref($button) eq 'CODE')
333         {
334             &$button($fh, $vertical);
335         }
336         elsif (ref($button) eq 'SCALAR')
337         {
338             print $fh "$$button" if defined($$button);
339         }
340         elsif (ref($button) eq 'ARRAY')
341         {
342             my $text = $button->[1];
343             my $button_href = $button->[0];
344             # verify that $button_href is simple text and text is a reference
345             if (defined($button_href) and !ref($button_href) 
346                and defined($text) and (ref($text) eq 'SCALAR') and defined($$text))
347             {             # use given text
348                 if ($Texi2HTML::HREF{$button_href})
349                 {
350                   my $anchor_attributes = '';
351                   if ($USE_ACCESSKEY and (defined($BUTTONS_ACCESSKEY{$button_href})) and ($BUTTONS_ACCESSKEY{$button_href} ne ''))
352                   {
353                       $anchor_attributes = "accesskey=\"$BUTTONS_ACCESSKEY{$button_href}\"";
354                   }
355                   if ($USE_REL_REV and (defined($BUTTONS_REL{$button_href})) and ($BUTTONS_REL{$button_href} ne ''))
356                   {
357                       $anchor_attributes .= " rel=\"$BUTTONS_REL{$button_href}\"";
358                   }
359                   print $fh "" .
360                         &$anchor('',
361                                     $Texi2HTML::HREF{$button_href},
362                                     get_navigation_text($$text),
363                                     $anchor_attributes
364                                    );
365                 }
366                 else
367                 {
368                   print $fh get_navigation_text($$text);
369                 }
370             }
371         }
372         elsif ($button eq ' ')
373         {                       # handle space button
374             print $fh
375                 ($ICONS && $ACTIVE_ICONS{' '}) ?
376                     &$button_icon_img($BUTTONS_NAME{$button}, $ACTIVE_ICONS{' '}) :
377                         $NAVIGATION_TEXT{' '};
378             #next;
379         }
380         elsif ($Texi2HTML::HREF{$button})
381         {                       # button is active
382             my $btitle = $BUTTONS_GOTO{$button} ?
383                 'title="' . $BUTTONS_GOTO{$button} . '"' : '';
384             if ($USE_ACCESSKEY and (defined($BUTTONS_ACCESSKEY{$button})) and ($BUTTONS_ACCESSKEY{$button} ne ''))
385             {
386                 $btitle .= " accesskey=\"$BUTTONS_ACCESSKEY{$button}\"";
387             }
388             if ($USE_REL_REV and (defined($BUTTONS_REL{$button})) and ($BUTTONS_REL{$button} ne ''))
389             {
390                 $btitle .= " rel=\"$BUTTONS_REL{$button}\"";
391             }
392             if ($ICONS && $ACTIVE_ICONS{$button})
393             {                   # use icon
394                 print $fh '' .
395                     &$anchor('',
396                         $Texi2HTML::HREF{$button},
397                         &$button_icon_img($BUTTONS_NAME{$button},
398                                    $ACTIVE_ICONS{$button},
399                                    $Texi2HTML::SIMPLE_TEXT{$button}),
400                         $btitle
401                       );
402             }
403             else
404             {                   # use text
405                 print $fh
406                     '[' .
407                         &$anchor('',
408                                     $Texi2HTML::HREF{$button},
409                                     get_navigation_text ($button),
410                                     $btitle
411                                    ) .
412                                        ']';
413             }
414         }
415         else
416         {                       # button is passive
417             print $fh
418                 $ICONS && $PASSIVE_ICONS{$button} ?
419                     &$button_icon_img($BUTTONS_NAME{$button},
420                                           $PASSIVE_ICONS{$button},
421                                           $Texi2HTML::SIMPLE_TEXT{$button}) :
422
423                                               "[" . get_navigation_text($button) . "]";
424         }
425         print $fh "</td>\n" if $vertical;
426         print $fh "</tr>\n" if $vertical;
427     }
428     print $fh "</td>" unless $beginofline;
429     print $fh "</tr>" unless $vertical;
430     print $fh "</table>\n";
431 }
432
433
434 @Texi2HTML::Config::SECTION_BUTTONS =
435     ('left-aligned-cell-1', 'FastBack', 
436      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
437      'right-aligned-cell-1', 'FastForward',
438      'newline',
439      'left-aligned-cell-2', 'Back',
440      'center-aligned-cell-1', 'Up',
441      'right-aligned-cell-2', 'Forward'
442     );
443
444 # buttons for misc stuff
445 @Texi2HTML::Config::MISC_BUTTONS = ('center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About');
446
447 # buttons for chapter file footers
448 # (and headers but only if SECTION_NAVIGATION is false)
449 @Texi2HTML::Config::CHAPTER_BUTTONS =
450     ('left-aligned-cell-1', 'FastBack', 
451      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
452      'right-aligned-cell-1', 'FastForward',
453     );
454
455 # buttons for section file footers
456 @Texi2HTML::Config::SECTION_FOOTER_BUTTONS =
457     ('left-aligned-cell-1', 'FastBack', 
458      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
459      'right-aligned-cell-1', 'FastForward',
460      'newline',
461      'left-aligned-cell-2', 'Back',
462      'center-aligned-cell-1', 'Up',
463      'right-aligned-cell-2', 'Forward'
464     );
465
466 @Texi2HTML::Config::NODE_FOOTER_BUTTONS =
467     ('left-aligned-cell-1', 'FastBack', 
468      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
469      'right-aligned-cell-1', 'FastForward',
470      'newline',
471      'left-aligned-cell-2', 'Back',
472      'center-aligned-cell-1', 'Up',
473      'right-aligned-cell-2', 'Forward'
474     );
475
476 # $Texi2HTML::Config::SPLIT = 'section';
477 @Texi2HTML::Config::CSS_REFS      = ("lilypond.css");
478 $Texi2HTML::Config::USE_ACCESSKEY = 1;
479 $Texi2HTML::Config::USE_LINKS     = 1;
480 $Texi2HTML::Config::USE_REL_REV   = 1;
481 $Texi2HTML::Config::element_file_name    = \&split_at_numbered_sections;
482 $Texi2HTML::Config::print_element_header = \&lilypond_print_element_header;
483 $Texi2HTML::Config::print_page_foot      = \&print_lilypond_page_foot;
484 $Texi2HTML::Config::print_navigation     = \&lilypond_print_navigation;
485 $Texi2HTML::Config::external_ref         = \&lilypond_external_ref;
486 $Texi2HTML::Config::toc_body             = \&lilypond_toc_body;
487 $Texi2HTML::Config::css_lines            = \&lilypond_css_lines;
488
489
490 # For split pages, use index(.lang).html as start page!
491 if ($Texi2HTML::Config::SPLIT == 'section') {
492 #   my $lng = $Texi2HTML::THISDOC{'current_lang'};
493 #   if ($lng and ($lng ne "en")) {
494 #     $Texi2HTML::Config::TOP_FILE = 'index.'.$lng.'.html';
495 #   } else {
496     $Texi2HTML::Config::TOP_FILE = 'index.html';
497 #   }
498 }
499
500 # if ($Texi2HTML::THISDOC{'current_lang'}) {
501 #   $Texi2HTML::Config::EXTENSION = $Texi2HTML::THISDOC{'current_lang'} . "." . 
502 #         $docu_ext = $Texi2HTML::Config::EXTENSION;;
503 # }
504
505
506
507 # Try to make use of @translationof to generate files according to the original
508 # English section title...
509 sub lilypond_unknown($$$$$)
510 {
511     my $macro = shift;
512     my $line = shift;
513     my $pass = shift;
514     my $stack = shift;
515     my $state = shift;
516
517     # the @translationof macro provides the original English section title, 
518     # which should be used for file/anchor naming, while the title will be
519     # translated to each language
520     if ($pass == 1 and $macro eq "translationof") {
521       if (ref($state->{'element'})=='HASH') {
522         $state->{'element'}->{'translationof'} = main::normalise_space($line);
523       }
524       return ('', true, undef, undef);
525     } else {
526       return t2h_default_unknown($macro, $line, $pass, $stack, $state);
527     }
528 }
529 $Texi2HTML::Config::unknown                  = \&lilypond_unknown;
530
531 %css_map=();
532
533 return 1;