-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtextToHtml.py
executable file
·142 lines (112 loc) · 4.29 KB
/
textToHtml.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
#!/usr/bin/env python3
import sys
import subprocess
import shutil
options = {
######## USER INPUT #########
"sectionNr": True,
"TOC": True,
"TOC_depth": 2,
"pathToBib": "lib2.bib",
"template": "canvasTemplate.html",
"filters": ["highlightCode.py",
"addEquationNumbers.py"]
#############################
}
def helpMessage():
return ("""
OVERVIEW:
This tool helps to convert LaTeX (.tex) of Markdown (.md) files to
plain HTML. It supports syntax highlighting for code blocks (lstlisting),
equation numbers and links to equations. It provides these
functions in plain HTML, so no CSS or external sources are needed.
The only thing it can't handle are images, it will process them,
but they will probably not display in your HTML. They can however
be added manually (not by this tool).
DEPENDENCIES:
This tool requires a working (and added to the path) installation
of pandoc:
https://pandoc.org/installing.html
To use the filters for syntax highlighting and equation numbering
we need to have the following python packages installed (the
install command is a suggestion, conda, for example, comes with
these packages pre installed, they may be old though!).
* pandocfilters (INSTALL: pip3 install pandocfilters)
* pygments (INSTALL: pip3 install pygments)
USAGE:
To use the tool simply run this file with as input parameter the
.tex file and optionally the name of the output file. On Linux
./textToHtml.py <input>.tex
or
./textToHtml.py <input>.tex <output>.html
NB: for all the features to work the following files should be
accessible (preferably in the working directory) and .py files
should be executable:
* highlightCode.py
* addEquationNumbers.py
* htmlTemplate.html
ADDITIONAL OPTIONS:
To control the eventual look of the output HTML we can change
all the default options in the textToHtml.py file, the options
are at the top labelled 'USER INPUT'. The options to change
* table of contents
* section numbering
* templates controls the layout of the HTML file
* the bibliography source file
* and which filters to use
To change the colorscheme for syntax highlighting, change
the style variable in the highlightCode.py filter source file.
""")
def runPandoc(inputFile, outputFile="output.html", markdown=False):
global options
#defaults
args = ["pandoc", "--mathml", "--standalone", "--to=html"]
# input file format
if markdown:
args.append("--from=markdown")
else:
args.append("--from=latex")
# Processing options
if options["sectionNr"]:
args.append("--number-offset=0")
if options["TOC"]:
args.append("--toc")
if "TOC_depth" in options:
args.append(f"--toc-depth={options['TOC_depth']}")
if "pathToBib" in options:
args.append(f"--bibliography={options['pathToBib']}")
if "template" in options:
args.append(f"--template={options['template']}")
for filter in options["filters"]:
args.append(f"--filter={filter}")
args.append(f"--output={outputFile}")
args.append(inputFile)
# run pandocs
print(f"Processing: {inputFile}")
subprocess.Popen(args)
print(f"HTML is written to: {outputFile}")
if __name__ == "__main__":
if (shutil.which("pandoc") == None):
print("No pandoc installation found.")
exit()
userOptions = sys.argv
if len(userOptions) == 1:
print("Please specify at least an input file (.tex or .md)")
print("General usage: textToHtml.py <input>.tex <output>.html")
print("For a help message use: textToHtml.py help")
if len(userOptions) == 2:
if userOptions[1][-4:] == ".tex":
runPandoc(userOptions[1])
elif userOptions[1][-3:] == ".md":
runPandoc(userOptions[1], markdown=True)
elif userOptions[1].find("help") != -1:
print(helpMessage())
else:
print("Only .tex or .md files are supported.")
if len(userOptions) == 3:
if userOptions[1][-4:] == ".tex":
runPandoc(userOptions[1], userOptions[2])
elif userOptions[1][-3:] == ".md":
runPandoc(userOptions[1], userOptions[2], markdown=True)
else:
print("Only .tex or .md files are supported.")