From eb63a89c28a74ef175cdc17d17cac724686ea480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 15 Oct 2018 12:26:25 +0200 Subject: [PATCH] [wip] m.dot: support node and edge links. TODO: there's a separate for the edge label which should be removed and replaced with just TODO: update test files for graphviz 2:36 and 2.38 (ugh) --- pelican-plugins/dot2svg.py | 26 +++++++++++++++++++- pelican-plugins/m/test/dot/page.html | 36 ++++++++++++++++++++++++++++ pelican-plugins/m/test/dot/page.rst | 11 +++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/pelican-plugins/dot2svg.py b/pelican-plugins/dot2svg.py index bece7ad9..0bb6d553 100644 --- a/pelican-plugins/dot2svg.py +++ b/pelican-plugins/dot2svg.py @@ -43,11 +43,21 @@ # and _class_src = re.compile(r"""<g id="(edge|node)\d+" class="(?P<type>edge|node)(?P<classes>[^"]*)">[\n]?<title>(?P<title>[^<]*) <(?Pellipse|polygon|path) fill="(?P[^"]+)" stroke="[^"]+" """) - _class_dst = r""" {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 +# and . +_class_with_link_inside_src = re.compile(r"""<g id="(edge|node)\d+" class="(?P<type>edge|node)(?P<classes>[^"]*)">[\n]?<title>(?P<title>[^<]*) +[^>]+)> +<(?Pellipse|polygon|path) fill="(?P[^"]+)" stroke="[^"]+" """) +_class_with_link_inside_dst = r""" +{title} + +<{element} """ + _attributes_src = re.compile(r"""<(?Pellipse|polygon|polyline) fill="[^"]+" stroke="[^"]+" """) _attributes_dst = r"""<\g """ @@ -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('-', '-').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) diff --git a/pelican-plugins/m/test/dot/page.html b/pelican-plugins/m/test/dot/page.html index ff53a240..d0da4caf 100644 --- a/pelican-plugins/m/test/dot/page.html +++ b/pelican-plugins/m/test/dot/page.html @@ -200,6 +200,42 @@

m.dot

+

Links:

+
+ + + +A + + +link to # + + + + +B + + +link to / + + + + +A->B + + + + + + +link to m.css + + + + + +
+

Figures:

diff --git a/pelican-plugins/m/test/dot/page.rst b/pelican-plugins/m/test/dot/page.rst index c3d09d29..39817e58 100644 --- a/pelican-plugins/m/test/dot/page.rst +++ b/pelican-plugins/m/test/dot/page.rst @@ -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