def pelican_init(pelicanobj): global global_siteurl global_siteurl = pelicanobj.settings["SITEURL"] """ Prepare configurations for the MD plugin """ try: import markdown from .plantuml_md import PlantUMLMarkdownExtension except: # Markdown not available logger.debug("[plantuml] Markdown support not available") return # Register the Markdown plugin config = {"siteurl": pelicanobj.settings["SITEURL"]} try: if "MD_EXTENSIONS" in pelicanobj.settings.keys(): # pre pelican 3.7.0 pelicanobj.settings["MD_EXTENSIONS"].append( PlantUMLMarkdownExtension(config) ) elif "MARKDOWN" in pelicanobj.settings.keys() and not ( "extension_configs" in pelicanobj.settings["MARKDOWN"]["extension_configs"] ): # from pelican 3.7.0 pelicanobj.settings["MARKDOWN"]["extension_configs"][ "plantuml.plantuml_md" ] = {} except: logger.error("[plantuml] Unable to configure plantuml markdown extension")
def pelican_init(pelicanobj): global global_siteurl global_siteurl = pelicanobj.settings['SITEURL'] """ Prepare configurations for the MD plugin """ try: import markdown from .plantuml_md import PlantUMLMarkdownExtension except: # Markdown not available logger.debug("[plantuml] Markdown support not available") return # Register the Markdown plugin config = { 'siteurl': pelicanobj.settings['SITEURL'] } try: if 'MD_EXTENSIONS' in pelicanobj.settings.keys(): # pre pelican 3.7.0 pelicanobj.settings['MD_EXTENSIONS'].append(PlantUMLMarkdownExtension(config)) elif 'MARKDOWN' in pelicanobj.settings.keys() and \ not ('extension_configs' in pelicanobj.settings['MARKDOWN']['extension_configs']): # from pelican 3.7.0 pelicanobj.settings['MARKDOWN']['extension_configs']['plantuml.plantuml_md'] = {} except: logger.error("[plantuml] Unable to configure plantuml markdown extension")
def find_albums(self, path=(), parent=None): album_path = os.path.join(self.path, self.settings['ALBUM_PATH'], *path) location = '/'.join(path) album = AlbumContent(location, self.settings) if parent: parent.albums.append(album) # Images don't have titles, use the basename instead. image_settings = dict(self.settings, SLUGIFY_SOURCE='basename') for filename in os.listdir(album_path): f = os.path.join(album_path, filename) if os.path.isdir(f): self.find_albums(path + (filename, ), album) else: try: PILImage.open(f) image = album.add_image(filename) page = Image('', settings=image_settings, source_path=filename) page.image = image image.page = page self.add_source_path(page) self.image_pages.append(page) except IOError: try: page = self.readers.read_file( base_path=self.path, path=f, content_class=Album, context=self.context, ) self.add_source_path(page) except Exception as e: logger.error('Could not process %s\n%s', f, e, exc_info=self.settings.get( 'DEBUG', False)) self._add_failed_source_path(f) continue if not is_valid_content(page, f): self._add_failed_source_path(f) continue page.album = album self.album_pages.append(page) album.pages.append(page)
def generate_uml_image(path, plantuml_code, imgformat): tf = tempfile.NamedTemporaryFile(delete=False) tf.write("@startuml\n".encode("utf8")) tf.write(plantuml_code.encode("utf8")) tf.write("\n@enduml".encode("utf8")) tf.flush() logger.debug("[plantuml] Temporary PlantUML source at " + (tf.name)) if imgformat == "png": imgext = ".png" outopt = "-tpng" elif imgformat == "svg": imgext = ".svg" outopt = "-tsvg" else: logger.error("Bad uml image format '" + imgformat + "', using png") imgext = ".png" outopt = "-tpng" # make a name name = tf.name + imgext # build cmd line cmdline = ["plantuml", "-o", path, outopt, tf.name] try: logger.debug("[plantuml] About to execute " + " ".join(cmdline)) p = Popen(cmdline, stdout=PIPE, stderr=PIPE) out, err = p.communicate() except Exception as exc: raise Exception("Failed to run plantuml: %s" % exc) else: if p.returncode == 0: # diagram was correctly generated, we can remove the temporary file (if not debugging) if not logger.isEnabledFor(logging.DEBUG): os.remove(tf.name) # renaming output image using an hash code, just to not pollute # output directory with a growing number of images name = os.path.join(path, os.path.basename(name)) newname = (os.path.join( path, "%08x" % (adler32(plantuml_code.encode()) & 0xFFFFFFFF)) + imgext) if os.path.exists(newname): os.remove(newname) os.rename(name, newname) return os.path.basename(newname) else: # the temporary file is still available as aid understanding errors raise RuntimeError("Error calling plantuml: %s" % err)
def generate_uml_image(path, plantuml_code, imgformat): tf = tempfile.NamedTemporaryFile(delete=False) tf.write('@startuml\n'.encode('utf8')) tf.write(plantuml_code.encode('utf8')) tf.write('\n@enduml'.encode('utf8')) tf.flush() logger.debug("[plantuml] Temporary PlantUML source at "+(tf.name)) if imgformat == 'png': imgext = ".png" outopt = "-tpng" elif imgformat == 'svg': imgext = ".svg" outopt = "-tsvg" else: logger.error("Bad uml image format '"+imgformat+"', using png") imgext = ".png" outopt = "-tpng" # make a name name = tf.name+imgext # build cmd line cmdline = ['plantuml', '-o', path, outopt, tf.name] try: logger.debug("[plantuml] About to execute "+" ".join(cmdline)) p = Popen(cmdline, stdout=PIPE, stderr=PIPE) out, err = p.communicate() except Exception as exc: raise Exception('Failed to run plantuml: %s' % exc) else: if p.returncode == 0: # diagram was correctly generated, we can remove the temporary file (if not debugging) if not logger.isEnabledFor(logging.DEBUG): os.remove(tf.name) # renaming output image using an hash code, just to not pollute # output directory with a growing number of images name = os.path.join(path, os.path.basename(name)) newname = os.path.join(path, "%08x" % (adler32(plantuml_code.encode()) & 0xffffffff))+imgext if os.path.exists(newname): os.remove(newname) os.rename(name, newname) return 'images/' + os.path.basename(newname) else: # the temporary file is still available as aid understanding errors raise RuntimeError('Error calling plantuml: %s' % err)
def find_albums(self, path=(), parent=None): album_path = os.path.join(self.path, self.settings['ALBUM_PATH'], *path) location = '/'.join(path) album = AlbumContent(location, self.settings) if parent: parent.albums.append(album) # Images don't have titles, use the basename instead. image_settings = dict(self.settings, SLUGIFY_SOURCE='basename') for filename in os.listdir(album_path): f = os.path.join(album_path, filename) if os.path.isdir(f): self.find_albums(path + (filename,), album) else: try: PILImage.open(f) image = album.add_image(filename) page = Image('', settings=image_settings, source_path=filename) page.image = image image.page = page self.add_source_path(page) self.image_pages.append(page) except IOError: try: page = self.readers.read_file( base_path=self.path, path=f, content_class=Album, context=self.context, ) self.add_source_path(page) except Exception as e: logger.error('Could not process %s\n%s', f, e, exc_info=self.settings.get('DEBUG', False)) self._add_failed_source_path(f) continue if not is_valid_content(page, f): self._add_failed_source_path(f) continue page.album = album self.album_pages.append(page) album.pages.append(page)
def pelican_init(pelicanobj): """ Prepare configurations for the MD plugin """ try: import markdown from plantuml_md import PlantUMLMarkdownExtension except: # Markdown not available logger.debug("[plantuml] Markdown support not available") return # Register the Markdown plugin config = { 'siteurl': pelicanobj.settings['SITEURL'] } try: pelicanobj.settings['MD_EXTENSIONS'].append(PlantUMLMarkdownExtension(config)) except: logger.error("[plantuml] Unable to configure plantuml markdown extension")
def run(self): source = self.state_machine.input_lines.source( self.lineno - self.state_machine.input_offset - 1) source_dir = os.path.dirname(os.path.abspath(source)) source_dir = utils.relative_path(None, source_dir) path = os.path.abspath(os.path.join('output', 'images')) if not os.path.exists(path): os.makedirs(path) nodes = [] body = '\n'.join(self.content) tf = tempfile.NamedTemporaryFile(delete=True) tf.write('@startuml\n') tf.write(body.encode('utf8')) tf.write('\n@enduml') tf.flush() imgformat = self.options.get('format', 'png') if imgformat == 'png': imgext = ".png" outopt = "-tpng" elif imgformat == 'svg': imgext = ".svg" outopt = "-tsvg" else: logger.error("Bad uml image format: " + imgformat) # make a name name = tf.name + imgext alt = self.options.get('alt', 'uml diagram') classes = self.options.pop('class', ['uml']) cmdline = ['plantuml', '-o', path, outopt, tf.name] try: p = Popen(cmdline, stdout=PIPE, stderr=PIPE) out, err = p.communicate() except Exception, exc: error = self.state_machine.reporter.error( 'Failed to run plantuml: %s' % (exc, ), literal_block(self.block_text, self.block_text), line=self.lineno) nodes.append(error)
def run(self): source = self.state_machine.input_lines.source(self.lineno - self.state_machine.input_offset - 1) source_dir = os.path.dirname(os.path.abspath(source)) source_dir = utils.relative_path(None, source_dir) path = os.path.abspath(os.path.join('output', 'images')) if not os.path.exists(path): os.makedirs(path) nodes = [] body = '\n'.join(self.content) tf = tempfile.NamedTemporaryFile(delete=True) tf.write('@startuml\n') tf.write(body.encode('utf8')) tf.write('\n@enduml') tf.flush() imgformat = self.options.get('format', 'png') if imgformat == 'png': imgext = ".png" outopt = "-tpng" elif imgformat == 'svg': imgext = ".svg" outopt = "-tsvg" else: logger.error("Bad uml image format: "+imgformat) # make a name name = tf.name+imgext alt = self.options.get('alt', 'uml diagram') classes = self.options.pop('class', ['uml']) cmdline = ['plantuml', '-o', path, outopt, tf.name ] try: p = Popen(cmdline, stdout=PIPE, stderr=PIPE) out, err = p.communicate() except Exception, exc: error = self.state_machine.reporter.error( 'Failed to run plantuml: %s' % (exc, ), literal_block(self.block_text, self.block_text), line=self.lineno) nodes.append(error)
def pelican_init(pelicanobj): """ Prepare configurations for the MD plugin """ try: import markdown from plantuml_md import PlantUMLMarkdownExtension except: # Markdown not available logger.debug("[plantuml] Markdown support not available") return # Register the Markdown plugin config = {'siteurl': pelicanobj.settings['SITEURL']} try: pelicanobj.settings['MD_EXTENSIONS'].append( PlantUMLMarkdownExtension(config)) except: logger.error( "[plantuml] Unable to configure plantuml markdown extension")
def run(self): source = self.state_machine.input_lines.source(self.lineno - self.state_machine.input_offset - 1) source_dir = os.path.dirname(os.path.abspath(source)) source_dir = utils.relative_path(None, source_dir) path = os.path.abspath(os.path.join('content', 'uml')) if not os.path.exists(path): os.makedirs(path) nodes = [] body = '\n'.join(self.content) tf = tempfile.NamedTemporaryFile(delete=True) tf.write('@startuml\n'.encode('utf-8')) tf.write(body.encode('utf8')) tf.write('\n@enduml'.encode('utf-8')) tf.flush() imgformat = self.options.get('format', 'png') if imgformat == 'png': imgext = ".png" outopt = "-tpng" elif imgformat == 'svg': imgext = ".svg" outopt = "-tsvg" else: logger.error("Bad uml image format: " + imgformat) # make a name name = tf.name + imgext alt = self.options.get('alt', 'uml diagram') classes = self.options.pop('class', ['uml']) cmdline = ['plantuml', '-o', path, outopt, tf.name] try: p = Popen(cmdline, stdout=PIPE, stderr=PIPE) out, err = p.communicate() except Exception as exc: error = self.state_machine.reporter.error( 'Failed to run plantuml: %s' % (exc, ), literal_block(self.block_text, self.block_text), line=self.lineno) nodes.append(error) else: if p.returncode == 0: # renaming output image using an hash code, just to not pullate # output directory with a growing number of images name = os.path.join(path, os.path.basename(name)) newname = os.path.join(path, "%08x" % (adler32(body.encode('utf8')) & 0xffffffff))+imgext try: # for Windows os.remove(newname) except Exception as exc: logger.debug('File '+newname+' does not exist, not deleted') os.rename(name, newname) url = global_siteurl + '/uml/' + os.path.basename(newname) imgnode = image(uri=url, classes=classes, alt=alt) nodes.append(imgnode) else: error = self.state_machine.reporter.error( 'Error in "%s" directive: %s' % (self.name, err), literal_block(self.block_text, self.block_text), line=self.lineno) nodes.append(error) return nodes
def run(self): path = os.path.abspath(os.path.join('content', 'uml')) if not os.path.exists(path): os.makedirs(path) nodes = [] body = '\n'.join(self.content) tf = tempfile.NamedTemporaryFile(delete=True) tf.write('@startuml\n'.encode('utf-8')) tf.write(body.encode('utf8')) tf.write('\n@enduml'.encode('utf-8')) tf.flush() imgformat = self.options.get('format', 'png') if imgformat == 'png': imgext = ".png" outopt = "-tpng" elif imgformat == 'svg': imgext = ".svg" outopt = "-tsvg" else: logger.error("Bad uml image format: " + imgformat) # make a name name = tf.name + imgext alt = self.options.get('alt', 'uml diagram') classes = self.options.pop('class', ['uml']) cmdline = ['plantuml', '-o', path, outopt, tf.name] try: p = Popen(cmdline, stdout=PIPE, stderr=PIPE) out, err = p.communicate() except Exception as exc: error = self.state_machine.reporter.error( 'Failed to run plantuml: %s' % exc, literal_block(self.block_text, self.block_text), line=self.lineno) nodes.append(error) else: if p.returncode == 0: # renaming output image using an hash code, just to not pullate # output directory with a growing number of images name = os.path.join(path, os.path.basename(name)) newname = os.path.join( path, "%08x" % (adler32(body.encode('utf8')) & 0xffffffff)) + imgext try: # for Windows os.remove(newname) except Exception as exc: logger.debug('File ' + newname + ' does not exist, not deleted') os.rename(name, newname) url = global_siteurl + '/uml/' + os.path.basename(newname) imgnode = image(uri=url, classes=classes, alt=alt) nodes.append(imgnode) else: error = self.state_machine.reporter.error( 'Error in "%s" directive: %s' % (self.name, err), literal_block(self.block_text, self.block_text), line=self.lineno) nodes.append(error) return nodes