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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
import string, re
import sys
from plasTeX.Renderers import Renderer
from plasTeX.Base.TeX import Primitives
# import generated OpenCV function names
# if the file function_names.py does not exist, it
# can be generated using the script find_function_names.sh
try:
from function_names import opencv_function_names
except:
opencv_function_names = []
pass
class XmlRenderer(Renderer):
def default(self, node):
""" Rendering method for all non-text nodes """
s = []
# Handle characters like \&, \$, \%, etc.
if len(node.nodeName) == 1 and node.nodeName not in string.letters:
return self.textDefault(node.nodeName)
# Start tag
s.append('<%s>' % node.nodeName)
# See if we have any attributes to render
if node.hasAttributes():
s.append('<attributes>')
for key, value in node.attributes.items():
# If the key is 'self', don't render it
# these nodes are the same as the child nodes
if key == 'self':
continue
s.append('<%s>%s</%s>' % (key, unicode(value), key))
s.append('</attributes>')
# Invoke rendering on child nodes
s.append(unicode(node))
# End tag
s.append('</%s>' % node.nodeName)
return u'\n'.join(s)
def textDefault(self, node):
""" Rendering method for all text nodes """
return node.replace('&','&').replace('<','<').replace('>','>')
from plasTeX.Renderers import Renderer as BaseRenderer
class reStructuredTextRenderer(BaseRenderer):
aliases = {
'superscript': 'active::^',
'subscript': 'active::_',
'dollar': '$',
'percent': '%',
'opencurly': '{',
'closecurly': '}',
'underscore': '_',
'ampersand': '&',
'hashmark': '#',
'space': ' ',
'tilde': 'active::~',
'at': '@',
'backslash': '\\',
}
def __init__(self, *args, **kwargs):
BaseRenderer.__init__(self, *args, **kwargs)
# Load dictionary with methods
for key in vars(type(self)):
if key.startswith('do__'):
self[self.aliases[key[4:]]] = getattr(self, key)
elif key.startswith('do_'):
self[key[3:]] = getattr(self, key)
self.indent = 0
self.in_func = False
self.in_cvarg = False
self.descriptions = 0
self.after_parameters = False
self.func_short_desc = ''
def do_document(self, node):
return unicode(node)
def do_par(self, node):
if self.indent == -1:
pre = ""
post = ""
else:
pre = "\n" + (" " * self.indent)
post = "\n"
return pre + unicode(node).lstrip(" ") + post
def do_chapter(self, node):
t = str(node.attributes['title'])
section_files = []
for section in node.subsections:
try:
filename = section.filenameoverride
if filename is not None:
section_files.append(filename)
except:
pass
toc = ".. toctree::\n :maxdepth: 2\n\n"
for file in section_files:
if file[-4:] != '.rst':
print >>sys.stderr, "WARNING: unexpected file extension:", file
else:
toc += " %s\n" % file[:-4]
toc += "\n\n"
return "\n\n%s\n%s\n%s\n\n" % ('*' * len(t), t, '*' * len(t)) + toc + unicode(node)
def do_section(self, node):
t = str(node.attributes['title'])
return "\n\n%s\n%s\n\n" % (t, '=' * len(t)) + unicode(node)
def do_subsection(self, node):
t = str(node.attributes['title'])
return "\n\n%s\n%s\n\n" % (t, '-' * len(t)) + unicode(node)
def do_cvdefX(self, node, lang):
if self.language != lang:
return u""
self.indent = -1
self.in_func = False
decl = unicode(node.attributes['a']).rstrip(' ;') # remove trailing ';'
decl_list = decl.split(";")
r = u""
for d in decl_list:
r += u"\n\n.. %s:: %s\n\n" % ({'c' : 'cfunction', 'cpp' : 'cfunction', 'py' : 'function'}[self.language], d.strip())
self.indent = 4
if self.func_short_desc != '':
r += self.ind() + self.func_short_desc + '\n\n'
self.func_short_desc = ''
return r
def do_cvdefC(self, node):
return self.do_cvdefX(node, 'c')
def do_cvcode(self, node):
#body = unicode(node.source).replace(u"\n",u"").replace(u"\\newline", u"\n");
#body = body.replace(u"\\par", u"\n").replace(u"\\cvcode{", "").replace(u"\\", u"")[:-1];
body = unicode(node.source).replace(u"\\newline", u"\n").replace("_ ", "_");
body = body.replace(u"\\par", u"\n").replace(u"\\cvcode{", "").replace(u"\n\n",u"\n");
body = body.replace(u",\n", ",\n ").replace(u"\\", u"")[:-1];
lines = body.split(u"\n")
self.indent += 4
body = "\n".join([u"%s %s" % (self.ind(), s) for s in lines])
r = (u"\n\n%s::\n\n" % self.ind()) + unicode(body) + u"\n\n"
self.indent -= 4
return r
def do_cvdefCpp(self, node):
lang = 'cpp'
if self.language != lang:
return u""
self.indent = -1
self.in_func = False
decl = unicode(node.source).replace(u"\n",u"").replace(u"\\newline", u"").replace(u"_ ", u"_");
decl = decl.replace(u"\\par", u"").replace(u"\\cvdefCpp{", "").replace(u"\\", u"").rstrip(u" ;}");
decl_list = decl.split(";")
r = u""
for d in decl_list:
r += u"\n\n.. %s:: %s\n\n" % ({'c' : 'cfunction', 'cpp' : 'cfunction', 'py' : 'function'}[self.language], d.strip())
self.indent = 4
if self.func_short_desc != '':
r += self.ind() + self.func_short_desc + '\n\n'
self.func_short_desc = ''
return r
def do_cvdefPy(self, node):
return self.do_cvdefX(node, 'py')
def do_description(self, node):
self.descriptions += 1
desc = unicode(node)
self.descriptions -= 1
if self.descriptions == 0:
self.after_parameters = True
return u"\n\n" + desc + u"\n\n"
def do_includegraphics(self, node):
filename = '../' + str(node.attributes['file']).strip()
if not os.path.isfile(filename):
print >>sys.stderr, "WARNING: missing image file", filename
return u""
return u"\n\n%s.. image:: %s\n\n" % (self.ind(), filename)
def do_xfunc(self, node, a='title'):
t = self.get_func_prefix() + unicode(node.attributes[a]).strip()
print "====>", t
label = u"\n\n.. index:: %s\n\n.. _%s:\n\n" % (t, t)
self.in_func = True
self.descriptions = 0
self.after_parameters = False
self.indent = 0
#return u"" + unicode(node)
# Would like to look ahead to reorder things, but cannot see more than 2 ahead
if 0:
print "NODES:", node.source
n = node.nextSibling
while (n != None) and (n.nodeName != 'cvfunc'):
print " ", n.nodeName, len(n.childNodes)
n = n.nextSibling
print "-----"
return label + u"\n\n%s\n%s\n\n" % (t, '-' * len(t)) + unicode(node)
def do_cvfunc(self, node):
return self.do_xfunc(node)
def do_cvclass(self, node):
return self.do_xfunc(node)
def get_func_prefix(self):
return u""
if self.language == 'c':
return u"cv"
if self.language == 'cpp':
return u"cv\\:\\:"
if self.language == 'py':
return u"cv\\."
return u""
def do_cvFunc(self, node):
return self.do_xfunc(node, ['title','alt'][self.language == 'cpp'])
def do_cvCPyFunc(self, node):
return self.do_xfunc(node)
def do_cvCppFunc(self, node):
return self.do_xfunc(node)
def do_cvstruct(self, node):
t = str(node.attributes['title']).strip()
self.after_parameters = False
self.indent = 4
return u".. ctype:: %s" % t + unicode(node)
def do_cvmacro(self, node):
t = str(node.attributes['title']).strip()
self.after_parameters = False
self.indent = 4
return u".. cmacro:: %s" % t + unicode(node)
def showTree(self, node, i = 0):
n = node
while n != None:
print "%s[%s]" % (" " * i, n.nodeName)
if len(n.childNodes) != 0:
self.showTree(n.childNodes[0], i + 4)
n = n.nextSibling
def do_Huge(self, node):
return unicode(node)
def do_tabular(self, node):
if 0:
self.showTree(node)
rows = []
for row in node.childNodes:
cols = []
for col in row.childNodes:
cols.append(unicode(col).strip())
rows.append(cols)
maxes = [ 0 ] * len(rows[0])
for r in rows:
maxes = [ max(m,len(c)) for m,c in zip(maxes, r) ]
sep = "+" + "+".join([ ('-' * (m + 4)) for m in maxes]) + "+"
s = ""
s += sep + "\n"
for r in rows:
#s += "|" + "|".join([ ' ' + c.ljust(m + 3) for c,m in zip(r, maxes) ]) + "|" + "\n"
#s += sep + "\n"
s += self.ind() + "|" + "|".join([ ' ' + c.ljust(m + 3) for c,m in zip(r, maxes) ]) + "|" + "\n"
s += self.ind() + sep + "\n"
return unicode(s)
def do_verbatim(self, node):
return u"\n\n::\n\n " + unicode(node.source.replace('\n', '\n ')) + "\n\n"
def do_index(self, node):
return u""
# No idea why this does not work... JCB
return u"\n\n.. index:: (%s)\n\n" % node.attributes['entry']
def do_label(self, node):
return u""
def fixup_funcname(self, str):
"""
add parentheses to a function name if not already present
"""
str = str.strip()
if str[-1] != ')':
return str + '()'
return str
def gen_reference(self, name):
"""
try to guess whether *name* is a function, struct or macro
and if yes, generate the appropriate reference markup
"""
name = name.strip()
if name[0:2] == 'cv':
return u":cfunc:`%s`" % self.fixup_funcname(name)
elif 'cv'+name in opencv_function_names:
if self.language in ['c', 'cpp']:
return u":cfunc:`cv%s`" % self.fixup_funcname(name)
else:
return u":func:`%s`" % self.fixup_funcname(name)
elif name[0:2] == 'Cv' or name[0:3] == 'Ipl':
return u":ctype:`%s`" % name
elif name[0:2] == 'CV':
return u":cmacro:`%s`" % name
return None
def do_xcross(self, refname):
# try to guess whether t is a function, struct or macro
# and if yes, generate the appropriate reference markup
#rst_ref = self.gen_reference(refname)
#if rst_ref is not None:
# return rst_ref
return u":ref:`%s`" % refname
def do_cross(self, node):
return self.do_xcross(str(node.attributes['name']).strip())
def do_cvCross(self, node):
prefix = self.get_func_prefix()
if self.language == 'cpp':
t = prefix + str(node.attributes['altname']).strip()
return u":ref:`%s`" % t
else:
t = prefix + str(node.attributes['name']).strip()
return self.do_xcross(t)
def do_cvCPyCross(self, node):
t = self.get_func_prefix() + str(node.attributes['name']).strip()
return self.do_xcross(t)
def do_cvCppCross(self, node):
t = self.get_func_prefix() + str(node.attributes['name']).strip()
return u":ref:`%s`" % t
def ind(self):
return u" " * self.indent
def do_cvarg(self, node):
self.indent += 4
# Nested descriptions occur e.g. when a flag parameter can
# be one of several constants. We want to render the inner
# description differently than the outer parameter descriptions.
if self.in_cvarg or self.after_parameters:
defstr = unicode(node.attributes['def'])
assert not (u"\xe2" in unicode(defstr))
self.indent -= 4
param_str = u"\n%s * **%s** - %s\n"
return param_str % (self.ind(), str(node.attributes['item']).strip(), self.fix_quotes(defstr).strip(" "))
# save that we are in a paramater description
self.in_cvarg = True
defstr = unicode(node.attributes['def'])
assert not (u"\xe2" in unicode(defstr))
self.in_cvarg = False
self.indent -= 4
param_str = u"\n%s:param %s: %s"
return param_str % (self.ind(), str(node.attributes['item']).strip(), self.fix_quotes(defstr).strip())
#lines = defstr.split('\n')
#return u"\n%s%s\n%s\n" % (self.ind(), str(node.attributes['item']).strip(), "\n".join([self.ind()+" "+l for l in lines]))
def do_bgroup(self, node):
return u"bgroup(%s)" % node.source
def do_url(self, node):
return unicode(node.attributes['loc'])
def do_enumerate(self, node):
return unicode(node)
def do_itemize(self, node):
return unicode(node)
def do_item(self, node):
#if node.attributes['term'] != None:
if node.attributes.get('term',None):
self.indent += 4
defstr = unicode(node).strip()
assert not (u"\xe2" in unicode(defstr))
self.indent -= 4
return u"\n%s* %s *\n%s %s\n" % (self.ind(), unicode(node.attributes['term']).strip(), self.ind(), defstr)
else:
return u"\n\n%s* %s" % (self.ind(), unicode(node).strip())
def do_textit(self, node):
return "*%s*" % unicode(node.attributes['self'])
def do_texttt(self, node):
t = unicode(node)
# try to guess whether t is a function, struct or macro
# and if yes, generate the appropriate reference markup
rst_ref = self.gen_reference(t)
if rst_ref is not None:
return rst_ref
return u"``%s``" % t
def do__underscore(self, node):
return u"_"
def default(self, node):
print "DEFAULT dropping", node.nodeName
return unicode(node)
def do_lstlisting(self, node):
self.in_func = False
lines = node.source.split('\n')
self.indent += 2
body = "\n".join([u"%s %s" % (self.ind(), s) for s in lines[1:-1]])
r = (u"\n\n%s::\n\n" % self.ind()) + unicode(body) + u"\n\n"
if self.func_short_desc != '':
r = self.ind() + self.func_short_desc + '\n\n' + r
self.func_short_desc = ''
self.indent -= 2
return r
def do_math(self, node):
return u":math:`%s`" % node.source
def do_displaymath(self, node):
words = self.fix_quotes(node.source).strip().split()
return u"\n\n%s.. math::\n\n%s %s\n\n" % (self.ind(), self.ind(), " ".join(words[1:-1]))
def do_maketitle(self, node):
return u""
def do_setcounter(self, node):
return u""
def do_tableofcontents(self, node):
return u""
def do_titleformat(self, node):
return u""
def do_subsubsection(self, node):
return u""
def do_include(self, node):
return u""
def fix_quotes(self, s):
s = s.replace(u'\u2013', "'")
s = s.replace(u'\u2019', "'")
s = s.replace(u'\u2264', "#<2264>")
s = s.replace(u'\xd7', "#<d7>")
return s
def do_cvC(self, node):
if self.language == 'c':
return unicode(node.attributes['a'])
return unicode("")
def do_cvCpp(self, node):
if self.language == 'cpp':
return unicode(node.attributes['a'])
return unicode("")
def do_cvPy(self, node):
if self.language == 'py':
return unicode(node.attributes['a'])
return unicode("")
def do_cvCPy(self, node):
if self.language == 'c' or self.language == 'py':
return unicode(node.attributes['a'])
return unicode("")
def do_ifthenelse(self, node):
# print "IFTHENELSE: [%s],[%s],[%s]" % (node.attributes['test'], str(node.attributes['then']), node.attributes['else'])
print "CONDITION", unicode(node.attributes['test']).strip() == u'true'
if unicode(node.attributes['test']).strip() == u'true':
print "TRUE: [%s]" % str(node.attributes['then'])
return unicode(node.attributes['then'])
else:
return unicode(node.attributes['else'])
def do_equal(self, node):
first = unicode(node.attributes['first']).strip()
second = unicode(node.attributes['second']).strip()
if first == second:
return u'true'
else:
return u'false'
def textDefault(self, node):
if self.in_func:
self.func_short_desc += self.fix_quotes(unicode(node)).strip(" ")
return u""
s = unicode(node)
s = self.fix_quotes(s)
return s
return node.replace('\\_','_')
from plasTeX.TeX import TeX
import os
import pickle
def preprocess_conditionals(fname, suffix, conditionals):
print 'conditionals', conditionals
f = open("../" + fname + ".tex", 'r')
fout = open(fname + suffix + ".tex", 'w')
print 'write', fname + suffix + ".tex"
ifstack=[True]
for l in f.readlines():
ll = l.lstrip()
if ll.startswith("\\if"):
ifstack.append(conditionals.get(ll.rstrip()[3:], False))
elif ll.startswith("\\else"):
ifstack[-1] = not ifstack[-1]
elif ll.startswith("\\fi"):
ifstack.pop()
elif not False in ifstack:
fout.write(l)
f.close()
fout.close()
def parse_documentation_source(language):
# Instantiate a TeX processor and parse the input text
tex = TeX()
tex.ownerDocument.config['files']['split-level'] = 0
master_f = open("../online-opencv.tex", "rt")
out_master_f = open(("../online-opencv-%s.tex" % language), "wt")
flist = []
for l in master_f.readlines():
outl = l
if l.startswith("\\newcommand{\\targetlang}{}"):
outl = l.replace("}", ("%s}" % language))
elif l.startswith("\\input{"):
flist.append(re.findall(r"\{(.+)\}", l)[0])
outl = l.replace("}", ("-%s}" % language))
out_master_f.write(outl)
master_f.close()
out_master_f.close()
index_f = open("index.rst.copy", "rt")
index_lines = list(index_f.readlines())
index_f.close()
out_index_f = open("index.rst", "wt")
header_line = "OpenCV |version| %s Reference" % {"py": "Python", "c": "C", "cpp": "C++"}[language]
index_lines = [header_line + "\n", "="*len(header_line) + "\n", "\n"] + index_lines
for l in index_lines:
out_index_f.write(l)
out_index_f.close()
for f in flist:
preprocess_conditionals(f, '-' + language,
{'C':language=='c', 'Python':language=='py',
'Py':language=='py', 'CPy':(language=='py' or language == 'c'),
'Cpp':language=='cpp', 'plastex':True})
if 1:
tex.input("\\input{online-opencv-%s.tex}" % language)
else:
src0 = r'''
\documentclass{book}
\usepackage{myopencv}
\begin{document}'''
src1 = r'''
\end{document}
'''
lines = list(open("../CvReference.tex"))
LINES = 80
tex.input(src0 + "".join(lines[:LINES]) + src1)
return tex.parse()
language = sys.argv[1]
document = parse_documentation_source(language)
rest = reStructuredTextRenderer()
rest.language = language
rest.render(document)