forked from minsk-hackerspace/OO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd2tex.py
executable file
·189 lines (142 loc) · 3.64 KB
/
md2tex.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
#!/usr/bin/python
import CommonMark
import sys
from pprint import pprint
import json
import datetime
md_text = ''
for line in sys.stdin:
md_text += line
parser = CommonMark.DocParser()
ast = parser.parse(md_text)
# CommonMark.dumpAST(ast)
# quit()
files_dir = 'md2tex/'
config = {
'header' : files_dir + '01_header.tex',
'macro' : files_dir + '02_macro.tex',
'hypersetup' : files_dir + '03_hypersetup.tex',
'title' : files_dir + '04_title.tex'
}
def md2tex_file(name):
with open (name, 'r') as myfile:
print("".join(myfile.readlines()))
md2tex_file(config['header'])
md2tex_file(config['macro'])
md2tex_file(config['hypersetup'])
'''
Code blocks may be used for python code in future.
For all document:
Replace — with "---
Before 1-st SetextHeader - title page.
After 1-st SetextHeader - body.
SetextHeader - first - \section{}, next - \newpage\section{}
Paragraph - \begin{numberedpars}
List - \begin{itemize}
ListItem - \item
Emph - \emph{}
Strong - \textbf{\large
'''
today = datetime.date.today()
year = today.year
day = today.day
month = {'January': 'января',
'Feburary': 'февраля',
'March': 'марта',
'April': 'апреля',
'May': 'мая',
'June': 'июня',
'July': 'июля',
'August': 'августа',
'September': 'сентября',
'October': 'октября',
'November': 'ноября',
'December': 'декабря'
}[today.strftime("%B")]
in_title_page = True
in_list_item = False
start = 'start'
end = 'end'
convert_dict ={
False: {
'Document': {
start: '\n\n\\begin{document}\n\n',
end: '\n\n\\end{document}\n\n'
},
'SetextHeader': {
start: '\n\\section{',
end: '}\n'
},
'Paragraph': {
start: '\n\\begin{numberedpars}\n',
end: '\n\\end{numberedpars}\n'
},
'List': {
start: '\n\\begin{itemize}\n',
end: '\n\\end{itemize}\n'
},
'ListItem': {
start: '\n\\item\n',
end: '\n'
},
'Emph': {
start: '\\emph{',
end: '}'
},
'Softbreak': {
start: '\n'
},
'Entity': {},
'Str': {},
'Strong': {},
'FencedCode': {}
},
True: {
'Emph': {
start: '\\emph{',
end: '}'
},
'Softbreak': {
start: '\n'
}
}
}
def iter_ast (node):
global in_title_page
global in_list_item
global convert_dict
cd = convert_dict[in_list_item]
if node.t in cd and start in cd[node.t] and len(cd[node.t][start]) > 0:
print(cd[node.t][start], end='')
if node.t == 'Document':
print('\\date{\\large г. Минск\\\\%d}' % year)
print('\\titlehead{\\raggedleft \\begin{minipage}{15em}\\flushleft')
print('Утвержден \\\\\n Учредительным Общим Собранием Просветительского общественного объединения \\RuName \\\\')
print('года %d %s «%d»' % (year, month, day))
print('\\end{minipage}}')
md2tex_file(config['title'])
if node.t == 'ListItem':
in_list_item = True
if type(node.children) is list and len(node.children) > 0:
for n in node.children:
iter_ast(n)
if type(node.inline_content) is list and len(node.inline_content) > 0:
for n in node.inline_content:
iter_ast(n)
if type(node.c) is list and len(node.c) > 0:
for n in node.c:
iter_ast(n)
if node.t == 'Entity':
print({
'—': '"---'
}[node.c], end='')
elif type(node.c) is str:
print(node.c, end='')
if node.t == 'ListItem':
in_list_item = False
if node.t == 'SetextHeader' and in_title_page:
in_title_page = False
convert_dict[False]['SetextHeader'][start] = '\\newpage\\section{'
if node.t in cd and end in cd[node.t] and len(cd[node.t][end]) > 0:
print(cd[node.t][end], end='')
iter_ast(ast)