/
dot.py
115 lines (99 loc) · 4.49 KB
/
dot.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
#
# This file is part of m.css.
#
# Copyright © 2017, 2018, 2019 Vladimír Vondruš <mosra@centrum.cz>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
import pelican
import re
import subprocess
from docutils import nodes
from docutils.parsers import rst
from docutils.parsers.rst import directives
from docutils.parsers.rst.roles import set_classes
import dot2svg
def _is_graph_figure(parent):
# The parent has to be a figure, marked as m-figure
if not isinstance(parent, nodes.figure): return False
if 'm-figure' not in parent.get('classes', []): return False
# And as a first visible node of such type
for child in parent:
if not isinstance(child, nodes.Invisible): return False
return True
class Dot(rst.Directive):
has_content = True
optional_arguments = 1
final_argument_whitespace = True
option_spec = {'class': directives.class_option,
'name': directives.unchanged}
def run(self, source):
set_classes(self.options)
# If this is the first real node inside a graph figure, put the SVG
# directly inside
parent = self.state.parent
if _is_graph_figure(parent):
svg = dot2svg.dot2svg(source, attribs=' class="{}"'.format(' '.join(['m-graph'] + self.options.get('classes', []))))
node = nodes.raw('', svg, format='html')
return [node]
# Otherwise wrap it in a <div class="m-graph">
svg = dot2svg.dot2svg(source)
container = nodes.container(**self.options)
container['classes'] = ['m-graph'] + container['classes']
node = nodes.raw('', svg, format='html')
container.append(node)
return [container]
class Digraph(Dot):
def run(self):
# We need to pass "" for an empty title to get rid of <title>,
# otherwise the output contains <title>%3</title> (wtf!)
return Dot.run(self, 'digraph "{}" {{\n{}}}'.format(
self.arguments[0] if self.arguments else '',
'\n'.join(self.content)))
class StrictDigraph(Dot):
def run(self):
# We need to pass "" for an empty title to get rid of <title>,
# otherwise the output contains <title>%3</title> (wtf!)
return Dot.run(self, 'strict digraph "{}" {{\n{}}}'.format(
self.arguments[0] if self.arguments else '',
'\n'.join(self.content)))
class Graph(Dot):
def run(self):
# We need to pass "" for an empty title to get rid of <title>,
# otherwise the output contains <title>%3</title> (wtf!)
return Dot.run(self, 'graph "{}" {{\n{}}}'.format(
self.arguments[0] if self.arguments else '',
'\n'.join(self.content)))
class StrictGraph(Dot):
def run(self):
# We need to pass "" for an empty title to get rid of <title>,
# otherwise the output contains <title>%3</title> (wtf!)
return Dot.run(self, 'strict graph "{}" {{\n{}}}'.format(
self.arguments[0] if self.arguments else '',
'\n'.join(self.content)))
def configure(pelicanobj):
dot2svg.configure(
pelicanobj.settings.get('M_DOT_FONT', 'Source Sans Pro'),
pelicanobj.settings.get('M_DOT_FONT_SIZE', 16.0))
def register():
pelican.signals.initialized.connect(configure)
rst.directives.register_directive('digraph', Digraph)
rst.directives.register_directive('strict-digraph', StrictDigraph)
rst.directives.register_directive('graph', Graph)
rst.directives.register_directive('strict-graph', StrictGraph)