Skip to content

Commit 4eb548e

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 228c9b8 commit 4eb548e

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

plugins/dot2svg.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,21 @@
4343
_class_src = re.compile(r"""<g id="(edge|node|clust)\d+" class="(?P<type>edge|node|cluster)(?P<classes>[^"]*)">
4444
<title>(?P<title>[^<]*)</title>
4545
<(?P<element>ellipse|polygon|path|text)( 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> """
@@ -111,6 +121,20 @@ def element_repl(match):
111121
element=match.group('element'))
112122
svg = _class_src.sub(element_repl, svg)
113123

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

plugins/m/test/dot/page-240.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.

plugins/m/test/dot/page.rst

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

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

62+
Links:
63+
64+
.. digraph::
65+
66+
A [label="link to #" URL="#" style=filled class="m-primary"]
67+
B [label="link to /" URL="#" class="m-success"]
68+
69+
A -> B [label="link to m.css" URL="http://mcss.mosra.cz/" class="m-warning"]
70+
71+
Figures:
72+
6273
.. graph-figure:: This is a title.
6374

6475
.. digraph:: A to B

0 commit comments

Comments
 (0)