-
Notifications
You must be signed in to change notification settings - Fork 7
/
ParseMdtoLatex.py
546 lines (392 loc) · 15.1 KB
/
ParseMdtoLatex.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import os, re, sys, yaml
from subprocess import call
from optparse import OptionParser
################################################################################################################################
# Markdown to Latex parser
#
# This script parses tutorials written in Markdown to Latex. It assumes that Pandoc is installed.
# Although Pandoc does most of the conversion, the script does some pre- and post-processing to better convert
# difficult tags and things specific to the site's Markdown tutorials (figures, references etc.).
#
# The script can also compile the resulting .tex file into .pdf and remove all temporary files produced.
#
# WARNING
# This is NOT a proper recursive parser. It is just a hastily written script that helps to convert files. It
# was a trade-off between the time it takes to convert things manually and the time it would take to write a
# real parser. After running the script the file should always still be inspected by hand!
#
# Details
# - Tables are not converted and should be done by hand
# - Some unicode characters are not correctly converted (this is an issue with pdflatex)
# - It is assumed that images are in html <figure> tags in the Markdown tutorial
# - The only image sizing that is recognized is width as a percentage of the panel
# - If a different image size or no image size is specified the image is displayed at its actual size,
# unless it is bigger than the page margins, in which case the image will be resized to fit within the
# margins.
# - Horizontal lines will be removed and replaced with new pages. This could be an issue when code blocks
# contain series of dashes (----) as these will also be removed.
#
# TODO:
# - Contents of figure captions are not always parsed correctly (if it contains math etc.)
#
################################################################################################################################
# Parameters
################################################################################################################################
usage = "usage: %prog [option]"
parser = OptionParser(usage=usage)
parser.add_option("-i","--inputfile",
dest = "inputfile",
default = "",
metavar = "file",
help = "Input file [required]")
parser.add_option("-o","--outputfile",
dest = "outputfile",
default = "",
metavar = "file",
help = "Name of the output file (by default it is the same as the input) [required]")
parser.add_option("-t","--template",
dest = "template",
default = "",
metavar = "file",
help = "Pandoc template file to use (if not specified use pandoc default) [required]")
parser.add_option("-T","--title",
dest = "title",
default = "Untitled tutorial",
metavar = "string",
help = "Title of tutorial in quotes (if not already in yaml header) [required]")
parser.add_option("-S","--subtitle",
dest = "subtitle",
default = "",
metavar = "string",
help = "Subtitle of tutorial in quotes (if not already in yaml header) [required]")
parser.add_option("-V","--version",
dest = "version",
default = "2.x",
metavar = "string",
help = "Version of BEAST the tutorial is written for (if not already in yaml header) [required]")
parser.add_option("-r","--removefiles",
action = "store_true",
default = False,
dest = "remove",
metavar = "boolean",
help = "Remove temporary files (default=%default) [required]")
parser.add_option("-L","--latex",
action = "store_true",
default = False,
dest = "latex",
metavar = "boolean",
help = "Compile output file with pdflatex (default=%default) [required]")
(options,args) = parser.parse_args()
inputfile = os.path.abspath(options.inputfile)
outputfile = os.path.abspath(options.outputfile) if options.outputfile != "" else inputfile[:inputfile.rfind('.')]+".tex"
template = os.path.abspath(options.template) if options.template != "" else ""
title = options.title
subtitle = options.subtitle
version = options.version
remove = options.remove
latex = options.latex
signature = "% This file was created (at least in part) by the script ParseMdtoLatex by Louis du Plessis\n" \
+ "% (Available from https://github.com/taming-the-beast)\n\n"
################################################################################################################################
def getPandocCall(inputfile, outputfile, template=""):
call = ["pandoc"]
# Output file
call.append("-o")
call.append(outputfile)
# Standalone file
call.append("-s")
if (template != ""):
call.append("--template")
call.append(template)
# Do not parse html blocks
call.append("-R")
# Use listings package for code blocks
call.append("--listings")
# Input file
call.append(inputfile)
return(call)
#
def getYamlHeader(text):
match = re.match("(\A---.*?)---",text, re.DOTALL)
header = yaml.load(match.groups(0)[0])
if (title != "Untitled tutorial"):
header["title"] = title
if (subtitle != ""):
header["subtitle"] = subtitle
if ("beastversion" not in header):
header["beastversion"] = version
if ("author" in header.keys()):
authors = header["author"].split(",")
if (len(authors) > 1):
header["author"] = ", ".join(authors[:-1]) + " and " + authors[-1]
else:
header["author"] = "Anonymous"
return((header,text[match.end():]))
#
def parseLiquid(text, header=None):
start = 0
while (True):
# Match the next liquid tag
# {% (tag) (content) %}
# Can be multiline
match = re.search(r'{%\s(.*?)\s(.*?)%}', text, re.DOTALL)
# No more matches
if (match == None):
break
(tag,content) = match.groups()
# Process tags
if (tag == "eq"):
replacement = "\\begin{equation}\n\t%s\n\end{equation}" % content.strip()
elif (tag == "eqinline"):
replacement = "`$ %s $`" % content.strip()
#replacement = ""
elif (tag == "cite"):
replacement = "\citep{%s}" % content[:content.find("--")].strip()
elif (tag == "bibliography"):
parts = content.split()
for i in range(0,len(parts)):
if (parts[i] == "--file"):
bibfile = parts[i+1]
if (bibfile.find('/') > 0):
bibfile = bibfile[bibfile.rfind('/')+1:]
if (header != None):
header["bibtex"] = bibfile
replacement = ""
else:
sys.stdout.write("WARNING Unsupported tag: %s\n" % (text[match.start():match.end()]))
sys.stdout.write("skipping...\n")
replacement = ""
text = text[:match.start()] + replacement + text[match.end():]
return(text)
#
def parseFigures(text):
start = 0
while (True):
# Match the next html figure tag
# Can be multiline
match = re.search(r'<figure>.*?<a id="(.*?)".*?<img(.*?)>.*?<figcaption>(.*?)</figcaption>.*?</figure>', text, re.DOTALL)
# No more matches
if (match == None):
break
(label, figure, caption) = match.groups()
# Process \includegraphics
scale = "[max width=\\textwidth, max height=0.9\\textheight]"
for part in figure.strip().split():
(tag,content) = part.split("=")
if (tag == "src"):
figfile = content.replace('"',"")
if (tag == "style"):
if (content[1:6].lower() == "width" and content[-3:-1] == '%;'):
mult = int(content[content.find(':')+1:content.find('%')])/100
scale = "[width=%.6f\\textwidth]" % mult
else:
sys.stdout.write("WARNING Unsupported image style specification %s in '%.20s...'\n" % (part, caption))
sys.stdout.write("skipping...\n")
# Process \caption
# TODO: Should actually run the caption through pandoc to parse the caption body text
caption = caption.replace("%","\%")
if (re.match(r'[f|F]igure\s\d+:',caption)):
caption = caption[caption.find(':')+1:].strip()
replacement = "\\begin{figure}\n\t\centering\n"
replacement += "\t\includegraphics%s{%s}\n" % (scale, figfile)
replacement += "\t\caption{%s}\n" % caption.strip()
replacement += "\t\label{%s}\n" % label.strip()
replacement += "\end{figure}\n"
text = text[:match.start()] + replacement + text[match.end():]
return(text)
#
# Assumes each figure contains only one image
def parseFiguresStepwise(text):
start = 0
while (True):
# Match the next html figure tag
# Can be multiline
match = re.search(r'<figure>(.*?)</figure>', text, re.DOTALL)
# No more matches
if (match == None):
break
label = re.search(r'<a.+?id="(.*?)"', match.groups()[0], re.DOTALL)
figure = re.search(r'<img(.*?)>', match.groups()[0], re.DOTALL)
caption = re.search(r'<figcaption>(.*?)</figcaption>', match.groups()[0], re.DOTALL)
replacement = "\\begin{figure}\n\t\centering\n"
# Process \includegraphics
if (figure != None):
scale = "[max width=\\textwidth, max height=0.9\\textheight]"
for part in figure.groups()[0].strip().split():
(tag,content) = part.split("=")
if (tag == "src"):
figfile = content.replace('"',"")
if (tag == "style"):
if (content[1:6].lower() == "width" and content[-3:-1] == '%;'):
mult = int(content[content.find(':')+1:content.find('%')])/100
scale = "[width=%.6f\\textwidth]" % mult
else:
sys.stdout.write("WARNING Unsupported image style specification %s in '%.20s...'\n" % (part, caption))
sys.stdout.write("skipping...\n")
replacement += "\t\includegraphics%s{%s}\n" % (scale, figfile)
#
# Process \caption
if (caption != None):
# Should actually run the caption through pandoc to parse the caption body text
captionstr = caption.groups()[0].replace("%","\%")
if (re.match(r'[f|F]igure\s\d+:',captionstr)):
captionstr = captionstr[captionstr.find(':')+1:]
replacement += "\t\caption{%s}\n" % captionstr.strip()
#
# Process label
if (label != None):
replacement += "\t\label{%s}\n" % label.groups()[0].strip()
replacement += "\end{figure}\n"
text = text[:match.start()] + replacement + text[match.end():]
return(text)
#
def parseFigureRefs(text):
start = 0
while (True):
# Match the next figure reference
# [Figure (number)](#(label))
# Can be multiline
match = re.search(r'\[Figure\s\d+\]\(#(.+?)\)', text, re.DOTALL)
# No more matches
if (match == None):
break
label = match.groups(0)[0]
replacement = "Figure \\ref{%s}" % label.strip()
text = text[:match.start()] + replacement + text[match.end():]
return(text)
#
def removeMdLines(text):
start = 0
while (True):
# Match the next horizontal line
# Three or more dashes (---)
# Can be multiline
match = re.search(r'\n\n---*', text, re.DOTALL)
# No more matches
if (match == None):
break
text = text[:match.start()] + "\n\clearpage\n" + text[match.end():]
return(text)
#
def formatInlineMath(text):
start = 0
while (True):
# Match the next inline equation
# \lstinline!$(math)$!
# Can be multiline
match = re.search(r'\\lstinline\!\$(.+?)\$\!', text, re.DOTALL)
# No more matches
if (match == None):
break
label = match.groups(0)[0]
replacement = "$ %s $" % label.replace("\\\\","\\").strip()
text = text[:match.start()] + replacement + text[match.end():]
return(text)
#
def formatSuperscript(text):
super_regex = [r'(\\\^\{\}\((.+?)\))([^\)])', r'(\\\^\{\}(\w+?))(\W)']
for regex in super_regex:
start = 0
while (True):
# Match the next text superscript (no parentheses)
# \^{}(superscript)
# Can be multiline
#match = re.search(r'\\\^\{\}(\w+?)\W', text, re.DOTALL)
#match = re.search(r'\\\^\{\}[\((.+?)\)[^\)]', text, re.DOTALL)
match = re.search(regex, text, re.DOTALL)
# No more matches
if (match == None):
break
replacement = "$^{%s}$%s" % (match.groups(1)[1], match.groups(1)[2])
text = text[:match.start()] + replacement + text[match.end():]
return(text)
#
def removeRefSection(text):
start = 0
while (True):
# Remove the heading relevant references at the end
# \section{Relevant References}\label{relevant-references}
# Can be multiline
match = re.search(r'\\section\{Relevant References\}\\label{relevant-references}', text, re.DOTALL & re.IGNORECASE)
# No more matches
if (match == None):
break
text = text[:match.start()] + text[match.end():]
return(text)
#
################################################################################################################################
##################
# Pre-processing #
##################
text = open(inputfile,'r').read()
# Read header
(header, text) = getYamlHeader(text)
# Parse liquid tags
text = parseLiquid(text, header=header)
# Parse figures in html tags
text = parseFiguresStepwise(text)
# Parse figure references
text = parseFigureRefs(text)
# Remove horizontal lines
text = removeMdLines(text)
# Add header to text and save temporary file
outfile = open(inputfile[:inputfile.rfind('.')]+"-temp"+inputfile[inputfile.rfind('.'):],'w', encoding='utf-8')
outfile.write("---\n")
#for key in header:
# outfile.write("%s : %s\n" % (key, header[key]))
yaml.dump(header, outfile, default_flow_style=False, encoding='utf-8')
outfile.write("---\n")
outfile.write(text)
outfile.close()
#####################
# Pandoc conversion #
#####################
call(getPandocCall(inputfile[:inputfile.rfind('.')]+"-temp"+inputfile[inputfile.rfind('.'):],
inputfile[:inputfile.rfind('.')]+"-temp.tex", template=template))
###################
# Post-processing #
###################
text = open(inputfile[:inputfile.rfind('.')]+"-temp.tex",'r').read()
# Framed boxes (quotes)
text = text.replace("\\begin{quote}","\\begin{framed}").replace("\end{quote}","\end{framed}")
# Format inline math (not properly converted by pandoc)
text = formatInlineMath(text)
# Format text superscripts (does not work perfectly)
text = formatSuperscript(text)
# Remove relevant references heading
text = removeRefSection(text)
# Remove unknown latex tags that pandoc inserts
# remove \toprule, \bottomrule, \tightlist
text = text.replace("\\toprule","").replace("\\bottomrule","").replace("\\tightlist","")
outfile = open(outputfile,'w',encoding='utf-8')
outfile.write(signature + text)
outfile.close()
# Remove temporary files
if (remove):
os.remove(inputfile[:inputfile.rfind('.')]+"-temp.tex")
os.remove(inputfile[:inputfile.rfind('.')]+"-temp"+inputfile[inputfile.rfind('.'):])
#################
# Compile latex #
#################
if (latex):
outputpath = outputfile[:outputfile.rfind('/')]+"/"
filename = outputfile[outputfile.rfind('/')+1:outputfile.rfind('.')]
here = os.getcwd()
os.chdir(outputpath)
call(['pdflatex',filename+'.tex'])
if ('bibtex' in header.keys()):
print (['bibtex',filename])
call(['bibtex',filename])
call(['pdflatex',filename+'.tex'])
call(['pdflatex',filename+'.tex'])
# Remove temporary files
if (remove):
os.remove(filename+".aux")
os.remove(filename+".log")
os.remove(filename+".out")
os.remove(filename+".run.xml")
if ('bibtex' in header.keys()):
os.remove(filename+"-blx.bib")
os.remove(filename+".bbl")
os.remove(filename+".blg")
os.chdir(here)