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