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 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(body.encode('utf8')) tf.flush() imgext = ".png" # make a name name = tf.name + imgext output_path = os.path.join(path, os.path.basename(name)) alt = self.options.get('alt', 'ditaa diagram') classes = self.options.pop('class', ['ditaa']) cmdline = ['ditaa', '-v', '-o', tf.name, output_path] 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 ditaa: %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 generate_thumbnails(pelican): global PENDING_THUMBNAILS settings = pelican.settings path_prefix = os.path.join(settings['PATH'], settings['ALBUM_PATH']) output_prefix = os.path.join(settings['OUTPUT_PATH'], settings['THUMBNAIL_OUTPUT_PATH']) if settings['THUMBNAIL_OUTPUT_FORMAT'] == 'PNG': ext = 'png' else: ext = 'jpg' for image, w, h, quality in PENDING_THUMBNAILS: src = os.path.join(path_prefix, image) src_stat = os.stat(src) path, filename = os.path.split(image) filename = os.path.splitext(filename)[0] destdir = os.path.join(output_prefix, path, '%dx%d@%d' % (w, h, quality)) if not os.path.isdir(destdir): os.makedirs(destdir) dest = os.path.join(destdir, '%s.%s' % (filename, ext)) if os.path.isfile(dest): dest_stat = os.stat(dest) if dest_stat.st_mtime == src_stat.st_mtime: logger.debug('Not generating %s' % dest) continue logger.info('Generating %s' % dest) im = Image.open(src) im_w, im_h = im.size if not w: w = int(im_w * (float(h) / im_h)) im.thumbnail((w, h)) elif not h: h = int(im_h * (float(w) / im_w)) im.thumbnail((w, h)) else: im = ImageOps.fit(im, (w, h), Image.ANTIALIAS) if ext == 'png': im.save(dest, 'PNG', optimize=True) else: im.save(dest, 'JPEG', quality=quality) os.utime(dest, (src_stat.st_atime, src_stat.st_mtime)) PENDING_THUMBNAILS = set()
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 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 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 generate_people_page(generator): current = defaultdict(dict) alumni = defaultdict(dict) i = 0 logger.debug("Gathering persons for people page ...") for page in generator.pages: if page.metadata.get("template") == "person": i += 1 if page.alumni_or_current.lower() == "alumni": alumni[page.position][page.title] = page else: current[page.position][page.title] = page # logger.debug('\tPage {}: {}'.format(page.title, dir(page))) # logger.debug('\tPage {}: {}'.format(page.title, repr(page.__dict__))) logger.debug("\t Done. Found {} person pages.".format(i)) logger.debug("Adding persons to people page ...") for page in generator.pages: # logger.debug('Iterating over pages again ... {}'.format(page.title)) if page.metadata.get("template") == "people": page.persons = current if page.metadata.get("template") == "alumni": page.persons = alumni logger.debug("\t Done.")
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
def init_headinglower(sender): logger.debug('Init Headinglower Plugin')
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) if self.options.get('libs', False): libs = '\n'.join( ('\\usetikzlibrary{%s}' % x) for x in self.options.get('libs', False).split(',')) else: libs = '' tf = tempfile.NamedTemporaryFile(delete=False) tf.write(( '\\documentclass{standalone}\n\\usepackage{xeCJK,fontspec,xunicode}\\usepackage{tikz}\setCJKmainfont{Noto Sans CJK TC}\n%s\n\\begin{document}\\begin{tikzpicture}\n' % libs).encode('utf-8')) tf.write(body.encode('utf8')) tf.write('\n\\end{tikzpicture}\\end{document}'.encode('utf-8')) tf.flush() imgformat = self.options.get('format', 'svg') imgext = ".svg" # make a name name = tf.name + imgext output_path = os.path.join(path, os.path.basename(name)) alt = self.options.get('alt', 'tikz diagram') classes = self.options.pop('class', ['tikz']) cmdline = ['tikz2svg', tf.name, output_path] logger.debug("running: " + ' '.join(cmdline)) try: p = Popen(cmdline, stdout=PIPE, stderr=PIPE) out, err = p.communicate() logger.debug("tikz2svg out: " + out.decode('utf-8')) logger.debug("tikz2svg err: " + err.decode('utf-8')) except Exception as exc: error = self.state_machine.reporter.error( 'Failed to run tikz: %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 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(output_path, 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
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) & 0xffffffff))+imgext try: # for Windows os.remove(newname) except Exception, exc: logger.debug('File '+newname+' does not exist, not deleted') os.rename(name, newname) url = global_siteurl + '/images/' + 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 custom_url(generator, metadata):
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(body.encode('utf8')) tf.flush() imgext = ".png" # make a name name = tf.name + imgext output_path = os.path.join(path, os.path.basename(name)) alt = self.options.get('alt', 'ditaa diagram') classes = self.options.pop('class', ['ditaa']) cmdline = ['ditaa', '-v', '-o', tf.name, output_path] 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 ditaa: %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 init_rmwidont(sender): logger.debug('Init rmwidont Plugin')
'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) & 0xffffffff)) + imgext try: # for Windows os.remove(newname) except Exception, exc: logger.debug('File ' + newname + ' does not exist, not deleted') os.rename(name, newname) url = global_siteurl + '/images/' + 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): 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 init_mod_typogrify(sender): logger.debug('Init mod_typogrify Plugin')