Skip to content

Commit

Permalink
[wip] m.dot: support node and edge links.
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
mosra committed Oct 15, 2018
1 parent e0d5836 commit eb63a89
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
26 changes: 25 additions & 1 deletion pelican-plugins/dot2svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@
# and <title>
_class_src = re.compile(r"""<g id="(edge|node)\d+" class="(?P<type>edge|node)(?P<classes>[^"]*)">[\n]?<title>(?P<title>[^<]*)</title>
<(?P<element>ellipse|polygon|path) fill="(?P<fill>[^"]+)" stroke="[^"]+" """)

_class_dst = r"""<g class="{classes}">
<title>{title}</title>
<{element} """

# Nodes that are links have an additional group containing a link inside. Again
# Graphviz < 2.40 (Ubuntu 16.04 and older) doesn't have a linebreak between <g>
# and <title>.
_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>
<g id="a_(edge|node)\d+"><a (?P<link>[^>]+)>
<(?P<element>ellipse|polygon|path) fill="(?P<fill>[^"]+)" stroke="[^"]+" """)
_class_with_link_inside_dst = r"""<g class="{classes}">
<title>{title}</title>
<g><a {link}>
<{element} """

_attributes_src = re.compile(r"""<(?P<element>ellipse|polygon|polyline) fill="[^"]+" stroke="[^"]+" """)

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

# Links have additional group around, second pass with a different regex
def element_link_repl(match):
classes = ['m-' + match.group('type')] + match.group('classes').replace('&#45;', '-').split()
# distinguish between solid and filled nodes
if match.group('type') == 'node' and match.group('fill') == 'none':
classes += ['m-flat']

return _class_with_link_inside_dst.format(
link=match.group('link'),
classes=' '.join(classes),
title=match.group('title'),
element=match.group('element'))
svg = _class_with_link_inside_src.sub(element_link_repl, svg)

# Remove unnecessary fill and stroke attributes
svg = _attributes_src.sub(_attributes_dst, svg)

Expand Down
36 changes: 36 additions & 0 deletions pelican-plugins/m/test/dot/page.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions pelican-plugins/m/test/dot/page.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ Structs:

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

Links:

.. digraph::

A [label="link to #" URL="#" style=filled class="m-primary"]
B [label="link to /" URL="#" class="m-success"]

A -> B [label="link to m.css" URL="http://mcss.mosra.cz/" class="m-warning"]

Figures:

.. graph-figure:: This is a title.

.. digraph:: A to B
Expand Down

0 comments on commit eb63a89

Please sign in to comment.