This repository has been archived by the owner on Aug 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
markdeep_diagram.py
172 lines (134 loc) · 5.07 KB
/
markdeep_diagram.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
import re
import os
import string
import base64
import tempfile
import markdown
import hashlib
from selenium import webdriver
from subprocess import call, PIPE
# Defines our basic inline image
DIAGRAM_EXPR = "<center class=\"md\">{}</center>"
# Base CSS template
DIAGRAM_CSS = r"""<style scoped>
svg.diagram{display:block;font-family:'Ubuntu Mono';font-size:14px;text-align:center;stroke-linecap:round;stroke-width:1.5px;stroke:#000;fill:#000}.md
svg.diagram .opendot{fill:#FFF}.md
svg.diagram text{stroke:none}.md
</style>
"""
class MarkdeepDiagramPreprocessor(markdown.preprocessors.Preprocessor):
# These are our cached expressions that are stored in latex.cache
cached = {}
# Basic markdeep setup
markdeep_preamble = r"""
<!DOCTYPE html>
<html>
<body>
<diagram>
{}
</diagram>
<script>window.markdeepOptions = {{mode: 'html'}};</script>
<script src="https://casual-effects.com/markdeep/latest/markdeep.min.js"></script>
</body>
</html>
"""
def __init__(self, configs):
try:
cache_file = open('markdeep.cache', 'r+')
for line in cache_file.readlines():
key, val = line.strip("\n").split("$")
self.cached[key] = val
except IOError:
pass
self.re_diagram = re.compile(r'<markdeep-diagram>[\n\r]*(?P<code>.*?)[\n\r]*</markdeep-diagram>', re.MULTILINE | re.DOTALL)
def _diagram_to_svg(self, markdeep):
"""Generates a SVG representation of Diagram string"""
import urlparse, urllib
def path2url(path):
return urlparse.urljoin(
'file:', urllib.pathname2url(os.path.abspath(path)))
# Generate the temporary file
tempfile.tempdir = ""
tmp_file_fd, path = tempfile.mkstemp()
with open(path + ".html", "w") as fd:
fd.write(self.markdeep_preamble.format(markdeep + " "))
driver = webdriver.PhantomJS() # or add to your PATH
driver.set_window_size(1024*2, 768*2) # optional
driver.get(path2url(path + ".html"))
ps = driver.page_source
driver.quit()
re_svg = re.compile("<svg.*?</svg>", re.MULTILINE | re.DOTALL)
svg = re_svg.findall(ps)[0]
fsvg = "%s.svg" % path
fsvgo = "%s-opt.svg" % path
with open(fsvg, "w") as f:
f.write(svg)
cmd = "svgo --multipass %s %s" % (fsvg, fsvgo)
status = call(cmd.split(), stdout=PIPE)
# Read the png and encode the data
svg = open(fsvgo, "rb")
data = svg.read()
svg.close()
self._cleanup(path)
return data
def _cleanup(self, path, err=False):
# don't clean up the log if there's an error
extensions = ["", "-opt.svg", ".svg", ".html"]
if err:
extensions.pop()
# now do the actual cleanup, passing on non-existent files
for extension in extensions:
try:
os.remove("%s%s" % (path, extension))
except (IOError, OSError):
pass
def run(self, lines):
"""Parses the actual page"""
# Re-creates the entire page so we can parse in a multine env.
page = "\n".join(lines)
# Figure out our text strings and math-mode strings
tex_expr = [(self.re_diagram, x) for x in self.re_diagram.findall(page)]
# No sense in doing the extra work
if not len(tex_expr):
return page.split("\n")
# Parse the expressions
new_cache = {}
id = 0
for reg, expr in tex_expr:
# print reg, mode, expr
hash_expr = hashlib.sha1(expr).hexdigest()
if hash_expr in self.cached:
data = self.cached[hash_expr]
else:
print expr
data = self._diagram_to_svg(expr)
new_cache[hash_expr] = data
id += 1
diagram = DIAGRAM_EXPR.format(data)
page = reg.sub(diagram, page, 1)
# Cache our data
cache_file = open('markdeep.cache', 'a')
for key, value in new_cache.items():
cache_file.write("%s$%s\n" % (key, value))
cache_file.close()
# Make sure to resplit the lines
return page.split("\n")
class MarkdeepDiagramPostprocessor(markdown.postprocessors.Postprocessor):
"""This post processor extension just allows us to further
refine, if necessary, the document after it has been parsed."""
def run(self, text):
# Inline a style for default behavior
text = DIAGRAM_CSS + text
return text
class MarkdeepDiagram(markdown.Extension):
"""Wrapper for LaTeXPreprocessor"""
def extendMarkdown(self, md, md_globals):
# Our base LaTeX extension
md.preprocessors.add('markdeep',
MarkdeepDiagramPreprocessor(self), ">html_block")
# Our cleanup postprocessing extension
md.postprocessors.add('markdeep',
MarkdeepDiagramPostprocessor(self), ">amp_substitute")
def makeExtension(*args, **kwargs):
"""Wrapper for a MarkDeep extension"""
return MarkdeepDiagram(*args, **kwargs)