-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyx2pub.py
52 lines (37 loc) · 1.11 KB
/
lyx2pub.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
"""LyX2Pub -- combine LyX with any LaTeX template for publishing.
Usage:
lyx2pub [options] <file.lyx>
Options:
<file.lyx>
-t --template=<template.tex> Template file (where %DOCUMENT% is replaced with the content)
"""
import re
import subprocess
from docopt import docopt
document_replace = "%DOCUMENT%"
document_re = re.compile(r"\\begin\{document\}(.*)\\end\{document\}", flags=re.DOTALL)
def lyx2pub(input_path, template_path):
export_path = "lyx2pub_export.tex"
output_path = "lyx2pub_output.tex"
subprocess.run(
[
"lyx",
"-E", "pdflatex", export_path,
input_path,
],
)
with open(export_path) as f:
data = f.read()
with open(template_path) as f:
template = f.read()
content = document_re.search(data).group(1)
assert document_replace in template
combined = template.replace(document_replace, content)
with open(output_path, "w") as f:
f.write(combined)
def main():
args = docopt(__doc__)
lyx2pub(
input_path=args["<file.lyx>"],
template_path=args["--template"],
)