-
Notifications
You must be signed in to change notification settings - Fork 10
/
converters.py
172 lines (137 loc) · 3.89 KB
/
converters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
Support spellchecking various file formats by converting them to plain text
"""
import json
import sys
import xml.etree.ElementTree
supported_extensions = ["txt", "html", "md", "markdown", "rst", "ipynb", "json", "xliff"]
def convert(source, texttype):
"""
Convert files of various types to plaintext
Args:
texttype (str):
file extension of the input file
source (str):
content of the input file
Returns:
str:
plaintext output
"""
if texttype == "html":
return html2text(source)
if texttype in ["md", "markdown"]:
return html2text(markdown2html(source))
if texttype in ["rst"]:
return html2text(rst2html(source))
if texttype == "ipynb":
return html2text(markdown2html(ipynb2markdown(source)))
if texttype == "json":
return transifexjson2txt(source)
if texttype == "xliff":
return xliff2txt(source)
if texttype != "txt":
print("filetype not detected, assuming plaintext")
return source
def html2text(html):
"""
convert HTML to plaintext by parsing it with BeautifulSoup and removing code
Args:
html (str):
HTML string
Returns:
str: plaintext
"""
try:
from bs4 import BeautifulSoup
except ImportError:
notinstalled("beautifulsoup4", "HTML", "text")
sys.exit(4)
soup = BeautifulSoup(html, "html.parser")
# remove scripts from html
for script in soup(["script", "style", "code", "pre"]) + soup("span", {"class": "literal"}):
script.extract()
return soup.get_text()
def markdown2html(markdown):
"""
convert Markdown to HTML via ``markdown2``
Args:
markdown (str):
Markdown text
Returns:
str: HTML
"""
try:
import markdown2
except ImportError:
notinstalled("markdown2", "markdown", "HTML")
sys.exit(4)
return markdown2.markdown(markdown)
def ipynb2markdown(ipynb):
"""
Extract Markdown cells from iPython Notebook
Args:
ipynb (str):
iPython notebook JSON file
Returns:
str: Markdown
"""
j = json.loads(ipynb)
markdown = ""
for cell in j["cells"]:
if cell["cell_type"] == "markdown":
markdown += "".join(cell["source"]) + "\n"
return markdown
def rst2html(rst):
"""
convert reStructuredText to HTML with ``docutils``
Args:
rst (str):
reStructuredText
Returns:
str: HTML
"""
try:
from docutils.core import publish_string
except ImportError:
notinstalled("docutils", "ReStructuredText", "HTML")
sys.exit(4)
return publish_string(rst, writer_name="html5")
def transifexjson2txt(jsondata):
"""
extract translations from Transifex JSON file
Args:
jsondata (str):
Transifex export file
Returns:
str: Plaintext translations
"""
data = json.loads(jsondata)
text = ""
for category, content in data.items():
for key, value in content.items():
text += value + "\n"
return text
def xliff2txt(source):
"""
extract translations from ``XLIFF`` file
Args:
source (str):
XLIFF XML string
Returns:
str: Plaintext translations
"""
root = xml.etree.ElementTree.fromstring(source)
text = ""
for file in root:
for body in file:
for trans_unit in body:
value = trans_unit.find("{urn:oasis:names:tc:xliff:document:1.1}target").text
if value:
text += value + "\n\n"
return text
def notinstalled(package, convert_from, convert_to):
print(
"""{package} is needed to convert {source} to {target}
you can install it with pip:
pip install {package}""".format(package=package, source=convert_from, target=convert_to)
)