forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathditaa.py
More file actions
220 lines (198 loc) · 7.75 KB
/
ditaa.py
File metadata and controls
220 lines (198 loc) · 7.75 KB
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
# -*- coding: utf-8 -*-
"""
sphinx.ext.ditaa
~~~~~~~~~~~~~~~~~~~~~~~~~
Allow ditaa-formatted graphs to by included in Sphinx-generated
documents inline.
:copyright: Copyright 2017 by Yongping Guo
:license: BSD, see LICENSE for details.
"""
from __future__ import print_function
import re, os
import codecs
import posixpath
from os import path
from math import ceil
from subprocess import Popen, PIPE
try:
from hashlib import sha1 as sha
except ImportError:
from sha import sha
from docutils import nodes
from docutils.parsers.rst import directives
from sphinx.errors import SphinxError
from sphinx.util.osutil import ensuredir, ENOENT, EPIPE
from sphinx.util import relative_uri
#from sphinx.util.compat import Directive
from docutils.parsers.rst import Directive
mapname_re = re.compile(r'<map id="(.*?)"')
svg_dim_re = re.compile(r'<svg\swidth="(\d+)pt"\sheight="(\d+)pt"', re.M)
class DitaaError(SphinxError):
category = 'Ditaa error'
class ditaa(nodes.General, nodes.Element):
pass
class Ditaa(directives.images.Image):
"""
Directive to insert ditaa markup.
"""
has_content = True
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = False
option_spec = {
# image parameter
'name': directives.unchanged,
'class': directives.unchanged,
'alt': directives.unchanged,
'title': directives.unchanged,
'height': directives.unchanged,
'width': directives.unchanged,
'scale': directives.unchanged,
'align': directives.unchanged,
'target': directives.unchanged,
'inline': directives.unchanged,
# ditaa parameter
'--no-antialias': directives.flag,
'--background': directives.unchanged,
'--no-antialias': directives.flag,
'--no-separation': directives.flag,
'--encoding': directives.unchanged,
'--html': directives.flag,
'--overwrite': directives.flag,
'--round-corners': directives.flag,
'--no-shadows': directives.flag,
'--scale': directives.unchanged,
'--transparent': directives.flag,
'--tabs': directives.unchanged,
'--fixed-slope': directives.flag,
}
def run(self):
if self.arguments:
print(self.arguments)
document = self.state.document
if self.content:
return [document.reporter.warning(
'Ditaa directive cannot have both content and '
'a filename argument', line=self.lineno)]
env = self.state.document.settings.env
rel_filename, filename = env.relfn2path(self.arguments[0])
env.note_dependency(rel_filename)
try:
fp = codecs.open(filename, 'r', 'utf-8')
try:
dotcode = fp.read()
finally:
fp.close()
except (IOError, OSError):
return [document.reporter.warning(
'External Ditaa file %r not found or reading '
'it failed' % filename, line=self.lineno)]
else:
dotcode = '\n'.join(self.content)
if not dotcode.strip():
return [self.state_machine.reporter.warning(
'Ignoring "ditaa" directive without content.',
line=self.lineno)]
node = ditaa()
node['code'] = dotcode
node['options'] = []
node['img_options'] = {}
for k,v in self.options.items():
if k[:2] == '--':
node['options'].append(k)
if v is not None:
node['options'].append(v)
else:
node['img_options'][k] = v
#if v is not None:
# node['options'].append("%s" %(v))
return [node]
def render_ditaa(app, code, options, format, prefix='ditaa'):
"""Render ditaa code into a PNG output file."""
# ditaa expects UTF-8 by default
code = code.encode('utf-8')
hashkey = str(code) + str(options) + \
str(app.builder.config.ditaa) + \
str(app.builder.config.ditaa_args)
infname = '%s-%s.%s' % (prefix, sha(hashkey.encode('utf-8')).hexdigest(), "ditaa")
outfname = '%s-%s.%s' % (prefix, sha(hashkey.encode('utf-8')).hexdigest(), "png")
rel_imgpath = (format == "html") and relative_uri(app.builder.env.docname, app.builder.imagedir) or ''
infullfn = path.join(app.builder.outdir, app.builder.imagedir, infname)
outrelfn = posixpath.join(relative_uri(app.builder.env.docname, app.builder.imagedir), outfname)
outfullfn = path.join(app.builder.outdir, app.builder.imagedir, outfname)
#inrelfn = posixpath.join(relative_uri(app.builder.env.docname, app.builder.imagedir), infname)
if path.isfile(outfullfn):
return outrelfn, outfullfn
ensuredir(path.dirname(outfullfn))
ditaa_args = [app.builder.config.ditaa]
ditaa_args.extend(app.builder.config.ditaa_args)
ditaa_args.extend(options)
ditaa_args.extend( [infname, outfname] ) # use relative path
f = open(infullfn, 'wb')
f.write(code)
f.close()
currpath = os.getcwd()
os.chdir(path.join(app.builder.outdir, app.builder.imagedir))
try:
if app.builder.config.ditaa_log_enable:
print("rending %s" %(outfullfn))
#app.builder.warn(ditaa_args)
p = Popen(ditaa_args, stdout=PIPE, stdin=PIPE, stderr=PIPE)
except OSError as err:
if err.errno != ENOENT: # No such file or directory
raise
app.builder.warn('ditaa command %r cannot be run (needed for ditaa '
'output), check the ditaa setting' %
app.builder.config.ditaa)
app.builder._ditaa_warned_dot = True
os.chdir(currpath)
return None, None
os.chdir(currpath)
wentWrong = False
try:
# Ditaa may close standard input when an error occurs,
# resulting in a broken pipe on communicate()
stdout, stderr = p.communicate(code)
except OSError as err:
if err.errno != EPIPE:
raise
wentWrong = True
except IOError as err:
if err.errno != EINVAL:
raise
wentWrong = True
if wentWrong:
# in this case, read the standard output and standard error streams
# directly, to get the error message(s)
stdout, stderr = p.stdout.read(), p.stderr.read()
p.wait()
if p.returncode != 0:
raise DitaaError('ditaa exited with error:\n[stderr]\n%s\n'
'[stdout]\n%s' % (stderr, stdout))
return outrelfn, outfullfn
def on_doctree_resolved(app, doctree):
#print "app.builder.env.docname: ", app.builder.env.docname
for node in doctree.traverse(ditaa):
# Generate the output png files
relfn, outfn = render_ditaa(app, node['code'], node['options'], app.builder.format, "ditaa")
image = nodes.image(uri=relfn, candidates={'*': outfn}, **node['img_options'])
#for (k, v) in options.items():
# image[k] = v
node.parent.replace(node, image)
def setup(app):
app.add_node(ditaa, html=(None, None),
#latex=(latex_visit_ditaa, None),
)
app.add_directive('ditaa', Ditaa)
try:
ditaa_cmd = os.environ['SPHINX_DITAA_CMD']
except:
ditaa_cmd = 'ditaa'
try:
ditaa_arg = os.environ['SPHINX_DITAA_ARG'].split(':')
except:
ditaa_arg = []
app.add_config_value('ditaa', ditaa_cmd , 'html')
app.add_config_value('ditaa_args', ditaa_arg, 'html')
app.add_config_value('ditaa_log_enable', True, 'html')
app.connect('doctree-read', on_doctree_resolved)