~jderose/ubuntu/raring/python3.3/fix-1131183

« back to all changes in this revision

Viewing changes to debian/pyhtml2devhelp.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-26 15:47:17 UTC
  • Revision ID: package-import@ubuntu.com-20130126154717-9mu2ob20tnuxbkok
Tags: 3.3.0-10ubuntu1
* Merge with Debian; remaining changes:
  - Build-depend on python3:any instead of python3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/python
 
1
#! /usr/bin/python3
2
2
 
3
 
import formatter, htmllib
 
3
from html.parser import HTMLParser
 
4
import formatter
4
5
import os, sys, re
5
6
 
6
 
class PyHTMLParser(htmllib.HTMLParser):
 
7
class PyHTMLParser(HTMLParser):
7
8
    pages_to_include = set(('whatsnew/index.html', 'tutorial/index.html', 'using/index.html',
8
9
                            'reference/index.html', 'library/index.html', 'howto/index.html',
9
10
                            'extending/index.html', 'c-api/index.html', 'install/index.html',
10
11
                            'distutils/index.html'))
11
12
 
12
13
    def __init__(self, formatter, basedir, fn, indent, parents=set()):
13
 
        htmllib.HTMLParser.__init__(self, formatter)
 
14
        HTMLParser.__init__(self, formatter)
14
15
        self.basedir = basedir
15
16
        self.dir, self.fn = os.path.split(fn)
16
17
        self.data = ''
27
28
        text = self.link['text']
28
29
        indent = self.indent + self.sub_indent
29
30
        if self.last_indent == indent:
30
 
            print '%s</sub>' % ('  ' * self.last_indent)
 
31
            print('%s</sub>' % ('  ' * self.last_indent))
31
32
            self.sub_count -= 1
32
 
        print '%s<sub link="%s" name="%s">' % ('  ' * indent, new_href, text)
 
33
        print('%s<sub link="%s" name="%s">' % ('  ' * indent, new_href, text))
33
34
        self.sub_count += 1
34
35
        self.last_indent = self.indent + self.sub_indent
35
36
 
 
37
    def handle_starttag(self, tag, attrs):
 
38
        if tag == 'a':
 
39
            self.start_a(attrs)
 
40
        elif tag == 'li':
 
41
            self.start_li(attrs)
 
42
 
 
43
    def handle_endtag(self, tag):
 
44
        if tag == 'a':
 
45
            self.end_a()
 
46
        elif tag == 'li':
 
47
            self.end_li()
 
48
 
36
49
    def start_li(self, attrs):
37
50
        self.sub_indent += 1
38
51
        self.next_link = True
40
53
    def end_li(self):
41
54
        indent = self.indent + self.sub_indent
42
55
        if self.sub_count > 0:
43
 
            print '%s</sub>' % ('  ' * self.last_indent)
 
56
            print('%s</sub>' % ('  ' * self.last_indent))
44
57
            self.sub_count -= 1
45
58
            self.last_indent -= 1
46
59
        self.sub_indent -= 1
72
85
                    'license.html', 'copyright.html'):
73
86
            return
74
87
 
75
 
        if self.link.has_key('class'):
 
88
        if 'class' in self.link:
76
89
            if self.link['class'] in ('biglink'):
77
90
                process = True
78
91
            if self.link['class'] in ('reference external'):
87
100
 
88
101
    def finish(self):
89
102
        if self.sub_count > 0:
90
 
            print '%s</sub>' % ('  ' * self.last_indent)
 
103
            print('%s</sub>' % ('  ' * self.last_indent))
91
104
 
92
105
    def handle_data(self, data):
93
106
        self.data += data
99
112
        parser = PyHTMLParser(formatter.NullFormatter(),
100
113
                              self.basedir, href, self.indent + 1,
101
114
                              self.parents)
102
 
        text = file(self.basedir + '/' + href).read()
 
115
        text = open(self.basedir + '/' + href, encoding='latin_1').read()
103
116
        parser.feed(text)
104
117
        parser.finish()
105
118
        parser.close()
106
119
        if parent in self.parents:
107
120
            self.parents.remove(parent)
108
121
 
109
 
class PyIdxHTMLParser(htmllib.HTMLParser):
 
122
class PyIdxHTMLParser(HTMLParser):
110
123
    def __init__(self, formatter, basedir, fn, indent):
111
 
        htmllib.HTMLParser.__init__(self, formatter)
 
124
        HTMLParser.__init__(self, formatter)
112
125
        self.basedir = basedir
113
126
        self.dir, self.fn = os.path.split(fn)
114
127
        self.data = ''
136
149
            # Save it in case we need it again
137
150
            self.last_text = re.sub(' \([\w\-\.\s]+\)', '', text)
138
151
        indent = self.indent
139
 
        print '%s<function link="%s" name="%s"/>' % ('  ' * indent, new_href, text)
 
152
        print('%s<function link="%s" name="%s"/>' % ('  ' * indent, new_href, text))
 
153
 
 
154
    def handle_starttag(self, tag, attrs):
 
155
        if tag == 'a':
 
156
            self.start_a(attrs)
 
157
        elif tag == 'dl':
 
158
            self.start_dl(attrs)
 
159
        elif tag == 'dt':
 
160
            self.start_dt(attrs)
 
161
        elif tag == 'h2':
 
162
            self.start_h2(attrs)
 
163
        elif tag == 'td':
 
164
            self.start_td(attrs)
 
165
        elif tag == 'table':
 
166
            self.start_table(attrs)
 
167
 
 
168
    def handle_endtag(self, tag):
 
169
        if tag == 'a':
 
170
            self.end_a()
 
171
        elif tag == 'dl':
 
172
            self.end_dl()
 
173
        elif tag == 'dt':
 
174
            self.end_dt()
 
175
        elif tag == 'h2':
 
176
            self.end_h2()
 
177
        elif tag == 'td':
 
178
            self.end_td()
 
179
        elif tag == 'table':
 
180
            self.end_table()
140
181
 
141
182
    def start_dl(self, attrs):
142
183
        if self.last_text:
165
206
                if v == '_':
166
207
                    self.active = True
167
208
 
 
209
    def end_h2(self):
 
210
        pass
 
211
 
168
212
    def start_td(self, attrs):
169
213
        self.indented = False
170
214
        self.last_text = ''
171
215
 
 
216
    def end_td(self):
 
217
        pass
 
218
 
172
219
    def start_table(self, attrs):
173
220
        pass
174
221
 
184
231
        self.data = ''
185
232
        
186
233
    def end_a(self):
187
 
        text = self.data.replace('\t', '').replace('\n', ' ').replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
 
234
        text = self.data.replace('\t', '').replace('\n', ' ')
 
235
        text = text.replace("Whats ", "What's ")
188
236
        self.link['text'] = text
189
237
        # handle a tag without href attribute
190
238
        try:
196
244
    def handle_data(self, data):
197
245
        self.data += data
198
246
 
 
247
    def handle_entityref(self, name):
 
248
        self.data += '&%s;' % name
 
249
 
199
250
def main():
200
251
    base = sys.argv[1]
201
252
    fn = sys.argv[2]
202
253
    version = sys.argv[3]
203
254
 
204
255
    parser = PyHTMLParser(formatter.NullFormatter(), base, fn, indent=0)
205
 
    print '<?xml version="1.0" encoding="iso-8859-1"?>'
206
 
    print '<book title="Python %s Documentation" name="Python %s" version="%s" link="index.html">' % (version, version, version)
207
 
    print '<chapters>'
 
256
    print('<?xml version="1.0" encoding="iso-8859-1"?>')
 
257
    print('<book title="Python %s Documentation" name="Python %s" version="%s" link="index.html">' % (version, version, version))
 
258
    print('<chapters>')
208
259
    parser.parse_file(fn)
209
 
    print '</chapters>'
 
260
    print('</chapters>')
210
261
 
211
 
    print '<functions>'
 
262
    print('<functions>')
212
263
 
213
264
    fn = 'genindex-all.html'
214
265
    parser = PyIdxHTMLParser(formatter.NullFormatter(), base, fn, indent=1)
215
 
    text = file(base + '/' + fn).read()
 
266
    text = open(base + '/' + fn, encoding='latin_1').read()
216
267
    parser.feed(text)
217
268
    parser.close()
218
269
 
219
 
    print '</functions>'
220
 
    print '</book>'
 
270
    print('</functions>')
 
271
    print('</book>')
221
272
 
222
273
main()