Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Graphviz/dot: support node and edge links #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion plugins/dot2svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@
_class_src = re.compile(r"""<g id="(edge|node|clust)\d+" class="(?P<type>edge|node|cluster)(?P<classes>[^"]*)">
<title>(?P<title>[^<]*)</title>
<(?P<element>ellipse|polygon|path|text)( 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 @@ -111,6 +121,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 plugins/m/test/dot/page-240.html

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

11 changes: 11 additions & 0 deletions plugins/m/test/dot/page.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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