|
43 | 43 | _class_src = re.compile(r"""<g id="(edge|node|clust)\d+" class="(?P<type>edge|node|cluster)(?P<classes>[^"]*)"> |
44 | 44 | <title>(?P<title>[^<]*)</title> |
45 | 45 | <(?P<element>ellipse|polygon|path|text)( fill="(?P<fill>[^"]+)" stroke="[^"]+")? """) |
46 | | - |
47 | 46 | _class_dst = r"""<g class="{classes}"> |
48 | 47 | <title>{title}</title> |
49 | 48 | <{element} """ |
50 | 49 |
|
| 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 | + |
51 | 61 | _attributes_src = re.compile(r"""<(?P<element>ellipse|polygon|polyline) fill="[^"]+" stroke="[^"]+" """) |
52 | 62 |
|
53 | 63 | _attributes_dst = r"""<\g<element> """ |
@@ -111,6 +121,20 @@ def element_repl(match): |
111 | 121 | element=match.group('element')) |
112 | 122 | svg = _class_src.sub(element_repl, svg) |
113 | 123 |
|
| 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('-', '-').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 | + |
114 | 138 | # Remove unnecessary fill and stroke attributes |
115 | 139 | svg = _attributes_src.sub(_attributes_dst, svg) |
116 | 140 |
|
|
0 commit comments