]>
Commit | Line | Data |
---|---|---|
f329fa92 | 1 | #!/usr/bin/env python |
2 | ||
3 | import sys | |
4 | import os | |
5 | import re | |
6 | ||
7 | ## @package thtml2doxy | |
8 | # Roughly translates THtml C++ comments to Doxygen. | |
9 | # | |
10 | # Usage: | |
11 | # | |
12 | # `thtml2doxy file1 [file2 [file3...]]` | |
13 | # | |
14 | # @author Dario Berzano <dario.berzano@cern.ch> | |
15 | # @date 2014-12-04 | |
16 | ||
17 | ## The main function. | |
18 | # | |
19 | # **Note:** this program only has this function. | |
20 | def main(argv): | |
21 | ||
22 | #reblock = r'^(\s*)//\s+(.*)\s*$' | |
23 | reblock = r'^//\s+(.*)\s*$' | |
24 | reignore = r'((Begin|End)_Html|^_+$)' | |
25 | restrip = r'(?i)</?P>|</?H[0-9]>|<BR/?>' | |
26 | refield = r'(?i)^(date|authors?):\s*(.*)$' | |
27 | reclass = r'/?([^./]+)\.[^.]+$' | |
28 | ||
29 | inblock = False | |
30 | endblock = False | |
31 | ||
32 | for fn in argv[1:]: | |
33 | ||
34 | mclass = re.search( reclass, fn ) | |
35 | classname = mclass.group(1) | |
36 | ||
37 | fnout = fn + '.doxyout' | |
38 | try: | |
39 | with open(fn, 'r') as fp, open(fnout, 'w') as fout: | |
40 | print '%s...' % fn | |
41 | for rawline in fp: | |
42 | ||
43 | mblock = re.match(reblock, rawline) | |
44 | ||
45 | if mblock and not endblock and not inblock: | |
46 | # beginning of a block | |
47 | fout.write('/// \class %s\n' % classname) | |
48 | inblock = True | |
49 | elif not mblock and inblock: | |
50 | # end of a block | |
51 | inblock = False | |
52 | endblock = True | |
53 | ||
54 | if inblock: | |
55 | ||
56 | #indent = mblock.group(1) | |
57 | #text = mblock.group(2) | |
58 | indent = '' | |
59 | text = mblock.group(1) | |
60 | ||
61 | if re.match(reignore, text): | |
62 | pass | |
63 | else: | |
64 | ||
65 | stripped = re.sub( restrip, '', text ).strip() | |
66 | ||
67 | mfield = re.match(refield, stripped) | |
68 | if mfield: | |
69 | field = mfield.group(1).lower() | |
70 | value = mfield.group(2) | |
71 | fout.write( '%s/// \%s: %s\n' % (indent, field, value) ) | |
72 | ||
73 | else: | |
74 | # print by restoring indent and with correct comment | |
75 | if len(stripped) > 0: | |
76 | stripped = ' ' + stripped | |
77 | fout.write( '%s///%s\n' % (indent, stripped) ) | |
78 | ||
79 | else: | |
80 | # no comment | |
81 | fout.write( rawline ) | |
82 | ||
83 | except IOError as e: | |
84 | print '[ERROR] cannot open %s: %s' % (fn, e) | |
85 | ||
86 | try: | |
87 | os.remove( fn ) | |
88 | os.rename( fnout, fn ) | |
89 | except OSError as e: | |
90 | print '[ERROR] renaming/removing: %s (aborting)' % e | |
91 | return 1 | |
92 | ||
93 | return 0 | |
94 | ||
95 | if __name__ == '__main__': | |
96 | sys.exit( main( sys.argv ) ) |