# HG changeset patch # User Paul Boddie # Date 1543355292 -3600 # Node ID c1284f3e6af52acc94edb87d9f31ebafb8f663f8 # Parent 524700c23195f0e167a24ef0512a2842993f73bd Made SVG output inline, enabling convenient linking and avoiding attachment use. Eliminated the redundant identifier parameter for the Graphviz utility class. diff -r 524700c23195 -r c1284f3e6af5 moinformat/serialisers/html/graphviz.py --- a/moinformat/serialisers/html/graphviz.py Tue Nov 27 21:23:24 2018 +0100 +++ b/moinformat/serialisers/html/graphviz.py Tue Nov 27 22:48:12 2018 +0100 @@ -97,27 +97,36 @@ format = self.directives.get("format", ["svg"])[0] transforms = self.directives.get("transform", []) - # Graph output is stored for a known page only. + inline = format == "svg" + + # Non-inline graph output is stored for a known page only. pagename = self.metadata.get("pagename") - if not pagename: - return + + if not inline: + if not pagename: + return - # Get an identifier and usable filename to store the output. + # Get an identifier and usable filename to store the output. - identifier = get_output_identifier(text) - attachment = "%s.%s" % (identifier, format) - filename = self.output.get_attachment_filename(pagename, attachment) + identifier = get_output_identifier(text) + attachment = "%s.%s" % (identifier, format) + filename = self.output.get_attachment_filename(pagename, attachment) + + # Handle situations where no independent output is permitted. - # Handle situations where no independent output is permitted. + if not filename: + return - if not filename: - return + # Make sure that page attachments can be stored. - # Make sure that page attachments can be stored. + self.output.ensure_attachments(pagename) + target, _label = self.linker.translate("attachment:%s" % attachment) - self.output.ensure_attachments(pagename) - target, label = self.linker.translate("attachment:%s" % attachment) + # No filename is defined for inline output. + + else: + filename = None # Permit imagemaps only for image formats. @@ -126,7 +135,7 @@ # Configure Graphviz and invoke it. - graphviz = Graphviz(filter, text, identifier) + graphviz = Graphviz(filter, text) graphviz.call(format, transforms, filename) # Obtain any metadata. @@ -151,8 +160,13 @@ # For other output, create a file and embed the object. + elif not inline: + self.object(target, attributes) + + # Or for inline output, emit it in the document itself. + else: - self.object(target, attributes) + self.out(graphviz.get_inline_output()) serialiser = HTMLGraphvizSerialiser diff -r 524700c23195 -r c1284f3e6af5 moinformat/utils/graphviz.py --- a/moinformat/utils/graphviz.py Tue Nov 27 21:23:24 2018 +0100 +++ b/moinformat/utils/graphviz.py Tue Nov 27 22:48:12 2018 +0100 @@ -22,6 +22,7 @@ from os.path import exists, join from StringIO import StringIO from subprocess import Popen, PIPE +from xml.sax.saxutils import XMLGenerator import gzip import sha import xml.sax @@ -63,16 +64,9 @@ else: return s -class MetadataParser(xml.sax.handler.ContentHandler): - - "Parse metadata from the svg element." +class Parser(xml.sax.handler.ContentHandler): - def __init__(self): - self.attrs = {} - - def startElement(self, name, attrs): - if name == self.tagname: - self.attrs = dict(attrs) + "Common XML parsing functionality." def parse(self, f): @@ -87,11 +81,9 @@ finally: f.close() - def get_metadata(self, data, tagname): + def parse_data(self, data): - "Process 'data', returning attributes from 'tagname'." - - self.tagname = tagname + "Parse the given 'data'." f = StringIO(data) try: @@ -99,8 +91,32 @@ finally: f.close() +class MetadataParser(Parser): + + "Parse metadata from the svg element." + + def __init__(self): + self.attrs = {} + + def startElement(self, name, attrs): + if name == self.tagname: + self.attrs = dict(attrs) + + def get_metadata(self, data, tagname): + + "Process 'data', returning attributes from 'tagname'." + + self.tagname = tagname + self.parse_data(data) return self.attrs +class DocumentSelector(XMLGenerator, Parser): + + "Parse a document and obtain the serialisation of the document node." + + def startDocument(self): + pass + def get_output_identifier(text): "Return an output identifier for the given 'text'." @@ -179,17 +195,14 @@ "A Graphviz configuration for single or repeated invocation." - def __init__(self, filter, text, identifier): + def __init__(self, filter, text): """ - Employ the given 'filter' to produce a graph from the given 'text'. The - output 'identifier' for the text is used to provide a filename, if - required. + Employ the given 'filter' to produce a graph from the given 'text'. """ self.filter = filter self.text = text - self.identifier = identifier def call(self, format, transforms=None, filename=None): @@ -264,4 +277,20 @@ def get_output(self): return self.output + def get_inline_output(self): + + """ + Return a string containing the document element, excluding XML + boilerplate. + """ + + f = StringIO() + parser = DocumentSelector(f, "utf-8") + + try: + parser.parse_data(self.output) + return f.getvalue() + finally: + f.close() + # vim: tabstop=4 expandtab shiftwidth=4