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