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 t = directive.split(None, 1) 49 key = t[0] 50 value = len(t) > 1 and t[1] or None 51 else: 52 key = self.match_group("key") 53 value = self.match_group("value") 54 55 self.add_node(region, Directive(key, value)) 56 self.new_block(region) 57 58 59 60 # Regular expressions. 61 62 syntax = { 63 # At start of line: 64 65 "directive" : choice((join((r"^#", # # 66 group("directive", r".*?$"), # rest of line 67 optional(group("extra", r"\n")))), # nl (optional) 68 69 # Legacy GraphvizParser directive syntax: 70 71 join(("^//", # // 72 group("key", ".*?"), # text-excl-eq-nl 73 optional(join(("=", # eq (optional) 74 group("value", ".*?")))), # text-excl-nl (optional) 75 "\n")))), # nl 76 77 "regionend" : MoinParser.syntax["regionend"], 78 } 79 80 patterns = get_patterns(syntax) 81 82 83 84 # Pattern details. 85 86 region_pattern_names = ["directive", "regionend"] 87 88 89 90 # Pattern handlers. 91 92 parse_region_end = ParserBase.parse_region_end 93 94 handlers = { 95 "directive" : parse_directive, 96 "regionend" : parse_region_end, 97 } 98 99 parser = GraphvizParser 100 101 # vim: tabstop=4 expandtab shiftwidth=4