| 1 | import htmllib, formatter, string, os, pprint, types, sys, glob |
| 2 | |
| 3 | api_name = sys.argv[1] #'wxpyapi' |
| 4 | api_path = sys.argv[2] #'./docs/api/' |
| 5 | |
| 6 | |
| 7 | header_hhx = '''<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> |
| 8 | <HTML> |
| 9 | <head> |
| 10 | <meta name="GENERATOR" content="BuildAPIHelpBook"> |
| 11 | </HEAD><BODY> |
| 12 | <OBJECT type="text/site properties"> |
| 13 | <param name="ImageType" value="Folder"> |
| 14 | </OBJECT> |
| 15 | ''' |
| 16 | |
| 17 | entry_hhx = '''<LI> <OBJECT type="text/sitemap"> |
| 18 | <param name="Local" value="%s"> |
| 19 | <param name="Name" value="%s"> |
| 20 | </OBJECT> |
| 21 | ''' |
| 22 | |
| 23 | file_hhp = '''[OPTIONS] |
| 24 | Compatibility=1.1 |
| 25 | Full-text search=Yes |
| 26 | Contents file=%(n)s.hhc |
| 27 | Compiled file=%(n)s.chm |
| 28 | Default Window=wxHelp |
| 29 | Default topic=index.html |
| 30 | Index file=%(n)s.hhk |
| 31 | Title=wxPython API Documentation |
| 32 | |
| 33 | [WINDOWS] |
| 34 | wxHelp=,"%(n)s.hhc","%(n)s.hhk","index.html",,,,,,0x2420,,0x380e,,,,,0,,, |
| 35 | |
| 36 | [FILES] |
| 37 | ''' |
| 38 | |
| 39 | def read_segment(fn, start, end): |
| 40 | data = open(fn).read() |
| 41 | begin = data.find(start)+len(start) |
| 42 | return data[begin:data.find(end, begin)] |
| 43 | |
| 44 | |
| 45 | #------------------------------------------------------------------------------- |
| 46 | |
| 47 | def build_project(): |
| 48 | hhp = file_hhp % {'n' : api_name} |
| 49 | |
| 50 | def doglob(mask): |
| 51 | st = '' |
| 52 | for name in glob.glob(os.path.join(api_path, mask)): |
| 53 | name = os.path.split(name)[-1] |
| 54 | st += name + '\n' |
| 55 | return st |
| 56 | |
| 57 | hhp += doglob("*.html") |
| 58 | hhp += doglob("*.css") |
| 59 | |
| 60 | open(os.path.join(api_path, api_name+'.hhp'), 'w').write(hhp) |
| 61 | |
| 62 | #------------------------------------------------------------------------------- |
| 63 | |
| 64 | def build_contents(): |
| 65 | def traverse(l, r): |
| 66 | for i in l: |
| 67 | if type(i) is types.ListType: |
| 68 | r.append('<UL>'+os.linesep) |
| 69 | traverse(i, r) |
| 70 | r.append('</UL>'+os.linesep) |
| 71 | elif type(i) is types.TupleType: |
| 72 | r.append(entry_hhx%i) |
| 73 | else: |
| 74 | raise Exception, 'Unhandled type: %s'%type(i) |
| 75 | |
| 76 | data = read_segment(os.path.join(api_path, 'trees.html'), |
| 77 | '<!-- =========== START OF CLASS HIERARCHY =========== -->', |
| 78 | '<!-- =========== START OF NAVBAR =========== -->') |
| 79 | p = APIContentsParser(formatter.NullFormatter(formatter.NullWriter())) |
| 80 | p.feed(data) |
| 81 | |
| 82 | class_hierarchy = [] |
| 83 | traverse(p.current, class_hierarchy) |
| 84 | |
| 85 | data = read_segment(os.path.join(api_path, 'wx-module.html'), |
| 86 | '<!-- =========== START OF SUBMODULES =========== -->', |
| 87 | '<!-- =========== START OF CLASSES =========== -->') |
| 88 | p = APIContentsParser(formatter.NullFormatter(formatter.NullWriter())) |
| 89 | p.feed(data) |
| 90 | submodules = [] |
| 91 | traverse(p.current, submodules) |
| 92 | |
| 93 | hhc = header_hhx+\ |
| 94 | '<UL>'+os.linesep+entry_hhx%('wx-module.html', 'Submodules')+\ |
| 95 | ''.join(submodules)+'</UL>'+os.linesep+\ |
| 96 | '<UL>'+os.linesep+entry_hhx%('trees.html', 'Class Hierarchy')+\ |
| 97 | ''.join(class_hierarchy)+'</UL>'+os.linesep |
| 98 | |
| 99 | open(os.path.join(api_path, api_name+'.hhc'), 'w').write(hhc) |
| 100 | |
| 101 | |
| 102 | class APIContentsParser(htmllib.HTMLParser): |
| 103 | def __init__(self, formatter, verbose=0): |
| 104 | htmllib.HTMLParser.__init__(self, formatter, verbose) |
| 105 | |
| 106 | self.contents = [] |
| 107 | self.history = [] |
| 108 | self.current = self.contents |
| 109 | |
| 110 | self.cur_href = None |
| 111 | self.li_a_cnt = 0 |
| 112 | |
| 113 | def start_li(self, attrs): |
| 114 | self.li_a_cnt = 0 |
| 115 | |
| 116 | def start_a(self, attrs): |
| 117 | self.li_a_cnt += 1 |
| 118 | if self.li_a_cnt == 1: |
| 119 | if attrs[0][0] == 'href': |
| 120 | self.cur_href = attrs[0][1] |
| 121 | |
| 122 | def end_a(self): |
| 123 | self.cur_href = None |
| 124 | |
| 125 | def start_code(self, attrs): |
| 126 | self.save_bgn() |
| 127 | |
| 128 | def end_code(self): |
| 129 | text = string.strip(self.save_end()) |
| 130 | if self.cur_href and text: |
| 131 | self.current.append( (self.cur_href, text) ) |
| 132 | |
| 133 | def start_ul(self, attrs): |
| 134 | self.history.append(self.current) |
| 135 | self.current = [] |
| 136 | |
| 137 | def end_ul(self): |
| 138 | c = self.history.pop() |
| 139 | c.append(self.current) |
| 140 | self.current = c |
| 141 | |
| 142 | #------------------------------------------------------------------------------- |
| 143 | |
| 144 | def build_keywords(): |
| 145 | data = read_segment(os.path.join(api_path, 'indices.html'), |
| 146 | '<!-- =========== START OF IDENTIFIER INDEX =========== -->', |
| 147 | '<!-- =========== START OF NAVBAR =========== -->') |
| 148 | p = APIIndicesParser(formatter.NullFormatter(formatter.NullWriter())) |
| 149 | p.feed(data) |
| 150 | |
| 151 | hhk = header_hhx+ '<UL>'+os.linesep+\ |
| 152 | ''.join([entry_hhx%(u, k) for u, k in p.indices])+os.linesep+'</UL>' |
| 153 | open(os.path.join(api_path, api_name+'.hhk'), 'w').write(hhk) |
| 154 | |
| 155 | |
| 156 | class APIIndicesParser(htmllib.HTMLParser): |
| 157 | def __init__(self, formatter, verbose=0): |
| 158 | htmllib.HTMLParser.__init__(self, formatter, verbose) |
| 159 | |
| 160 | self.indices = [] |
| 161 | self.cur_href = None |
| 162 | self.tr_a_cnt = 0 |
| 163 | |
| 164 | def start_tr(self, attrs): |
| 165 | self.tr_a_cnt = 0 |
| 166 | |
| 167 | def end_tr(self): |
| 168 | self.tr_a_cnt = 0 |
| 169 | |
| 170 | def start_a(self, attrs): |
| 171 | self.tr_a_cnt += 1 |
| 172 | if self.tr_a_cnt == 1: |
| 173 | if attrs[0][0] == 'href': |
| 174 | self.cur_href = attrs[0][1] |
| 175 | |
| 176 | def end_a(self): |
| 177 | self.cur_href = None |
| 178 | |
| 179 | def start_code(self, attrs): |
| 180 | self.save_bgn() |
| 181 | |
| 182 | def end_code(self): |
| 183 | text = string.strip(self.save_end()) |
| 184 | if self.cur_href and text: |
| 185 | self.indices.append( (self.cur_href, text) ) |
| 186 | |
| 187 | #------------------------------------------------------------------------------- |
| 188 | |
| 189 | if __name__ == '__main__': |
| 190 | print 'Building project...' |
| 191 | build_project() |
| 192 | print 'Building contents...' |
| 193 | build_contents() |
| 194 | print 'Building keywords...' |
| 195 | build_keywords() |