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 options_list, files = getopt.getopt (sys.argv[1:],'o:s:hI:m:',
34 help_text = r"""Usage: %(program_name)s [OPTIONS]... TEXIFILE...
35 Extract files names for texinfo (sub)sections from the texinfo files.
38 -h, --help print this help
39 -I, --include=DIRECTORY append DIRECTORY to include search path
40 -m, --master-map-file=FILE use FILE as master map file
41 -o, --output=DIRECTORY write .xref-map files to DIRECTORY
42 -s, --split=MODE split manual according to MODE. Possible values
43 are section and custom (default)
47 sys.stdout.write ( text)
55 for opt in options_list:
58 if o == '-h' or o == '--help':
59 help (help_text % vars ())
60 if o == '-I' or o == '--include':
62 include_path.append (a)
64 print 'NOT A DIR from: ', os.getcwd (), a
65 elif o == '-o' or o == '--output':
67 elif o == '-s' or o == '--split':
69 elif o == '-m' or o == '--master-map-file':
70 if os.path.isfile (a):
73 raise Exception ('unknown option: ' + o)
76 if not os.path.isdir (outdir):
77 if os.path.exists (outdir):
81 include_re = re.compile (r'@include ((?!../lily-).*?\.i?texi)$', re.M)
82 whitespaces = re.compile (r'\s+')
83 section_translation_re = re.compile ('^@(node|(?:unnumbered|appendix)\
84 (?:(?:sub){0,2}sec)?|top|chapter|(?:sub){0,2}section|\
85 (?:major|chap|(?:sub){0,2})heading|lydoctitle|translationof) \
87 external_node_re = re.compile (r'\s+@c\s+external.*')
89 def expand_includes (m, filename):
90 include_name = m.group (1)
91 filepath = os.path.join (os.path.dirname (filename), include_name)
92 if os.path.exists (filepath):
93 return extract_sections (filepath)[1]
95 for directory in include_path:
96 filepath = os.path.join (directory, include_name)
97 if os.path.exists (filepath):
98 return extract_sections (filepath)[1]
99 print 'No such file: ' + include_name
100 print 'Search path: ' + ':'.join (include_path)
103 lang_re = re.compile (r'^@documentlanguage (.+)', re.M)
105 def extract_sections (filename):
107 f = open (filename, 'r')
110 # Search document language
111 m = lang_re.search (page)
112 if m and m.group (1) != 'en':
113 lang_suffix = '.' + m.group (1)
116 # Replace all includes by their list of sections and extract all sections
117 page = include_re.sub (lambda m: expand_includes (m, filename), page)
118 sections = section_translation_re.findall (page)
120 result += "@" + sec[0] + " " + sec[1] + "\n"
121 return (lang_suffix, result)
123 # Convert a given node name to its proper file name (normalization as
124 # explained in the texinfo manual:
125 # http://www.gnu.org/software/texinfo/manual/texinfo/html_node/HTML-Xref-Node-Name-Expansion.html
126 def texinfo_file_name(title):
127 # exception: The top node is always mapped to index.html
130 # File name normalization by texinfo (described in the texinfo manual):
131 # 1/2: letters and numbers are left unchanged
132 # 3/4: multiple, leading and trailing whitespace is removed
133 title = title.strip ();
134 title = whitespaces.sub (' ', title)
135 # 5: all remaining spaces are converted to '-'
136 # 6: all other 7- or 8-bit chars are replaced by _xxxx (xxxx=ascii character code)
138 for index in range(len(title)):
140 if char == ' ': # space -> '-'
142 elif ( ('0' <= char and char <= '9' ) or
143 ('A' <= char and char <= 'Z' ) or
144 ('a' <= char and char <= 'z' ) ): # number or letter
149 result += "_%04x" % ccode
151 result += "__%06x" % ccode
152 # 7: if name begins with number, prepend 't_g' (so it starts with a letter)
153 if (result != '') and (ord(result[0]) in range (ord('0'), ord('9'))):
154 result = 't_g' + result
157 texinfo_re = re.compile (r'@.*{(.*)}')
158 def remove_texinfo (title):
159 return texinfo_re.sub (r'\1', title)
161 def create_texinfo_anchor (title):
162 return texinfo_file_name (remove_texinfo (title))
164 unnumbered_re = re.compile (r'unnumbered.+|lydoctitle')
165 file_name_section_level = {
174 'unnumberedsubsec':1,
177 'unnumberedsubsubsec':0,
178 'appendixsubsubsec':0
180 if split in file_name_section_level:
181 splitting_level = file_name_section_level[split]
184 def process_sections (filename, lang_suffix, page):
185 sections = section_translation_re.findall (page)
186 basename = os.path.splitext (os.path.basename (filename))[0]
187 p = os.path.join (outdir, basename) + lang_suffix + '.xref-map'
191 this_filename = 'index'
193 this_unnumbered = False
197 # Write out the cached values to the file and start a new
199 if this_title and this_title != 'Top':
200 f.write (this_title + "\t" + this_filename + "\t" + this_anchor + "\n")
202 this_title = remove_texinfo (sec[1])
203 this_anchor = create_texinfo_anchor (sec[1])
204 # delete entry from master map file
205 if this_title in initial_map:
206 del initial_map[this_title]
207 elif sec[0] == "translationof":
208 (original_node, external_node) = external_node_re.subn ('', sec[1])
209 original_node = remove_texinfo (original_node)
210 # The following binds the translator to use the
211 # translated node name in cross-references in case
213 if external_node and original_node in initial_map:
214 del initial_map[original_node]
215 anchor = create_texinfo_anchor (sec[1])
216 # If @translationof is used, it gives the original
217 # node name, which we use for the anchor and the file
218 # name (if it is a numbered node)
220 if not this_unnumbered:
221 this_filename = anchor
222 elif original_node in initial_map:
223 this_filename = initial_map[original_node][2]
225 # Some pages might not use a node for every section, so
226 # treat this case here, too: If we already had a section
227 # and encounter another one before the next @node, we
228 # write out the old one and start with the new values
229 if had_section and this_title:
230 f.write (this_title + "\t" + this_filename + "\t" + this_anchor + "\n")
231 this_title = remove_texinfo (sec[1])
232 this_anchor = create_texinfo_anchor (sec[1])
235 if split == 'custom':
236 # unnumbered nodes use the previously used file name,
237 # only numbered nodes get their own filename! However,
238 # top-level @unnumbered still get their own file.
239 this_unnumbered = unnumbered_re.match (sec[0])
240 if not this_unnumbered:
241 this_filename = this_anchor
242 elif split == 'node':
243 this_filename = this_anchor
245 if sec[0] in file_name_section_level and \
246 file_name_section_level[sec[0]] >= splitting_level:
247 this_filename = this_anchor
249 if this_title and this_title != 'Top':
250 f.write (this_title + "\t" + this_filename + "\t" + this_anchor + "\n")
252 for node in initial_map:
253 f.write ("\t".join (initial_map[node]) + "\n")
256 xref_map_line_re = re.compile (r'(.*?)\t(.*?)\t(.*?)$')
258 for line in open (master_map_file):
259 m = xref_map_line_re.match (line)
261 initial_map[m.group (1)] = (m.group (1), m.group (2), m.group (3))
263 for filename in files:
264 print "extract_texi_filenames.py: Processing %s" % filename
265 (lang_suffix, sections) = extract_sections (filename)
266 process_sections (filename, lang_suffix, sections)