Convert HTML text from cplusplus.com to Groff-formatted text.
(data, name)
| 148 | |
| 149 | |
| 150 | def html2groff(data, name): |
| 151 | """Convert HTML text from cplusplus.com to Groff-formatted text.""" |
| 152 | # Remove sidebar |
| 153 | try: |
| 154 | data = data[data.index('<div class="C_doc">'):] |
| 155 | except ValueError: |
| 156 | pass |
| 157 | |
| 158 | # Pre replace all |
| 159 | for rp in pre_rps: |
| 160 | data = re.compile(rp[0], rp[2]).sub(rp[1], data) |
| 161 | |
| 162 | for table in re.findall(r'<table.*?>.*?</table>', data, re.S): |
| 163 | tbl = parse_table(escape_pre_section(table)) |
| 164 | # Escape column with '.' as prefix |
| 165 | tbl = re.compile(r'T{\n(\..*?)\nT}', re.S).sub(r'T{\n\\E \1\nT}', tbl) |
| 166 | data = data.replace(table, tbl) |
| 167 | |
| 168 | # Replace all |
| 169 | for rp in rps: |
| 170 | data = re.compile(rp[0], rp[2]).sub(rp[1], data) |
| 171 | |
| 172 | # Upper case all section headers |
| 173 | for st in re.findall(r'.SH .*\n', data): |
| 174 | data = data.replace(st, st.upper()) |
| 175 | |
| 176 | # Add tags to member/inherited member functions |
| 177 | # e.g. insert -> vector::insert |
| 178 | # |
| 179 | # .SE is a pseudo macro I created which means 'SECTION END' |
| 180 | # The reason I use it is because I need a marker to know where section |
| 181 | # ends. |
| 182 | # re.findall find patterns which does not overlap, which means if I do |
| 183 | # this: secs = re.findall(r'\n\.SH "(.+?)"(.+?)\.SH', data, re.S) |
| 184 | # re.findall will skip the later .SH tag and thus skip the later section. |
| 185 | # To fix this, '.SE' is used to mark the end of the section so the next |
| 186 | # '.SH' can be find by re.findall |
| 187 | |
| 188 | page_type = re.search(r'\n\.SH "TYPE"\n(.+?)\n', data) |
| 189 | if page_type and 'class' in page_type.group(1): |
| 190 | class_name = re.search( |
| 191 | r'\n\.SH "NAME"\n(?:.*::)?(.+?) ', data).group(1) |
| 192 | |
| 193 | secs = re.findall(r'\n\.SH "(.+?)"(.+?)\.SE', data, re.S) |
| 194 | |
| 195 | for sec, content in secs: |
| 196 | # Member functions |
| 197 | if ('MEMBER' in sec and |
| 198 | 'NON-MEMBER' not in sec and |
| 199 | 'INHERITED' not in sec and |
| 200 | sec != 'MEMBER TYPES'): |
| 201 | content2 = re.sub(r'\n\.IP "([^:]+?)"', r'\n.IP "%s::\1"' |
| 202 | % class_name, content) |
| 203 | # Replace (constructor) (destructor) |
| 204 | content2 = re.sub(r'\(constructor\)', r'%s' % class_name, |
| 205 | content2) |
| 206 | content2 = re.sub(r'\(destructor\)', r'~%s' % class_name, |
| 207 | content2) |
no test coverage detected