]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/extract_texi_filenames.py
texi2html: Properly extract sectioning and node commands
[lilypond.git] / buildscripts / extract_texi_filenames.py
1 #!@PYTHON@
2 # -*- coding: utf-8 -*-
3 # extract_texi_filenames.py
4
5 # USAGE:  extract_texi_filenames.py [-o OUTDIR] FILES
6 #
7 # -o OUTDIR specifies that output files should rather be written in OUTDIR
8 #
9 # Description:
10 # This script parses the .texi file given and creates a file with the
11 # nodename <=> filename/anchor map.
12 # The idea behind: Unnumbered subsections go into the same file as the
13 # previous numbered section, @translationof gives the original node name,
14 # which is then used for the filename/anchor.
15 #
16 # If this script is run on a file texifile.texi, it produces a file
17 # texifile[.LANG].xref-map with tab-separated entries of the form
18 #        NODE\tFILENAME\tANCHOR
19 # LANG is the document language in case it's not 'en'
20 # Note: The filename does not have any extension appended!
21 # This file can then be used by our texi2html init script to determine 
22 # the correct file name and anchor for external refs
23
24 import sys
25 import re
26 import os
27 import getopt
28
29 optlist, args = getopt.getopt (sys.argv[1:],'o:')
30 files = args
31
32 outdir = '.'
33 for x in optlist:
34     if x[0] == '-o':
35         outdir = x[1]
36
37 if not os.path.isdir (outdir):
38     if os.path.exists (outdir):
39         os.unlink (outdir)
40     os.makedirs (outdir)
41
42 include_re = re.compile (r'@include ((?!../lily-).*?)\.texi$', re.M)
43 whitespaces = re.compile (r'\s+')
44 section_translation_re = re.compile (r'^@(node|(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?|top|chapter|(?:sub){0,2}section|(?:major|chap|(?:sub){0,2})heading|translationof) (.*?)\s*$', re.MULTILINE)
45
46 def expand_includes (m, filename):
47     filepath = os.path.join (os.path.dirname (filename), m.group(1)) + '.texi'
48     if os.path.exists (filepath):
49         return extract_sections (filepath)[1]
50     else:
51         print "Unable to locate include file " + filepath
52         return ''
53
54 lang_re = re.compile (r'^@documentlanguage (.+)', re.M)
55
56 def extract_sections (filename):
57     result = ''
58     f = open (filename, 'r')
59     page = f.read ()
60     f.close()
61     # Search document language
62     m = lang_re.search (page)
63     if m and m.group (1) != 'en':
64         lang_suffix = '.' + m.group (1)
65     else:
66         lang_suffix = ''
67     # Replace all includes by their list of sections and extract all sections
68     page = include_re.sub (lambda m: expand_includes (m, filename), page)
69     sections = section_translation_re.findall (page)
70     for sec in sections:
71         result += "@" + sec[0] + " " + sec[1] + "\n"
72     return (lang_suffix, result)
73
74 # Convert a given node name to its proper file name (normalization as explained
75 # in the texinfo manual:
76 # http://www.gnu.org/software/texinfo/manual/texinfo/html_node/HTML-Xref-Node-Name-Expansion.html
77 def texinfo_file_name(title):
78     # exception: The top node is always mapped to index.html
79     if title == "Top":
80         return "index"
81     # File name normalization by texinfo (described in the texinfo manual):
82     # 1/2: letters and numbers are left unchanged
83     # 3/4: multiple, leading and trailing whitespace is removed
84     title = title.strip ();
85     title = whitespaces.sub (' ', title)
86     # 5:   all remaining spaces are converted to '-'
87     # 6:   all other 7- or 8-bit chars are replaced by _xxxx (xxxx=ascii character code)
88     result = ''
89     for index in range(len(title)):
90         char = title[index]
91         if char == ' ': # space -> '-'
92             result += '-'
93         elif ( ('0' <= char and char <= '9' ) or
94                ('A' <= char and char <= 'Z' ) or
95                ('a' <= char and char <= 'z' ) ):  # number or letter
96             result += char
97         else:
98             ccode = ord(char)
99             if ccode <= 0xFFFF:
100                 result += "_%04x" % ccode
101             else:
102                 result += "__%06x" % ccode
103     # 7: if name begins with number, prepend 't_g' (so it starts with a letter)
104     if (result != '') and (ord(result[0]) in range (ord('0'), ord('9'))):
105         result = 't_g' + result
106     return result
107
108 texinfo_re = re.compile (r'@.*{(.*)}')
109 def remove_texinfo (title):
110     return texinfo_re.sub (r'\1', title)
111
112 def create_texinfo_anchor (title):
113     return texinfo_file_name (remove_texinfo (title))
114
115 unnumbered_re = re.compile (r'unnumbered.*')
116 def process_sections (filename, lang_suffix, page):
117     sections = section_translation_re.findall (page)
118     basename = os.path.splitext (os.path.basename (filename))[0]
119     p = os.path.join (outdir, basename) + lang_suffix + '.xref-map'
120     f = open (p, 'w')
121
122     this_title = ''
123     this_filename = 'index'
124     this_anchor = ''
125     this_unnumbered = False
126     had_section = False
127     for sec in sections:
128         if sec[0] == "node":
129             # Write out the cached values to the file and start a new section:
130             if this_title != '' and this_title != 'Top':
131                     f.write (this_title + "\t" + this_filename + "\t" + this_anchor + "\n")
132             had_section = False
133             this_title = remove_texinfo (sec[1])
134             this_anchor = create_texinfo_anchor (sec[1])
135         elif sec[0] == "translationof":
136             anchor = create_texinfo_anchor (sec[1])
137             # If @translationof is used, it gives the original node name, which
138             # we use for the anchor and the file name (if it is a numbered node)
139             this_anchor = anchor
140             if not this_unnumbered:
141                 this_filename = anchor
142         else:
143             # Some pages might not use a node for every section, so treat this
144             # case here, too: If we already had a section and encounter enother
145             # one before the next @node, we write out the old one and start
146             # with the new values
147             if had_section and this_title != '':
148                 f.write (this_title + "\t" + this_filename + "\t" + this_anchor + "\n")
149                 this_title = remove_texinfo (sec[1])
150                 this_anchor = create_texinfo_anchor (sec[1])
151             had_section = True
152
153             # unnumbered nodes use the previously used file name, only numbered
154             # nodes get their own filename! However, top-level @unnumbered
155             # still get their own file.
156             this_unnumbered = unnumbered_re.match (sec[0])
157             if not this_unnumbered or sec[0] == "unnumbered":
158                 this_filename = this_anchor
159
160     if this_title != '' and this_title != 'Top':
161         f.write (this_title + "\t" + this_filename + "\t" + this_anchor + "\n")
162     f.close ()
163
164
165 for filename in files:
166     print "extract_texi_filenames.py: Processing %s" % filename
167     (lang_suffix, sections) = extract_sections (filename)
168     process_sections (filename, lang_suffix, sections)