Convert HTML text from cppreference.com to Groff-formatted text.
(data, name)
| 196 | |
| 197 | |
| 198 | def html2groff(data, name): |
| 199 | """Convert HTML text from cppreference.com to Groff-formatted text.""" |
| 200 | # Remove header and footer |
| 201 | try: |
| 202 | data = data[data.index('<div id="cpp-content-base">'):] |
| 203 | data = data[:data.index('<div class="printfooter">') + 25] |
| 204 | except ValueError: |
| 205 | pass |
| 206 | |
| 207 | # Remove non-printable characters |
| 208 | data = ''.join([x for x in data if x in string.printable]) |
| 209 | |
| 210 | for table in re.findall( |
| 211 | r'<table class="(?:wikitable|dsctable)"[^>]*>.*?</table>', |
| 212 | data, re.S): |
| 213 | tbl = parse_table(table) |
| 214 | # Escape column with '.' as prefix |
| 215 | tbl = re.compile(r'T{\n(\..*?)\nT}', re.S).sub(r'T{\n\\E \1\nT}', tbl) |
| 216 | data = data.replace(table, tbl) |
| 217 | |
| 218 | # Pre replace all |
| 219 | for rp in rps: |
| 220 | data = re.compile(rp[0], rp[2]).sub(rp[1], data) |
| 221 | |
| 222 | # Remove non-printable characters |
| 223 | data = ''.join([x for x in data if x in string.printable]) |
| 224 | |
| 225 | # Upper case all section headers |
| 226 | for st in re.findall(r'.SH .*\n', data): |
| 227 | data = data.replace(st, st.upper()) |
| 228 | |
| 229 | # Add tags to member/inherited member functions |
| 230 | # e.g. insert -> vector::insert |
| 231 | # |
| 232 | # .SE is a pseudo macro I created which means 'SECTION END' |
| 233 | # The reason I use it is because I need a marker to know where section |
| 234 | # ends. |
| 235 | # re.findall find patterns which does not overlap, which means if I do |
| 236 | # this: secs = re.findall(r'\n\.SH "(.+?)"(.+?)\.SH', data, re.S) |
| 237 | # re.findall will skip the later .SH tag and thus skip the later section. |
| 238 | # To fix this, '.SE' is used to mark the end of the section so the next |
| 239 | # '.SH' can be find by re.findall |
| 240 | |
| 241 | try: |
| 242 | idx = data.index('.IEND') |
| 243 | except ValueError: |
| 244 | idx = None |
| 245 | |
| 246 | def add_header_multi(prefix, g): |
| 247 | if ',' in g.group(1): |
| 248 | res = ', '.join(['%s::%s' % (prefix, x.strip()) |
| 249 | for x in g.group(1).split(',')]) |
| 250 | else: |
| 251 | res = '%s::%s' % (prefix, g.group(1)) |
| 252 | |
| 253 | return '\n.IP "%s"' % res |
| 254 | |
| 255 | if idx: |
no test coverage detected