]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-texi2html.init
ea55108e618f83b890c6915402513a450f94fd5a
[lilypond.git] / lilypond-texi2html.init
1 #!/usr/bin/env perl
2
3 # {
4 package Texi2HTML::Config;
5
6 my $lastfilename;
7 my $docnr = 0;
8 my $page_toc_depth = 2;
9
10 use Data::Dumper;
11 $Data::Dumper::Maxdepth = 2;
12
13 sub print_element_info($) 
14 {
15   my $element = shift;
16   print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
17   print "Element: $element\n";
18   print Dumper($element);
19 }
20
21
22 # This function makes sure that files are only generated for numbered sections,
23 # but not for unnumbered ones. It is called after texi2html has done its own
24 # splitting and simply returns the filename for the node given as first argument
25 # Nodes with the same filename will be printed out to the same filename, so 
26 # this really all we need. Also, make sure that the file names for sections
27 # are derived from the section title. We also might want to name the anchors
28 # according to node titles, which works by simply overriding the id element of 
29 # the $element hash.
30 sub split_at_numbered_sections($$$)
31 {
32   my $element = shift;
33   my $type = shift;
34   my $docu_name = shift;
35   my $docu_ext = $Texi2HTML::Config::EXTENSION;
36   if ( $Texi2HTML::THISDOC{'current_lang'} ) {
37     $docu_ext = $Texi2HTML::THISDOC{'current_lang'} . "." . $docu_ext;
38   }
39
40 #   if ($$element{number} eq "1.1") {
41 #     print_element_info ($element);
42 #   }
43
44   # TOC, footer, about etc. are called with undefined $element and $type == "toc"|"stoc"|"foot"|"about"
45   if ($type eq "toc" or $type eq "stoc" or $type eq "foot" or $type eq "about") {
46     my $filename = "lilypond_$type";
47     $filename .= ".$docu_ext" if (defined($docu_ext));
48 #     print "Standard file of type $type\n";
49     return $filename;
50   } else {
51     # derive the name of the anchor (i.e. the part after # in the links!), 
52     # don't use texi2html's SECx.x default!
53     my $anchor = $$element{texi};
54     $anchor =~ tr/\ ?:'/-/d;
55     $$element{id} = $anchor;
56     # Numbered sections will get a filename Section_1.1.2, unnumbered sections will use 
57     # the file name of the previous numbered section:
58     if ($$element{number}) {
59       my $filename = $anchor;
60       $filename .= ".$docu_ext" if (defined($docu_ext));
61       $docnr += 1;
62       $$element{doc_nr} = $docnr;
63       $lastfilename = $filename;
64       return $filename;
65     } else {
66       $$element{doc_nr} = $docnr;
67       return $lastfilename;
68     }
69   }
70
71   return;
72 }
73
74
75 # The default formatting of external refs returns e.g. 
76 # "(lilypond-internals)Timing_translator", while we simply want "Timing_translator".
77 # Solution: Remove all (...) from the file_and_node argument before calling
78 # the default handler!
79 sub lilypond_external_ref($$$$$$)
80 {
81   my $type = shift;
82   my $section = shift;
83   my $book = shift;
84   my $file_node = shift;
85   my $href = shift;
86   my $cross_ref = shift;
87
88   $file_node =~ s/\(.*\)//;
89   return t2h_default_external_ref($type, $section, $book, $file_node, $href, $cross_ref);
90 }
91
92
93 # recursively generate the TOC entries for the element and its children (which
94 # are only shown up to maxlevel. All ancestors of the current element are also 
95 # shown with their immediate children, irrespective of their level.
96 sub generate_ly_toc_entries($$$)
97 {
98   my $element = shift;
99   my $element_path = shift;
100   my $maxlevel = shift;
101   # Skip undefined sections, plus all sections generated by index splitting
102   return() if (not defined($element) or exists($element->{'index_page'}));
103   my @result = ();
104   my $level = $element->{'toc_level'};
105   my $is_parent_of_current = $element_path->{$element->{'number'}};
106   my $print_children = ( ($level < $maxlevel) or $is_parent_of_current );
107   my $ind = '  ' x $level;
108   my $this_css_class = $is_parent_of_current ? " class=\"toc_current\"" : "";
109
110   my $entry = "$ind<li$this_css_class>" . &$anchor ($element->{'tocid'}, "$element->{'file'}#$element->{'id'}",$element->{'text'});
111
112   my $children = $element->{'section_childs'};
113   if ( $print_children and defined($children) and (ref($children) eq "ARRAY") ) {
114     push (@result, $entry);
115     my @child_result = ();
116     foreach (@$children) {
117       push (@child_result, generate_ly_toc_entries($_, $element_path, $maxlevel));
118     }
119     # if no child nodes were generated, e.g. for the index, where expanded pages
120     # are ignored, don't generate a list at all...
121     if (@child_result) {
122       push (@result, "$ind<ul$NO_BULLET_LIST_ATTRIBUTE>");
123       push (@result, @child_result);
124       push (@result, "$ind</ul></li>\n");
125     }
126   } else {
127     push (@result, $entry . "</li>\n");
128   }
129   return @result;
130 }
131
132
133 # Print a customized TOC, containing only the first two levels plus the whole
134 # path to the current page
135 sub lilypond_generate_page_toc_body($)
136 {
137     my $element = shift;
138     my $current_element = $element;
139     my %parentelements;
140     $parentelements{$element->{'number'}} = 1;
141     # Find the path to the current element
142     while ( defined($current_element->{'sectionup'}) and 
143            ($current_element->{'sectionup'} ne $current_element) )
144     {
145       $parentelements{$current_element->{'sectionup'}->{'number'}} = 1
146               if ($current_element->{'sectionup'}->{'number'} ne '');
147       $current_element = $current_element->{'sectionup'};
148     }
149     return () if not defined($current_element);
150     # Create the toc entries recursively
151     my @toc_entries = ("<div class=\"contents\">", "<ul$NO_BULLET_LIST_ATTRIBUTE>");
152     my $children = $current_element->{'section_childs'};
153     foreach ( @$children ) {
154       push (@toc_entries, generate_ly_toc_entries($_, \%parentelements, $page_toc_depth));
155     }
156     push (@toc_entries, "</ul>");
157     push (@toc_entries, "</div>");
158     return @toc_entries;
159 }
160
161 my @this_page_toc = ();
162
163 sub lilypond_print_element_header
164 {
165   my $fh = shift;
166   my $first_in_page = shift;
167   my $previous_is_top = shift;
168   if ($first_in_page and not @this_page_toc) {
169     if (defined($Texi2HTML::THIS_ELEMENT)) {
170       # Create the TOC for this page
171       @this_page_toc = lilypond_generate_page_toc_body($Texi2HTML::THIS_ELEMENT);
172     }
173   }
174   return T2H_DEFAULT_print_element_header( $fh, $first_in_page, $previous_is_top);
175 }
176
177
178
179
180
181
182
183
184
185
186 # Print out the TOC in a <div> at the end of th page, which will be formatted as a
187 # sidebar mimicking a TOC frame
188 sub print_lilypond_page_foot($)
189 {
190   my $fh = shift;
191 #   my @lines = @{$Texi2HTML::OVERVIEW};
192 #   my $lines = $Texi2HTML::TOC_LINES;
193   my @lines = @this_page_toc;
194   if (not @lines) {
195     print "We have no toc lines, generate generic ones for main::element_top::\n";
196 #     print Dumper( $main::element_top );
197 #     print Dumper( $main::section_top );
198 #     print Dumper( $main::node_top );
199 #     @lines = lilypond_generate_page_toc_body( $main::element_top );
200 #     @lines = lilypond_generate_page_toc_body($Texi2HTML::THIS_ELEMENT);
201     @lines = @$Texi2HTML::TOC_LINES;
202   }
203   if (@lines) {
204     print $fh "<div id=\"tocframe\">";
205     print $fh '<h4> ' . $Texi2HTML::NAME{'Contents'}  . "</h4>\n";
206     foreach my $line (@lines) {
207       print $fh $line;
208     }
209     print $fh "</div>";
210     @this_page_toc = ();
211   }
212   T2H_DEFAULT_print_page_foot($fh);
213 }
214
215
216
217
218
219
220
221 sub get_navigation_text
222 {
223   my $button = shift;
224   my $text = $NAVIGATION_TEXT{$button};
225   if ( ($button eq 'Back') or ($button eq 'FastBack') ) {
226     $text = $text . $Texi2HTML::NODE{$button} . "&nbsp;";
227   } elsif ( ($button eq 'Forward') or ($button eq 'FastForward') ) {
228     $text = "&nbsp;" . $Texi2HTML::NODE{$button} . $text;
229   } elsif ( $button eq 'Up' ) {
230     $text = "&nbsp;Up:&nbsp;" . $Texi2HTML::NODE{$button} . "&nbsp;";
231   }
232   return $text;
233 }
234
235
236 # Don't automatically create left-aligned table cells for every link, but 
237 # instead create a <td> only on an appropriate '(left|right|center)-aligned-cell-n'
238 # button text. It's alignment as well as the colspan will be taken from the
239 # name of the button. Also, add 'newline' button text to create a new table
240 # row. The texts of the buttons are generated by get_navigation_text and 
241 # will contain the name of the next/previous section/chapter.
242 sub lilypond_print_navigation
243 {
244     my $fh = shift;
245     my $buttons = shift;
246     my $vertical = shift;
247     my $spacing = 1;
248 #     print $fh '<table cellpadding="', $spacing, '" cellspacing="', $spacing,
249 #       "\" border=\"0\" class=\"nav_table\">\n";
250     print $fh "<table class=\"nav_table\">\n";
251
252     print $fh "<tr>" unless $vertical;
253     my $beginofline = 1;
254     foreach my $button (@$buttons)
255     {
256         print $fh qq{<tr valign="top" align="left">\n} if $vertical;
257         # Allow (left|right|center)-aligned-cell and newline as buttons!
258         if ( $button =~ /^(.*)-aligned-cell-(.*)$/ ) 
259         {
260           print $fh qq{</td>} unless $beginofline;
261           print $fh qq{<td valign="middle" align="$1" colspan="$2">};
262           $beginofline = 0;
263         } 
264         elsif ( $button eq 'newline' ) 
265         {
266           print $fh qq{</td>} unless $beginofline;
267           print $fh qq{</tr>};
268           print $fh qq{<tr>};
269           $beginofline = 1;
270
271         } 
272         elsif (ref($button) eq 'CODE')
273         {
274             &$button($fh, $vertical);
275         }
276         elsif (ref($button) eq 'SCALAR')
277         {
278             print $fh "$$button" if defined($$button);
279         }
280         elsif (ref($button) eq 'ARRAY')
281         {
282             my $text = $button->[1];
283             my $button_href = $button->[0];
284             # verify that $button_href is simple text and text is a reference
285             if (defined($button_href) and !ref($button_href) 
286                and defined($text) and (ref($text) eq 'SCALAR') and defined($$text))
287             {             # use given text
288                 if ($Texi2HTML::HREF{$button_href})
289                 {
290                   print $fh "" .
291                         &$anchor('',
292                                     $Texi2HTML::HREF{$button_href},
293                                     $$text
294                                    ) 
295                                     ;
296                 }
297                 else
298                 {
299                   print $fh $$text;
300                 }
301             }
302         }
303         elsif ($button eq ' ')
304         {                       # handle space button
305             print $fh
306                 ($ICONS && $ACTIVE_ICONS{' '}) ?
307                     &$button_icon_img($BUTTONS_NAME{$button}, $ACTIVE_ICONS{' '}) :
308                         $NAVIGATION_TEXT{' '};
309             #next;
310         }
311         elsif ($Texi2HTML::HREF{$button})
312         {                       # button is active
313             my $btitle = $BUTTONS_GOTO{$button} ?
314                 'title="' . $BUTTONS_GOTO{$button} . '"' : '';
315             if ($ICONS && $ACTIVE_ICONS{$button})
316             {                   # use icon
317                 print $fh '' .
318                     &$anchor('',
319                         $Texi2HTML::HREF{$button},
320                         &$button_icon_img($BUTTONS_NAME{$button},
321                                    $ACTIVE_ICONS{$button},
322                                    $Texi2HTML::SIMPLE_TEXT{$button}),
323                         $btitle
324                       );
325             }
326             else
327             {                   # use text
328                 print $fh
329                     '[' .
330                         &$anchor('',
331                                     $Texi2HTML::HREF{$button},
332                                     get_navigation_text($button),
333                                     $btitle
334                                    ) .
335                                        ']';
336             }
337         }
338         else
339         {                       # button is passive
340             print $fh
341                 $ICONS && $PASSIVE_ICONS{$button} ?
342                     &$button_icon_img($BUTTONS_NAME{$button},
343                                           $PASSIVE_ICONS{$button},
344                                           $Texi2HTML::SIMPLE_TEXT{$button}) :
345
346                                               "[" . get_navigation_text($button) . "]";
347         }
348         print $fh "</td>\n" if $vertical;
349         print $fh "</tr>\n" if $vertical;
350     }
351     print $fh "</td>" unless $beginofline;
352     print $fh "</tr>" unless $vertical;
353     print $fh "</table>\n";
354 }
355
356
357 @Texi2HTML::Config::SECTION_BUTTONS =
358     ('left-aligned-cell-1', 'FastBack', 
359      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
360      'right-aligned-cell-1', 'FastForward',
361      'newline',
362      'left-aligned-cell-2', 'Back',
363      'center-aligned-cell-1', 'Up',
364      'right-aligned-cell-2', 'Forward'
365     );
366
367 # buttons for misc stuff
368 @Texi2HTML::Config::MISC_BUTTONS = ('center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About');
369
370 # buttons for chapter file footers
371 # (and headers but only if SECTION_NAVIGATION is false)
372 @Texi2HTML::Config::CHAPTER_BUTTONS =
373     ('left-aligned-cell-1', 'FastBack', 
374      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
375      'right-aligned-cell-1', 'FastForward',
376     );
377
378 # buttons for section file footers
379 @Texi2HTML::Config::SECTION_FOOTER_BUTTONS =
380     ('left-aligned-cell-1', 'FastBack', 
381      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
382      'right-aligned-cell-1', 'FastForward',
383      'newline',
384      'left-aligned-cell-2', 'Back',
385      'center-aligned-cell-1', 'Up',
386      'right-aligned-cell-2', 'Forward'
387     );
388
389 @Texi2HTML::Config::NODE_FOOTER_BUTTONS =
390     ('left-aligned-cell-1', 'FastBack', 
391      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
392      'right-aligned-cell-1', 'FastForward',
393      'newline',
394      'left-aligned-cell-2', 'Back',
395      'center-aligned-cell-1', 'Up',
396      'right-aligned-cell-2', 'Forward'
397     );
398
399 # $Texi2HTML::Config::SPLIT = 'section';
400 $Texi2HTML::Config::PREFIX = 'lilypond';
401 # $Texi2HTML::Config::FRAMES = 1;
402 @Texi2HTML::Config::CSS_REFS = ("lilypond.css");
403 $Texi2HTML::Config::element_file_name = \&split_at_numbered_sections;
404 $Texi2HTML::Config::print_element_header = \&lilypond_print_element_header;
405 $Texi2HTML::Config::print_page_foot = \&print_lilypond_page_foot;
406 $Texi2HTML::Config::print_navigation = \&lilypond_print_navigation;
407 $Texi2HTML::Config::external_ref = \&lilypond_external_ref;
408
409
410
411 return 1;