forked from d2l-ai/d2l-vi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
198 lines (160 loc) · 7.22 KB
/
utils.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
# encoding=utf8
import codecs
import filecmp
import re
import sys
import argparse
BEGIN_BLOCK_COMMENT = '<!--\n'
END_BLOCK_COMMENT = '-->\n\n'
TRANSLATE_INDICATOR = '*dịch đoạn phía trên*'
HEADER_INDICATOR = ' *dịch tiêu đề phía trên*\n'
IMAGE_CAPTION_INDICATOR = '*dịch chú thích ảnh phía trên*'
START_FILE = '<!-- ===================== Bắt đầu dịch Phần ==================== -->\n<!-- ========================================= REVISE PHẦN - BẮT ĐẦU =================================== -->\n'
END_FILE = '<!-- ===================== Kết thúc dịch Phần ==================== -->\n<!-- ========================================= REVISE PHẦN - KẾT THÚC ===================================-->\n'
SUFIX_PATH = 'docs/contributors_template_vn.md'
# Our special mark in markdown, e.g. :label:`chapter_intro`
MARK_RE_MD = re.compile(':([-\/\\._\w\d]+):`([\*-\/\\\._\w\d]+)`')
parser = argparse.ArgumentParser(description='Dịch Dive into Deep Learning')
parser.add_argument('--convert', type=str, help='path to md file')
def is_blank_line(line):
return line.strip() == ''
class MyLine(object):
def __init__(self, line_str, in_code_block):
self.line_str = line_str.replace(' -- ', ' \-\- ')
self.in_code_block = in_code_block
self.end_comment_if_next_line_blank = None
def process(self, file_writer, last_line):
if self.in_code_block:
file_writer.write(self.line_str)
else:
self._process(file_writer, last_line)
return self
def _process(self, file_writer, last_line):
raise NotImplementedError
class NormalLine(MyLine):
def __init__(self, line_str, in_code_block):
super(NormalLine, self).__init__(line_str, in_code_block)
self.end_comment_if_next_line_blank = True
def _process(self, file_writer, last_line):
if isinstance(last_line, BlankLine):
file_writer.write(BEGIN_BLOCK_COMMENT)
file_writer.write(self.line_str)
class BlankLine(MyLine):
def __init__(self, line_str, in_code_block):
super(BlankLine, self).__init__(line_str, in_code_block)
self.end_comment_if_next_line_blank = False
def _process(self, file_writer, last_line):
if last_line.end_comment_if_next_line_blank:
file_writer.write(END_BLOCK_COMMENT)
file_writer.write(TRANSLATE_INDICATOR)
file_writer.write('\n')
file_writer.write('\n')
class HeaderLine(MyLine):
def __init__(self, line_str, in_code_block):
super(HeaderLine, self).__init__(line_str, in_code_block)
self.end_comment_if_next_line_blank = False
self.heading = 0
cnt = 0
for char in self.line_str:
if char == '#':
cnt += 1
elif char == ' ':
self.heading = cnt
break
else:
assert False, self.line_str
def _process(self, file_writer, last_line):
assert isinstance(last_line, BlankLine),\
last_line.line_str
file_writer.write(BEGIN_BLOCK_COMMENT)
file_writer.write(self.line_str)
file_writer.write(END_BLOCK_COMMENT)
file_writer.write('#'*self.heading + HEADER_INDICATOR)
class ImageLine(MyLine):
def __init(self, line_str, in_code_block):
assert not in_code_block
super(ImageLine, self).__init__(line_str, in_code_block)
def _process(self, file_writer, last_line):
close_square_bracket_id = self.line_str.index(']')
assert self.line_str[close_square_bracket_id+1] == '(', self.line_str
# assert self.line_str.endswith(')'), self.line_str
file_writer.write(BEGIN_BLOCK_COMMENT)
file_writer.write(self.line_str)
file_writer.write(END_BLOCK_COMMENT)
file_writer.write(
'![' + IMAGE_CAPTION_INDICATOR + ']' + self.line_str[close_square_bracket_id+1:]
)
class CodeMarkerLine(MyLine):
def __init__(self, line_str, in_code_block):
super(CodeMarkerLine, self).__init__(line_str, in_code_block)
self.end_comment_if_next_line_blank = False
def _process(self, file_writer, last_line):
""" the print is printed in the super class"""
file_writer.write(self.line_str)
class MathLine(MyLine):
def __init__(self, line_str, in_code_block):
super(MathLine, self).__init__(line_str, in_code_block)
self.end_comment_if_next_line_blank = False
def _process(self, file_writer, last_line):
file_writer.write(self.line_str)
return self
class LabelLine(MyLine):
def __init__(self, line_str, in_code_block):
super(LabelLine, self).__init__(line_str, in_code_block)
self.end_comment_if_next_line_blank = False
def _process(self, file_writer, last_line):
# assert isinstance(last_line, HeaderLine) or isinstance(last_line, ImageLine), 'last line: {}\nthis_line: {}'.format(
# last_line.line_str, self.line_str
# )
file_writer.write(self.line_str)
# file_writer.write('\n')
return self
def block_comment(input_md, output_md, add_prefix_suffix=False):
last_line = BlankLine('', False)
in_code_block = False
with codecs.open(input_md, 'r', encoding='utf-8') as input_handle,\
codecs.open(output_md, 'w', encoding='utf-8') as output_handle,\
codecs.open(SUFIX_PATH, 'r', encoding='utf-8') as surfix_handle:
if add_prefix_suffix:
output_handle.write(START_FILE)
output_handle.write('\n')
for line_str in input_handle:
line_str = line_str.rstrip() + '\n'
line_str = line_str.replace(' -- ', ' \-\- ')
match = MARK_RE_MD.match(line_str)
if is_blank_line(line_str):
line_type = BlankLine
elif line_str.startswith('#'):
line_type = HeaderLine
elif line_str.startswith('!['):
line_type = ImageLine
elif line_str.startswith('$'):
line_type = MathLine
elif line_str.startswith('```'):
in_code_block = not in_code_block
line_type = CodeMarkerLine
elif match is not None and match[1] in ['label', 'eqlabel']:
line_type = LabelLine
else:
line_type = NormalLine
this_line = line_type(line_str, in_code_block)
last_line = this_line.process(output_handle, last_line)
assert in_code_block is False
# TODO: simplify 5 lines below
if isinstance(last_line, BlankLine) or isinstance(last_line, LabelLine)\
or isinstance(last_line, CodeMarkerLine) or isinstance(last_line, ImageLine):
print('skip')
else:
output_handle.write(END_BLOCK_COMMENT)
output_handle.write(TRANSLATE_INDICATOR)
if add_prefix_suffix:
output_handle.write('\n')
output_handle.write(END_FILE)
output_handle.write('\n')
for line in surfix_handle:
output_handle.write(line)
if __name__ == '__main__':
args = parser.parse_args()
input_md = args.convert
output_md = input_md[:-len('.md')] + '_vn.md'
block_comment(input_md, output_md, add_prefix_suffix=True)