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, choice, get_patterns, group, \ 24 optional 25 from moinformat.parsers.moin import MoinParser 26 from moinformat.tree.graphviz import Directive 27 from moinformat.tree.moin import Text 28 29 join = "".join 30 31 # Parser functionality. 32 33 class GraphvizParser(ParserBase): 34 35 "A parser for Graphviz content, identifying format directives." 36 37 format = "graphviz" 38 39 # Parser handler methods. 40 41 def parse_directive(self, region): 42 43 "Handle format directives." 44 45 directive = self.match_group("directive") 46 47 if directive: 48 key, value = directive.split(None, 1) 49 else: 50 key = self.match_group("key") 51 value = self.match_group("value") 52 53 self.add_node(region, Directive(key, value)) 54 self.new_block(region) 55 56 57 58 # Regular expressions. 59 60 syntax = { 61 # At start of line: 62 63 "directive" : choice((join((r"^#", # # 64 group("directive", r".*?$"), # rest of line 65 optional(group("extra", r"\n")))), # nl (optional) 66 67 # Legacy GraphvizParser directive syntax: 68 69 join(("^//", # // 70 group("key", ".*?"), # text-excl-eq-nl 71 optional(join(("=", # eq (optional) 72 group("value", ".*?")))), # text-excl-nl (optional) 73 "\n")))), # nl 74 75 "regionend" : MoinParser.syntax["regionend"], 76 } 77 78 patterns = get_patterns(syntax) 79 80 81 82 # Pattern details. 83 84 region_pattern_names = ["directive", "regionend"] 85 86 87 88 # Pattern handlers. 89 90 parse_region_end = ParserBase.parse_region_end 91 92 handlers = { 93 "directive" : parse_directive, 94 "regionend" : parse_region_end, 95 } 96 97 parser = GraphvizParser 98 99 # vim: tabstop=4 expandtab shiftwidth=4