# HG changeset patch # User Paul Boddie # Date 1543355443 -3600 # Node ID b4757958122bb9f6b5e49b96cdfa50dd90e3346a # Parent f67f4d353edcc7a8e31f42b714db176df6b7c1f3# Parent c1284f3e6af52acc94edb87d9f31ebafb8f663f8 Merged changes from the default branch. diff -r f67f4d353edc -r b4757958122b moinconvert --- a/moinconvert Mon Nov 26 22:59:52 2018 +0100 +++ b/moinconvert Tue Nov 27 22:50:43 2018 +0100 @@ -78,6 +78,10 @@ dirname, progname = split(sys.argv[0]) args = sys.argv[1:] + if "--help" in args: + show_help(progname) + sys.exit(0) + # Option values. l = filenames = [] @@ -307,6 +311,53 @@ if theme: theme.install_resources() +def show_help(progname): + + "Show the help text." + + print >>sys.stderr, help_text % progname + +help_text = """\ +Usage: %s [ ] ( --all | ... ) + +Input options: + +--all Detect all document files in the specified input directory +--input-dir Indicate an input directory containing document files +--input-dir-type Indicate the type of input directory involved + (default: directory) +--input-encoding Indicate the character encoding used in document files +--input-page-sep Indicate the separator used in filenames to encode + hierarchical relationships (subpages and descendant pages) +--pagename Indicate the page name corresponding to an indicated + filename, with each successive instance of this option + corresponding to each successive filename instance + +Output options: + +--format Indicate the format to be used for serialised documents + (default: html) +--fragment Indicates that an output fragment, not an entire document, + is to be generated, skipping any theming activities +--output-dir Indicate an output directory to contain serialised document + files +--output-encoding Indicate the character encoding used in serialised document + files +--theme Indicate a theme for serialised documents, typically + requiring an output directory to be useful +--tree Produce a document tree representation on standard output + instead of generating output files + +Configuration options: + +--macros Perform macro evaluation/expansion before serialising + documents +--mapping Indicate a name and corresponding URL to be used to + translate interwiki links +--root Indicate the root page name to be used + (default: FrontPage) +""" + if __name__ == "__main__": main() diff -r f67f4d353edc -r b4757958122b moinformat/input/directory.py --- a/moinformat/input/directory.py Mon Nov 26 22:59:52 2018 +0100 +++ b/moinformat/input/directory.py Tue Nov 27 22:50:43 2018 +0100 @@ -25,7 +25,7 @@ class DirectoryInput(Input): - "A directory output context." + "A directory input context." name = "directory" diff -r f67f4d353edc -r b4757958122b moinformat/parsers/common.py --- a/moinformat/parsers/common.py Mon Nov 26 22:59:52 2018 +0100 +++ b/moinformat/parsers/common.py Tue Nov 27 22:50:43 2018 +0100 @@ -356,6 +356,11 @@ self.set_region(items, region) + # Only parse directives if the region is transparent. + + if region.transparent: + self.parse_region_directives(region) + # Parse inline and opaque regions. if not region.transparent: @@ -410,11 +415,6 @@ region.transparent = False parser = parser or self.get_parser("moin") - # Only parse directives if the region is transparent. - - if region.transparent: - self.parse_region_directives(region) - parser.parse_region_content(self.items, region) def parse_region_header(self, region): diff -r f67f4d353edc -r b4757958122b moinformat/parsers/graphviz.py --- a/moinformat/parsers/graphviz.py Mon Nov 26 22:59:52 2018 +0100 +++ b/moinformat/parsers/graphviz.py Tue Nov 27 22:50:43 2018 +0100 @@ -20,7 +20,8 @@ this program. If not, see . """ -from moinformat.parsers.common import ParserBase, get_patterns, group, optional +from moinformat.parsers.common import ParserBase, choice, get_patterns, group, \ + optional from moinformat.parsers.moin import MoinParser from moinformat.tree.graphviz import Directive from moinformat.tree.moin import Text @@ -41,9 +42,11 @@ "Handle format directives." + directive = self.match_group("directive") key = self.match_group("key") value = self.match_group("value") - self.add_node(region, Directive(key, value)) + + self.add_node(region, Directive(key, value, directive)) self.new_block(region) @@ -53,11 +56,17 @@ syntax = { # At start of line: - "directive" : join(("^//", # // - group("key", ".*?"), # text-excl-eq-nl - optional(join(("=", # eq (optional) - group("value", ".*?")))), # text-excl-nl (optional) - "\n")), # nl + "directive" : choice((join((r"^#", # # + group("directive", r".*?$"), # rest of line + optional(group("extra", r"\n")))), # nl (optional) + + # Legacy GraphvizParser directive syntax: + + join(("^//", # // + group("key", ".*?"), # text-excl-eq-nl + optional(join(("=", # eq (optional) + group("value", ".*?")))), # text-excl-nl (optional) + "\n")))), # nl "regionend" : MoinParser.syntax["regionend"], } diff -r f67f4d353edc -r b4757958122b moinformat/serialisers/html/graphviz.py --- a/moinformat/serialisers/html/graphviz.py Mon Nov 26 22:59:52 2018 +0100 +++ b/moinformat/serialisers/html/graphviz.py Tue Nov 27 22:50:43 2018 +0100 @@ -57,7 +57,7 @@ def end_block(self): pass - def directive(self, key, value): + def directive(self, key, value, directive): if not self.directives.has_key(key): self.directives[key] = [] self.directives[key].append(value) @@ -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 f67f4d353edc -r b4757958122b moinformat/serialisers/moin/graphviz.py --- a/moinformat/serialisers/moin/graphviz.py Mon Nov 26 22:59:52 2018 +0100 +++ b/moinformat/serialisers/moin/graphviz.py Tue Nov 27 22:50:43 2018 +0100 @@ -31,8 +31,11 @@ def end_block(self): pass - def directive(self, key, value): - self.out("//%s%s\n" % (value and "%s=" % key or key, value or "")) + def directive(self, key, value, directive): + if directive: + self.out("#%s\n" % directive) + else: + self.out("//%s%s\n" % (value and "%s=" % key or key, value or "")) def text(self, text): self.out(text) diff -r f67f4d353edc -r b4757958122b moinformat/tree/graphviz.py --- a/moinformat/tree/graphviz.py Mon Nov 26 22:59:52 2018 +0100 +++ b/moinformat/tree/graphviz.py Tue Nov 27 22:50:43 2018 +0100 @@ -25,17 +25,24 @@ "Format directive for Graphviz output." - def __init__(self, key=None, value=None): - self.key = key - self.value = value + def __init__(self, key=None, value=None, directive=None): + self.directive = directive + + if key or value: + self.key = key + self.value = value + else: + t = directive.split(None, 1) + self.key = t[0] + self.value = len(t) > 1 and t[1] or None def __repr__(self): - return "Directive(%r, %r)" % (self.key, self.value) + return "Directive(%r, %r, %r)" % (self.key, self.value, self.directive) def prettyprint(self, indent=""): - return "%sDirective: key=%r value=%r" % (indent, self.key, self.value) + return "%sDirective: key=%r value=%r directive=%r" % (indent, self.key, self.value, self.directive) def to_string(self, out): - out.directive(self.key, self.value) + out.directive(self.key, self.value, self.directive) # vim: tabstop=4 expandtab shiftwidth=4 diff -r f67f4d353edc -r b4757958122b moinformat/utils/graphviz.py --- a/moinformat/utils/graphviz.py Mon Nov 26 22:59:52 2018 +0100 +++ b/moinformat/utils/graphviz.py Tue Nov 27 22:50:43 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 diff -r f67f4d353edc -r b4757958122b tests/test_regions_opaque.txt --- a/tests/test_regions_opaque.txt Mon Nov 26 22:59:52 2018 +0100 +++ b/tests/test_regions_opaque.txt Tue Nov 27 22:50:43 2018 +0100 @@ -1,5 +1,6 @@ Hello {{{{#!xxx +# Not a directive A region {{{ Another