-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocx-to-gmb.py
218 lines (164 loc) · 6.82 KB
/
docx-to-gmb.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from docx import Document
from docx.text.paragraph import Paragraph
from docx.text.run import Run
from pathlib import Path
from sys import argv
from abc import ABC, abstractmethod
iso885915_utf_map = {
ord(u"\u2018"): ord(u"'"), # LEFT SINGLE QUOTATION MARK
ord(u"\u2019"): ord(u"'"), # RIGHT SINGLE QUOTATION MARK
ord(u"\u201c"): ord(u'"'), # LEFT DOUBLE QUOTATION MARK
ord(u"\u201d"): ord(u'"'), # RIGHT DOUBLE QUOTATION MARK
ord(u"\u2026"): "...", # HORIZONTAL ELLIPSIS
ord(u"\u2013"): ord(u"-"), # EN DASH
ord(u"\xe7"): ord(u"c") # LATIN SMALL LETTER C WITH CEDILLA
}
class ParagraphProcessor:
def __init__(self):
self.buffer = ""
self.italic_active = False
self.bold_active = False
def append(self, text: str):
self.buffer = self.buffer + text
def set_italic(self, italic: bool):
if italic != self.italic_active:
self.append("*")
self.italic_active = italic
def set_bold(self, bold: bool):
if bold != self.bold_active:
self.append("**")
self.bold_active = bold
def append_run(self, run: Run):
if is_pagebreak(run):
self.append("\n\\pagebreakNum\n")
return
self.set_italic(bool(run.italic))
self.set_bold(bool(run.bold))
self.append(run.text.translate(iso885915_utf_map))
def finalize(self) -> str:
self.set_bold(False)
self.set_italic(False)
return self.buffer
class StyleGroupProcessor(ABC):
def __init__(self):
self.initialized = False
self.lines = []
@staticmethod
@abstractmethod
def accepts_paragraph(paragraph: Paragraph) -> bool:
return False
def post_process(self, paragraph_info: Paragraph, paragraph_line: str) -> str:
return paragraph_line
def get_paragraph_separator(self):
return "\n\n"
def on_finalize(self):
pass
def append_line(self, text: str):
self.lines.append(text)
def append_paragraph(self, paragraph: Paragraph):
assert self.accepts_paragraph(paragraph)
paragraph_processor = ParagraphProcessor()
for run in paragraph.runs:
paragraph_processor.append_run(run)
paragraph_line = paragraph_processor.finalize()
paragraph_line = self.post_process(paragraph, paragraph_line)
self.append_line(paragraph_line + self.get_paragraph_separator())
def finalize(self, output: list):
self.on_finalize()
for line in self.lines:
output.append(line)
class HeaderGroupProcessor(StyleGroupProcessor):
@staticmethod
def accepts_paragraph(paragraph: Paragraph) -> bool:
return paragraph.style.name.lower().startswith("heading")
def post_process(self, paragraph_info: Paragraph, paragraph_line: str) -> str:
header_level = int(paragraph_info.style.name.split(" ")[1])
header_prefix = "#" * header_level + " "
return header_prefix + paragraph_line
class DescriptiveGroupProcessor(StyleGroupProcessor):
descriptive_header_added = False
@staticmethod
def accepts_paragraph(paragraph: Paragraph) -> bool:
return paragraph.style.name.lower().startswith("descriptive")
def post_process(self, paragraph_info: Paragraph, paragraph_line: str) -> str:
if not self.descriptive_header_added:
self.add_descriptive_header()
return paragraph_line
def on_finalize(self):
if self.descriptive_header_added:
self.close_descriptive_header()
def add_descriptive_header(self):
self.append_line('\n\n<div class="descriptive">\n\n')
self.descriptive_header_added = True
def close_descriptive_header(self):
self.append_line("\n\n</div>\n\n")
class GreenBoxGroupProcessor(StyleGroupProcessor):
HEADER_PREFIX = "#" * 5 + " "
first_paragraph = True
@staticmethod
def accepts_paragraph(paragraph: Paragraph) -> bool:
return paragraph.style.name.lower() == "green box"
def post_process(self, paragraph_info: Paragraph, paragraph_line: str) -> str:
prefix = "> "
if self.first_paragraph:
prefix = prefix + self.HEADER_PREFIX
self.first_paragraph = False
return prefix + paragraph_line
def get_paragraph_separator(self):
return "\n>\n"
class ListGroupProcessor(StyleGroupProcessor):
@staticmethod
def accepts_paragraph(paragraph: Paragraph) -> bool:
return paragraph.style.name.lower().startswith("list")
def post_process(self, paragraph_info: Paragraph, paragraph_line: str) -> str:
return "* " + paragraph_line
def get_paragraph_separator(self):
return "\n"
class NormalGroupProcessor(StyleGroupProcessor):
@staticmethod
def accepts_paragraph(paragraph: Paragraph) -> bool:
return paragraph.style.name.lower() == "normal"
def is_pagebreak(run: Run) -> bool:
return 'w:br' in run._element.xml and 'type="page"' in run._element.xml
def make_group_processor_for_paragraph(paragraph: Paragraph) -> StyleGroupProcessor:
if HeaderGroupProcessor.accepts_paragraph(paragraph):
return HeaderGroupProcessor()
elif DescriptiveGroupProcessor.accepts_paragraph(paragraph):
return DescriptiveGroupProcessor()
elif GreenBoxGroupProcessor.accepts_paragraph(paragraph):
return GreenBoxGroupProcessor()
elif ListGroupProcessor.accepts_paragraph(paragraph):
return ListGroupProcessor()
elif NormalGroupProcessor.accepts_paragraph(paragraph):
return NormalGroupProcessor()
else:
raise Exception("Can't find processor for style " + paragraph.style.name)
def convert_document(doc: Document, out):
if len(doc.paragraphs) == 0:
print("Document has not paragraphs.")
return
output_lines = []
style_group_processor = make_group_processor_for_paragraph(doc.paragraphs[0])
assert style_group_processor is not None
for paragraph in doc.paragraphs:
if not style_group_processor.accepts_paragraph(paragraph):
style_group_processor.finalize(output_lines)
style_group_processor = make_group_processor_for_paragraph(paragraph)
assert style_group_processor is not None
style_group_processor.append_paragraph(paragraph)
style_group_processor.finalize(output_lines)
out.writelines(output_lines)
# ============================
# MAIN
# ============================
if len(argv) <= 2:
print("Usage: python docx-to-md.py <source-file> <out-file>")
exit(1)
source_path = Path(argv[1])
if not source_path.is_file():
print(source_path.name)
print("Source file is not a file.")
source_document = Document(source_path.resolve())
out_path = argv[2]
with open(out_path, 'w') as out_file:
convert_document(source_document, out_file)