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