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