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 format = "graphviz" 37 38 # Parser handler methods. 39 40 def parse_directive(self, region): 41 42 "Handle format directives." 43 44 key = self.match_group("key") 45 value = self.match_group("value") 46 self.add_node(region, Directive(key, value)) 47 self.new_block(region) 48 49 50 51 # Regular expressions. 52 53 syntax = { 54 # At start of line: 55 56 "directive" : join(("^//", # // 57 group("key", ".*?"), # text-excl-eq-nl 58 optional(join(("=", # eq (optional) 59 group("value", ".*?")))), # text-excl-nl (optional) 60 "\n")), # nl 61 62 "regionend" : MoinParser.syntax["regionend"], 63 } 64 65 patterns = get_patterns(syntax) 66 67 68 69 # Pattern details. 70 71 region_pattern_names = ["directive", "regionend"] 72 73 74 75 # Pattern handlers. 76 77 parse_region_end = ParserBase.parse_region_end 78 79 handlers = { 80 "directive" : parse_directive, 81 "regionend" : parse_region_end, 82 } 83 84 parser = GraphvizParser 85 86 # vim: tabstop=4 expandtab shiftwidth=4