def process(value, format): [[ident, classes, keyvals], code] = value if "graphviz" in classes: caption, typef, keyvals = get_caption(keyvals) filetype = get_extension(format, "png", html="png", latex="pdf") dest = get_filename4code("graphviz", code, filetype) if not os.path.isfile(dest): import pygraphviz g = pygraphviz.AGraph(string=code) g.layout() g.draw(dest) print1('INFO', 'Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])]) elif "standalone" in classes: caption, typef, keyvals = get_caption(keyvals) filetype = get_extension(format, "png", html="png", latex="pdf") dest = get_filename4code("standalone", code, filetype) if not os.path.isfile(dest): success = gen_standalone(code, dest) if not success: return Para( [Str(">>> Error: This image could not be generated.")]) return Para([Image([ident, [], keyvals], caption, [dest, typef])]) elif "include" in classes: caption, typef, keyvals = get_caption(keyvals) keyvalDict = dict(keyvals) listing = '' with open(keyvalDict['src'], 'r') as f: listing = f.read() lang = keyvalDict.get('lang', 'python') code = 'Failed to find any listing.' if 'start' in keyvalDict: # Assume that both start and end are line numbers. start = int(keyvalDict['start']) end = int(keyvalDict['end']) code = '\n'.join(listing.split('\n')[start:end]) elif 'pat' in keyvalDict: pat = r'%s' % keyvalDict['pat'] print1(pat) m = re.search(pat, listing, re.DOTALL) if m: code = m.group(0) else: code = "Pattern '%s' not found in '%s'" % (pat, keyvalDict['src']) else: code = 'No listing found.' return CodeBlock([ident, [], keyvals], code)
def process( value, format ): [[ident, classes, keyvals], code] = value if "graphviz" in classes: caption, typef, keyvals = get_caption(keyvals) filetype = get_extension(format, "png", html="png", latex="pdf") dest = get_filename4code("graphviz", code, filetype) if not os.path.isfile(dest): import pygraphviz g = pygraphviz.AGraph(string=code) g.layout() g.draw(dest) print1('INFO', 'Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])]) elif "standalone" in classes: caption, typef, keyvals = get_caption(keyvals) filetype = get_extension(format, "png", html="png", latex="pdf") dest = get_filename4code("standalone", code, filetype) if not os.path.isfile(dest): success = gen_standalone(code, dest) if not success: return Para([ Str(">>> Error: This image could not be generated.")] ) return Para([Image([ident, [], keyvals], caption, [dest, typef])]) elif "include" in classes: caption, typef, keyvals = get_caption(keyvals) keyvalDict = dict(keyvals) listing = '' with open( keyvalDict['src'], 'r' ) as f: listing = f.read() lang = keyvalDict.get( 'lang', 'python') code = 'Failed to find any listing.' if 'start' in keyvalDict: # Assume that both start and end are line numbers. start = int( keyvalDict['start'] ) end = int( keyvalDict['end'] ) code = '\n'.join( listing.split('\n')[start:end] ) elif 'pat' in keyvalDict: pat = r'%s' % keyvalDict['pat'] print1( pat ) m = re.search( pat, listing, re.DOTALL ) if m: code = m.group(0) else: code = "Pattern '%s' not found in '%s'" % (pat, keyvalDict['src']) else: code = 'No listing found.' return CodeBlock([ident, [], keyvals], code)
def mermaid(key, value, format_, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "mermaid" in classes: caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("mermaid", code) filetype = get_extension(format_, "png", html="svg", latex="png") src = filename + '.mmd' dest = filename + '.' + filetype if not os.path.isfile(dest): txt = code.encode(sys.getfilesystemencoding()) with open(src, "wb") as f: f.write(txt) # Default command to execute cmd = [MERMAID_BIN, "-i", src, "-o", dest, "-s", SCALE] if PUPPETEER_CFG is not None: cmd.extend(["-p", PUPPETEER_CFG]) if os.path.isfile('.puppeteer.json'): cmd.extend(["-p", ".puppeteer.json"]) subprocess.check_call(cmd) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def plantuml(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "plantuml" in classes: sys.stderr.write('>>>>> Dealwith plantuml code in ' + os.path.abspath(os.path.curdir) + '\n') sys.stderr.write('value[ident,[classes],[[keyvals]]],format == ' + ident + str(classes) + str(keyvals) + format + '\n') caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("plantuml", code) for k, v in keyvals: if 'title' == k: filename = "plantuml-images/" + v filetype = get_extension(format, "png", html="svg", revealjs="svg", latex="eps") sys.stderr.write('filename: ' + filename + '\n') sys.stderr.write('filetype: ' + filetype + '\n') src = filename + '.uml' dest = filename + '.' + filetype if os.path.isfile(dest): os.remove(dest) sys.stderr.write('Creating .uml file: ' + src + '\n') if (sys.version.startswith('2')): txt = code.encode(sys.getfilesystemencoding()) # python2:type(code)=unicode; type(txt)=str if (sys.version.startswith('3')): # txt = code.encode(sys.getfilesystemencoding()) # python3:type(code)=str; type(txt)=bytes txt = code # sys.stderr.write(str(type(code)) + " " + str(type(txt))) # Remove any char untile @, # because there are some characters like space, 0x10(backspace)... in txt txtx = re.sub('^[^@]', '', txt) if not txtx.startswith('@start'): txt = "@startuml\n" + txt + "\n@enduml\n" with open(src, "w") as f: f.write(txt) sys.stderr.write('Creating image: ' + dest + '\n') rootpath = os.path.dirname(os.path.abspath(__file__)) call([ "java", "-jar", rootpath + "/plantuml.jar", "-t" + filetype, src ]) sys.stderr.write('Created image ' + dest + '\n') ret = Para([Image([ident, [], keyvals], caption, [dest, typef])]) sys.stderr.write('Ret: ' + str(ret) + '\n') return ret
def plantuml(key, value, format_, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "plantuml" in classes: caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("plantuml", code) filetype = get_extension(format_, "png", html="svg", latex="png") src = filename + '.uml' dest = filename + '.' + filetype if not os.path.isfile(dest): txt = code.encode(sys.getfilesystemencoding()) if not txt.startswith(b"@start"): txt = b"@startuml\n" + txt + b"\n@enduml\n" with open(src, "wb") as f: f.write(txt) subprocess.check_call(PLANTUML_BIN.split() + ["-t" + filetype, src]) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def wavedrom(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "wavedrom" in classes: caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("wavedrom", code) filetype = get_extension(format, "png", html5="svg", html="svg", latex="eps") src = filename + '.json' svg = filename + '.svg' dest = filename + '.' + filetype if not os.path.isfile(dest): txt = code with open(src, mode="w") as f: f.write(txt) subprocess.run(["wavedrompy", "--input", src, "--svg", svg], stdout=subprocess.DEVNULL) subprocess.run(["inkscape", "-z", "-e", dest, svg], stdout=subprocess.DEVNULL) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def plantuml(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "plantuml" in classes: caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("plantuml", code) filetype = get_extension(format, "png", html="svg", latex="pdf") src = filename + '.puml' dest = filename + '.' + filetype if not os.path.isfile(dest): txt = code.encode(sys.getfilesystemencoding()) if not txt.startswith("@start"): txt = "@startuml\n" + txt + "\n@enduml\n" with open(src, "w") as f: f.write(txt) with open('plantUMLErrors.log', "w") as outfile: call([ "java", "-jar", "filters/plantuml/plantuml.jar", "-t" + filetype, src ], stdout=outfile) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def blockdiag(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value all_kw = { "actdiag", "blockdiag", "nwdiag", "packetdiag", "rackdiag", "seqdiag" } kw = all_kw & set(classes) if len(kw) == 1: caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("blockdiag", code) filetype = get_extension(format, "png", html="svg", latex="pdf") src = filename + '.diag' dest = filename + '.' + filetype if not os.path.isfile(dest): cmd = str(list(kw)[0]) if not code.startswith(cmd): code = cmd + "{\n" + code + "\n}\n" with open(src, "w") as f: f.write(code) call([cmd, "-a", "-T" + filetype, src]) sys.stderr.write('Created image (' + cmd + ") " + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def plantuml(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "plantuml" in classes or "puml" in classes: caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("plantuml", code) # HTML5を追加 filetype = get_extension(format, "png", html5="svg", html="svg", latex="eps") src = filename + '.uml' dest = filename + '.' + filetype if not os.path.isfile(dest): # エンコード関連をコメントアウト # txt = code.encode(sys.getfilesystemencoding()) # txt = str(code) txt = code if not txt.startswith("@start"): txt = "@startuml\n" + txt + "\n@enduml\n" with open(src, "w") as f: f.write(txt) # フィルターと同じディレクトリにplantuml.jarをおいておく plantuml_jar = os.path.join(os.path.dirname(__file__) , "plantuml.jar") # subprosess.callから変更 subprocess.run(["java", "-jar", plantuml_jar, "-t"+filetype, src, "-charset", "UTF-8"]) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def plantuml(key, value, format_, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "plantuml" in classes: caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("plantuml", code) filetype = get_extension(format_, "png", html="svg", latex="png") src = filename + '.uml' dest = filename + '.' + filetype # Generate image only once if not os.path.isfile(dest): txt = code.encode(sys.getfilesystemencoding()) if not txt.startswith(b"@start"): txt = b"@startuml\n" + txt + b"\n@enduml\n" with open(src, "wb") as f: f.write(txt) subprocess.check_call(PLANTUML_BIN.split() + ["-t" + filetype, src]) sys.stderr.write('Created image ' + dest + '\n') # Update symlink each run for ind, keyval in enumerate(keyvals): if keyval[0] == 'plantuml-filename': link = keyval[1] keyvals.pop(ind) rel_mkdir_symlink(dest, link) dest = link break return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def ascii2svg(key, value, format, meta): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "a2s" in classes: caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("a2s", code) typepandoc = get_extension(format, "pdf") typea2s = "svg" src = filename + '.a2s' desta2s = filename + '.' + typea2s fontName = "" metaMonoFont = meta.get('monofont', None) if metaMonoFont: fontName = stringify(metaMonoFont['c']) if not os.path.isfile(desta2s): txt = code.encode(sys.getfilesystemencoding()) txt = txt.decode('utf-8') with open(src, "w") as f: f.write(txt) call(['a2s "-f%s" -i%s -o%s' % (fontName, src, desta2s)], shell=True) sys.stderr.write('Created image ' + desta2s + '\n') return Para([Image([ident, [], keyvals], caption, [desta2s, typef])])
def tikz(key, value, format, _): if key == 'RawBlock': [fmt, code] = value if fmt == "latex" and re.match("\\\\begin{tikzpicture}", code): outfile = get_filename4code("tikz", code) filetype = get_extension(format, "png", html="png", latex="pdf") src = outfile + '.' + filetype if not os.path.isfile(src): tikz2image(code, filetype, outfile) sys.stderr.write('Created image ' + src + '\n') return Para([Image(['', [], []], [], [src, ""])])
def tikz(key, value, format, _): if key == 'RawBlock': [fmt, code] = value if fmt == "latex" and re.match( r'\begin{tikzpicture}', code): outfile = get_filename4code("tikz", code) filetype = get_extension(format, "png", html="png", latex="pdf") src = outfile + '.' + filetype if not os.path.isfile(src): tikz2image(code, filetype, outfile) log('Created image ' + src ) return Para([Image(['', [], []], [], [src, ""])])
def image_with_standalone(k, v, fmt, meta): if k == 'Image': desc = v[2][0] if r'\documentclass' in desc: code = desc outfile = get_filename4code("tikz", code) filetype = get_extension(format, "png", html="png", latex="pdf") src = outfile + '.' + filetype if not os.path.isfile(src): tikz2image(code, filetype, outfile) return Para([Image(['', [], []], [], [src, ""])])
def plantuml(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "plantuml" in classes: caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("plantuml", code) filetype = get_extension(format, "png", html="svg", latex="pdf") src = filename + '.puml' plantuml_output = filename + '.' + filetype dest_spec = "" # Key to specify final destination the file for ind, keyval in enumerate(keyvals): if keyval[0] == 'plantuml-filename': dest_spec = keyval[1] keyvals.pop(ind) break # Generate image only once if not os.path.isfile(plantuml_output): txt = code.encode(sys.getfilesystemencoding()) if not txt.startswith(b"@start"): txt = b"@startuml\n" + txt + b"\n@enduml\n" with open(src, "wb") as f: f.write(txt) # Must not let messages go to stdout, as it will corrupt JSON in filter with open('plantUMLErrors.log', "w") as log_file: call([ "java", "-jar", "filters/plantuml/plantuml.jar", "-t" + filetype, src ], stdout=log_file) sys.stderr.write('Created image ' + plantuml_output + '\n') if not dest_spec == "": sys.stderr.write('Copying image from ' + plantuml_output + ' to ' + dest_spec + '\n') shutil.copy2(plantuml_output, dest_spec) plantuml_output = dest_spec for ind, keyval in enumerate(keyvals): if keyval[0] == 'hide-image': if keyval[1] == 'true': sys.stderr.write('INFO: Not showing image ' + plantuml_output + '\n') return [] # surpress image in JSON return Para([ Image([ident, [], keyvals], caption, [plantuml_output, typef]) ])
def tikz(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "tikz" in classes: caption, typef, keyvals = get_caption(keyvals) # sys.stderr.write("{};; {};; {}".format(caption, typef, keyvals)) outfile = get_filename4code("tikz", code) filetype = get_extension(format, "svg", html="svg", latex="pdf") dest = outfile + '.' + filetype if not os.path.isfile(dest): tikz2image(code, filetype, outfile) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def abc(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "abc" in classes: caption, typef, keyvals = get_caption(keyvals) outfile = get_filename4code("abc", code) filetype = get_extension(format, "png", html="png", latex="pdf") dest = outfile + '.' + filetype if not os.path.isfile(dest): abc2eps(code.encode("utf-8"), filetype, outfile) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def abc(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "abc" in classes: caption, typef, keyvals = get_caption(keyvals) outfile = get_filename4code("abc", code) filetype = get_extension(format, "png", html="png", latex="pdf") dest = outfile + '.' + filetype if not os.path.isfile(dest): abc2eps(code.encode("utf-8"), filetype, outfile) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def __init__(self, codec, fmt, meta): 'init by decoding the CodeBlock-s value' self.codec = codec # save original codeblock for later self.fmt = fmt # some workers (flydraw) need access to this self.stdout = '' # catches stdout by self.cmd, if any self.stderr = '' # catches stderr by self.cmd, if any if codec is None: return # initial dispatch creation # Options from to codeblock, meta data or imagine defaults cb = self.get_cb_opts(codec) # codeblock attrs kd = self.get_md_opts(meta).get(self.klass, {}) # metadata.klass md = self.md_opts # metadata (toplevel) opts = [x for x in dir(self) if x.startswith('im_')] for opt in opts: val = cb.get( opt, # 1 codeblock.opt kd.get( opt, # 2 imagine.klass.opt (meta) md.get( opt, # 3 imagine.opt (meta) getattr(self, opt)))) # 4 class.opt (class's code) setattr(self, opt, val) # post-process options self.im_opt = self.im_opt.split() self.im_out = self.im_out.lower().replace(',', ' ').split() self.im_log = int(self.im_log) self.im_fmt = pf.get_extension(fmt, self.im_fmt) if self.im_name: self.msg(4, self.klass, 'image name', self.im_name) if not self.im_prg: # if no im_prg was found, fallback to klass's cmdmap self.im_prg = self.cmdmap.get(self.klass, None) if self.im_prg is None: self.msg(0, self.klass, 'not listed in', self.cmdmap) raise Exception('no worker found for %s' % self.klass) self.basename = pf.get_filename4code(self.im_dir, str(codec)) self.outfile = self.basename + "_" + self.im_name + '.%s' % self.im_fmt self.inpfile = self.basename + "_" + self.im_name + '.%s' % self.klass # _name.lower() if not os.path.isfile(self.inpfile): self.write('w', self.code, self.inpfile)
def graphviz(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "graphviz" in classes: caption, typef, keyvals = get_caption(keyvals) filetype = get_extension(format, "png", html="png", latex="pdf") dest = get_filename4code("graphviz", code, filetype) if not os.path.isfile(dest): g = pygraphviz.AGraph(string=code) g.layout() g.draw(dest) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def process(value, format): [[ident, classes, keyvals], code] = value if "graphviz" in classes: caption, typef, keyvals = get_caption(keyvals) filetype = get_extension(format, "png") dest = get_filename4code("graphviz", code, filetype) if not os.path.isfile(dest): import pygraphviz g = pygraphviz.AGraph(string=code) g.layout() g.draw(dest) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])]) elif "standalone" in classes: caption, typef, keyvals = get_caption(keyvals) filetype = get_extension(format, "png", html="png", latex="pdf") dest = get_filename4code("standalone", code, filetype) if not os.path.isfile(dest): gen_standalone(code, dest) else: print1('[INFO] Image file %s already generated' % dest) return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def plantuml(key, value, format_, meta): if key == 'Header': if 'tikz' in (str(meta)): os.environ["PLANTUML_LATEX_EXPORT"] = 'latex' if key == 'CodeBlock': if os.getenv("DEBUG", "f").lower() in ("1", "true"): print("plantuml", key, value, format_, meta) [[ident, classes, keyvals], code] = value if "plantuml" in classes: caption, typef, keyvals = get_caption(keyvals) if "PLANTUML_LATEX_EXPORT" in os.environ: latex_img_format = "latex" else: latex_img_format = "eps" filename = get_filename4code("plantuml", code) filetype = get_extension(format_, "png", html="svg", latex=latex_img_format, beamer=latex_img_format) src = filename + '.uml' dest = filename + '.' + filetype if not os.path.isfile(dest): txt = code.encode(sys.getfilesystemencoding()) if not txt.startswith(b"@start"): txt = b"@startuml\n" + txt + b"\n@enduml\n" with open(src, "wb") as f: f.write(txt) subprocess.check_call(PLANTUML_BIN.split() + ["-t" + filetype, src]) sys.stderr.write('Created image ' + dest + '\n') if (filetype == "latex") and (latex_img_format == 'latex'): latex = open(dest).read() return RawBlock( 'latex', latex.split("\\begin{document}")[-1].split( "\\end{document}")[0]) else: return Para( [Image([ident, [], keyvals], caption, [dest, typef])])
def graphviz(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "graphviz" in classes: caption, typef, keyvals = get_caption(keyvals) prog, keyvals = get_value(keyvals, u"prog", u"dot") filetype = get_extension(format, "svg", html="svg", latex="pdf") dest = get_filename4code("graphviz", code, filetype) if not os.path.isfile(dest): g = pygraphviz.AGraph(string=code) g.layout() g.draw(dest, prog=prog) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def tex2png(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "tex2png" in classes: caption, typef, keyvals = get_caption(keyvals) filetype = get_extension(format, "png", html="png", latex="pdf") dest = get_filename4code("tex2png", code, filetype) abs_dest = os.path.abspath( os.path.expanduser(os.path.expandvars(dest))) if not os.path.isfile(dest): # sys.stderr.write('DEBUG code="' + code + '"\n') p = subprocess.run(["tex2png.sh", abs_dest], text=True, input=code) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def plantuml(key, value, format_, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value # print(value) # print("caga") sys.stderr.write(str(ident)) if "plantuml" in classes: caption, typef, keyvals = get_caption(keyvals) imageFolder = Klasor("den/plantuml") filename = get_filename4code( 'DenemeProje2/webNdocs/static/images/Converted_Pdf', code) filetype = get_extension(format_, "png", html="svg", latex="png") src = filename + '.uml' dest = filename + '.' + filetype # print(src) # Generate image only once if not os.path.isfile(dest): txt = code.encode(sys.getfilesystemencoding()) if not txt.startswith(b"@start"): txt = b"@startuml\n" + txt + b"\n@enduml\n" with open(src, "wb") as f: f.write(txt) subprocess.check_call(PLANTUML_BIN.split() + ["-t" + filetype, src]) sys.stderr.write('Created image ' + dest + '\n') # Update symlink each run for ind, keyval in enumerate(keyvals): if keyval[0] == 'plantuml-filename': link = keyval[1] keyvals.pop(ind) if os.path.islink(link): os.remove(link) os.symlink(dest, link) dest = link break return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def plantuml(key, value, format, meta): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "plot" in classes: caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("plot", code) filetype = get_extension(format, "png", html="svg", latex="eps") dest = filename + '.' + filetype if not os.path.isfile(dest): src = code.encode(sys.getfilesystemencoding()) src = src.decode('utf-8') df = pd.read_csv(src, skipinitialspace=True, iterator=False) # df.plot() plt.figure(dpi=None, facecolor="white")
def mscgen(key, value, format, _): if key == 'CodeBlock': (ident, classes, keyvals), code = value if "mscgen" in classes: caption, typef, keyvals = get_caption(keyvals) prog, keyvals = get_value(keyvals, "prog", "mscgen") filetype = get_extension(format, PICTURE_FORMAT) dest = get_filename4code("mscgen", code, filetype) if not os.path.isfile(dest): result = subprocess.run( [ "mscgen", "-T", "png", "-o", dest, "-", ], input=code.encode(), ) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def plantuml(key, value, format, meta): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "plantuml" in classes: caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("plantuml", code) filetype = get_extension(format, "png", html="svg", latex="eps") src = filename + '.uml' dest = filename + '.' + filetype skinparams = [ "skinparam monochrome true", "skinparam shadowing false", ] metaMonoFont = meta.get('monofont', None) if metaMonoFont: fontName = stringify(metaMonoFont['c']) skinparams.append("skinparam defaultFontName %s" % fontName) if not os.path.isfile(dest): txt = code.encode(sys.getfilesystemencoding()) txt = txt.decode('utf-8') if not txt.startswith("@start"): params = "" for skinparam in skinparams: params += "%s\n" % skinparam txt = "@startuml\n" + params + txt + "\n@enduml\n" with open(src, "w") as f: f.write(txt) call(["plantuml.jar -t%s %s" % (filetype, src)], shell=True) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def plantuml(key, value, format_, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "plantuml" in classes: caption, typef, keyvals = _get_caption(keyvals, format_) filename = get_filename4code("plantuml", code) filetype = get_extension(format_, "png", html5="svg", latex="svg", context="svg") src = filename + '.uml' dest = filename + '.' + filetype # Generate image only once if not os.path.isfile(dest): txt = code.encode(sys.getfilesystemencoding()) if not txt.startswith(b"@start"): txt = b"@startuml\n" + txt + b"\n@enduml\n" with open(src, "wb") as f: f.write(txt) # The PlantUML complementary jars (PDF modules) have log messages to stdout that corrupt the JSON stream with open('plantUMLErrors.log', "a+") as log_file: subprocess.check_call(PLANTUML_BIN.split() + ["-t" + filetype, src], stdout=log_file, stderr=log_file) sys.stderr.write('Created image ' + dest + '\n') # Update symlink each run for ind, keyval in enumerate(keyvals): if keyval[0] == 'plantuml-filename': link = keyval[1] keyvals.pop(ind) rel_mkdir_symlink(dest, link) dest = link break # print('Caption: ', caption, file=sys.stderr) return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def graphviz(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "graphviz" in classes: caption, typef, keyvals = get_caption(keyvals) prog, keyvals = get_value(keyvals, u"prog", u"dot") filetype = get_extension(format, "png", html="png", latex="pdf") dest = get_filename4code("graphviz", code, filetype) filename = "" for a,fname in keyvals: if(a == "filename"): valid_chars = "-_ %s%s" % (string.ascii_letters, string.digits) fname = ''.join(x for x in fname if x in valid_chars) if fname != "": filename = "graphviz-images/"+fname+"."+filetype dest = filename if (filename != "") or (not os.path.isfile(dest)): g = pygraphviz.AGraph(string=code) g.layout() g.draw(dest, prog=prog) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def plantuml(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "plantuml" in classes: caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("plantuml", code) filetype = get_extension(format, "png", html="svg", latex="eps") src = filename + '.uml' dest = filename + '.' + filetype if not os.path.isfile(dest): txt = code if not txt.startswith("@start"): txt = "@startuml\n" + txt + "\n@enduml\n" with open(src, "w") as f: f.write(txt) call(["java", "-jar", sys.argv[1], "-t" + filetype, src]) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def graphviz(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "graph" in classes: caption, typef, keyvals = get_caption(keyvals) prog, keyvals = get_value(keyvals, u"prog", u"dot") filetype = get_extension(format, "svg", html="svg", latex="pdf") dest = get_filename4code(document_name(), code, filetype) # If name attribute exists, overwrite standard filename for eachKeys in keyvals: if 'name' in eachKeys[0]: filename = "graph-images/" + eachKeys[1][:-4] dest = filename + '.' + filetype if not os.path.isfile(dest): g = pygraphviz.AGraph(string=code) g.layout() g.draw(dest, prog=prog) sys.stderr.write('Created image ' + dest + '\n') image = Image([ident, classes, keyvals], caption, [dest, typef]) return Para([image])
def plantuml(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "plantuml" in classes: caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("plantuml", code) filetype = get_extension(format, "png", html="svg", latex="eps") src = filename + '.uml' dest = filename + '.' + filetype if not os.path.isfile(dest): txt = code.encode(sys.getfilesystemencoding()) if not txt.startswith("@start"): txt = "@startuml\n" + txt + "\n@enduml\n" with open(src, "w") as f: f.write(txt) call(["java", "-jar", "plantuml.jar", "-t"+filetype, src]) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def ditaa(key, value, format, _): if key == 'CodeBlock': [[ident, classes, keyvals], code] = value if "ditaa" in classes: caption, typef, keyvals = get_caption(keyvals) filename = get_filename4code("ditaa", code) filetype = get_extension(format, "png", html="svg", latex="eps") src = filename + '.txt' dest = filename + '.' + filetype if not os.path.isfile(dest): with open(src, "w") as f: f.write(code) call(["java", "-jar", "ditaa.jar", src]) pprint.pprint([ 'Created image ', dest, 'typef=', typef, 'ident=', ident, 'keyvals=', keyvals, 'caption=', caption ], sys.stderr) return Para([Image([ident, [], keyvals], caption, [dest, typef])])
def graphviz(key, value, format, _): if key == 'CodeBlock': (ident, classes, keyvals), code = value if "graphviz" in classes: caption, typef, keyvals = get_caption(keyvals) prog, keyvals = get_value(keyvals, "prog", "dot") filetype = get_extension(format, PICTURE_FORMAT) dest = get_filename4code("graphviz", code, filetype) if not os.path.isfile(dest): result = subprocess.run( [ "dot", "-T{}".format(PICTURE_FORMAT), "-Gdpi=300", ], stdout=subprocess.PIPE, input=code.encode(), ) with open(dest, 'wb') as fd: fd.write(result.stdout) sys.stderr.write('Created image ' + dest + '\n') return Para([Image([ident, [], keyvals], caption, ["/" + dest, typef])])
def fmt(self, fmt, **specials): '(re)set image file extension based on output document format' self.outfmt = pf.get_extension(fmt, self.outfmt, **specials) self.outfile = self.basename + '.%s' % self.outfmt