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