Skip to content

Commit eb63a89

Browse files
committed
[wip] m.dot: support node and edge links.
TODO: there's a separate <g id="a_edge1&#45;label"> for the edge label which should be removed and replaced with just <g> TODO: update test files for graphviz 2:36 and 2.38 (ugh)
1 parent e0d5836 commit eb63a89

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

pelican-plugins/dot2svg.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,21 @@
4343
# and <title>
4444
_class_src = re.compile(r"""<g id="(edge|node)\d+" class="(?P<type>edge|node)(?P<classes>[^"]*)">[\n]?<title>(?P<title>[^<]*)</title>
4545
<(?P<element>ellipse|polygon|path) fill="(?P<fill>[^"]+)" stroke="[^"]+" """)
46-
4746
_class_dst = r"""<g class="{classes}">
4847
<title>{title}</title>
4948
<{element} """
5049

50+
# Nodes that are links have an additional group containing a link inside. Again
51+
# Graphviz < 2.40 (Ubuntu 16.04 and older) doesn't have a linebreak between <g>
52+
# and <title>.
53+
_class_with_link_inside_src = re.compile(r"""<g id="(edge|node)\d+" class="(?P<type>edge|node)(?P<classes>[^"]*)">[\n]?<title>(?P<title>[^<]*)</title>
54+
<g id="a_(edge|node)\d+"><a (?P<link>[^>]+)>
55+
<(?P<element>ellipse|polygon|path) fill="(?P<fill>[^"]+)" stroke="[^"]+" """)
56+
_class_with_link_inside_dst = r"""<g class="{classes}">
57+
<title>{title}</title>
58+
<g><a {link}>
59+
<{element} """
60+
5161
_attributes_src = re.compile(r"""<(?P<element>ellipse|polygon|polyline) fill="[^"]+" stroke="[^"]+" """)
5262

5363
_attributes_dst = r"""<\g<element> """
@@ -108,6 +118,20 @@ def element_repl(match):
108118
element=match.group('element'))
109119
svg = _class_src.sub(element_repl, svg)
110120

121+
# Links have additional group around, second pass with a different regex
122+
def element_link_repl(match):
123+
classes = ['m-' + match.group('type')] + match.group('classes').replace('&#45;', '-').split()
124+
# distinguish between solid and filled nodes
125+
if match.group('type') == 'node' and match.group('fill') == 'none':
126+
classes += ['m-flat']
127+
128+
return _class_with_link_inside_dst.format(
129+
link=match.group('link'),
130+
classes=' '.join(classes),
131+
title=match.group('title'),
132+
element=match.group('element'))
133+
svg = _class_with_link_inside_src.sub(element_link_repl, svg)
134+
111135
# Remove unnecessary fill and stroke attributes
112136
svg = _attributes_src.sub(_attributes_dst, svg)
113137

pelican-plugins/m/test/dot/page.html

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pelican-plugins/m/test/dot/page.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ Structs:
5555

5656
another [label="a | { b | c } | d | e" shape=record]
5757

58+
Links:
59+
60+
.. digraph::
61+
62+
A [label="link to #" URL="#" style=filled class="m-primary"]
63+
B [label="link to /" URL="#" class="m-success"]
64+
65+
A -> B [label="link to m.css" URL="http://mcss.mosra.cz/" class="m-warning"]
66+
67+
Figures:
68+
5869
.. graph-figure:: This is a title.
5970

6071
.. digraph:: A to B

0 commit comments

Comments
 (0)