2 # -*- coding: utf-8 -*-
3 # extract_texi_filenames.py
5 # USAGE: extract_texi_filenames.py [-o OUTDIR] FILES
7 # -o OUTDIR specifies that output files should rather be written in OUTDIR
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.
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
29 optlist, args = getopt.getopt (sys.argv[1:],'o:')
37 if not os.path.isdir (outdir):
38 if os.path.exists (outdir):
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)
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]
51 print "Unable to locate include file " + filepath
54 lang_re = re.compile (r'^@documentlanguage (.+)', re.M)
56 def extract_sections (filename):
58 f = open (filename, 'r')
61 # Search document language
62 m = lang_re.search (page)
63 if m and m.group (1) != 'en':
64 lang_suffix = '.' + m.group (1)
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)
71 result += "@" + sec[0] + " " + sec[1] + "\n"
72 return (lang_suffix, result)
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
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)
89 for index in range(len(title)):
91 if char == ' ': # space -> '-'
93 elif ( ('0' <= char and char <= '9' ) or
94 ('A' <= char and char <= 'Z' ) or
95 ('a' <= char and char <= 'z' ) ): # number or letter
100 result += "_%04x" % ccode
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
108 texinfo_re = re.compile (r'@.*{(.*)}')
109 def remove_texinfo (title):
110 return texinfo_re.sub (r'\1', title)
112 def create_texinfo_anchor (title):
113 return texinfo_file_name (remove_texinfo (title))
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'
123 this_filename = 'index'
125 this_unnumbered = False
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")
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)
140 if not this_unnumbered:
141 this_filename = anchor
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])
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
160 if this_title != '' and this_title != 'Top':
161 f.write (this_title + "\t" + this_filename + "\t" + this_anchor + "\n")
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)