clone

a browser-first diagram editor with live svg previews, local drafts and encrypted, accountless sharing

main
diagramzip / renderers / python / blockdiag-family / src / blockdiag / utils / rst / nodes.py
nodes.py 1.9K
1# -*- coding: utf-8 -*-2# Copyright 2011 Takeshi KOMIYA3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.1516import os17from hashlib import sha11819from docutils import nodes2021import blockdiag.builder22import blockdiag.drawer23import blockdiag.parser242526class blockdiag(nodes.General, nodes.Element):27 name = 'blockdiag'28 processor = blockdiag2930 def to_diagram(self):31 try:32 tree = self.processor.parser.parse_string(self['code'])33 except Exception:34 code = '%s { %s }' % (self.name, self['code'])35 tree = self.processor.parser.parse_string(code)36 self['code'] = code # replace if succeeded3738 return self.processor.builder.ScreenNodeBuilder.build(tree)3940 def to_drawer(self, image_format, filename, fontmap, **kwargs):41 diagram = self.to_diagram()42 return self.processor.drawer.DiagramDraw(image_format, diagram,43 filename, fontmap=fontmap,44 **kwargs)4546 def get_path(self, **options):47 options.update(self['options'])48 hashseed = (self['code'] + str(options)).encode('utf-8')49 hashed = sha1(hashseed).hexdigest()5051 filename = "%s-%s.%s" % (self.name, hashed, options['format'].lower())52 outputdir = options.get('outputdir')53 if outputdir:54 filename = os.path.join(outputdir, filename)5556 return filename