1 #!/usr/bin/env python 2 3 """ 4 Graphviz serialiser, generating content for embedding in HTML documents. 5 6 Copyright (C) 2018, 2019 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from moinformat.serialisers.common import Serialiser, escape_attr, escape_text 23 from moinformat.utils.graphviz import Graphviz, IMAGE_FORMATS, \ 24 get_output_identifier 25 from moinformat.utils.links import LinkTarget 26 27 # Utility functions. 28 29 def select_keys(d, keys): 30 31 "Select from 'd' the given 'keys'." 32 33 if not d: 34 return {} 35 36 out = {} 37 38 for key in keys: 39 if d.has_key(key): 40 out[key] = d[key] 41 42 return out 43 44 45 46 # The serialiser class. 47 48 class HTMLGraphvizSerialiser(Serialiser): 49 50 "Serialisation of Graphviz regions." 51 52 input_formats = ["graphviz", "dot"] 53 formats = ["html"] 54 55 def init(self): 56 self.directives = {} 57 58 def start_block(self): 59 pass 60 61 def end_block(self): 62 pass 63 64 def directive(self, key, value, directive): 65 if not self.directives.has_key(key): 66 self.directives[key] = [] 67 self.directives[key].append(value) 68 69 def text(self, text): 70 self.process_graph(text) 71 72 73 74 # Special methods for graph production. 75 76 def _tag(self, tagname, attrname, target, attributes, closing): 77 l = ["%s='%s'" % (attrname, escape_attr(target))] 78 for key, value in attributes.items(): 79 l.append("%s='%s'" % (key, value)) 80 self.out("<%s %s%s>" % (tagname, " ".join(l), closing and " /" or "")) 81 82 def image(self, target, attributes): 83 self._tag("img", "src", target, attributes, True) 84 85 def object(self, target, attributes): 86 self._tag("object", "data", target, attributes, False) 87 self.out("</object>") 88 89 def raw(self, text): 90 self.out(text) 91 92 93 94 # Graph output preparation. 95 96 def process_graph(self, text): 97 98 "Process the graph 'text' using the known directives." 99 100 filter = self.directives.get("filter", ["dot"])[0] 101 format = self.directives.get("format", ["svg"])[0] 102 transforms = self.directives.get("transform", []) 103 104 # Add transforms if appropriate. 105 # NOTE: Maybe move this to the Graphviz class itself. 106 107 document_index = self.metadata.get("document_index") 108 109 if document_index and "fixlinks" not in transforms: 110 transforms.insert(0, "fixlinks") 111 112 # Determine the inline status, with only SVG being appropriate. 113 114 inline = format == "svg" and not self.metadata.get("no_inline") 115 116 # Non-inline graph output is stored for a known page only. 117 118 pagename = self.metadata.get("pagename") 119 120 if not inline: 121 if not pagename: 122 return 123 124 # Get an identifier and usable filename to store the output. 125 126 identifier = get_output_identifier(text) 127 attachment = "%s.%s" % (identifier, format) 128 filename = self.output.get_attachment_filename(pagename, attachment) 129 130 # Handle situations where no independent output is permitted. 131 132 if not filename: 133 return 134 135 # Make sure that page attachments can be stored. 136 137 self.output.ensure_attachments(pagename) 138 link = self.linker.translate(LinkTarget("attachment", attachment)) 139 140 # No filename is defined for inline output. 141 142 else: 143 filename = None 144 145 # Permit imagemaps only for image formats. 146 147 if format in IMAGE_FORMATS: 148 cmapx = self.directives.has_key("cmapx") 149 150 # Configure Graphviz and invoke it. 151 152 graphviz = Graphviz(filter, text) 153 graphviz.call(format, transforms, filename, self.metadata) 154 155 # Obtain any metadata. 156 157 attributes = select_keys(graphviz.get_metadata(), ["width", "height"]) 158 159 # For image output, create a file directly and reference it. 160 161 if format in IMAGE_FORMATS: 162 163 # Produce, embed and reference an imagemap if requested. 164 165 if cmapx: 166 graphviz.call("cmapx") 167 mapid = graphviz.get_metadata().get("id") 168 169 if mapid: 170 self.raw(graphviz.get_output()) 171 attributes["usemap"] = "#%s" % im_attributes["id"] 172 173 self.image(link.get_target(), attributes) 174 175 # For other output, create a file and embed the object. 176 177 elif not inline: 178 self.object(link.get_target(), attributes) 179 180 # Or for inline output, emit it in the document itself. 181 182 else: 183 self.out(graphviz.get_inline_output()) 184 185 serialiser = HTMLGraphvizSerialiser 186 187 # vim: tabstop=4 expandtab shiftwidth=4