1 #!/usr/bin/env python 2 3 """ 4 Graphviz region metadata parser. This only identifies metadata, with the actual 5 graph data being interpreted by Graphviz itself. 6 7 Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> 8 9 This program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation; either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 details. 18 19 You should have received a copy of the GNU General Public License along with 20 this program. If not, see <http://www.gnu.org/licenses/>. 21 """ 22 23 from moinformat.parsers.common import ParserBase, get_patterns, group, optional 24 from moinformat.parsers.moin import MoinParser 25 from moinformat.tree.graphviz import Directive 26 from moinformat.tree.moin import Text 27 28 join = "".join 29 30 # Parser functionality. 31 32 class GraphvizParser(ParserBase): 33 34 "A parser for Graphviz content, identifying format directives." 35 36 # Parser handler methods. 37 38 def parse_directive(self, region): 39 40 "Handle format directives." 41 42 key = self.match_group("key") 43 value = self.match_group("value") 44 self.add_node(region, Directive(key, value)) 45 self.new_block(region) 46 47 48 49 # Regular expressions. 50 51 syntax = { 52 # At start of line: 53 54 "directive" : join(("^//", # // 55 group("key", ".*?"), # text-excl-eq-nl 56 optional(join(("=", # eq (optional) 57 group("value", ".*?")))), # text-excl-nl (optional) 58 "\n")), # nl 59 60 "regionend" : MoinParser.syntax["regionend"], 61 } 62 63 patterns = get_patterns(syntax) 64 65 66 67 # Pattern details. 68 69 region_pattern_names = ["directive", "regionend"] 70 71 72 73 # Pattern handlers. 74 75 parse_region_end = ParserBase.parse_region_end 76 77 handlers = { 78 "directive" : parse_directive, 79 "regionend" : parse_region_end, 80 } 81 82 parser = GraphvizParser 83 84 # vim: tabstop=4 expandtab shiftwidth=4