]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-texi2html.init
Docs: Make Patrick's style the default and Andrew's style the alternative
[lilypond.git] / lilypond-texi2html.init
1 #!/usr/bin/env perl
2
3 ### texi2html customization script for Lilypond
4 ### Author: Reinhold Kainhofer <reinhold@kainhofer.com>, 2008.
5 ###         Some code parts copied from texi2html and adapted. These functions
6 ###         were written mainly by Patrice Dumas
7 ### License: GPLv2+
8 ###
9 ###
10 ### Features implemented here:
11 ### -) For split manuals, the main page is index.html.
12 ### -) All @unnumbered* sections are placed into the same file
13 ###    (implemented by split_at_numbered_sections)
14 ### -) Use our custom CSS file, with IE-specific fixes in another CSS file,
15 ###    impelmented by lilypond_css_lines
16 ### -) TOC (folded, with the current page highlighted) in an overflown <div>
17 ###    is added to every page; implemented by:
18 ###           lilypond_print_element_header -- building of the TOC
19 ###           lilypond_toc_body -- generation of customized TOC output
20 ###           lilypond_print_page_head -- start <div id="main">
21 ###           print_lilypond_page_foot -- closing id=main, output of footer & TOC
22 ### -) External refs are formatted only as "Text of the node" (not as >>see
23 ###    "NODE" section "SECTION" in "BOOK"<< like with default texi2html). Also,
24 ###    the leading "(book-name)" is removed.
25 ###    Implemented by overriding lilypond_external_ref
26 ### -) Navigation bars on top/bottom of the page and between sections are not
27 ###    left-aligned, but use a combination of left/center/right aligned table
28 ###    cells; For this, I heavily extend the texi2html code to allow for
29 ###    differently aligned cells and for multi-line tables);
30 ###    Implemented in lilypond_print_navigation
31 ### -) Different formatting than the default: example uses the same formatting
32 ###    as quote.
33 ### -) Allow translated section titles: All section titles can be translated,
34 ###    the original (English) title is associated with @translationof. This is
35 ###    needed, because the file name / anchor is generated from the original
36 ###    English title, since otherwise language-autoselection would break with
37 ###    posted links.
38 ###    Since it is then no longer possible to obtain the file name from the
39 ###    section title, I keep a sectionname<=>filename/anchor around. This way,
40 ###    xrefs from other manuals can simply load that map and retrieve the
41 ###    correct file name for the link. Implemented in:
42 ###           lilypond_unknown (handling of @translationof, in case
43 ###                             extract_texi_filenames.py messes up...)
44 ###           lilypond_element_file_name (correct file name: use the map)
45 ###           lilypond_element_target_name (correct anchor: use the map)
46 ###           lilypond_init_map (read in the externally created map from disk)
47 ###           lilypond_external_href (load the map for xrefs, use the correct
48 ###                                   link target)
49 ### -) The HTML anchors for all sections are derived from the node name /
50 ###    section title (pre-generated in the .xref-map file). Implemented by:
51 ###           lilypond_element_target_name (adjust section anchors)
52 ### -) Use the standard footnote format "<sup>nr</sup> text" instead of the
53 ###    ugly format of texi2html (<h3>(nr)</h3><p>text</p>). Implemented in
54 ###           makeinfo_like_foot_line_and_ref
55 ###           makeinfo_like_foot_lines
56 ###           makeinfo_like_paragraph
57 ###
58 ###
59 ### Useful helper functions:
60 ### -) texinfo_file_name($node_name): returns a texinfo-compatible file name
61 ###    for the given string $node_name (whitespace trimmed/replaced by -,
62 ###    non-standard chars replaced by _xxxx (ascii char code) and forced to
63 ###    start with a letter by prepending t_g if necessary)
64
65
66 package Texi2HTML::Config;
67
68
69
70
71
72 #############################################################################
73 ###  SETTINGS FOR TEXI2HTML
74 #############################################################################
75
76 @Texi2HTML::Config::CSS_REFS      = (
77     {FILENAME => "lilypond-mccarty.css", TITLE => "Patrick McCarty's design"}
78 );
79 @Texi2HTML::Config::ALT_CSS_REFS      = (
80     {FILENAME => "lilypond.css", TITLE => "Andrew Hawryluk's design" }
81 );
82 $Texi2HTML::Config::USE_ACCESSKEY = 1;
83 $Texi2HTML::Config::USE_LINKS     = 1;
84 $Texi2HTML::Config::USE_REL_REV   = 1;
85 $Texi2HTML::Config::SPLIT_INDEX   = 0;
86 $Texi2HTML::Config::SEPARATED_FOOTNOTES = 0; # Print footnotes on same page, not separated
87 if ($Texi2HTML::Config::SPLIT eq 'section') {
88   $Texi2HTML::Config::element_file_name    = \&lilypond_element_file_name;
89 }
90 $Texi2HTML::Config::element_target_name  = \&lilypond_element_target_name;
91 $Texi2HTML::Config::print_element_header = \&lilypond_print_element_header;
92 $Texi2HTML::Config::print_page_foot      = \&print_lilypond_page_foot;
93 $Texi2HTML::Config::print_navigation     = \&lilypond_print_navigation;
94 $Texi2HTML::Config::external_ref         = \&lilypond_external_ref;
95 $Texi2HTML::Config::external_href        = \&lilypond_external_href;
96 $Texi2HTML::Config::toc_body             = \&lilypond_toc_body;
97 $Texi2HTML::Config::css_lines            = \&lilypond_css_lines;
98 $Texi2HTML::Config::unknown              = \&lilypond_unknown;
99 $Texi2HTML::Config::print_page_head      = \&lilypond_print_page_head;
100 # $Texi2HTML::Config::foot_line_and_ref    = \&lilypond_foot_line_and_ref;
101 $Texi2HTML::Config::foot_line_and_ref  = \&makeinfo_like_foot_line_and_ref;
102 $Texi2HTML::Config::foot_lines         = \&makeinfo_like_foot_lines;
103 $Texi2HTML::Config::paragraph          = \&makeinfo_like_paragraph;
104
105
106
107 # Examples should be formatted similar to quotes:
108 $Texi2HTML::Config::complex_format_map->{'example'} = {
109   'begin' => q{"<blockquote><pre class=\"example\">"},
110   'end' => q{"</pre></blockquote>\n"},
111  };
112
113 %Texi2HTML::config::misc_pages_targets = (
114    'Overview' => 'Overview',
115    'Contents' => 'Contents',
116    'About' => 'About'
117 );
118
119
120 my @section_to_filename;
121
122
123
124
125 #############################################################################
126 ###  DEBUGGING
127 #############################################################################
128
129 use Data::Dumper;
130 $Data::Dumper::Maxdepth = 2;
131
132 sub print_element_info($)
133 {
134   my $element = shift;
135   print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
136   print "Element: $element\n";
137   print Dumper($element);
138 }
139
140
141
142
143
144 #############################################################################
145 ###  HELPER FUNCTIONS
146 #############################################################################
147
148 # Convert a given node name to its proper file name (normalization as explained
149 # in the texinfo manual:
150 # http://www.gnu.org/software/texinfo/manual/texinfo/html_node/HTML-Xref-Node-Name-Expansion.html
151 sub texinfo_file_name($)
152 {
153   my $text = shift;
154   my $result = '';
155   # File name normalization by texinfo:
156   # 1/2: letters and numbers are left unchanged
157   # 3/4: multiple, leading and trailing whitespace is removed
158   $text = main::normalise_space($text);
159   # 5/6: all remaining spaces are converted to '-', all other 7- or 8-bit
160   #      chars are replaced by _xxxx (xxxx=ascii character code)
161   while ($text ne '') {
162     if ($text =~ s/^([A-Za-z0-9]+)//o) { # number or letter stay unchanged
163       $result .= $1;
164     } elsif ($text =~ s/^ //o) { # space -> '-'
165       $result .= '-';
166     } elsif ($text =~ s/^(.)//o) { # Otherwise use _xxxx (ascii char code)
167       my $ccode = ord($1);
168       if ( $ccode <= 0xFFFF ) {
169         $result .= sprintf("_%04x", $ccode);
170       } else {
171         $result .= sprintf("__%06x", $ccode);
172       }
173     }
174   }
175   # 7: if name does not begin with a letter, prepend 't_g' (so it starts with a letter)
176   if ($result !~ /^[a-zA-Z]/) {
177     $result = 't_g' . $result;
178   }
179   # DONE
180   return $result
181 }
182
183
184 # Load a file containing a nodename<=>filename map (tab-sepatared, i.e.
185 # NODENAME\tFILENAME\tANCHOR
186 # Returns a ref to a hash "Node title" => ["FilenameWithoutExt", "Anchor"]
187 sub load_map_file ($)
188 {
189     my $mapfile = shift;
190     my $node_map = ();
191
192     if (open(XREFFILE, $mapfile)) {
193         my $line;
194         while ( $line = <XREFFILE> ) {
195             # parse the tab-separated entries and insert them into the map:
196             chomp($line);
197             my @entries = split(/\t/, $line);
198             if (scalar (@entries) == 3) {
199               $node_map->{$entries[0]} = [$entries[1], $entries[2]];
200             } else {
201               print STDERR "Invalid entry in the node file $mapfile: $line\n";
202             }
203         }
204         close (XREFFILE);
205     } else {
206         print STDERR "WARNING: Unable to load the map file $mapfile\n";
207     }
208     return $node_map;
209 }
210
211
212 # Split the given path into dir and basename (with .texi removed). Used mainly
213 # to get the path/basename of the original texi input file
214 sub split_texi_filename ($)
215 {
216   my $docu = shift;
217   my $docu_dir, $docu_name;
218   if ($docu =~ /(.*\/)/) {
219     chop($docu_dir = $1);
220     $docu_name = $docu;
221     $docu_name =~ s/.*\///;
222   } else {
223      $docu_dir = '.';
224      $docu_name = $docu;
225   }
226   $docu_name =~ s/\.te?x(i|info)?$//;
227   return ($docu_dir, $docu_name);
228 }
229
230
231
232
233
234 #############################################################################
235 ###  CSS HANDLING
236 #############################################################################
237
238 # Include our standard CSS file, not hard-coded CSS code directly in the HTML!
239 # For IE, conditionally include the lilypond-ie-fixes.css style sheet
240 sub lilypond_css_lines ($$)
241 {
242     my $import_lines = shift;
243     my $rule_lines = shift;
244     return if (defined($Texi2HTML::THISDOC{'CSS_LINES'}));
245     if (@$rule_lines or @$import_lines)
246     {
247         $Texi2HTML::THISDOC{'CSS_LINES'} = "<style type=\"text/css\">\n<!--\n";
248         $Texi2HTML::THISDOC{'CSS_LINES'} .= join('',@$import_lines) . "\n" if (@$import_lines);
249         $Texi2HTML::THISDOC{'CSS_LINES'} .= join('',@$rule_lines) . "\n" if (@$rule_lines);
250         $Texi2HTML::THISDOC{'CSS_LINES'} .= "-->\n</style>\n";
251     }
252     foreach my $ref (@CSS_REFS)
253     {
254         $Texi2HTML::THISDOC{'CSS_LINES'} .= "<link rel=\"stylesheet\" type=\"text/css\" title=\"$ref->{TITLE}\" href=\"$ref->{FILENAME}\">\n";
255     }
256     foreach my $ref (@ALT_CSS_REFS)
257     {
258         $Texi2HTML::THISDOC{'CSS_LINES'} .= "<link rel=\"alternate stylesheet\" type=\"text/css\" href=\"$ref->{FILENAME}\" title=\"$ref->{TITLE}\">\n";
259     }
260     $Texi2HTML::THISDOC{'CSS_LINES'} .= "<!--[if lte IE 7]>\n<link href=\"lilypond-ie-fixes.css\" rel=\"stylesheet\" type=\"text/css\">\n<![endif]-->\n";
261 }
262
263
264
265
266
267 #############################################################################
268 ###  SPLITTING BASED ON NUMBERED SECTIONS
269 #############################################################################
270
271 my $lastfilename;
272 my $docnr = 0;
273 my $node_to_filename_map = ();
274
275
276 # This function makes sure that files are only generated for numbered sections,
277 # but not for unnumbered ones. It is called after texi2html has done its own
278 # splitting and simply returns the filename for the node given as first argument
279 # Nodes with the same filename will be printed out to the same filename, so
280 # this really all we need. Also, make sure that the file names for sections
281 # are derived from the section title. We also might want to name the anchors
282 # according to node titles, which works by simply overriding the id element of
283 # the $element hash.
284 # If an external nodename<=>filename/anchor map file is found (loaded in
285 # lilypond_init_out, use the externally created values, otherwise use the
286 # same logic here.
287 sub lilypond_element_file_name($$$)
288 {
289   my $element = shift;
290   my $type = shift;
291   my $docu_name = shift;
292   my $docu_ext = $Texi2HTML::Config::EXTENSION;
293
294   my $node_name = main::remove_texi($element->{'node_ref'}->{'texi'});
295   # the snippets page does not use nodes for the snippets, so in this case
296   # we'll have to use the section name!
297   if ($node_name eq '') {
298     $node_name = main::remove_texi($element->{'texi'});
299   }
300
301   # If we have an entry in the section<=>filename map, use that one, otherwise
302   # generate the filename/anchor here. In the latter case, external manuals
303   # will not be able to retrieve the file name for xrefs!!! Still, I already
304   # had that code, so I'll leave it in in case something goes wrong with the
305   # extract_texi_filenames.py script in the lilypond build process!
306   if (exists ($node_to_filename_map->{$node_name})) {
307     (my $filename, my $anchor) = @{$node_to_filename_map->{$node_name}};
308     $filename .= ".$docu_ext" if (defined($docu_ext));
309
310     # unnumbered sections (except those at top-level!) always go to the same
311     # file as the previous numbered section
312     if (not ($element->{number}) and not ($lastfilename eq '') and ($element->{level} > 1)) {
313       $filename = $lastfilename;
314     }
315     if (($filename eq $lastfilename)) {
316       $$element{doc_nr} = $docnr;
317     } else {
318       $docnr += 1;
319       $$element{doc_nr} = $docnr;
320       $lastfilename = $filename;
321     }
322     return $filename;
323
324   } elsif ($type eq "top" or $type eq "toc" or $type eq "doc" or $type eq "stoc" or $type eq "foot" or $type eq "about") {
325     return;
326   } else {
327     print STDERR "WARNING: Node '$node_name' was NOT found in the map\n"
328         unless ($node_name eq '') or ($element->{'tag'} eq 'unnumberedsec')
329                or ($node_name =~ /NOT REALLY USED/);
330
331     # Numbered sections will get a filename Node_title, unnumbered sections will use
332     # the file name of the previous numbered section:
333     if (($element->{number}) or ($lastfilename eq '') or ($element->{level} == 1)) {
334       # normalize to the same file name as texinfo
335       if ($element->{translationof}) {
336         $node_name = main::remove_texi($element->{translationof});
337       }
338       my $filename = texinfo_file_name($node_name);
339       $filename .= ".$docu_ext" if (defined($docu_ext));
340       $docnr += 1;
341       $$element{doc_nr} = $docnr;
342       $lastfilename = $filename;
343       return $filename;
344     } else {
345       $$element{doc_nr} = $docnr;
346       return $lastfilename;
347     }
348   }
349
350   return;
351 }
352
353 sub lilypond_element_target_name($$$)
354 {
355   my $element = shift;
356   my $target = shift;
357   my $id = shift;
358   # Target is based on node name (or sec name for secs without node attached)
359   my $node_name = main::remove_texi($element->{'node_ref'}->{'texi'});
360   if ($node_name eq '') {
361     $node_name = main::remove_texi($element->{'texi'});
362   }
363
364   # If we have an entry in the section<=>filename map, use that one, otherwise
365   # generate the anchor here.
366   if (exists ($node_to_filename_map->{$node_name})) {
367     (my $filename, $target) = @{$node_to_filename_map->{$node_name}};
368   } else {
369     my $anchor = $node_name;
370     if ($element->{translationof}) {
371       $anchor = main::remove_texi($element->{translationof});
372     }
373     # normalize to the same file name as texinfo
374     $target = texinfo_file_name($anchor);
375   }
376   # TODO: Once texi2html correctly prints out the target and not the id for
377   #       the sections, change this back to ($id, $target)
378   return ($target, $target);
379 }
380
381
382 ## Load the map file for the corrently processed texi file. We do this
383 #  using a command init handler, since texi2html does not have any
384 #  other hooks that are called after THISDOC is filled but before phase 2
385 #  of the texi2html conversion.
386 sub lilypond_init_map ()
387 {
388     my ($docu_dir, $docu_name) = split_texi_filename ($Texi2HTML::THISDOC{'input_file_name'});
389     my $map_filename = main::locate_include_file ("${docu_name}.$Texi2HTML::THISDOC{current_lang}.xref-map")
390         || main::locate_include_file ("${docu_name}.xref-map");
391     $node_to_filename_map = load_map_file ($map_filename);
392 }
393 push @Texi2HTML::Config::command_handler_init, \&lilypond_init_map;
394
395
396
397 #############################################################################
398 ###  CLEANER LINK TITLE FOR EXTERNAL REFS
399 #############################################################################
400
401 # The default formatting of external refs returns e.g.
402 # "(lilypond-internals)Timing_translator", so we remove all (...) from the
403 # file_and_node argument. Also, we want only a very simple format, so we don't
404 # even call the default handler!
405 sub lilypond_external_ref($$$$$$)
406 {
407   my $type = shift;
408   my $section = shift;
409   my $book = shift;
410   my $file_node = shift;
411   my $href = shift;
412   my $cross_ref = shift;
413
414   my $displaytext = '';
415
416   # 1) if we have a cross ref name, that's the text to be displayed:
417   # 2) For the top node, use the (printable) name of the manual, unless we
418   #    have an explicit cross ref name
419   # 3) In all other cases use the section name
420   if ($cross_ref ne '') {
421     $displaytext = $cross_ref;
422   } elsif (($section eq '') or ($section eq 'Top')) {
423     $displaytext = $book;
424   } else {
425     $displaytext = $section;
426   }
427
428   $displaytext = &$anchor('', $href, $displaytext) if ($displaytext ne '');
429   return &$I('%{node_file_href}', { 'node_file_href' => $displaytext });
430
431 #  Default: format as "see <a ..>NODE</a> section 'SECTION' in BOOK". We don't want this!
432 #   return t2h_default_external_ref($type, $section, $book, $file_node, $href, $cross_ref);
433 }
434
435
436
437
438
439 #############################################################################
440 ###  HANDLING TRANSLATED SECTIONS: handle @translationof, secname<->filename
441 ###                  map stored on disk, xrefs in other manuals load that map
442 #############################################################################
443
444
445 # Try to make use of @translationof to generate files according to the original
446 # English section title...
447 sub lilypond_unknown($$$$$)
448 {
449     my $macro = shift;
450     my $line = shift;
451     my $pass = shift;
452     my $stack = shift;
453     my $state = shift;
454
455     # the @translationof macro provides the original English section title,
456     # which should be used for file/anchor naming, while the title will be
457     # translated to each language
458     # It is already used by extract_texi_filenames.py, so this should not be
459     # necessary here at all. Still, I'll leave the code in just in case the
460     # python script messed up ;-)
461     if ($pass == 1 and $macro eq "translationof") {
462       if (ref($state->{'element'}) eq 'HASH') {
463         $state->{'element'}->{'translationof'} = main::normalise_space($line);
464       }
465       return ('', true, undef, undef);
466     } else {
467       return t2h_default_unknown($macro, $line, $pass, $stack, $state);
468     }
469 }
470
471
472
473
474 my %translated_books = ();
475 # Construct a href to an external source of information.
476 # node is the node with texinfo @-commands
477 # node_id is the node transliterated and transformed as explained in the
478 #         texinfo manual
479 # node_xhtml_id is the node transformed such that it is unique and can
480 #     be used to make an html cross ref as explained in the texinfo manual
481 # file is the file in '(file)node'
482 sub lilypond_external_href($$$)
483 {
484   my $node = shift;
485   my $node_id = shift;
486   my $node_hxmlt_id = shift;
487   my $file = shift;
488   my $original_func = \&t2h_default_external_href;
489
490   # 1) Keep a hash of book->section_map
491   # 2) if not file in keys hash => try to load the map (assign empty map if
492   #    non-existent => will load only once!)
493   # 3) if node in the section=>(file, anchor) map, replace node_id and
494   #    node_xhtml_id by the map's values
495   # 4) call the t2h_default_external_href with these values (or the old ones if not found)
496
497   if (($node_id ne '') and defined($file) and ($node_id ne 'Top')) {
498     my $map_name = $file;
499     $map_name =~ s/-big-page//;
500
501     # Load the map if we haven't done so already
502     if (!exists($translated_books{$map_name})) {
503       my ($docu_dir, $docu_name) = split_texi_filename ($Texi2HTML::THISDOC{'input_file_name'});
504       my $map_filename = main::locate_include_file ("${map_name}.$Texi2HTML::THISDOC{current_lang}.xref-map")
505           || main::locate_include_file ("${map_name}.xref-map");
506       $translated_books{$map_name} = load_map_file ($map_filename);
507     }
508
509     # look up translation. use these values instead of the old filename/anchor
510     my $section_name_map = $translated_books{$map_name};
511     my $node_text = main::remove_texi($node);
512     if (defined($section_name_map->{$node_text})) {
513       ($node_id, $node_hxmlt_id) = @{$section_name_map->{$node_text}};
514     } else {
515       print STDERR "WARNING: Unable to find node '$node_text' in book $map_name.\n";
516     }
517   }
518
519   if (defined $file) {
520     return &$original_func($node, $node_id, $node_hxmlt_id, $file);
521   } else {
522     return &$original_func($node, $node_id, $node_hxmlt_id);
523   }
524 }
525
526
527
528
529
530 #############################################################################
531 ###  CUSTOM TOC FOR EACH PAGE (in a frame on the left)
532 #############################################################################
533
534 my $page_toc_depth = 2;
535 my @default_toc = [];
536
537 # recursively generate the TOC entries for the element and its children (which
538 # are only shown up to maxlevel. All ancestors of the current element are also
539 # shown with their immediate children, irrespective of their level.
540 # Unnumbered entries are only printed out if they are at top-level or their
541 # parent element is an ancestor of the currently viewed node.
542 sub generate_ly_toc_entries($$$$)
543 {
544   my $element = shift;
545   my $element_path = shift;
546   my $maxlevel = shift;
547   my $always_show_unnumbered_children = shift;
548   # Skip undefined sections, plus all sections generated by index splitting
549   return() if (not defined($element) or exists($element->{'index_page'}));
550   my @result = ();
551   my $level = $element->{'toc_level'};
552   my $is_parent_of_current = $element->{'id'} && $element_path->{$element->{'id'}};
553   my $print_children = ( ($level < $maxlevel) or $is_parent_of_current );
554   my $ind = '  ' x $level;
555   my $this_css_class = $is_parent_of_current ? " class=\"toc_current\"" : "";
556
557   my $entry = "$ind<li$this_css_class>" . &$anchor ($element->{'tocid'}, "$element->{'file'}#$element->{'target'}",$element->{'text'});
558
559   my $children = $element->{'section_childs'};
560   # Don't add unnumbered entries, unless they are at top-level or a parent of the current!
561   if (not ($element->{'number'} or $always_show_unnumbered_children)) {
562     return @result;
563   }
564   if ( $print_children and defined($children) and (ref($children) eq "ARRAY") ) {
565     push (@result, $entry);
566     my @child_result = ();
567     foreach (@$children) {
568       push (@child_result, generate_ly_toc_entries($_, $element_path, $maxlevel, $is_parent_of_current));
569     }
570     # if no child nodes were generated, e.g. for the index, where expanded pages
571     # are ignored, don't generate a list at all...
572     if (@child_result) {
573       push (@result, "\n$ind<ul$NO_BULLET_LIST_ATTRIBUTE>\n");
574       push (@result, @child_result);
575       push (@result, "$ind</ul></li>\n");
576     }
577   } else {
578     push (@result, $entry . "</li>\n");
579   }
580   return @result;
581 }
582
583
584 # Print a customized TOC, containing only the first two levels plus the whole
585 # path to the current page
586 sub lilypond_generate_page_toc_body($)
587 {
588     my $element = shift;
589     my $current_element = $element;
590     my %parentelements;
591     $parentelements{$element->{'id'}} = 1;
592     # Find the path to the current element
593     while ( defined($current_element->{'sectionup'}) and
594            ($current_element->{'sectionup'} ne $current_element) )
595     {
596       $parentelements{$current_element->{'sectionup'}->{'id'}} = 1
597               if ($current_element->{'sectionup'}->{'id'} ne '');
598       $current_element = $current_element->{'sectionup'};
599     }
600     return () if not defined($current_element);
601     # Create the toc entries recursively
602     my @toc_entries = ("<div class=\"contents\">\n", "<ul$NO_BULLET_LIST_ATTRIBUTE>\n");
603     my $children = $current_element->{'section_childs'};
604     foreach ( @$children ) {
605       push (@toc_entries, generate_ly_toc_entries($_, \%parentelements, $page_toc_depth, False));
606     }
607     push (@toc_entries, "</ul>\n");
608     push (@toc_entries, "</div>\n");
609     return @toc_entries;
610 }
611
612 sub lilypond_print_toc_div ($$)
613 {
614   my $fh = shift;
615   my $tocref = shift;
616   my @lines = @$tocref;
617   # use default TOC if no custom lines have been generated
618   @lines = @default_toc if (not @lines);
619   if (@lines) {
620   
621     print $fh "\n\n<div id=\"tocframe\">\n";
622     print $fh '<h4 class="toc_header"> ' . &$anchor('',
623                                     $Texi2HTML::HREF{'Top'},
624                                     $Texi2HTML::NAME{'Top'},
625                                     'title="Start of the manual"'
626                                    ) . "</h4>\n";
627     foreach my $line (@lines) {
628       print $fh $line;
629     }
630     print $fh "</div>\n\n";
631   }
632 }
633
634 # Create the custom TOC for this page (partially folded, current page is
635 # highlighted) and store it in a global variable. The TOC is written out after
636 # the html contents (but positioned correctly using CSS), so that browsers with
637 # css turned off still show the contents first.
638 our @this_page_toc = ();
639 sub lilypond_print_element_header
640 {
641   my $first_in_page = shift;
642   my $previous_is_top = shift;
643   if ($first_in_page and not @this_page_toc) {
644     if (defined($Texi2HTML::THIS_ELEMENT)) {
645       # Create the TOC for this page
646       @this_page_toc = lilypond_generate_page_toc_body($Texi2HTML::THIS_ELEMENT);
647     }
648   }
649   return T2H_DEFAULT_print_element_header( $first_in_page, $previous_is_top);
650 }
651
652 # Generate the HTML output for the TOC
653 sub lilypond_toc_body($)
654 {
655     my $elements_list = shift;
656     # Generate a default TOC for pages without THIS_ELEMENT
657     @default_toc = lilypond_generate_page_toc_body(@$elements_list[0]);
658     return T2H_GPL_toc_body($elements_list);
659 }
660
661 # Print out the TOC in a <div> at the beginning of the page
662 sub lilypond_print_page_head($)
663 {
664     my $fh = shift;
665     T2H_DEFAULT_print_page_head($fh);
666     print $fh "<div id=\"main\">\n";
667 }
668
669 # Print out the TOC in a <div> at the end of th page, which will be formatted as a
670 # sidebar mimicking a TOC frame
671 sub print_lilypond_page_foot($)
672 {
673   my $fh = shift;
674   my $program_string = &$program_string();
675 #   print $fh "<p><font size='-1'>$program_string</font><br>$PRE_BODY_CLOSE</p>\n";
676   print $fh "<!-- FOOTER -->\n\n";
677   print $fh "<!-- end div#main here -->\n</div>\n\n";
678
679   # Print the TOC frame and reset the TOC:
680   lilypond_print_toc_div ($fh, \@this_page_toc);
681   @this_page_toc = ();
682
683   # Close the page:
684   print $fh "</body>\n</html>\n";
685 }
686
687
688
689
690
691 #############################################################################
692 ###  NICER / MORE FLEXIBLE NAVIGATION PANELS
693 #############################################################################
694
695 sub get_navigation_text
696 {
697   my $button = shift;
698   my $text = $NAVIGATION_TEXT{$button};
699   if ( ($button eq 'Back') or ($button eq 'FastBack') ) {
700     $text = $text . $Texi2HTML::NODE{$button} . "&nbsp;";
701   } elsif ( ($button eq 'Forward') or ($button eq 'FastForward') ) {
702     $text = "&nbsp;" . $Texi2HTML::NODE{$button} . $text;
703   } elsif ( $button eq 'Up' ) {
704     $text = "&nbsp;".$text.":&nbsp;" . $Texi2HTML::NODE{$button} . "&nbsp;";
705   }
706   return $text;
707 }
708
709
710 # Don't automatically create left-aligned table cells for every link, but
711 # instead create a <td> only on an appropriate '(left|right|center)-aligned-cell-n'
712 # button text. It's alignment as well as the colspan will be taken from the
713 # name of the button. Also, add 'newline' button text to create a new table
714 # row. The texts of the buttons are generated by get_navigation_text and
715 # will contain the name of the next/previous section/chapter.
716 sub lilypond_print_navigation
717 {
718     my $buttons = shift;
719     my $vertical = shift;
720     my $spacing = 1;
721     my $result = "<table class=\"nav_table\">\n";
722
723     $result .= "<tr>" unless $vertical;
724     my $beginofline = 1;
725     foreach my $button (@$buttons)
726     {
727         $result .= qq{<tr valign="top" align="left">\n} if $vertical;
728         # Allow (left|right|center)-aligned-cell and newline as buttons!
729         if ( $button =~ /^(.*)-aligned-cell-(.*)$/ )
730         {
731           $result .= qq{</td>} unless $beginofline;
732           $result .= qq{<td valign="middle" align="$1" colspan="$2">};
733           $beginofline = 0;
734         }
735         elsif ( $button eq 'newline' )
736         {
737           $result .= qq{</td>} unless $beginofline;
738           $result .= qq{</tr>};
739           $result .= qq{<tr>};
740           $beginofline = 1;
741
742         }
743         elsif (ref($button) eq 'CODE')
744         {
745             $result .= &$button($vertical);
746         }
747         elsif (ref($button) eq 'SCALAR')
748         {
749             $result .= "$$button" if defined($$button);
750         }
751         elsif (ref($button) eq 'ARRAY')
752         {
753             my $text = $button->[1];
754             my $button_href = $button->[0];
755             # verify that $button_href is simple text and text is a reference
756             if (defined($button_href) and !ref($button_href) 
757                and defined($text) and (ref($text) eq 'SCALAR') and defined($$text))
758             {             # use given text
759                 if ($Texi2HTML::HREF{$button_href})
760                 {
761                   my $anchor_attributes = '';
762                   if ($USE_ACCESSKEY and (defined($BUTTONS_ACCESSKEY{$button_href})) and ($BUTTONS_ACCESSKEY{$button_href} ne ''))
763                   {
764                       $anchor_attributes = "accesskey=\"$BUTTONS_ACCESSKEY{$button_href}\"";
765                   }
766                   if ($USE_REL_REV and (defined($BUTTONS_REL{$button_href})) and ($BUTTONS_REL{$button_href} ne ''))
767                   {
768                       $anchor_attributes .= " rel=\"$BUTTONS_REL{$button_href}\"";
769                   }
770                   $result .=  "" .
771                         &$anchor('',
772                                     $Texi2HTML::HREF{$button_href},
773                                     get_navigation_text($$text),
774                                     $anchor_attributes
775                                    );
776                 }
777                 else
778                 {
779                   $result .=  get_navigation_text($$text);
780                 }
781             }
782         }
783         elsif ($button eq ' ')
784         {                       # handle space button
785             $result .= 
786                 ($ICONS && $ACTIVE_ICONS{' '}) ?
787                     &$button_icon_img($BUTTONS_NAME{$button}, $ACTIVE_ICONS{' '}) :
788                         $NAVIGATION_TEXT{' '};
789             #next;
790         }
791         elsif ($Texi2HTML::HREF{$button})
792         {                       # button is active
793             my $btitle = $BUTTONS_GOTO{$button} ?
794                 'title="' . $BUTTONS_GOTO{$button} . '"' : '';
795             if ($USE_ACCESSKEY and (defined($BUTTONS_ACCESSKEY{$button})) and ($BUTTONS_ACCESSKEY{$button} ne ''))
796             {
797                 $btitle .= " accesskey=\"$BUTTONS_ACCESSKEY{$button}\"";
798             }
799             if ($USE_REL_REV and (defined($BUTTONS_REL{$button})) and ($BUTTONS_REL{$button} ne ''))
800             {
801                 $btitle .= " rel=\"$BUTTONS_REL{$button}\"";
802             }
803             if ($ICONS && $ACTIVE_ICONS{$button})
804             {                   # use icon
805                 $result .= '' .
806                     &$anchor('',
807                         $Texi2HTML::HREF{$button},
808                         &$button_icon_img($BUTTONS_NAME{$button},
809                                    $ACTIVE_ICONS{$button},
810                                    $Texi2HTML::SIMPLE_TEXT{$button}),
811                         $btitle
812                       );
813             }
814             else
815             {                   # use text
816                 $result .= 
817                     '[' .
818                         &$anchor('',
819                                     $Texi2HTML::HREF{$button},
820                                     get_navigation_text($button),
821                                     $btitle
822                                    ) .
823                                        ']';
824             }
825         }
826         else
827         {                       # button is passive
828             $result .= 
829                 $ICONS && $PASSIVE_ICONS{$button} ?
830                     &$button_icon_img($BUTTONS_NAME{$button},
831                                           $PASSIVE_ICONS{$button},
832                                           $Texi2HTML::SIMPLE_TEXT{$button}) :
833
834                                               "[" . get_navigation_text($button) . "]";
835         }
836         $result .= "</td>\n" if $vertical;
837         $result .= "</tr>\n" if $vertical;
838     }
839     $result .= "</td>" unless $beginofline;
840     $result .= "</tr>" unless $vertical;
841     $result .= "</table>\n";
842     return $result;
843 }
844
845
846 @Texi2HTML::Config::SECTION_BUTTONS =
847     ('left-aligned-cell-1', 'FastBack',
848      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
849      'right-aligned-cell-1', 'FastForward',
850      'newline',
851      'left-aligned-cell-2', 'Back',
852      'center-aligned-cell-1', 'Up',
853      'right-aligned-cell-2', 'Forward'
854     );
855
856 # buttons for misc stuff
857 @Texi2HTML::Config::MISC_BUTTONS = ('center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About');
858
859 # buttons for chapter file footers
860 # (and headers but only if SECTION_NAVIGATION is false)
861 @Texi2HTML::Config::CHAPTER_BUTTONS =
862     ('left-aligned-cell-1', 'FastBack',
863      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
864      'right-aligned-cell-1', 'FastForward',
865     );
866
867 # buttons for section file footers
868 @Texi2HTML::Config::SECTION_FOOTER_BUTTONS =
869     ('left-aligned-cell-1', 'FastBack',
870      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
871      'right-aligned-cell-1', 'FastForward',
872      'newline',
873      'left-aligned-cell-2', 'Back',
874      'center-aligned-cell-1', 'Up',
875      'right-aligned-cell-2', 'Forward'
876     );
877
878 @Texi2HTML::Config::NODE_FOOTER_BUTTONS =
879     ('left-aligned-cell-1', 'FastBack',
880      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
881      'right-aligned-cell-1', 'FastForward',
882      'newline',
883      'left-aligned-cell-2', 'Back',
884      'center-aligned-cell-1', 'Up',
885      'right-aligned-cell-2', 'Forward'
886     );
887
888
889
890
891
892 #############################################################################
893 ###  FOOTNOTE FORMATTING
894 #############################################################################
895
896 # Format footnotes in a nicer way: Instead of printing the number in a separate
897 # (nr) heading line, use the standard way of prepending <sup>nr</sup> immediately
898 # before the fn text.
899
900
901 # The following code is copied from texi2html's examples/makeinfo.init and
902 # should be updated when texi2html makes some changes there!
903
904 my $makekinfo_like_footnote_absolute_number = 0;
905
906 sub makeinfo_like_foot_line_and_ref($$$$$$$$)
907 {
908     my $foot_num = shift;
909     my $relative_num = shift;
910     my $footid = shift;
911     my $docid = shift;
912     my $from_file = shift;
913     my $footnote_file = shift;
914     my $lines = shift;
915     my $state = shift;
916
917     $makekinfo_like_footnote_absolute_number++;
918
919     # this is a bit obscure, this allows to add an anchor only if formatted
920     # as part of the document.
921     $docid = '' if ($state->{'outside_document'} or $state->{'multiple_pass'});
922
923     if ($from_file eq $footnote_file)
924     {
925         $from_file = $footnote_file = '';
926     }
927
928     my $foot_anchor = "<sup>" . &$anchor($docid, "$footnote_file#$footid", $relative_num) . "</sup>";
929     $foot_anchor = &$anchor($docid, "$footnote_file#$footid", "($relative_num)") if ($state->{'preformatted'});
930
931 #    unshift @$lines, "<li>";
932 #    push @$lines, "</li>\n";
933     return ($lines, $foot_anchor);
934 }
935
936 sub makeinfo_like_foot_lines($)
937 {
938     my $lines = shift;
939     unshift @$lines, "<hr>\n<h4>$Texi2HTML::I18n::WORDS->{'Footnotes_Title'}</h4>\n";
940 #<ol type=\"1\">\n";
941 #    push @$lines, "</ol>";
942     return $lines;
943 }
944
945 my %makekinfo_like_paragraph_in_footnote_nr;
946
947 sub makeinfo_like_paragraph ($$$$$$$$$$$$$)
948 {
949     my $text = shift;
950     my $align = shift;
951     my $indent = shift;
952     my $paragraph_command = shift;
953     my $paragraph_command_formatted = shift;
954     my $paragraph_number = shift;
955     my $format = shift;
956     my $item_nr = shift;
957     my $enumerate_style = shift;
958     my $number = shift;
959     my $command_stack_at_end = shift;
960     my $command_stack_at_begin = shift;
961     my $state = shift;
962 #print STDERR "format: $format\n" if (defined($format));
963 #print STDERR "paragraph @$command_stack_at_end; @$command_stack_at_begin\n";
964     $paragraph_command_formatted = '' if (!defined($paragraph_command_formatted) or
965           exists($special_list_commands{$format}->{$paragraph_command}));
966     return '' if ($text =~ /^\s*$/);
967     foreach my $style(t2h_collect_styles($command_stack_at_begin))
968     {
969        $text = t2h_begin_style($style, $text);
970     }
971     foreach my $style(t2h_collect_styles($command_stack_at_end))
972     {
973        $text = t2h_end_style($style, $text);
974     }
975     if (defined($paragraph_number) and defined($$paragraph_number))
976     {
977          $$paragraph_number++;
978          return $text  if (($format eq 'itemize' or $format eq 'enumerate') and
979             ($$paragraph_number == 1));
980     }
981     my $open = '<p';
982     if ($align)
983     {
984         $open .= " align=\"$paragraph_style{$align}\"";
985     }
986     my $footnote_text = '';
987     if (defined($command_stack_at_begin->[0]) and $command_stack_at_begin->[0] eq 'footnote')
988     {
989         my $state = $Texi2HTML::THISDOC{'state'};
990         $makekinfo_like_paragraph_in_footnote_nr{$makekinfo_like_footnote_absolute_number}++;
991         if ($makekinfo_like_paragraph_in_footnote_nr{$makekinfo_like_footnote_absolute_number} <= 1)
992         {
993            $open.=' class="footnote"';
994            my $document_file = $state->{'footnote_document_file'};
995            if ($document_file eq $state->{'footnote_footnote_file'})
996            {
997                $document_file = '';
998            }
999            my $docid = $state->{'footnote_place_id'};
1000            my $doc_state = $state->{'footnote_document_state'};
1001            $docid = '' if ($doc_state->{'outside_document'} or $doc_state->{'multiple_pass'});
1002            my $foot_label = &$anchor($state->{'footnote_footnote_id'},
1003                  $document_file . "#$state->{'footnote_place_id'}",
1004                  "$state->{'footnote_number_in_page'}");
1005            $footnote_text = "<small>[${foot_label}]</small> ";
1006         }
1007     }
1008     return $open.'>'.$footnote_text.$text.'</p>';
1009 }
1010
1011
1012 #############################################################################
1013 ###  OTHER SETTINGS
1014 #############################################################################
1015
1016 # For split pages, use index.html as start page!
1017 if ($Texi2HTML::Config::SPLIT eq 'section') {
1018   $Texi2HTML::Config::TOP_FILE = 'index.html';
1019 }
1020
1021
1022 return 1;