]>
Commit | Line | Data |
---|---|---|
ce1245e1 RD |
1 | import re |
2 | ||
3 | conversion_table = { | |
4 | "B" : "**", | |
5 | "I" : "*", | |
6 | "TT": "``", | |
7 | "P" : "\n", | |
8 | "BR": "\n", | |
9 | } | |
10 | ||
11 | html_classlink_re = "<A HREF=\".*?\">(.*?)</A>" | |
12 | ||
13 | def htmlToReST(html): | |
14 | # \n is useless in the HTML docs, we'll use P tags to break paragraphs. | |
15 | restText = html.replace("\n", "") | |
16 | restText = restText.replace("*", "\\*") | |
17 | if restText.find("<P>") == 0: | |
18 | restText = restText[3:] | |
19 | ||
20 | link_regex = re.compile(html_classlink_re, re.DOTALL | re.MULTILINE | re.IGNORECASE) | |
21 | restText = link_regex.sub("`\g<1>`", restText) | |
22 | ||
23 | ||
24 | for htmltag in conversion_table: | |
25 | ||
26 | for tagname in [htmltag, htmltag.lower()]: | |
27 | restText = restText.replace("<%s>" % tagname, conversion_table[htmltag]) | |
28 | restText = restText.replace("</%s>" % tagname, conversion_table[htmltag]) | |
29 | ||
30 | # we need to escape any remaining double-quotes | |
31 | restText = restText.replace('"', '\\"') | |
32 | return restText.strip() |