]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-texi2html.init
texi2html: Create tab-separated file containing the NODE<=>FILENAME map
[lilypond.git] / lilypond-texi2html.init
1 #!/usr/bin/env perl
2
3 package Texi2HTML::Config;
4
5 my $lastfilename;
6 my $docnr = 0;
7 my $page_toc_depth = 2;
8 my @default_toc = [];
9 my @section_to_filename;
10
11 use Data::Dumper;
12 $Data::Dumper::Maxdepth = 2;
13
14 sub print_element_info($) 
15 {
16   my $element = shift;
17   print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
18   print "Element: $element\n";
19   print Dumper($element);
20 }
21
22
23 # Convert a given node name to its proper file name (normalization as explained
24 # in the texinfo manual:
25 # http://www.gnu.org/software/texinfo/manual/texinfo/html_node/HTML-Xref-Node-Name-Expansion.html
26 sub texinfo_file_name($)
27 {
28   my $str = shift;
29   # File name normalization by texinfo:
30   # 1/2: letters and numbers are left unchanged
31   # 3/4: multiple, leading and trailing whitespace is removed
32   $str = main::normalise_space($str);
33   # 5/6: all remaining spaces are converted to '-', all other 7- or 8-bit 
34   #      chars are replaced by _xxxx (xxxx=ascii character code)
35   my @chars = split(//, $str);
36   my $str = '';
37   foreach my $char (@chars) {
38     if ( $char eq ' ' ) { # space -> '-'
39       $str .= '-';
40     } elsif ( ('0' le $char and $char le '9' ) or
41               ('A' le $char and $char le 'Z' ) or
42               ('a' le $char and $char le 'z' ) ) { # number or letter
43       $str .= $char;
44     } else {
45       my $ccode = ord($char);
46       my $addstr;
47       if ( ord($char)<= 0xFFFF ) {
48         $addstr = sprintf("_%4x", $ccode);
49       } else {
50         $addstr = sprintf("__%6x", $ccode);
51       }
52       # padding is done by spaces, replace by '0'
53       $addstr =~ s/\ /0/g;
54       $str .= $addstr;
55     }
56   }
57   # 7: if name begins with number, prepend 't_g' (so it starts with a letter)
58   if ($str =~ /^[0-9]/) {
59     $str = 't_g' . $str;
60   }
61   # DONE
62   return $str
63 }
64
65
66
67 # This function makes sure that files are only generated for numbered sections,
68 # but not for unnumbered ones. It is called after texi2html has done its own
69 # splitting and simply returns the filename for the node given as first argument
70 # Nodes with the same filename will be printed out to the same filename, so 
71 # this really all we need. Also, make sure that the file names for sections
72 # are derived from the section title. We also might want to name the anchors
73 # according to node titles, which works by simply overriding the id element of 
74 # the $element hash.
75 sub split_at_numbered_sections($$$)
76 {
77   my $element = shift;
78   my $type = shift;
79   my $docu_name = shift;
80   my $docu_ext = $Texi2HTML::Config::EXTENSION;
81
82   # TOC, footer, about etc. are called with undefined $element and $type == "toc"|"stoc"|"foot"|"about"
83   if ($type eq "toc" or $type eq "stoc" or $type eq "foot" or $type eq "about") {
84     return;
85   } else {
86     # derive the name of the anchor (i.e. the part after # in the links!), 
87     # don't use texi2html's SECx.x default!
88     my $sec_name = main::remove_texi($$element{texi});
89     my $anchor = $sec_name;
90     if ($$element{translationof}) {
91       $anchor = main::remove_texi($$element{translationof});
92     }
93     # normalize to the same file name as texinfo
94     $anchor = texinfo_file_name($anchor);
95     $$element{id} = $anchor;
96     # Numbered sections will get a filename Section_1.1.2, unnumbered sections will use 
97     # the file name of the previous numbered section:
98     if ($$element{number}) {
99       my $filename = $anchor;
100       $filename .= ".$docu_ext" if (defined($docu_ext));
101       $docnr += 1;
102       $$element{doc_nr} = $docnr;
103       $lastfilename = $filename;
104       push (@section_to_filename, [$sec_name, $filename, $anchor]);
105       return $filename;
106     } else {
107       $$element{doc_nr} = $docnr;
108       push (@section_to_filename, [$sec_name, $lastfilename, $anchor]);
109       return $lastfilename;
110     }
111   }
112
113   return;
114 }
115
116 sub lilypond_finish_out()
117 {
118   my $map_filename = "$Texi2HTML::THISDOC{'destination_directory'}$Texi2HTML::THISDOC{'file_base_name'}_xref.map";
119   if (open(XREFFILE, ">$map_filename")) {
120     foreach (@section_to_filename) {
121       my ($sec, $file, $anchor) = @$_;
122       print XREFFILE "$sec\t$file\t$anchor\n";
123     }
124     close XREFFILE;
125   } else {
126     print "Can't open $map_filename for writing: $! The map of X-refs will not be written out\n";
127   }
128 }
129
130
131
132 # The default formatting of external refs returns e.g. 
133 # "(lilypond-internals)Timing_translator", so we remove all (...) from the 
134 # file_and_node argument. Also, we want only a very simple format, so we don't
135 # even call the default handler!
136 sub lilypond_external_ref($$$$$$)
137 {
138   my $type = shift;
139   my $section = shift;
140   my $book = shift;
141   my $file_node = shift;
142   my $href = shift;
143   my $cross_ref = shift;
144
145   $file_node =~ s/\(.*\)//;
146   $file_node = &$anchor('', $href, $file_node) if ($file_node ne '');
147   return &$I('%{node_file_href}', { 'node_file_href' => $file_node });
148
149 #  Default: format as "see <a ..>NODE</a> section 'SECTION' in BOOK"
150 #   return t2h_default_external_ref($type, $section, $book, $file_node, $href, $cross_ref);
151 }
152
153
154
155
156 # recursively generate the TOC entries for the element and its children (which
157 # are only shown up to maxlevel. All ancestors of the current element are also 
158 # shown with their immediate children, irrespective of their level.
159 sub generate_ly_toc_entries($$$)
160 {
161   my $element = shift;
162   my $element_path = shift;
163   my $maxlevel = shift;
164   # Skip undefined sections, plus all sections generated by index splitting
165   return() if (not defined($element) or exists($element->{'index_page'}));
166   my @result = ();
167   my $level = $element->{'toc_level'};
168   my $is_parent_of_current = $element_path->{$element->{'number'}};
169   my $print_children = ( ($level < $maxlevel) or $is_parent_of_current );
170   my $ind = '  ' x $level;
171   my $this_css_class = $is_parent_of_current ? " class=\"toc_current\"" : "";
172
173   my $entry = "$ind<li$this_css_class>" . &$anchor ($element->{'tocid'}, "$element->{'file'}#$element->{'id'}",$element->{'text'});
174
175   my $children = $element->{'section_childs'};
176   if ( $print_children and defined($children) and (ref($children) eq "ARRAY") ) {
177     push (@result, $entry);
178     my @child_result = ();
179     foreach (@$children) {
180       push (@child_result, generate_ly_toc_entries($_, $element_path, $maxlevel));
181     }
182     # if no child nodes were generated, e.g. for the index, where expanded pages
183     # are ignored, don't generate a list at all...
184     if (@child_result) {
185       push (@result, "$ind<ul$NO_BULLET_LIST_ATTRIBUTE>");
186       push (@result, @child_result);
187       push (@result, "$ind</ul></li>\n");
188     }
189   } else {
190     push (@result, $entry . "</li>\n");
191   }
192   return @result;
193 }
194
195
196 # Print a customized TOC, containing only the first two levels plus the whole
197 # path to the current page
198 sub lilypond_generate_page_toc_body($)
199 {
200     my $element = shift;
201     my $current_element = $element;
202     my %parentelements;
203     $parentelements{$element->{'number'}} = 1;
204     # Find the path to the current element
205     while ( defined($current_element->{'sectionup'}) and 
206            ($current_element->{'sectionup'} ne $current_element) )
207     {
208       $parentelements{$current_element->{'sectionup'}->{'number'}} = 1
209               if ($current_element->{'sectionup'}->{'number'} ne '');
210       $current_element = $current_element->{'sectionup'};
211     }
212     return () if not defined($current_element);
213     # Create the toc entries recursively
214     my @toc_entries = ("<div class=\"contents\">", "<ul$NO_BULLET_LIST_ATTRIBUTE>");
215     my $children = $current_element->{'section_childs'};
216     foreach ( @$children ) {
217       push (@toc_entries, generate_ly_toc_entries($_, \%parentelements, $page_toc_depth));
218     }
219     push (@toc_entries, "</ul>");
220     push (@toc_entries, "</div>");
221     return @toc_entries;
222 }
223
224 my @this_page_toc = ();
225
226 sub lilypond_print_element_header
227 {
228   my $fh = shift;
229   my $first_in_page = shift;
230   my $previous_is_top = shift;
231   if ($first_in_page and not @this_page_toc) {
232     if (defined($Texi2HTML::THIS_ELEMENT)) {
233       # Create the TOC for this page
234       @this_page_toc = lilypond_generate_page_toc_body($Texi2HTML::THIS_ELEMENT);
235     }
236   }
237   return T2H_DEFAULT_print_element_header( $fh, $first_in_page, $previous_is_top);
238 }
239
240 sub lilypond_toc_body($)
241 {
242     my $elements_list = shift;
243     # Generate a default TOC for pages without THIS_ELEMENT
244     @default_toc = lilypond_generate_page_toc_body(@$elements_list[0]);
245     return T2H_GPL_toc_body($elements_list);
246 }
247
248
249 sub lilypond_css_lines ($$)
250 {
251     my $import_lines = shift;
252     my $rule_lines = shift;
253     return if (defined($CSS_LINES));
254 #     return if (!@$rule_lines and !@$import_lines and (! keys(%css_map)));
255     if (@$rule_lines or @$import_lines)
256     {
257         $CSS_LINES = "<style type=\"text/css\">\n<!--\n";
258         $CSS_LINES .= join('',@$import_lines) . "\n" if (@$import_lines);
259         $CSS_LINES .= join('',@$rule_lines) . "\n" if (@$rule_lines);
260         $CSS_LINES .= "-->\n</style>\n";
261     }
262     foreach my $ref (@CSS_REFS)
263     {
264         $CSS_LINES .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"$ref\">\n";
265     }
266     $CSS_LINES .= "<!--[if lte IE 7]>\n<link href=\"lilypond-ie-fixes.css\" rel=\"stylesheet\" type=\"text/css\">\n<![endif]-->\n";
267 }
268
269
270
271
272
273
274
275 # Print out the TOC in a <div> at the end of th page, which will be formatted as a
276 # sidebar mimicking a TOC frame
277 sub print_lilypond_page_foot($)
278 {
279   my $fh = shift;
280   my $program_string = &$program_string();
281   print $fh "<p><font size='-1'>$program_string</font><br>$PRE_BODY_CLOSE</p>\n";
282   
283   # Print the TOC frame:
284   my @lines = @this_page_toc;
285   # use default TOC if no custom lines have been generated
286   @lines = @default_toc if (not @lines);
287   if (@lines) {
288     print $fh "\n\n<div id=\"tocframe\">";
289     print $fh '<h4> ' . $Texi2HTML::NAME{'Contents'}  . "</h4>\n";
290     foreach my $line (@lines) {
291       print $fh $line;
292     }
293     print $fh "</div>";
294     @this_page_toc = ();
295   }
296   
297   # Close the page:
298   print $fh "</body>\n</html>\n";
299 }
300
301
302
303
304
305
306 sub get_navigation_text
307 {
308   my $button = shift;
309   my $text = $NAVIGATION_TEXT{$button};
310   if ( ($button eq 'Back') or ($button eq 'FastBack') ) {
311     $text = $text . $Texi2HTML::NODE{$button} . "&nbsp;";
312   } elsif ( ($button eq 'Forward') or ($button eq 'FastForward') ) {
313     $text = "&nbsp;" . $Texi2HTML::NODE{$button} . $text;
314   } elsif ( $button eq 'Up' ) {
315     $text = "&nbsp;".$text.":&nbsp;" . $Texi2HTML::NODE{$button} . "&nbsp;";
316   }
317   return $text;
318 }
319
320
321 # Don't automatically create left-aligned table cells for every link, but 
322 # instead create a <td> only on an appropriate '(left|right|center)-aligned-cell-n'
323 # button text. It's alignment as well as the colspan will be taken from the
324 # name of the button. Also, add 'newline' button text to create a new table
325 # row. The texts of the buttons are generated by get_navigation_text and 
326 # will contain the name of the next/previous section/chapter.
327 sub lilypond_print_navigation
328 {
329     my $fh = shift;
330     my $buttons = shift;
331     my $vertical = shift;
332     my $spacing = 1;
333 #     print $fh '<table cellpadding="', $spacing, '" cellspacing="', $spacing,
334 #       "\" border=\"0\" class=\"nav_table\">\n";
335     print $fh "<table class=\"nav_table\">\n";
336
337     print $fh "<tr>" unless $vertical;
338     my $beginofline = 1;
339     foreach my $button (@$buttons)
340     {
341         print $fh qq{<tr valign="top" align="left">\n} if $vertical;
342         # Allow (left|right|center)-aligned-cell and newline as buttons!
343         if ( $button =~ /^(.*)-aligned-cell-(.*)$/ ) 
344         {
345           print $fh qq{</td>} unless $beginofline;
346           print $fh qq{<td valign="middle" align="$1" colspan="$2">};
347           $beginofline = 0;
348         } 
349         elsif ( $button eq 'newline' ) 
350         {
351           print $fh qq{</td>} unless $beginofline;
352           print $fh qq{</tr>};
353           print $fh qq{<tr>};
354           $beginofline = 1;
355
356         } 
357         elsif (ref($button) eq 'CODE')
358         {
359             &$button($fh, $vertical);
360         }
361         elsif (ref($button) eq 'SCALAR')
362         {
363             print $fh "$$button" if defined($$button);
364         }
365         elsif (ref($button) eq 'ARRAY')
366         {
367             my $text = $button->[1];
368             my $button_href = $button->[0];
369             # verify that $button_href is simple text and text is a reference
370             if (defined($button_href) and !ref($button_href) 
371                and defined($text) and (ref($text) eq 'SCALAR') and defined($$text))
372             {             # use given text
373                 if ($Texi2HTML::HREF{$button_href})
374                 {
375                   my $anchor_attributes = '';
376                   if ($USE_ACCESSKEY and (defined($BUTTONS_ACCESSKEY{$button_href})) and ($BUTTONS_ACCESSKEY{$button_href} ne ''))
377                   {
378                       $anchor_attributes = "accesskey=\"$BUTTONS_ACCESSKEY{$button_href}\"";
379                   }
380                   if ($USE_REL_REV and (defined($BUTTONS_REL{$button_href})) and ($BUTTONS_REL{$button_href} ne ''))
381                   {
382                       $anchor_attributes .= " rel=\"$BUTTONS_REL{$button_href}\"";
383                   }
384                   print $fh "" .
385                         &$anchor('',
386                                     $Texi2HTML::HREF{$button_href},
387                                     get_navigation_text($$text),
388                                     $anchor_attributes
389                                    );
390                 }
391                 else
392                 {
393                   print $fh get_navigation_text($$text);
394                 }
395             }
396         }
397         elsif ($button eq ' ')
398         {                       # handle space button
399             print $fh
400                 ($ICONS && $ACTIVE_ICONS{' '}) ?
401                     &$button_icon_img($BUTTONS_NAME{$button}, $ACTIVE_ICONS{' '}) :
402                         $NAVIGATION_TEXT{' '};
403             #next;
404         }
405         elsif ($Texi2HTML::HREF{$button})
406         {                       # button is active
407             my $btitle = $BUTTONS_GOTO{$button} ?
408                 'title="' . $BUTTONS_GOTO{$button} . '"' : '';
409             if ($USE_ACCESSKEY and (defined($BUTTONS_ACCESSKEY{$button})) and ($BUTTONS_ACCESSKEY{$button} ne ''))
410             {
411                 $btitle .= " accesskey=\"$BUTTONS_ACCESSKEY{$button}\"";
412             }
413             if ($USE_REL_REV and (defined($BUTTONS_REL{$button})) and ($BUTTONS_REL{$button} ne ''))
414             {
415                 $btitle .= " rel=\"$BUTTONS_REL{$button}\"";
416             }
417             if ($ICONS && $ACTIVE_ICONS{$button})
418             {                   # use icon
419                 print $fh '' .
420                     &$anchor('',
421                         $Texi2HTML::HREF{$button},
422                         &$button_icon_img($BUTTONS_NAME{$button},
423                                    $ACTIVE_ICONS{$button},
424                                    $Texi2HTML::SIMPLE_TEXT{$button}),
425                         $btitle
426                       );
427             }
428             else
429             {                   # use text
430                 print $fh
431                     '[' .
432                         &$anchor('',
433                                     $Texi2HTML::HREF{$button},
434                                     get_navigation_text ($button),
435                                     $btitle
436                                    ) .
437                                        ']';
438             }
439         }
440         else
441         {                       # button is passive
442             print $fh
443                 $ICONS && $PASSIVE_ICONS{$button} ?
444                     &$button_icon_img($BUTTONS_NAME{$button},
445                                           $PASSIVE_ICONS{$button},
446                                           $Texi2HTML::SIMPLE_TEXT{$button}) :
447
448                                               "[" . get_navigation_text($button) . "]";
449         }
450         print $fh "</td>\n" if $vertical;
451         print $fh "</tr>\n" if $vertical;
452     }
453     print $fh "</td>" unless $beginofline;
454     print $fh "</tr>" unless $vertical;
455     print $fh "</table>\n";
456 }
457
458
459 @Texi2HTML::Config::SECTION_BUTTONS =
460     ('left-aligned-cell-1', 'FastBack', 
461      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
462      'right-aligned-cell-1', 'FastForward',
463      'newline',
464      'left-aligned-cell-2', 'Back',
465      'center-aligned-cell-1', 'Up',
466      'right-aligned-cell-2', 'Forward'
467     );
468
469 # buttons for misc stuff
470 @Texi2HTML::Config::MISC_BUTTONS = ('center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About');
471
472 # buttons for chapter file footers
473 # (and headers but only if SECTION_NAVIGATION is false)
474 @Texi2HTML::Config::CHAPTER_BUTTONS =
475     ('left-aligned-cell-1', 'FastBack', 
476      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
477      'right-aligned-cell-1', 'FastForward',
478     );
479
480 # buttons for section file footers
481 @Texi2HTML::Config::SECTION_FOOTER_BUTTONS =
482     ('left-aligned-cell-1', 'FastBack', 
483      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
484      'right-aligned-cell-1', 'FastForward',
485      'newline',
486      'left-aligned-cell-2', 'Back',
487      'center-aligned-cell-1', 'Up',
488      'right-aligned-cell-2', 'Forward'
489     );
490
491 @Texi2HTML::Config::NODE_FOOTER_BUTTONS =
492     ('left-aligned-cell-1', 'FastBack', 
493      'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
494      'right-aligned-cell-1', 'FastForward',
495      'newline',
496      'left-aligned-cell-2', 'Back',
497      'center-aligned-cell-1', 'Up',
498      'right-aligned-cell-2', 'Forward'
499     );
500
501 # $Texi2HTML::Config::SPLIT = 'section';
502 @Texi2HTML::Config::CSS_REFS      = ("lilypond.css");
503 $Texi2HTML::Config::USE_ACCESSKEY = 1;
504 $Texi2HTML::Config::USE_LINKS     = 1;
505 $Texi2HTML::Config::USE_REL_REV   = 1;
506 $Texi2HTML::Config::element_file_name    = \&split_at_numbered_sections;
507 $Texi2HTML::Config::print_element_header = \&lilypond_print_element_header;
508 $Texi2HTML::Config::print_page_foot      = \&print_lilypond_page_foot;
509 $Texi2HTML::Config::print_navigation     = \&lilypond_print_navigation;
510 $Texi2HTML::Config::external_ref         = \&lilypond_external_ref;
511 $Texi2HTML::Config::toc_body             = \&lilypond_toc_body;
512 $Texi2HTML::Config::css_lines            = \&lilypond_css_lines;
513 $Texi2HTML::Config::finish_out           = \&lilypond_finish_out;
514
515
516 # For split pages, use index(.lang).html as start page!
517 if ($Texi2HTML::Config::SPLIT == 'section') {
518 #   my $lng = $Texi2HTML::THISDOC{'current_lang'};
519 #   if ($lng and ($lng ne "en")) {
520 #     $Texi2HTML::Config::TOP_FILE = 'index.'.$lng.'.html';
521 #   } else {
522     $Texi2HTML::Config::TOP_FILE = 'index.html';
523 #   }
524 }
525
526 # if ($Texi2HTML::THISDOC{'current_lang'}) {
527 #   $Texi2HTML::Config::EXTENSION = $Texi2HTML::THISDOC{'current_lang'} . "." . 
528 #         $docu_ext = $Texi2HTML::Config::EXTENSION;;
529 # }
530
531
532
533 # Try to make use of @translationof to generate files according to the original
534 # English section title...
535 sub lilypond_unknown($$$$$)
536 {
537     my $macro = shift;
538     my $line = shift;
539     my $pass = shift;
540     my $stack = shift;
541     my $state = shift;
542
543     # the @translationof macro provides the original English section title, 
544     # which should be used for file/anchor naming, while the title will be
545     # translated to each language
546     if ($pass == 1 and $macro eq "translationof") {
547       if (ref($state->{'element'})=='HASH') {
548         $state->{'element'}->{'translationof'} = main::normalise_space($line);
549       }
550       return ('', true, undef, undef);
551     } else {
552       return t2h_default_unknown($macro, $line, $pass, $stack, $state);
553     }
554 }
555 $Texi2HTML::Config::unknown                  = \&lilypond_unknown;
556
557 %css_map=();
558
559 return 1;