111 |
110 |
112 def parse_article(self): |
111 def parse_article(self): |
113 """Assume we are at ``# NUM`` line.""" |
112 """Assume we are at ``# NUM`` line.""" |
114 num = self.num |
113 num = self.num |
115 phrase_buf = [] |
114 phrase_buf = [] |
116 phrases = [] |
|
117 while True: |
115 while True: |
118 self.readline() |
116 self.readline() |
119 if self.eof: |
117 if self.eof: |
120 if len(phrase_buf) > 0: |
|
121 phrases.append(" ".join(phrase_buf)) |
|
122 break |
118 break |
123 m = self.NUM_RE.match(self.line) |
119 m = self.NUM_RE.match(self.line) |
124 if m: |
120 if m: |
125 if len(phrase_buf) > 0: |
|
126 phrases.append(" ".join(phrase_buf)) |
|
127 self.num = m.group(1) |
121 self.num = m.group(1) |
128 break |
122 break |
129 m = self.PHRASE_START_RE.match(self.line) |
123 phrase_buf.append(self.line) |
130 if m: |
124 if len(phrase_buf) == 0: |
131 if len(phrase_buf) > 0: |
|
132 phrases.append(" ".join(phrase_buf)) |
|
133 phrase_buf = [m.group(1)] |
|
134 else: |
|
135 phrase_buf.append(self.line) |
|
136 if len(phrases) == 0: |
|
137 raise ParseException("""There are no any phrases...""") |
125 raise ParseException("""There are no any phrases...""") |
138 if num in self.dom: |
126 if num in self.dom: |
139 raise ParseException("""Conflicting key: {}...""".format(num)) |
127 raise ParseException("""Conflicting key: {}...""".format(num)) |
140 self.dom[num] = phrases |
128 self.dom[num] = " ".join(phrase_buf) |
141 |
129 |
142 FIN = io.open(FINAME, mode='r', buffering=1, encoding="utf-8") |
130 FIN = io.open(FINAME, mode='r', buffering=1, encoding="utf-8") |
143 |
131 |
144 PARSER = Parser() |
132 PARSER = Parser() |
145 try: |
133 try: |
214 export.exportInto(fname) |
196 export.exportInto(fname) |
215 |
197 |
216 def close(self): |
198 def close(self): |
217 self.collection.close() |
199 self.collection.close() |
218 |
200 |
219 def write_lines(buf, lines): |
|
220 odd = True |
|
221 for line in lines: |
|
222 if odd: |
|
223 buf.append("<div class='line odd'>") |
|
224 else: |
|
225 buf.append("<div class='line even'>") |
|
226 buf.append("- ") |
|
227 buf.append(line) |
|
228 buf.append("</div>") |
|
229 odd = not odd |
|
230 |
|
231 # Looks like anki libs change working directory to media directory of current deck |
201 # Looks like anki libs change working directory to media directory of current deck |
232 # Therefore absolute path should be stored before creating temporary deck |
202 # Therefore absolute path should be stored before creating temporary deck |
233 FONAME = os.path.abspath(FONAME) |
203 FONAME = os.path.abspath(FONAME) |
234 TMPDIR = tempfile.mkdtemp(dir=os.path.dirname(FONAME)) |
204 TMPDIR = tempfile.mkdtemp(dir=os.path.dirname(FONAME)) |
235 |
205 |
236 try: |
206 try: |
237 BUILDER = AnkiDbBuilder(TMPDIR, NAME) |
207 BUILDER = AnkiDbBuilder(TMPDIR, NAME) |
238 |
208 |
239 for num, lines in DOM.items(): |
209 for num, phrase in DOM.items(): |
240 buf = [] |
210 buf = [] |
241 write_lines(buf, lines) |
211 buf.append("<div class='line'>") |
|
212 buf.append(phrase) |
|
213 buf.append("</div>") |
242 front = "".join(buf) |
214 front = "".join(buf) |
243 BUILDER.add_note(num, front) |
215 BUILDER.add_note(num, front) |
244 BUILDER.export(FONAME) |
216 BUILDER.export(FONAME) |
245 finally: |
217 finally: |
246 BUILDER.close() |
218 BUILDER.close() |