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