1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/moinformat/parsers/graphviz.py Sun Jul 29 23:28:13 2018 +0200
1.3 @@ -0,0 +1,84 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Graphviz region metadata parser. This only identifies metadata, with the actual
1.8 +graph data being interpreted by Graphviz itself.
1.9 +
1.10 +Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk>
1.11 +
1.12 +This program is free software; you can redistribute it and/or modify it under
1.13 +the terms of the GNU General Public License as published by the Free Software
1.14 +Foundation; either version 3 of the License, or (at your option) any later
1.15 +version.
1.16 +
1.17 +This program is distributed in the hope that it will be useful, but WITHOUT
1.18 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.19 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.20 +details.
1.21 +
1.22 +You should have received a copy of the GNU General Public License along with
1.23 +this program. If not, see <http://www.gnu.org/licenses/>.
1.24 +"""
1.25 +
1.26 +from moinformat.parsers.common import ParserBase, get_patterns, group, optional
1.27 +from moinformat.parsers.moin import MoinParser
1.28 +from moinformat.tree.graphviz import Directive
1.29 +from moinformat.tree.moin import Text
1.30 +
1.31 +join = "".join
1.32 +
1.33 +# Parser functionality.
1.34 +
1.35 +class GraphvizParser(ParserBase):
1.36 +
1.37 + "A parser for Graphviz content, identifying format directives."
1.38 +
1.39 + # Parser handler methods.
1.40 +
1.41 + def parse_directive(self, region):
1.42 +
1.43 + "Handle format directives."
1.44 +
1.45 + key = self.match_group("key")
1.46 + value = self.match_group("value")
1.47 + self.add_node(region, Directive(key, value))
1.48 + self.new_block(region)
1.49 +
1.50 +
1.51 +
1.52 + # Regular expressions.
1.53 +
1.54 + syntax = {
1.55 + # At start of line:
1.56 +
1.57 + "directive" : join(("^//", # //
1.58 + group("key", ".*?"), # text-excl-eq-nl
1.59 + optional(join(("=", # eq (optional)
1.60 + group("value", ".*?")))), # text-excl-nl (optional)
1.61 + "\n")), # nl
1.62 +
1.63 + "regionend" : MoinParser.syntax["regionend"],
1.64 + }
1.65 +
1.66 + patterns = get_patterns(syntax)
1.67 +
1.68 +
1.69 +
1.70 + # Pattern details.
1.71 +
1.72 + region_pattern_names = ["directive", "regionend"]
1.73 +
1.74 +
1.75 +
1.76 + # Pattern handlers.
1.77 +
1.78 + parse_region_end = ParserBase.parse_region_end
1.79 +
1.80 + handlers = {
1.81 + "directive" : parse_directive,
1.82 + "regionend" : parse_region_end,
1.83 + }
1.84 +
1.85 +parser = GraphvizParser
1.86 +
1.87 +# vim: tabstop=4 expandtab shiftwidth=4