-
Notifications
You must be signed in to change notification settings - Fork 4
/
extract_text.py
82 lines (67 loc) · 2.69 KB
/
extract_text.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
#!/usr/bin/env python
# Copyright (c) 2017, Aeva M. Palecek
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import glob
import json
from util import extract_text
from parser_common import UnknownFileHeader
if __name__ == "__main__":
paths = []
def ingest(path):
if os.path.isdir(path):
for dirpath, dir_names, file_names in os.walk(path):
for file_name in file_names:
path = os.path.join(dirpath, file_name)
paths.append(path)
if os.path.isfile(path):
paths.append(path)
mode = "JSON"
for arg in sys.argv[1:]:
if arg == "--markdown":
mode = "MD"
continue
map(ingest, glob.glob(arg))
data = []
for level_path in paths:
try:
level_text = extract_text(level_path)
except UnknownFileHeader:
continue
if level_text:
data.append({
"level" : os.path.split(level_path)[-1],
"text" : map(lambda x: x.encode("utf-8"), level_text),
})
if mode == "JSON":
print json.dumps(data, ensure_ascii=False)
elif mode == "MD":
markdown = u''
for packet in data:
level = packet["level"]
signs = packet["text"]
markdown += u"\n# {}\n".format(level.encode("utf-8"))
for sign_index, sign in enumerate(signs):
sign = sign.decode("utf-8")
markdown += u"#### block {}\n".format(sign_index)
lines = sign.split(u"\n")
for line_index, line in enumerate(lines):
page_start = line_index % 3 == 0
page_end = line_index % 3 == 2
terminus = line_index == len(lines)-1
if page_start:
markdown += u"> ```\n"
markdown += u"> {}\n".format(line)
if page_end or terminus:
markdown += u"> ```\n\n"
print markdown.encode("utf-8")