Fix graphviz: workaround for wrong map ID which graphviz generates

This commit is contained in:
Takeshi KOMIYA 2018-01-29 21:09:28 +09:00
parent 2312a0a5bc
commit 8494638e45
3 changed files with 28 additions and 18 deletions

View File

@ -25,6 +25,7 @@ Bugs fixed
* #4449: apidoc: include "empty" packages that contain modules
* #3917: citation labels are tranformed to ellipsis
* #4501: graphviz: epub3 validation error caused if graph is not clickable
* #4514: graphviz: workaround for wrong map ID which graphviz generates
Testing
--------

View File

@ -47,22 +47,29 @@ class ClickableMapDefinition(object):
maptag_re = re.compile('<map id="(.*?)"')
href_re = re.compile('href=".*?"')
def __init__(self, filename, content):
# type: (unicode, unicode) -> None
def __init__(self, filename, content, dot=''):
# type: (unicode, unicode, unicode) -> None
self.id = None
self.filename = filename
self.content = content.splitlines()
self.clickable = [] # type: List[unicode]
self.parse()
self.parse(dot=dot)
def parse(self):
# type: () -> None
def parse(self, dot=None):
# type: (None) -> None
matched = self.maptag_re.match(self.content[0]) # type: ignore
if not matched:
raise GraphvizError('Invalid clickable map file found: %s' % self.filename)
self.id = matched.group(1)
if self.id == '%3':
# graphviz generates wrong ID if graph name not specified
# https://gitlab.com/graphviz/graphviz/issues/1327
hashed = sha1(dot.encode('utf-8')).hexdigest()
self.id = 'grapviz%s' % hashed[-10:]
self.content[0] = self.content[0].replace('%3', self.id)
for line in self.content:
if self.href_re.search(line):
self.clickable.append(line)
@ -289,7 +296,7 @@ def render_dot_html(self, node, code, options, prefix='graphviz',
self.body.append(svgtag)
else:
with codecs.open(outfn + '.map', 'r', encoding='utf-8') as mapfile:
imgmap = ClickableMapDefinition(outfn + '.map', mapfile.read())
imgmap = ClickableMapDefinition(outfn + '.map', mapfile.read(), dot=code)
if imgmap.clickable:
# has a map
self.body.append('<img src="%s" alt="%s" usemap="#%s" %s/>\n' %

View File

@ -118,30 +118,32 @@ def test_graphviz_i18n(app, status, warning):
def test_graphviz_parse_mapfile():
# digraph {
# }
# empty graph
code = ('# digraph {\n'
'# }\n')
content = ('<map id="%3" name="%3">\n'
'</map>')
cmap = ClickableMapDefinition('dummy.map', content)
cmap = ClickableMapDefinition('dummy.map', content, code)
assert cmap.filename == 'dummy.map'
assert cmap.id == '%3'
assert cmap.id == 'grapvizb08107169e'
assert len(cmap.clickable) == 0
assert cmap.generate_clickable_map() == ''
# digraph {
# foo [href="http://www.google.com/"];
# foo -> bar;
# }
# normal graph
code = ('digraph {\n'
' foo [href="http://www.google.com/"];\n'
' foo -> bar;\n'
'}\n')
content = ('<map id="%3" name="%3">\n'
'<area shape="poly" id="node1" href="http://www.google.com/" title="foo" alt=""'
' coords="77,29,76,22,70,15,62,10,52,7,41,5,30,7,20,10,12,15,7,22,5,29,7,37,12,'
'43,20,49,30,52,41,53,52,52,62,49,70,43,76,37"/>\n'
'</map>')
cmap = ClickableMapDefinition('dummy.map', content)
cmap = ClickableMapDefinition('dummy.map', content, code)
assert cmap.filename == 'dummy.map'
assert cmap.id == '%3'
assert cmap.id == 'grapviza4ccdd48ce'
assert len(cmap.clickable) == 1
assert cmap.generate_clickable_map() == content
assert cmap.generate_clickable_map() == content.replace('%3', cmap.id)
# inheritance-diagram:: sphinx.builders.html
content = (
@ -168,7 +170,7 @@ def test_graphviz_parse_mapfile():
' alt="" coords="11,3,141,19"/>\n'
'</map>'
)
cmap = ClickableMapDefinition('dummy.map', content)
cmap = ClickableMapDefinition('dummy.map', content, 'dummy_code')
assert cmap.filename == 'dummy.map'
assert cmap.id == 'inheritance66ff5471b9'
assert len(cmap.clickable) == 0