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
|
#! /usr/bin/python3
from html.parser import HTMLParser
from xml.sax.saxutils import escape
import os, sys, re
class PyHTMLParser(HTMLParser):
pages_to_include = set(('whatsnew/index.html', 'tutorial/index.html', 'using/index.html',
'reference/index.html', 'library/index.html', 'howto/index.html',
'extending/index.html', 'c-api/index.html', 'install/index.html',
))
def __init__(self, basedir, fn, indent, parents=set()):
HTMLParser.__init__(self, convert_charrefs=True)
self.basedir = basedir
self.dir, self.fn = os.path.split(fn)
self.data = ''
self.parents = parents
self.link = {}
self.indent = indent
self.last_indent = indent - 1
self.sub_indent = 0
self.sub_count = 0
self.next_link = False
def escape(self, text):
return escape(text, {'"': '"'})
def process_link(self):
new_href = self.escape(os.path.join(self.dir, self.link['href']))
text = self.escape(self.link['text'])
indent = self.indent + self.sub_indent
if self.last_indent == indent:
print('%s</sub>' % (' ' * self.last_indent))
self.sub_count -= 1
print('%s<sub link="%s" name="%s">' % (' ' * indent, new_href, text))
self.sub_count += 1
self.last_indent = self.indent + self.sub_indent
def handle_starttag(self, tag, attrs):
if tag == 'a':
self.start_a(attrs)
elif tag == 'li':
self.start_li(attrs)
def handle_endtag(self, tag):
if tag == 'a':
self.end_a()
elif tag == 'li':
self.end_li()
def start_li(self, attrs):
self.sub_indent += 1
self.next_link = True
def end_li(self):
indent = self.indent + self.sub_indent
if self.sub_count > 0:
print('%s</sub>' % (' ' * self.last_indent))
self.sub_count -= 1
self.last_indent -= 1
self.sub_indent -= 1
def start_a(self, attrs):
self.link = {}
for attr in attrs:
self.link[attr[0]] = attr[1]
self.data = ''
def end_a(self):
process = False
text = self.escape(self.data.replace('\t', '').replace('\n', ' '))
self.link['text'] = text
# handle a tag without href attribute
try:
href = self.link['href']
except KeyError:
return
abs_href = os.path.join(self.basedir, href)
if abs_href in self.parents:
return
if href.startswith('..') or href.startswith('http:') \
or href.startswith('mailto:') or href.startswith('news:'):
return
if href in ('', 'about.html', 'modindex.html', 'genindex.html', 'glossary.html',
'search.html', 'contents.html', 'download.html', 'bugs.html',
'license.html', 'copyright.html'):
return
if 'class' in self.link:
if self.link['class'] in ('biglink'):
process = True
if self.link['class'] in ('reference external'):
if self.next_link:
process = True
next_link = False
if process == True:
self.process_link()
if href in self.pages_to_include:
self.parse_file(os.path.join(self.dir, href))
def finish(self):
if self.sub_count > 0:
print('%s</sub>' % (' ' * self.last_indent))
def handle_data(self, data):
self.data += data
def parse_file(self, href):
# TODO basedir bestimmen
parent = os.path.join(self.basedir, self.fn)
self.parents.add(parent)
parser = PyHTMLParser(self.basedir, href, self.indent + 1,
self.parents)
text = open(self.basedir + '/' + href, encoding='latin_1').read()
parser.feed(text)
parser.finish()
parser.close()
if parent in self.parents:
self.parents.remove(parent)
class PyIdxHTMLParser(HTMLParser):
def __init__(self, basedir, fn, indent):
HTMLParser.__init__(self, convert_charrefs=True)
self.basedir = basedir
self.dir, self.fn = os.path.split(fn)
self.data = ''
self.link = {}
self.indent = indent
self.active = False
self.indented = False
self.nolink = False
self.header = ''
self.last_letter = 'Z'
self.last_text = ''
def escape(self, text):
return escape(text, {'"': '"'})
def process_link(self):
new_href = self.escape(os.path.join(self.dir, self.link['href']))
text = self.escape(self.link['text'])
if not self.active:
return
if text.startswith('['):
return
if self.link.get('rel', None) in ('prev', 'parent', 'next', 'contents', 'index'):
return
if self.indented:
text = self.last_text + ' ' + text
else:
# Save it in case we need it again
self.last_text = re.sub(' \([\w\-\.\s]+\)', '', text)
indent = self.indent
print('%s<function link="%s" name="%s"/>' % (' ' * indent, new_href, text))
def handle_starttag(self, tag, attrs):
if tag == 'a':
self.start_a(attrs)
elif tag == 'dl':
self.start_dl(attrs)
elif tag == 'dt':
self.start_dt(attrs)
elif tag == 'h2':
self.start_h2(attrs)
elif tag == 'td':
self.start_td(attrs)
elif tag == 'table':
self.start_table(attrs)
def handle_endtag(self, tag):
if tag == 'a':
self.end_a()
elif tag == 'dl':
self.end_dl()
elif tag == 'dt':
self.end_dt()
elif tag == 'h2':
self.end_h2()
elif tag == 'td':
self.end_td()
elif tag == 'table':
self.end_table()
def start_dl(self, attrs):
if self.last_text:
# Looks like we found the second part to a command
self.indented = True
def end_dl(self):
self.indented = False
def start_dt(self, attrs):
self.data = ''
self.nolink = True
def end_dt(self):
if not self.active:
return
if self.nolink == True:
# Looks like we found the first part to a command
self.last_text = re.sub(' \([\w\-\.\s]+\)', '', self.data)
self.nolink = False
def start_h2(self, attrs):
for k, v in attrs:
if k == 'id':
self.header = v
if v == '_':
self.active = True
def end_h2(self):
pass
def start_td(self, attrs):
self.indented = False
self.last_text = ''
def end_td(self):
pass
def start_table(self, attrs):
pass
def end_table(self):
if self.header == self.last_letter:
self.active = False
def start_a(self, attrs):
self.nolink = False
self.link = {}
for attr in attrs:
self.link[attr[0]] = attr[1]
self.data = ''
def end_a(self):
text = self.data.replace('\t', '').replace('\n', ' ')
text = text.replace("Whats ", "What's ")
self.link['text'] = text
# handle a tag without href attribute
try:
href = self.link['href']
except KeyError:
return
self.process_link()
def handle_data(self, data):
self.data += data
def handle_entityref(self, name):
# not meant to be called while convert_charrefs is true
raise AssertionError('entityrefs should not be handled any more')
def main():
base = sys.argv[1]
fn = sys.argv[2]
version = escape(sys.argv[3])
parser = PyHTMLParser(base, fn, indent=0)
print('<?xml version="1.0" encoding="iso-8859-1"?>')
print('<book title="Python %s Documentation" name="Python %s" version="%s" link="index.html">' % (version, version, version))
print('<chapters>')
parser.parse_file(fn)
print('</chapters>')
print('<functions>')
fn = 'genindex-all.html'
parser = PyIdxHTMLParser(base, fn, indent=1)
text = open(base + '/' + fn, encoding='latin_1').read()
parser.feed(text)
parser.close()
print('</functions>')
print('</book>')
main()
|