Skip to content

Commit 24bd026

Browse files
tormodvoldenperexg
authored andcommitted
doc: Port from htmllib to html.parser HTMLParser
htmllib was deprecated in 2.6 and removed in 3.x. Instead of dealing with non-breakable space in the HTMLParser, transform to spaces in parse_asoundlib_api() instead. At the same time adjust to the current ALSA API web page format. Closes: #15 Signed-off-by: Tormod Volden <[email protected]> Signed-off-by: Jaroslav Kysela <[email protected]>
1 parent 139c00a commit 24bd026

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

doc/APICoverage.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import os, pickle, urllib.request, urllib.parse, urllib.error, sys
2626
from pyparsing import *
2727
from html.entities import entitydefs
28-
from htmllib import HTMLParser
28+
from html.parser import HTMLParser
2929
from formatter import AbstractFormatter, DumbWriter
3030

3131
# cache dir (preparsed source and HTML asoundlib API)
@@ -114,29 +114,24 @@ class AsoundlibAPIHTMLParser(HTMLParser):
114114
HTML asoundlib API from the alsa website.
115115
"""
116116

117-
HTMLParser.entitydefs['nbsp'] = ' '
118-
119117
def __init__(self, name, data):
120-
f = AbstractFormatter(DumbWriter(open(name, 'w'), 100))
121-
HTMLParser.__init__(self, f)
118+
self.f = AbstractFormatter(DumbWriter(open(name, 'w'), 100))
119+
HTMLParser.__init__(self)
122120
self.feed(data)
123121
self.close()
124122

125-
def start_h1(self, attrs):
126-
HTMLParser.start_h1(self, attrs)
127-
self.handle_data("--- titlestart")
128-
self.do_br(None)
129-
130-
def start_table(self, attrs):
131-
if len(attrs) == 1 and attrs[0] == ("class", "memname"):
132-
self.handle_data("--- itemstart")
133-
self.do_br(None)
123+
def handle_data(self, data):
124+
self.f.add_literal_data(data)
134125

135-
def start_tr(self, attrs):
136-
self.do_br(None)
137-
138-
def anchor_end(self):
139-
pass
126+
def handle_starttag(self, tag, attrs):
127+
if tag == "div":
128+
if len(attrs) == 1 and attrs[0] == ("class", "title"):
129+
self.handle_data("\n--- titlestart\n")
130+
if len(attrs) == 1 and attrs[0] == ("class", "ingroups"):
131+
self.handle_data("\n\n")
132+
elif tag == 'table':
133+
if len(attrs) == 1 and attrs[0] == ("class", "memname"):
134+
self.handle_data("\n--- itemstart")
140135

141136
def parse_asoundlib_api(lines):
142137
"""
@@ -154,7 +149,8 @@ def parse_asoundlib_api(lines):
154149
comment = ""
155150
enumsublist = []
156151
for line in lines:
157-
line = line[:-1]
152+
# convert &nbsp; to space
153+
line = line[:-1].replace('\xa0', ' ')
158154
if False:
159155
if id(current) == id(defines):
160156
print("defines ", end=' ')
@@ -168,7 +164,7 @@ def parse_asoundlib_api(lines):
168164
print(" ", end=' ')
169165
print("%s %d %s" % (id(current), state, line))
170166

171-
if line.startswith('Define Documentation'):
167+
if line.startswith('Macro Definition Documentation'):
172168
current = defines
173169
state = 0
174170
elif line.startswith('Typedef Documentation'):
@@ -185,36 +181,42 @@ def parse_asoundlib_api(lines):
185181
elif line.startswith('--- titlestart'):
186182
state = 5
187183
elif state == 5:
188-
title = line
184+
title = line.strip()
189185
state = 0
190186
elif current == None:
191187
continue
192188
elif state == 1:
193189
if line == "":
190+
name = ' '.join(name.split())
194191
state = 2
195192
else:
196193
name += line
197-
elif state == 2:
194+
elif state == 2 and line != "":
195+
comment = line.strip()
198196
if id(current) == id(enums):
199197
state = 3
200198
else:
201-
comment = line
202199
current.append((name, comment))
203200
name = ""
204201
comment = ""
205202
state = 0
206-
elif state == 3 and line.startswith('Enumerator:'):
203+
elif state == 3 and line.startswith('Enumerator'):
204+
enum, subcomment = line[10:].split(' ', 1)
205+
enumsublist = [(enum.strip(), subcomment.strip())]
206+
linewasempty = False
207207
state = 4
208-
enumsublist = []
209208
elif state == 4:
210-
if line == "":
209+
if linewasempty and line == "":
211210
current.append((name, comment, enumsublist))
212211
name = ""
213212
comment = ""
214213
state = 0
214+
elif line == "":
215+
linewasempty = True
215216
else:
216217
enum, subcomment = line.split(' ', 1)
217-
enumsublist.append((enum, subcomment))
218+
enumsublist.append((enum.strip(), subcomment.strip()))
219+
linewasempty = False
218220

219221
return (title, defines, typedefs, enums, functions)
220222

@@ -353,7 +355,7 @@ def print_api_coverage(urls, look_constant, look_usage, excludes):
353355
name = names[-1]
354356
if ')' in name:
355357
names = d[0].split('(')
356-
name = names[-2].split()[-1]
358+
name = names[-2].removesuffix(') ').removeprefix('* ')
357359
print_name(d[0], d[1], name, look_constant, look_usage, el)
358360
print_stat(title, "Typedefs")
359361
print("\n"*2)

0 commit comments

Comments
 (0)