3 package Texi2HTML::Config;
6 $Data::Dumper::Maxdepth = 2;
8 @Texi2HTML::Config::CSS_REFS = ("lilypond.css");
9 $Texi2HTML::Config::USE_ACCESSKEY = 1;
10 $Texi2HTML::Config::USE_LINKS = 1;
11 $Texi2HTML::Config::USE_REL_REV = 1;
12 $Texi2HTML::Config::element_file_name = \&split_at_numbered_sections;
13 $Texi2HTML::Config::print_element_header = \&lilypond_print_element_header;
14 $Texi2HTML::Config::print_page_foot = \&print_lilypond_page_foot;
15 $Texi2HTML::Config::print_navigation = \&lilypond_print_navigation;
16 $Texi2HTML::Config::external_ref = \&lilypond_external_ref;
17 $Texi2HTML::Config::external_href = \&lilypond_external_href;
18 $Texi2HTML::Config::toc_body = \&lilypond_toc_body;
19 $Texi2HTML::Config::css_lines = \&lilypond_css_lines;
20 $Texi2HTML::Config::finish_out = \&lilypond_finish_out;
25 my $page_toc_depth = 2;
27 my @section_to_filename;
30 sub print_element_info($)
33 print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
34 print "Element: $element\n";
35 print Dumper($element);
39 # Convert a given node name to its proper file name (normalization as explained
40 # in the texinfo manual:
41 # http://www.gnu.org/software/texinfo/manual/texinfo/html_node/HTML-Xref-Node-Name-Expansion.html
42 sub texinfo_file_name($)
45 # File name normalization by texinfo:
46 # 1/2: letters and numbers are left unchanged
47 # 3/4: multiple, leading and trailing whitespace is removed
48 $str = main::normalise_space($str);
49 # 5/6: all remaining spaces are converted to '-', all other 7- or 8-bit
50 # chars are replaced by _xxxx (xxxx=ascii character code)
51 my @chars = split(//, $str);
53 foreach my $char (@chars) {
54 if ( $char eq ' ' ) { # space -> '-'
56 } elsif ( ('0' le $char and $char le '9' ) or
57 ('A' le $char and $char le 'Z' ) or
58 ('a' le $char and $char le 'z' ) ) { # number or letter
61 my $ccode = ord($char);
63 if ( ord($char)<= 0xFFFF ) {
64 $addstr = sprintf("_%4x", $ccode);
66 $addstr = sprintf("__%6x", $ccode);
68 # padding is done by spaces, replace by '0'
73 # 7: if name begins with number, prepend 't_g' (so it starts with a letter)
74 if ($str =~ /^[0-9]/) {
83 # This function makes sure that files are only generated for numbered sections,
84 # but not for unnumbered ones. It is called after texi2html has done its own
85 # splitting and simply returns the filename for the node given as first argument
86 # Nodes with the same filename will be printed out to the same filename, so
87 # this really all we need. Also, make sure that the file names for sections
88 # are derived from the section title. We also might want to name the anchors
89 # according to node titles, which works by simply overriding the id element of
91 sub split_at_numbered_sections($$$)
95 my $docu_name = shift;
96 my $docu_ext = $Texi2HTML::Config::EXTENSION;
98 # TOC, footer, about etc. are called with undefined $element and $type == "toc"|"stoc"|"foot"|"about"
99 if ($type eq "toc" or $type eq "stoc" or $type eq "foot" or $type eq "about") {
102 # derive the name of the anchor (i.e. the part after # in the links!),
103 # don't use texi2html's SECx.x default!
104 my $sec_name = main::remove_texi($$element{texi});
105 my $anchor = $sec_name;
106 if ($$element{translationof}) {
107 $anchor = main::remove_texi($$element{translationof});
109 # normalize to the same file name as texinfo
110 $anchor = texinfo_file_name($anchor);
111 $$element{id} = $anchor;
112 # Numbered sections will get a filename Section_1.1.2, unnumbered sections will use
113 # the file name of the previous numbered section:
114 if ($$element{number}) {
115 my $filename = $anchor;
116 $filename .= ".$docu_ext" if (defined($docu_ext));
118 $$element{doc_nr} = $docnr;
119 $lastfilename = $filename;
120 push (@section_to_filename, [$sec_name, $filename, $anchor]);
123 $$element{doc_nr} = $docnr;
124 push (@section_to_filename, [$sec_name, $lastfilename, $anchor]);
125 return $lastfilename;
132 sub lilypond_finish_out()
134 my $map_filename = "$Texi2HTML::THISDOC{'destination_directory'}$Texi2HTML::THISDOC{'file_base_name'}_xref.map";
135 if (open(XREFFILE, ">$map_filename")) {
136 foreach (@section_to_filename) {
137 my ($sec, $file, $anchor) = @$_;
138 print XREFFILE "$sec\t$file\t$anchor\n";
142 print "Can't open $map_filename for writing: $! The map of X-refs will not be written out\n";
148 # The default formatting of external refs returns e.g.
149 # "(lilypond-internals)Timing_translator", so we remove all (...) from the
150 # file_and_node argument. Also, we want only a very simple format, so we don't
151 # even call the default handler!
152 sub lilypond_external_ref($$$$$$)
157 my $file_node = shift;
159 my $cross_ref = shift;
161 $file_node =~ s/\(.*\)//;
162 $file_node = &$anchor('', $href, $file_node) if ($file_node ne '');
163 return &$I('%{node_file_href}', { 'node_file_href' => $file_node });
165 # Default: format as "see <a ..>NODE</a> section 'SECTION' in BOOK"
166 # return t2h_default_external_ref($type, $section, $book, $file_node, $href, $cross_ref);
170 my %translated_books = ();
171 # Construct a href to an external source of information.
172 # node is the node with texinfo @-commands
173 # node_id is the node transliterated and transformed as explained in the
175 # node_xhtml_id is the node transformed such that it is unique and can
176 # be used to make an html cross ref as explained in the texinfo manual
177 # file is the file in '(file)node'
178 sub lilypond_external_href($$$)
182 my $node_xhtml_id = shift;
184 my $original_func = \&t2h_default_external_href;
187 # 1) Keep a hash of book->section_map
188 # 2) if not file in keys hash => try to load the map (assign empty map is non-existent => will load only once!)
189 # 3) if node in the section=>(file, anchor) map, replace node_id and node_xhtml_id by the map's values
190 # 4) call the t2h_default_external_href with these values (or the old ones if not found)
191 print STDERR "lilypond_external_href: texi_node='$node', node_file='$node_id', node_xhtml_id='$node_xhtml_id', file='$file'\n";
192 if (($node_id ne '') and defined($file)) {
193 if (!exists($translated_books{$file})) {
194 print STDERR "Map for book $file not yet loaded, trying to initialize\n";
195 # TODO: Load the file...
196 $translated_books{$file}={};
198 my $section_name_map = $translated_books{$file};
199 if (exists($section_name_map->{$node_id})) {
200 print STDERR "Found key $node_id in section_name_map\n";
201 # TODO: Assign the new values to $file, $node_id and $node_xhtml_id!
203 print STDERR "Unable to find key $node_id in section_name_map\n";
208 # $file = '' if (!defined($file));
209 # my $default_target_split = $EXTERNAL_CROSSREF_SPLIT;
216 # if ($NEW_CROSSREF_STYLE)
218 # $file =~ s/\.[^\.]*$//;
219 # $file =~ s/^.*\///;
221 # if (exists($Texi2HTML::THISDOC{'htmlxref'}->{$file}))
223 # if (exists($Texi2HTML::THISDOC{'htmlxref'}->{$file}->{'split'}))
226 # $href_split = $Texi2HTML::THISDOC{'htmlxref'}->{$file}->{'split'}->{'href'};
228 # if (exists($Texi2HTML::THISDOC{'htmlxref'}->{$file}->{'mono'}))
231 # $href_mono = $Texi2HTML::THISDOC{'htmlxref'}->{$file}->{'mono'}->{'href'};
235 # if ((not $target_mono) and (not $target_split))
236 # { # nothing specified for that manual, use default
237 # $target_split = $default_target_split;
239 # elsif ($target_split and $target_mono)
240 # { # depends on the splitting of the manual
241 # $target_split = $SPLIT;
243 # elsif ($target_mono)
244 # { # only mono specified
250 # if (defined($href_split))
252 # $file = "$href_split";
254 # elsif (defined($EXTERNAL_DIR))
256 # $file = "$EXTERNAL_DIR/$file";
260 # $file = "../$file";
265 # {# target not split
266 # if (defined($href_mono))
268 # $file = "$href_mono";
272 # if (defined($EXTERNAL_DIR))
274 # $file = "$EXTERNAL_DIR/$file";
278 # $file = "../$file";
280 # $file .= "." . $NODE_FILE_EXTENSION;
287 # if (defined($EXTERNAL_DIR))
289 # $file = $EXTERNAL_DIR . $file;
293 # $file = '../' . $file;
299 # $target_split = $default_target_split;
303 # if ($NEW_CROSSREF_STYLE)
307 # return $file . $TOP_NODE_FILE . '.' . $NODE_FILE_EXTENSION . '#Top';
309 # #return $file . '#Top';
313 # return $file . '#Top';
322 # if ($NEW_CROSSREF_STYLE)
325 # $target = $node_xhtml_id;
329 # $node = main::remove_texi($node);
330 # $node =~ s/[^\w\.\-]/-/g;
332 # my $file_basename = $node;
333 # $file_basename = $TOP_NODE_FILE if ($node =~ /^top$/i);
334 # if ($NEW_CROSSREF_STYLE)
338 # return $file . $file_basename . ".$NODE_FILE_EXTENSION" . '#' . $target;
342 # return $file . '#' . $target;
347 # return $file . $file_basename . ".$NODE_FILE_EXTENSION";
350 return &$original_func($node, $node_id, $node_hxmlt_id, $file);
352 return &$original_func($node, $node_id, $node_hxmlt_id);
358 # recursively generate the TOC entries for the element and its children (which
359 # are only shown up to maxlevel. All ancestors of the current element are also
360 # shown with their immediate children, irrespective of their level.
361 sub generate_ly_toc_entries($$$)
364 my $element_path = shift;
365 my $maxlevel = shift;
366 # Skip undefined sections, plus all sections generated by index splitting
367 return() if (not defined($element) or exists($element->{'index_page'}));
369 my $level = $element->{'toc_level'};
370 my $is_parent_of_current = $element_path->{$element->{'number'}};
371 my $print_children = ( ($level < $maxlevel) or $is_parent_of_current );
372 my $ind = ' ' x $level;
373 my $this_css_class = $is_parent_of_current ? " class=\"toc_current\"" : "";
375 my $entry = "$ind<li$this_css_class>" . &$anchor ($element->{'tocid'}, "$element->{'file'}#$element->{'id'}",$element->{'text'});
377 my $children = $element->{'section_childs'};
378 if ( $print_children and defined($children) and (ref($children) eq "ARRAY") ) {
379 push (@result, $entry);
380 my @child_result = ();
381 foreach (@$children) {
382 push (@child_result, generate_ly_toc_entries($_, $element_path, $maxlevel));
384 # if no child nodes were generated, e.g. for the index, where expanded pages
385 # are ignored, don't generate a list at all...
387 push (@result, "$ind<ul$NO_BULLET_LIST_ATTRIBUTE>");
388 push (@result, @child_result);
389 push (@result, "$ind</ul></li>\n");
392 push (@result, $entry . "</li>\n");
398 # Print a customized TOC, containing only the first two levels plus the whole
399 # path to the current page
400 sub lilypond_generate_page_toc_body($)
403 my $current_element = $element;
405 $parentelements{$element->{'number'}} = 1;
406 # Find the path to the current element
407 while ( defined($current_element->{'sectionup'}) and
408 ($current_element->{'sectionup'} ne $current_element) )
410 $parentelements{$current_element->{'sectionup'}->{'number'}} = 1
411 if ($current_element->{'sectionup'}->{'number'} ne '');
412 $current_element = $current_element->{'sectionup'};
414 return () if not defined($current_element);
415 # Create the toc entries recursively
416 my @toc_entries = ("<div class=\"contents\">", "<ul$NO_BULLET_LIST_ATTRIBUTE>");
417 my $children = $current_element->{'section_childs'};
418 foreach ( @$children ) {
419 push (@toc_entries, generate_ly_toc_entries($_, \%parentelements, $page_toc_depth));
421 push (@toc_entries, "</ul>");
422 push (@toc_entries, "</div>");
426 my @this_page_toc = ();
428 sub lilypond_print_element_header
431 my $first_in_page = shift;
432 my $previous_is_top = shift;
433 if ($first_in_page and not @this_page_toc) {
434 if (defined($Texi2HTML::THIS_ELEMENT)) {
435 # Create the TOC for this page
436 @this_page_toc = lilypond_generate_page_toc_body($Texi2HTML::THIS_ELEMENT);
439 return T2H_DEFAULT_print_element_header( $fh, $first_in_page, $previous_is_top);
442 sub lilypond_toc_body($)
444 my $elements_list = shift;
445 # Generate a default TOC for pages without THIS_ELEMENT
446 @default_toc = lilypond_generate_page_toc_body(@$elements_list[0]);
447 return T2H_GPL_toc_body($elements_list);
451 sub lilypond_css_lines ($$)
453 my $import_lines = shift;
454 my $rule_lines = shift;
455 return if (defined($CSS_LINES));
456 # return if (!@$rule_lines and !@$import_lines and (! keys(%css_map)));
457 if (@$rule_lines or @$import_lines)
459 $CSS_LINES = "<style type=\"text/css\">\n<!--\n";
460 $CSS_LINES .= join('',@$import_lines) . "\n" if (@$import_lines);
461 $CSS_LINES .= join('',@$rule_lines) . "\n" if (@$rule_lines);
462 $CSS_LINES .= "-->\n</style>\n";
464 foreach my $ref (@CSS_REFS)
466 $CSS_LINES .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"$ref\">\n";
468 $CSS_LINES .= "<!--[if lte IE 7]>\n<link href=\"lilypond-ie-fixes.css\" rel=\"stylesheet\" type=\"text/css\">\n<![endif]-->\n";
477 # Print out the TOC in a <div> at the end of th page, which will be formatted as a
478 # sidebar mimicking a TOC frame
479 sub print_lilypond_page_foot($)
482 my $program_string = &$program_string();
483 print $fh "<p><font size='-1'>$program_string</font><br>$PRE_BODY_CLOSE</p>\n";
485 # Print the TOC frame:
486 my @lines = @this_page_toc;
487 # use default TOC if no custom lines have been generated
488 @lines = @default_toc if (not @lines);
490 print $fh "\n\n<div id=\"tocframe\">";
491 print $fh '<h4> ' . $Texi2HTML::NAME{'Contents'} . "</h4>\n";
492 foreach my $line (@lines) {
500 print $fh "</body>\n</html>\n";
508 sub get_navigation_text
511 my $text = $NAVIGATION_TEXT{$button};
512 if ( ($button eq 'Back') or ($button eq 'FastBack') ) {
513 $text = $text . $Texi2HTML::NODE{$button} . " ";
514 } elsif ( ($button eq 'Forward') or ($button eq 'FastForward') ) {
515 $text = " " . $Texi2HTML::NODE{$button} . $text;
516 } elsif ( $button eq 'Up' ) {
517 $text = " ".$text.": " . $Texi2HTML::NODE{$button} . " ";
523 # Don't automatically create left-aligned table cells for every link, but
524 # instead create a <td> only on an appropriate '(left|right|center)-aligned-cell-n'
525 # button text. It's alignment as well as the colspan will be taken from the
526 # name of the button. Also, add 'newline' button text to create a new table
527 # row. The texts of the buttons are generated by get_navigation_text and
528 # will contain the name of the next/previous section/chapter.
529 sub lilypond_print_navigation
533 my $vertical = shift;
535 # print $fh '<table cellpadding="', $spacing, '" cellspacing="', $spacing,
536 # "\" border=\"0\" class=\"nav_table\">\n";
537 print $fh "<table class=\"nav_table\">\n";
539 print $fh "<tr>" unless $vertical;
541 foreach my $button (@$buttons)
543 print $fh qq{<tr valign="top" align="left">\n} if $vertical;
544 # Allow (left|right|center)-aligned-cell and newline as buttons!
545 if ( $button =~ /^(.*)-aligned-cell-(.*)$/ )
547 print $fh qq{</td>} unless $beginofline;
548 print $fh qq{<td valign="middle" align="$1" colspan="$2">};
551 elsif ( $button eq 'newline' )
553 print $fh qq{</td>} unless $beginofline;
559 elsif (ref($button) eq 'CODE')
561 &$button($fh, $vertical);
563 elsif (ref($button) eq 'SCALAR')
565 print $fh "$$button" if defined($$button);
567 elsif (ref($button) eq 'ARRAY')
569 my $text = $button->[1];
570 my $button_href = $button->[0];
571 # verify that $button_href is simple text and text is a reference
572 if (defined($button_href) and !ref($button_href)
573 and defined($text) and (ref($text) eq 'SCALAR') and defined($$text))
575 if ($Texi2HTML::HREF{$button_href})
577 my $anchor_attributes = '';
578 if ($USE_ACCESSKEY and (defined($BUTTONS_ACCESSKEY{$button_href})) and ($BUTTONS_ACCESSKEY{$button_href} ne ''))
580 $anchor_attributes = "accesskey=\"$BUTTONS_ACCESSKEY{$button_href}\"";
582 if ($USE_REL_REV and (defined($BUTTONS_REL{$button_href})) and ($BUTTONS_REL{$button_href} ne ''))
584 $anchor_attributes .= " rel=\"$BUTTONS_REL{$button_href}\"";
588 $Texi2HTML::HREF{$button_href},
589 get_navigation_text($$text),
595 print $fh get_navigation_text($$text);
599 elsif ($button eq ' ')
600 { # handle space button
602 ($ICONS && $ACTIVE_ICONS{' '}) ?
603 &$button_icon_img($BUTTONS_NAME{$button}, $ACTIVE_ICONS{' '}) :
604 $NAVIGATION_TEXT{' '};
607 elsif ($Texi2HTML::HREF{$button})
609 my $btitle = $BUTTONS_GOTO{$button} ?
610 'title="' . $BUTTONS_GOTO{$button} . '"' : '';
611 if ($USE_ACCESSKEY and (defined($BUTTONS_ACCESSKEY{$button})) and ($BUTTONS_ACCESSKEY{$button} ne ''))
613 $btitle .= " accesskey=\"$BUTTONS_ACCESSKEY{$button}\"";
615 if ($USE_REL_REV and (defined($BUTTONS_REL{$button})) and ($BUTTONS_REL{$button} ne ''))
617 $btitle .= " rel=\"$BUTTONS_REL{$button}\"";
619 if ($ICONS && $ACTIVE_ICONS{$button})
623 $Texi2HTML::HREF{$button},
624 &$button_icon_img($BUTTONS_NAME{$button},
625 $ACTIVE_ICONS{$button},
626 $Texi2HTML::SIMPLE_TEXT{$button}),
635 $Texi2HTML::HREF{$button},
636 get_navigation_text ($button),
643 { # button is passive
645 $ICONS && $PASSIVE_ICONS{$button} ?
646 &$button_icon_img($BUTTONS_NAME{$button},
647 $PASSIVE_ICONS{$button},
648 $Texi2HTML::SIMPLE_TEXT{$button}) :
650 "[" . get_navigation_text($button) . "]";
652 print $fh "</td>\n" if $vertical;
653 print $fh "</tr>\n" if $vertical;
655 print $fh "</td>" unless $beginofline;
656 print $fh "</tr>" unless $vertical;
657 print $fh "</table>\n";
661 @Texi2HTML::Config::SECTION_BUTTONS =
662 ('left-aligned-cell-1', 'FastBack',
663 'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
664 'right-aligned-cell-1', 'FastForward',
666 'left-aligned-cell-2', 'Back',
667 'center-aligned-cell-1', 'Up',
668 'right-aligned-cell-2', 'Forward'
671 # buttons for misc stuff
672 @Texi2HTML::Config::MISC_BUTTONS = ('center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About');
674 # buttons for chapter file footers
675 # (and headers but only if SECTION_NAVIGATION is false)
676 @Texi2HTML::Config::CHAPTER_BUTTONS =
677 ('left-aligned-cell-1', 'FastBack',
678 'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
679 'right-aligned-cell-1', 'FastForward',
682 # buttons for section file footers
683 @Texi2HTML::Config::SECTION_FOOTER_BUTTONS =
684 ('left-aligned-cell-1', 'FastBack',
685 'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
686 'right-aligned-cell-1', 'FastForward',
688 'left-aligned-cell-2', 'Back',
689 'center-aligned-cell-1', 'Up',
690 'right-aligned-cell-2', 'Forward'
693 @Texi2HTML::Config::NODE_FOOTER_BUTTONS =
694 ('left-aligned-cell-1', 'FastBack',
695 'center-aligned-cell-3', 'Top', 'Contents', 'Index', 'About',
696 'right-aligned-cell-1', 'FastForward',
698 'left-aligned-cell-2', 'Back',
699 'center-aligned-cell-1', 'Up',
700 'right-aligned-cell-2', 'Forward'
705 # For split pages, use index.html as start page!
706 if ($Texi2HTML::Config::SPLIT == 'section') {
707 $Texi2HTML::Config::TOP_FILE = 'index.html';
712 # Try to make use of @translationof to generate files according to the original
713 # English section title...
714 sub lilypond_unknown($$$$$)
722 # the @translationof macro provides the original English section title,
723 # which should be used for file/anchor naming, while the title will be
724 # translated to each language
725 if ($pass == 1 and $macro eq "translationof") {
726 if (ref($state->{'element'})=='HASH') {
727 $state->{'element'}->{'translationof'} = main::normalise_space($line);
729 return ('', true, undef, undef);
731 return t2h_default_unknown($macro, $line, $pass, $stack, $state);
734 $Texi2HTML::Config::unknown = \&lilypond_unknown;