1.1 --- a/moinformat/utils/graphviz.py Tue Nov 27 21:23:24 2018 +0100
1.2 +++ b/moinformat/utils/graphviz.py Tue Nov 27 22:48:12 2018 +0100
1.3 @@ -22,6 +22,7 @@
1.4 from os.path import exists, join
1.5 from StringIO import StringIO
1.6 from subprocess import Popen, PIPE
1.7 +from xml.sax.saxutils import XMLGenerator
1.8 import gzip
1.9 import sha
1.10 import xml.sax
1.11 @@ -63,16 +64,9 @@
1.12 else:
1.13 return s
1.14
1.15 -class MetadataParser(xml.sax.handler.ContentHandler):
1.16 -
1.17 - "Parse metadata from the svg element."
1.18 +class Parser(xml.sax.handler.ContentHandler):
1.19
1.20 - def __init__(self):
1.21 - self.attrs = {}
1.22 -
1.23 - def startElement(self, name, attrs):
1.24 - if name == self.tagname:
1.25 - self.attrs = dict(attrs)
1.26 + "Common XML parsing functionality."
1.27
1.28 def parse(self, f):
1.29
1.30 @@ -87,11 +81,9 @@
1.31 finally:
1.32 f.close()
1.33
1.34 - def get_metadata(self, data, tagname):
1.35 + def parse_data(self, data):
1.36
1.37 - "Process 'data', returning attributes from 'tagname'."
1.38 -
1.39 - self.tagname = tagname
1.40 + "Parse the given 'data'."
1.41
1.42 f = StringIO(data)
1.43 try:
1.44 @@ -99,8 +91,32 @@
1.45 finally:
1.46 f.close()
1.47
1.48 +class MetadataParser(Parser):
1.49 +
1.50 + "Parse metadata from the svg element."
1.51 +
1.52 + def __init__(self):
1.53 + self.attrs = {}
1.54 +
1.55 + def startElement(self, name, attrs):
1.56 + if name == self.tagname:
1.57 + self.attrs = dict(attrs)
1.58 +
1.59 + def get_metadata(self, data, tagname):
1.60 +
1.61 + "Process 'data', returning attributes from 'tagname'."
1.62 +
1.63 + self.tagname = tagname
1.64 + self.parse_data(data)
1.65 return self.attrs
1.66
1.67 +class DocumentSelector(XMLGenerator, Parser):
1.68 +
1.69 + "Parse a document and obtain the serialisation of the document node."
1.70 +
1.71 + def startDocument(self):
1.72 + pass
1.73 +
1.74 def get_output_identifier(text):
1.75
1.76 "Return an output identifier for the given 'text'."
1.77 @@ -179,17 +195,14 @@
1.78
1.79 "A Graphviz configuration for single or repeated invocation."
1.80
1.81 - def __init__(self, filter, text, identifier):
1.82 + def __init__(self, filter, text):
1.83
1.84 """
1.85 - Employ the given 'filter' to produce a graph from the given 'text'. The
1.86 - output 'identifier' for the text is used to provide a filename, if
1.87 - required.
1.88 + Employ the given 'filter' to produce a graph from the given 'text'.
1.89 """
1.90
1.91 self.filter = filter
1.92 self.text = text
1.93 - self.identifier = identifier
1.94
1.95 def call(self, format, transforms=None, filename=None):
1.96
1.97 @@ -264,4 +277,20 @@
1.98 def get_output(self):
1.99 return self.output
1.100
1.101 + def get_inline_output(self):
1.102 +
1.103 + """
1.104 + Return a string containing the document element, excluding XML
1.105 + boilerplate.
1.106 + """
1.107 +
1.108 + f = StringIO()
1.109 + parser = DocumentSelector(f, "utf-8")
1.110 +
1.111 + try:
1.112 + parser.parse_data(self.output)
1.113 + return f.getvalue()
1.114 + finally:
1.115 + f.close()
1.116 +
1.117 # vim: tabstop=4 expandtab shiftwidth=4