OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 '''This utility converts the html files as emitted by doxygen into ezt files | 7 '''This utility cleans up the html files as emitted by doxygen so |
8 that are suitable for inclusion into Google code site. | 8 that they are suitable for publication on a Google documentation site. |
9 | |
10 EZT stands for "EaZy Templating (for Python)". For more information, see | |
11 http://code.google.com/p/ezt/ | |
12 ''' | 9 ''' |
13 | 10 |
14 import optparse | 11 import optparse |
15 import os | 12 import os |
16 import re | 13 import re |
17 import shutil | 14 import shutil |
18 import string | 15 import string |
19 import sys | 16 import sys |
20 try: | 17 try: |
21 from BeautifulSoup import BeautifulSoup, Tag | 18 from BeautifulSoup import BeautifulSoup, Tag |
22 except (ImportError, NotImplementedError): | 19 except (ImportError, NotImplementedError): |
23 print ("This tool requires the BeautifulSoup package " | 20 print ("This tool requires the BeautifulSoup package " |
24 "(see http://www.crummy.com/software/BeautifulSoup/).\n" | 21 "(see http://www.crummy.com/software/BeautifulSoup/).\n" |
25 "Make sure that the file BeautifulSoup.py is either in this directory " | 22 "Make sure that the file BeautifulSoup.py is either in this directory " |
26 "or is available in your PYTHON_PATH") | 23 "or is available in your PYTHON_PATH") |
27 raise | 24 raise |
28 | 25 |
29 | 26 |
30 class EZTFixer(object): | 27 class HTMLFixer(object): |
31 '''This class converts the html strings as produced by Doxygen into ezt | 28 '''This class cleans up the html strings as produced by Doxygen |
32 strings as used by the Google code site tools | |
33 ''' | 29 ''' |
34 | 30 |
35 def __init__(self, html): | 31 def __init__(self, html): |
36 self.soup = BeautifulSoup(html) | 32 self.soup = BeautifulSoup(html) |
37 | 33 |
38 def FixTableHeadings(self): | 34 def FixTableHeadings(self): |
39 '''Fixes the doxygen table headings to EZT's liking. | 35 '''Fixes the doxygen table headings. |
40 | 36 |
41 This includes using <th> instead of <h2> for the heading, and putting | 37 This includes using <th> instead of <h2> for the heading, and putting |
42 the "name" attribute into the "id" attribute of the <tr> tag. | 38 the "name" attribute into the "id" attribute of the <tr> tag. |
43 | 39 |
44 For example, this html: | 40 For example, this html: |
45 <tr><td colspan="2"><h2><a name="pub-attribs"></a> | 41 <tr><td colspan="2"><h2><a name="pub-attribs"></a> |
46 Data Fields List</h2></td></tr> | 42 Data Fields List</h2></td></tr> |
47 | 43 |
48 would be converted to this: | 44 would be converted to this: |
49 <tr id="pub-attribs"><th colspan="2">Data Fields List</th></tr> | 45 <tr id="pub-attribs"><th colspan="2">Data Fields List</th></tr> |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 | 80 |
85 def FixAll(self): | 81 def FixAll(self): |
86 self.FixTableHeadings() | 82 self.FixTableHeadings() |
87 self.RemoveTopHeadings() | 83 self.RemoveTopHeadings() |
88 | 84 |
89 def __str__(self): | 85 def __str__(self): |
90 return str(self.soup) | 86 return str(self.soup) |
91 | 87 |
92 | 88 |
93 def main(): | 89 def main(): |
94 '''Main entry for the html2ezt utility | 90 '''Main entry for the doxy_cleanup utility |
95 | 91 |
96 html2ezt takes a list of html files and creates a set of ezt files with | 92 doxy_cleanup takes a list of html files and modifies them in place.''' |
97 the same basename and in the same directory as the original html files. | |
98 Each new ezt file contains a file that is suitable for presentation | |
99 on Google Codesite using the EZT tool.''' | |
100 | 93 |
101 parser = optparse.OptionParser(usage='Usage: %prog [options] files...') | 94 parser = optparse.OptionParser(usage='Usage: %prog [options] files...') |
102 | 95 |
103 parser.add_option('-m', '--move', dest='move', action='store_true', | 96 parser.add_option('-m', '--move', dest='move', action='store_true', |
104 default=False, help='move html files to "original_html"') | 97 default=False, help='move html files to "original_html"') |
105 | 98 |
106 options, files = parser.parse_args() | 99 options, files = parser.parse_args() |
107 | 100 |
108 if not files: | 101 if not files: |
109 parser.print_usage() | 102 parser.print_usage() |
110 return 1 | 103 return 1 |
111 | 104 |
112 for filename in files: | 105 for filename in files: |
113 try: | 106 try: |
114 with open(filename, 'r') as file: | 107 with open(filename, 'r') as file: |
115 html = file.read() | 108 html = file.read() |
116 | 109 |
117 fixer = EZTFixer(html) | 110 fixer = HTMLFixer(html) |
118 fixer.FixAll() | 111 fixer.FixAll() |
119 new_name = re.sub(re.compile('\.html$'), '.ezt', filename) | 112 with open(filename, 'w') as file: |
120 with open(new_name, 'w') as file: | |
121 file.write(str(fixer)) | 113 file.write(str(fixer)) |
122 if options.move: | 114 if options.move: |
123 new_directory = os.path.join( | 115 new_directory = os.path.join( |
124 os.path.dirname(os.path.dirname(filename)), 'original_html') | 116 os.path.dirname(os.path.dirname(filename)), 'original_html') |
125 if not os.path.exists(new_directory): | 117 if not os.path.exists(new_directory): |
126 os.mkdir(new_directory) | 118 os.mkdir(new_directory) |
127 shutil.move(filename, new_directory) | 119 shutil.move(filename, new_directory) |
128 except: | 120 except: |
129 print "Error while processing %s" % filename | 121 print "Error while processing %s" % filename |
130 raise | 122 raise |
131 | 123 |
132 return 0 | 124 return 0 |
133 | 125 |
134 if __name__ == '__main__': | 126 if __name__ == '__main__': |
135 sys.exit(main()) | 127 sys.exit(main()) |
OLD | NEW |