-
Notifications
You must be signed in to change notification settings - Fork 3
/
extradoc.py
executable file
·293 lines (265 loc) · 11.5 KB
/
extradoc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python
"""\
Extract documentation from one or more Python sources.
Documentation lies in all unprefixed, sextuple-quoted strings.
Usage: extradoc.py [OPTION]... [SOURCE]...
Options:
-c PREFIX Common prefix for all output files.
-s Split output in directory PREFIX, obey #+FILE directives.
-h Produce an HTML file, either PREFIX.html or PREFIX/NAME.html.
-o Produce an Org file, either PREFIX.org or PREFIX/NAME.org.
-p Produce a PDF file, either PREFIX.pdf or PREFIX/NAME.pdf.
-t Produce a translation file, name will be PREFIX.pot.
-v Be verbose and repeat all of Emacs output.
-D SYM Define SYMbol as being True
-D SYM=EXPR Define SYMbol with the value of EXPR.
-I TAGS Only include sections having one of TAGS in their header.
-X TAGS Exclude sections having one of TAGS in their header.
If no SOURCE are given, the program reads and process standard input.
Option -c is mandatory. If -h or -p are used and -o is not, file PREFIX.org
should not pre-exist, as the program internally writes it and then deletes it.
Some non-standard Org directives are recognized:
#+FILE: NAME.org Switch output to NAME.org, also requires -s.
#+IF EXPR Produce following lines only if EXPR is true, else skip.
#+ELIF EXPR Expected meaning within an #+IF block.
#+ELSE Expected meaning within an #+IF block.
#+ENDIF Expected meaning to end an #+IF block.
EXPRs above are Python expressions, eval context comes from -D options.
TAGS represents a comma-separated list of Org tags. To get through, a line
should go through the #+IF system, not be within an excluded section, and if
any included sections is specified, then either be part of one of them or
within the introduction (that is, before the first header).
"""
import codecs
import os
import polib
import re
import sys
import tokenize
encoding = 'UTF-8'
# Within an #+IF [#+ELIF]... [#+ELSE] #+ENDIF structure, the state may
# vary between FALSE, TRUE and SKIP. The state before the structure is
# saved on entry, and restored on exit. The state is TRUE at the
# beginning of a file. If the state before stacking was TRUE, it starts
# as FALSE within the structure; otherwise, the state starts as SKIP.
# States may only go from FALSE to any other state; or else, necessarily
# to SKIP. Within a structure, the state can be TRUE at most once,
# indicating that the tested condition was true. It can be FALSE
# earlier in the structure, and may only be SKIP after the TRUE segment.
FALSE, TRUE, SKIP = range(3)
class Main:
prefix = None
html = False
org = False
pdf = False
pot = False
split = False
verbose = False
included = set()
excluded = set()
def main(self, *arguments):
# Decode options.
self.context = Context()
import getopt
options, arguments = getopt.getopt(arguments, 'D:I:X:c:hopstv')
for option, value in options:
if option == '-D':
if '=' in value:
sym, expr = value.split('=', 1)
self.context[sym.strip()] = eval(value, None, self.context)
else:
self.context[value.strip()] = True
elif option == '-I':
self.included = set(tag.strip() for tag in value.split(','))
elif option == '-X':
self.excluded = set(tag.strip() for tag in value.split(','))
elif option == '-c':
self.prefix = value
elif option == '-d':
self.split = True
elif option == '-h':
self.html = True
elif option == '-o':
self.org = True
elif option == '-p':
self.pdf = True
elif option == '-s':
self.split = True
elif option == '-t':
self.pot = True
elif option == '-v':
self.verbose = True
if not self.prefix:
sys.exit("Option -c is mandatory.")
if ((self.html or self.pdf) and not self.org
and os.path.exists(self.prefix + '.org')):
sys.exit("File %s.org exists and -o not specified." % self.prefix)
# Prepare the work.
if not self.split and (self.html or self.org or self.pdf):
self.org_file = codecs.open(self.prefix + '.org', 'w', encoding)
else:
self.org_file = None
if self.pot:
self.po = polib.POFile()
self.po.metadata = {
'Project-Id-Version': '1.0',
'Report-Msgid-Bugs-To': '[email protected]',
'POT-Creation-Date': '2007-10-18 14:00+0100',
'PO-Revision-Date': '2007-10-18 14:00+0100',
'Last-Translator': 'you <[email protected]>',
'Language-Team': 'English <[email protected]>',
'MIME-Version': '1.0',
'Content-Type': 'text/plain; charset=%s' % encoding,
'Content-Transfer-Encoding': '8bit',
}
# Process all source files.
if arguments:
for argument in arguments:
self.extract_org_fragments(
codecs.open(argument, 'r', encoding),
argument)
else:
self.extract_org_fragments(sys.stdin, '<stdin>')
# Complete the work.
if self.pot:
self.po.save(self.prefix + '.pot')
if self.org_file is not None:
self.org_file.close()
if not self.split:
if self.html:
self.launch_emacs([
self.prefix + '.org',
'--eval', '(setq org-export-allow-BIND t)',
'--eval', '(org-export-as-html nil)'])
if self.pdf:
self.launch_emacs([
self.prefix + '.org',
'--eval', '(setq org-export-allow-BIND t)',
'--eval', '(org-export-as-pdf nil)'])
if (self.html or self.pdf) and not self.org:
os.remove(self.prefix + '.org')
def extract_org_fragments(self, file, name):
# LEVEL not None means inhibit copy from that header level on.
level = None
for line in self.each_org_line(file, name):
if line.startswith('#+FILE:'):
if self.split:
if self.html or self.org or self.pdf:
name = line[7:].strip()
self.org_file = codecs.open(
'%s/%s' % (self.prefix, name), 'w', encoding)
level = None
continue
if line.startswith('*'):
match = re.match('^(\\*+).*?((:[a-z]+)+:)?$', line)
if match:
here_level = len(match.group(1))
if level is None or here_level <= level:
if match.group(2):
tags = set(match.group(2).strip(':').split(':'))
else:
tags = set()
if tags & self.excluded:
level = here_level
elif self.included and not (tags & self.included):
level = here_level
else:
level = None
if level is None and self.org_file is not None:
self.org_file.write(line + '\n')
def each_org_line(self, file, name):
def if_bool(value):
return (FALSE, TRUE)[bool(value)]
def warn(diagnostic):
sys.stderr.write('%s:%s %s\n' % (name, line_no, diagnostic))
self.stack = []
self.state = TRUE
for (code, text, (line_no, _), _, _
) in tokenize.generate_tokens(file.readline):
if code == tokenize.STRING:
if text.startswith('"""'):
text = text[3:-3].replace('\\\n', '')
if self.pot:
self.po.append(polib.POEntry(
msgid=text,
occurrences=[(name, str(line_no))]))
for line in text.splitlines():
if line.startswith('#+IF '):
self.stack.append(self.state)
if self.state == TRUE:
value = eval(line[5:], self.context)
self.state = if_bool(value)
else:
self.state = SKIP
elif line.startswith('#+ELIF '):
if self.state == FALSE:
value = eval(line[7:], self.context)
self.state = if_bool(value)
else:
self.state = SKIP
elif line == '#+ELSE':
if self.state == FALSE:
self.state = TRUE
else:
self.state = SKIP
elif line == '#+ENDIF':
if self.stack:
self.state = self.stack.pop()
else:
warn("Spurious #+ENDIF line.")
elif self.state == TRUE:
if line.startswith('#+FILE:') and not self.split:
warn("#+FILE directive, and not -s.")
yield line
if self.stack:
sys.stderr.write("%s: Some #+ENDIF might be missing.\n" % name)
def launch_emacs(self, args):
import subprocess
for raw in subprocess.Popen(
['emacs', '--batch'] + args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT).stdout:
try:
line = raw.decode(encoding)
except UnicodeDecodeError:
line = raw.decode('ISO-8859-1')
if not self.verbose:
match = re.match('^Loading .*/([^/]+)\\.cache\\.\\.\\.$',
line)
if match:
sys.stderr.write("%s\n" % match.group(1))
continue
if line.startswith((
# Common
'Loading ',
'OVERVIEW',
'Saving file ',
'Wrote ',
# PDF
'Exporting to ',
'LaTeX export done, ',
'Processing LaTeX file ',
# Web
'(No changes need to be saved)',
'(Shell command succeeded ',
'Exporting...',
'HTML export done, ',
'Publishing file ',
'Resetting org-publish-cache',
'Skipping unmodified file ')):
continue
sys.stderr.write(line)
class Context(dict):
"dict type which defaults to either builtin objects or False."
def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
except KeyError:
try:
return getattr(__builtins__, key)
except AttributeError:
return False
run = Main()
main = run.main
if __name__ == '__main__':
main(*sys.argv[1:])