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