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