-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
296 lines (256 loc) · 7.22 KB
/
model.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from regl.grammar import Item, regl
from regl.lexer import ReglLexer
from regl import conf
import six
import re
class Node(object):
def __init__(self, document, children=None):
self.document = document
self.children = [] if children is None else children
def pre_to_html(self, ctx):
pass
def pre_to_LaTeX(self, ctx):
pass
class Document(object):
def __init__(self):
self.root = RootNode(self)
class parseTree_to_document_context:
def __init__(self, rootList):
self.document = Document()
self.stack = [(tuple(rootList), # i
self.document.root, # p
self.section_handler)] # h
def main(self):
while self.stack:
i, p, h = self.stack.pop()
h(i, p)
return self.document
def children_of(self, i):
if hasattr(i, 'content'):
if isinstance(i.content, tuple):
return i.content
return (i.content,)
return ()
def section_handler(self, i, p):
assert isinstance(i, tuple)
for c in i:
if isinstance(c, six.string_types):
p.children.append(
TextNode(self.document, c))
assert not self.children_of(c)
continue
assert isinstance(c, Item)
handler = self.section_handler
if c.name == conf.nilItemToken:
cp = NilItemNode(self.document)
elif c.name.startswith(
conf.articlePrefixToken):
cp = ArticleNode(self.document,
c.name)
elif c.name.startswith(
conf.NBPrefixToken):
cp = NBNode(self.document, c.name)
else:
cp = SectionNode(self.document,
c.name)
p.children.append(cp)
cc = self.children_of(c)
assert cc
self.stack.append((cc, cp, handler))
@staticmethod
def from_parseTree(v):
return Document.parseTree_to_document_context(v).main()
@staticmethod
def from_string(s):
f = six.StringIO(s)
return Document.from_parseTree(regl.parseString(
''.join(ReglLexer(f)))[0])
def to_html(self):
return self.to_x(lambda i,*args: i.pre_to_html(*args),
lambda i,*args: i.to_html(*args))
def to_LaTeX(self):
return self.to_x(lambda i,*args: i.pre_to_LaTeX(*args),
lambda i,*args: i.to_LaTeX(*args))
def to_x(self, pre_call, call):
stack = [[True, self.root, None, [], dict()]]
while stack:
frame = stack[-1]
#0 1 2 3 4
pre, i, to, res, ctx = frame
if pre:
frame[0] = False
else:
stack.pop()
if pre:
nctx = pre_call(i,ctx)
if nctx is None:
nctx = ctx
else:
frame[4] = nctx
if hasattr(i, 'children'):
for c in reversed(i.children):
stack.append([True, c, frame,
[], nctx])
else:
nres = call(i, res, ctx)
if to is None:
assert not stack
return nres
to[3].append(nres)
class RootNode(Node):
def to_html(self, children, ctx):
return ''.join(children)
def to_LaTeX(self, children, ctx):
c_text = ''.join(children)
text = r"""
\documentclass{article}
\usepackage[dutch]{babel}
\usepackage{amsthm}
\usepackage{eurosym}
\usepackage{makeidx}
\date{}
\makeindex
\newcommand{\noun}[1]{\textsc{#1}}
\newcommand{\comment}[1]{
\begin{enumerate}
\footnotesize
\item[//] \emph{#1}
\end{enumerate}}
\newcommand{\stub}[1]{\comment{Nog te herschrijven.}}
\newcommand{\stref}[1]{ST#1}
\newcommand{\defn}[1]{\textbf{#1}\index{#1}}
\newtheoremstyle{comment}%%
{3pt} %% Space Above
{3pt} %% Space below
{\footnotesize\hangindent=\parindent} %% Body font
{\parindent} %% Indent amount 1
{\bfseries} %% Theorem head font
{.} %% Punctuation after theorem head
{.5em} %% Space after theorem head
{} %% Theorem head space
\begin{document}
%s
\maketitle
\printindex
\end{document} """ % c_text
for c, r in six.iteritems(conf.LaTeXCharMap):
text = text.replace(c, r)
return text
class TextNode(Node):
def __init__(self, document, text):
super(TextNode, self).__init__(document)
self.text = text
self.re_keyword = re.compile(r'\*((?:\w\s?)*)\*')
self.re_quote = re.compile(r'"([^"]*)"')
def to_html(self, children, ctx):
assert not children
return "<p>%s</p>" % self.text
def to_LaTeX(self, children, ctx):
assert not children
text = self.re_keyword.sub(r'\\defn{\1}', self.text)
text = self.re_quote.sub(r"``\1''", text)
return text
class NilItemNode(Node):
def to_html(self, children, ctx):
return ''.join(children)
def to_LaTeX(self, children, ctx):
return ''.join(children)
class SectionNode(Node):
def __init__(self, document, title):
super(SectionNode, self).__init__(document)
self.title = title
def pre_to_html(self, ctx):
ctx = dict(ctx)
if not 'section-depth' in ctx:
ctx['section-depth'] = 0
ctx['section-depth'] += 1
return ctx
pre_to_LaTeX = pre_to_html
def to_html(self, children, ctx):
c_text = '' if children is None else ''.join(children)
return """ <h%s>%s</h%s>
<div class='section section%s'>%s</div> """ % (
ctx['section-depth'], self.title,
ctx['section-depth'],
ctx['section-depth'], c_text)
def to_LaTeX(self, children, ctx):
depth = ctx['section-depth']
if children is None:
children = ()
if depth == 1:
templ = r"""\title{%s}
\maketitle
%s"""
c_text = ''.join(children)
elif depth == 2:
templ = r"""\section*{%s}
%s """
c_text = ''.join(children)
elif depth == 3:
templ = r"""\subsection*{%s}
%s """
c_text = ''.join(children)
elif depth == 4:
templ = r"""\begin{enumerate}
\item[%s] %s
\end{enumerate} """
c_text = ''.join(children)
elif depth == 5:
templ = r"""\begin{enumerate}
\item[%s] %s
\end{enumerate} """
c_text = ''.join(children)
elif depth == 6:
templ = r"""\begin{enumerate}
\item[%s] %s
\end{enumerate} """
c_text = ''.join(children)
else:
templ = r"wur %s %s"
c_text = ''.join(children)
if self.title == '-':
return c_text
return templ % (self.title, c_text)
class ArticleNode(SectionNode):
def pre_to_html(self, ctx):
pass
def to_html(self, children, ctx):
c_text = '' if children is None else ''.join(children)
return """ <div class="articleTitle">%s</div>
<div class='article'>%s</div> """ % (
self.title, c_text)
def to_LaTeX(self, children, ctx):
c_text = '' if children is None else ''.join(children)
title = self.title
return r"""
\theoremstyle{definition}
\newtheorem*{%(title)s}{%(title)s}
\begin{%(title)s}
%(text)s
\end{%(title)s} """ % {"title": title,
"text": c_text}
class NBNode(SectionNode):
def pre_to_html(self, ctx):
pass
def to_html(self, children, ctx):
c_text = '' if children is None else ''.join(children)
return """ <div class="NBTitle">%s</div>
<div class='NB'>%s</div> """ % (
self.title, c_text)
def to_LaTeX(self, children, ctx):
c_text = '' if children is None else ''.join(children)
title = self.title
return r"""
\theoremstyle{comment}
\newtheorem*{%(title)s}{%(title)s}
\begin{%(title)s}
%(text)s
\end{%(title)s} """ % {"title": title,
"text": c_text}
if __name__ == '__main__':
import codecs
with codecs.open('test.regl', 'r', 'utf-8') as f:
doc = Document.from_parseTree(regl.parseString(
''.join(ReglLexer(f)))[0])
with codecs.open('test.tex', 'w', 'utf-8') as f2:
f2.write(doc.to_LaTeX())