From a63aa54a1a068f6277d64a89cbc59a5a656ef70a Mon Sep 17 00:00:00 2001 From: Justin Kinney Date: Wed, 16 Sep 2015 20:22:58 -0700 Subject: [PATCH 1/9] fixes https://github.com/astanin/py-markdown-ditaa/issues/1 --- README.md | 3 +++ mdx_ditaa.py | 8 ++++---- setup.py | 7 +++++++ 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 setup.py diff --git a/README.md b/README.md index 8aa76ff..4ef23b2 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,9 @@ desired command line to invoke `ditaa`, for example: where `{infile}` and `{outfile}` are placeholders for input and output file names. +### Testing +When running tests, be sure that your `PATH` contains the `ditaa` executable, or set `DTIAA_CMD` appropriately as described above. + Compatiblity: ditaa and fenced_code ----------------------------------- diff --git a/mdx_ditaa.py b/mdx_ditaa.py index cf9ba87..67c101c 100644 --- a/mdx_ditaa.py +++ b/mdx_ditaa.py @@ -3,7 +3,7 @@ """ # The MIT License (MIT) -# +# # Copyright (c) 2014 Sergey Astanin # # Permission is hereby granted, free of charge, to any person obtaining a copy @@ -15,7 +15,7 @@ # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. -# +# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -121,5 +121,5 @@ def extendMarkdown(self, md, md_globals): md.preprocessors.add("ditaa", DitaaPreprocessor(md), location) -def makeExtension(configs=None): - return DitaaExtension(configs=configs) +def makeExtension(**kwargs): + return DitaaExtension(**kwargs) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..13a5d8a --- /dev/null +++ b/setup.py @@ -0,0 +1,7 @@ +from setuptools import setup +setup( + name='mdx_ditaa', + version='1.0', + py_modules=['mdx_ditaa'], + install_requires = ['markdown>=2.5'], +) From 4f53d1d192346da2bd3953d746173e521be65719 Mon Sep 17 00:00:00 2001 From: Justin Kinney Date: Wed, 16 Sep 2015 20:22:58 -0700 Subject: [PATCH 2/9] fixes https://github.com/astanin/py-markdown-ditaa/issues/1 add setup.py --- README.md | 3 +++ mdx_ditaa.py | 8 ++++---- setup.py | 7 +++++++ 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 setup.py diff --git a/README.md b/README.md index 8aa76ff..4ef23b2 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,9 @@ desired command line to invoke `ditaa`, for example: where `{infile}` and `{outfile}` are placeholders for input and output file names. +### Testing +When running tests, be sure that your `PATH` contains the `ditaa` executable, or set `DTIAA_CMD` appropriately as described above. + Compatiblity: ditaa and fenced_code ----------------------------------- diff --git a/mdx_ditaa.py b/mdx_ditaa.py index cf9ba87..67c101c 100644 --- a/mdx_ditaa.py +++ b/mdx_ditaa.py @@ -3,7 +3,7 @@ """ # The MIT License (MIT) -# +# # Copyright (c) 2014 Sergey Astanin # # Permission is hereby granted, free of charge, to any person obtaining a copy @@ -15,7 +15,7 @@ # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. -# +# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -121,5 +121,5 @@ def extendMarkdown(self, md, md_globals): md.preprocessors.add("ditaa", DitaaPreprocessor(md), location) -def makeExtension(configs=None): - return DitaaExtension(configs=configs) +def makeExtension(**kwargs): + return DitaaExtension(**kwargs) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..13a5d8a --- /dev/null +++ b/setup.py @@ -0,0 +1,7 @@ +from setuptools import setup +setup( + name='mdx_ditaa', + version='1.0', + py_modules=['mdx_ditaa'], + install_requires = ['markdown>=2.5'], +) From 7a89a78a9eb40fb96871ba89f75b67fe676a66e8 Mon Sep 17 00:00:00 2001 From: Justin Kinney Date: Wed, 16 Sep 2015 20:58:14 -0700 Subject: [PATCH 3/9] Add support for extension configuration --- mdx_ditaa.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/mdx_ditaa.py b/mdx_ditaa.py index 67c101c..708e959 100644 --- a/mdx_ditaa.py +++ b/mdx_ditaa.py @@ -115,11 +115,31 @@ def run(self, lines): class DitaaExtension(Extension): + PreprocessorClass = DitaaPreprocessor + + def __init__(self, configs=[]): + ditaa_cmd = os.environ.get("DITAA_CMD", "ditaa {infile} {outfile} --overwrite") + ditaa_image_dir = os.environ.get("DITAA_IMAGE_DIR", ".") + print ditaa_cmd + print ditaa_image_dir + self.config = { + "ditaa_cmd": [ditaa_cmd, + "Full command line to launch ditaa. Defaults to:" + "{}".format(ditaa_cmd)], + "ditaa_image_dir": [ditaa_image_dir, + "Full path to directory where images will be saved."] + } + + for k, v in configs: + self.setConfig(k, v) + def extendMarkdown(self, md, md_globals): + ditaa_ext = self.PreprocessorClass(md) + ditaa_ext.config = self.getConfigs() md.registerExtension(self) location = " Date: Wed, 16 Sep 2015 21:01:15 -0700 Subject: [PATCH 4/9] remove unncessary argument --- mdx_ditaa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mdx_ditaa.py b/mdx_ditaa.py index 708e959..e83d38a 100644 --- a/mdx_ditaa.py +++ b/mdx_ditaa.py @@ -141,5 +141,5 @@ def extendMarkdown(self, md, md_globals): md.preprocessors.add("ditaa", DitaaPreprocessor(md), location) -def makeExtension(*args, **kwargs): - return DitaaExtension(*args, **kwargs) +def makeExtension(**kwargs): + return DitaaExtension(**kwargs) From ac1e86ac19dcf3d0ec06cc82bff66020104a4bce Mon Sep 17 00:00:00 2001 From: Justin Kinney Date: Wed, 16 Sep 2015 21:02:31 -0700 Subject: [PATCH 5/9] remove unncessary argument --- mdx_ditaa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mdx_ditaa.py b/mdx_ditaa.py index e83d38a..e8da6dc 100644 --- a/mdx_ditaa.py +++ b/mdx_ditaa.py @@ -141,5 +141,5 @@ def extendMarkdown(self, md, md_globals): md.preprocessors.add("ditaa", DitaaPreprocessor(md), location) -def makeExtension(**kwargs): - return DitaaExtension(**kwargs) +def makeExtension(configs={}): + return DitaaExtension(configs=configs) From f0d2461104b4b57d2e1f0acc3640f7a14630ddb5 Mon Sep 17 00:00:00 2001 From: Justin Kinney Date: Thu, 17 Sep 2015 07:59:39 -0700 Subject: [PATCH 6/9] Update some variable names to improve readability, add support for extension configuration and environment variable overrides --- mdx_ditaa.py | 73 ++++++++++++++++++++++++++++------------------------ setup.py | 2 +- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/mdx_ditaa.py b/mdx_ditaa.py index e8da6dc..b76431a 100644 --- a/mdx_ditaa.py +++ b/mdx_ditaa.py @@ -32,7 +32,6 @@ import tempfile import zlib - from markdown.preprocessors import Preprocessor from markdown.extensions import Extension @@ -45,39 +44,40 @@ def b(string): return string -DITAA_CMD = os.environ.get("DITAA_CMD", "ditaa {infile} {outfile} --overwrite") - - -def generate_image_path(plaintext): +def generate_image_path(plaintext, image_dir): adler32 = ctypes.c_uint32(zlib.adler32(b(plaintext))).value - imgbasename = "diagram-%x.png" % adler32 - ditaa_image_dir = os.environ.get("DITAA_IMAGE_DIR", ".") - imgpath = os.path.join(ditaa_image_dir, imgbasename) - return imgpath + img_basename = "diagram-%x.png" % adler32 + image_path = os.path.join(image_dir, img_basename) + return image_path -def generate_diagram(plaintext): +def generate_diagram(plaintext, cmd_path, image_dir): """Run ditaa with plaintext input. Return relative path to the generated image. """ - imgpath = generate_image_path(plaintext) - srcfd, srcfname = tempfile.mkstemp(prefix="ditaasrc", text=True) - outfd, outfname = tempfile.mkstemp(prefix="ditaaout", text=True) - with os.fdopen(srcfd, "w") as src: + + img_path = generate_image_path(plaintext, image_dir) + src_fd, src_fname = tempfile.mkstemp(prefix="ditaasrc", text=True) + out_fd, out_fname = tempfile.mkstemp(prefix="ditaaout", text=True) + with os.fdopen(src_fd, "w") as src: src.write(plaintext) try: - cmd = DITAA_CMD.format(infile=srcfname, outfile=imgpath).split() - with os.fdopen(outfd, "w") as out: + cmd = cmd_path.format(infile=src_fname, outfile=img_path).split() + with os.fdopen(out_fd, "w") as out: retval = subprocess.check_call(cmd, stdout=out) - return os.path.relpath(imgpath, os.getcwd()) + return os.path.relpath(img_path, os.getcwd()) except: return None finally: - os.unlink(srcfname) - os.unlink(outfname) + os.unlink(src_fname) + os.unlink(out_fname) class DitaaPreprocessor(Preprocessor): + + def __init__(self, *args, **config): + self.config = config + def run(self, lines): START_TAG = "```ditaa" END_TAG = "```" @@ -92,7 +92,7 @@ def run(self, lines): plen = len(ditaa_prefix) ditaa_lines = [dln[plen:] for dln in ditaa_lines] ditaa_code = "\n".join(ditaa_lines) - filename = generate_diagram(ditaa_code) + filename = generate_diagram(ditaa_code, self.config['ditaa_cmd'], self.config['ditaa_image_dir']) if filename: new_lines.append(ditaa_prefix + "![%s](%s)" % (filename, filename)) else: @@ -115,31 +115,36 @@ def run(self, lines): class DitaaExtension(Extension): + PreprocessorClass = DitaaPreprocessor - def __init__(self, configs=[]): - ditaa_cmd = os.environ.get("DITAA_CMD", "ditaa {infile} {outfile} --overwrite") - ditaa_image_dir = os.environ.get("DITAA_IMAGE_DIR", ".") - print ditaa_cmd - print ditaa_image_dir + def __init__(self, **kwargs): + ditaa_cmd = kwargs.get('ditaa_cmd', 'ditaa {infile} {outfile} --overwrite') + ditaa_image_dir = kwargs.get('ditaa_image_dir', '.') + + if 'DITAA_CMD' in os.environ: + ditaa_cmd = os.environ.get("DITAA_CMD") + if 'DITAA_IMAGE_DIR' in os.environ: + ditaa_image_dir = os.environ.get("DITAA_IMAGE_DIR") + self.config = { - "ditaa_cmd": [ditaa_cmd, + 'ditaa_cmd': [ditaa_cmd, "Full command line to launch ditaa. Defaults to:" "{}".format(ditaa_cmd)], - "ditaa_image_dir": [ditaa_image_dir, + 'ditaa_image_dir': [ditaa_image_dir, "Full path to directory where images will be saved."] } - for k, v in configs: - self.setConfig(k, v) + super(DitaaExtension, self).__init__(**kwargs) def extendMarkdown(self, md, md_globals): - ditaa_ext = self.PreprocessorClass(md) + ditaa_ext = self.PreprocessorClass(md, self.config) ditaa_ext.config = self.getConfigs() - md.registerExtension(self) + + #md.registerExtension(self) location = "=2.5'], ) From 452fadfe0c3877f540a3a9eca3e370dfe581cbe2 Mon Sep 17 00:00:00 2001 From: Justin Kinney Date: Wed, 16 Sep 2015 20:22:58 -0700 Subject: [PATCH 7/9] fixes https://github.com/astanin/py-markdown-ditaa/issues/1 add setup.py update some variable names to improve readability add support for extension configuration and environment variable overrides --- README.md | 3 ++ mdx_ditaa.py | 77 ++++++++++++++++++++++++++++++++++------------------ setup.py | 7 +++++ 3 files changed, 61 insertions(+), 26 deletions(-) create mode 100644 setup.py diff --git a/README.md b/README.md index 8aa76ff..4ef23b2 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,9 @@ desired command line to invoke `ditaa`, for example: where `{infile}` and `{outfile}` are placeholders for input and output file names. +### Testing +When running tests, be sure that your `PATH` contains the `ditaa` executable, or set `DTIAA_CMD` appropriately as described above. + Compatiblity: ditaa and fenced_code ----------------------------------- diff --git a/mdx_ditaa.py b/mdx_ditaa.py index cf9ba87..b76431a 100644 --- a/mdx_ditaa.py +++ b/mdx_ditaa.py @@ -3,7 +3,7 @@ """ # The MIT License (MIT) -# +# # Copyright (c) 2014 Sergey Astanin # # Permission is hereby granted, free of charge, to any person obtaining a copy @@ -15,7 +15,7 @@ # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. -# +# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -32,7 +32,6 @@ import tempfile import zlib - from markdown.preprocessors import Preprocessor from markdown.extensions import Extension @@ -45,39 +44,40 @@ def b(string): return string -DITAA_CMD = os.environ.get("DITAA_CMD", "ditaa {infile} {outfile} --overwrite") - - -def generate_image_path(plaintext): +def generate_image_path(plaintext, image_dir): adler32 = ctypes.c_uint32(zlib.adler32(b(plaintext))).value - imgbasename = "diagram-%x.png" % adler32 - ditaa_image_dir = os.environ.get("DITAA_IMAGE_DIR", ".") - imgpath = os.path.join(ditaa_image_dir, imgbasename) - return imgpath + img_basename = "diagram-%x.png" % adler32 + image_path = os.path.join(image_dir, img_basename) + return image_path -def generate_diagram(plaintext): +def generate_diagram(plaintext, cmd_path, image_dir): """Run ditaa with plaintext input. Return relative path to the generated image. """ - imgpath = generate_image_path(plaintext) - srcfd, srcfname = tempfile.mkstemp(prefix="ditaasrc", text=True) - outfd, outfname = tempfile.mkstemp(prefix="ditaaout", text=True) - with os.fdopen(srcfd, "w") as src: + + img_path = generate_image_path(plaintext, image_dir) + src_fd, src_fname = tempfile.mkstemp(prefix="ditaasrc", text=True) + out_fd, out_fname = tempfile.mkstemp(prefix="ditaaout", text=True) + with os.fdopen(src_fd, "w") as src: src.write(plaintext) try: - cmd = DITAA_CMD.format(infile=srcfname, outfile=imgpath).split() - with os.fdopen(outfd, "w") as out: + cmd = cmd_path.format(infile=src_fname, outfile=img_path).split() + with os.fdopen(out_fd, "w") as out: retval = subprocess.check_call(cmd, stdout=out) - return os.path.relpath(imgpath, os.getcwd()) + return os.path.relpath(img_path, os.getcwd()) except: return None finally: - os.unlink(srcfname) - os.unlink(outfname) + os.unlink(src_fname) + os.unlink(out_fname) class DitaaPreprocessor(Preprocessor): + + def __init__(self, *args, **config): + self.config = config + def run(self, lines): START_TAG = "```ditaa" END_TAG = "```" @@ -92,7 +92,7 @@ def run(self, lines): plen = len(ditaa_prefix) ditaa_lines = [dln[plen:] for dln in ditaa_lines] ditaa_code = "\n".join(ditaa_lines) - filename = generate_diagram(ditaa_code) + filename = generate_diagram(ditaa_code, self.config['ditaa_cmd'], self.config['ditaa_image_dir']) if filename: new_lines.append(ditaa_prefix + "![%s](%s)" % (filename, filename)) else: @@ -115,11 +115,36 @@ def run(self, lines): class DitaaExtension(Extension): + + PreprocessorClass = DitaaPreprocessor + + def __init__(self, **kwargs): + ditaa_cmd = kwargs.get('ditaa_cmd', 'ditaa {infile} {outfile} --overwrite') + ditaa_image_dir = kwargs.get('ditaa_image_dir', '.') + + if 'DITAA_CMD' in os.environ: + ditaa_cmd = os.environ.get("DITAA_CMD") + if 'DITAA_IMAGE_DIR' in os.environ: + ditaa_image_dir = os.environ.get("DITAA_IMAGE_DIR") + + self.config = { + 'ditaa_cmd': [ditaa_cmd, + "Full command line to launch ditaa. Defaults to:" + "{}".format(ditaa_cmd)], + 'ditaa_image_dir': [ditaa_image_dir, + "Full path to directory where images will be saved."] + } + + super(DitaaExtension, self).__init__(**kwargs) + def extendMarkdown(self, md, md_globals): - md.registerExtension(self) + ditaa_ext = self.PreprocessorClass(md, self.config) + ditaa_ext.config = self.getConfigs() + + #md.registerExtension(self) location = "=2.5'], +) From 2bbff1774d584ec20e769b01537d5ff13dde9860 Mon Sep 17 00:00:00 2001 From: Justin Kinney Date: Thu, 17 Sep 2015 15:39:42 -0700 Subject: [PATCH 8/9] Refactor some code into the DitaaPreprocessor class --- mdx_ditaa.py | 91 +++++++++++++++++++++++++++++++++------------------- setup.py | 2 +- 2 files changed, 59 insertions(+), 34 deletions(-) diff --git a/mdx_ditaa.py b/mdx_ditaa.py index b76431a..3012c83 100644 --- a/mdx_ditaa.py +++ b/mdx_ditaa.py @@ -28,6 +28,7 @@ import ctypes import os import platform +import shutil import subprocess import tempfile import zlib @@ -44,40 +45,49 @@ def b(string): return string -def generate_image_path(plaintext, image_dir): - adler32 = ctypes.c_uint32(zlib.adler32(b(plaintext))).value - img_basename = "diagram-%x.png" % adler32 - image_path = os.path.join(image_dir, img_basename) - return image_path - - -def generate_diagram(plaintext, cmd_path, image_dir): - """Run ditaa with plaintext input. - Return relative path to the generated image. - """ - - img_path = generate_image_path(plaintext, image_dir) - src_fd, src_fname = tempfile.mkstemp(prefix="ditaasrc", text=True) - out_fd, out_fname = tempfile.mkstemp(prefix="ditaaout", text=True) - with os.fdopen(src_fd, "w") as src: - src.write(plaintext) - try: - cmd = cmd_path.format(infile=src_fname, outfile=img_path).split() - with os.fdopen(out_fd, "w") as out: - retval = subprocess.check_call(cmd, stdout=out) - return os.path.relpath(img_path, os.getcwd()) - except: - return None - finally: - os.unlink(src_fname) - os.unlink(out_fname) - - class DitaaPreprocessor(Preprocessor): def __init__(self, *args, **config): self.config = config + def generate_image_path(self, plaintext): + """ + Return an image path based on a hash of the plaintext input. + """ + adler32 = ctypes.c_uint32(zlib.adler32(b(plaintext))).value + img_basename = "diagram-%x.png" % adler32 + image_path = os.path.join(self.config['ditaa_image_dir'], img_basename) + return image_path + + def generate_diagram(self, plaintext): + """ + Run ditaa with plaintext input. + Return relative path to the generated image. + """ + img_dest = self.generate_image_path(plaintext) + src_fd, src_fname = tempfile.mkstemp(prefix="ditaasrc", text=True) + out_fd, out_fname = tempfile.mkstemp(prefix="ditaaout", text=True) + with os.fdopen(src_fd, "w") as src: + src.write(plaintext) + try: + ditaa_cmd = self.config['ditaa_cmd'] + cmd = ditaa_cmd.format(infile=src_fname, outfile=img_dest) + with os.fdopen(out_fd, "w") as out: + retval = subprocess.check_call(cmd.split(), stdout=out) + + if self.config.get('extra_copy_path', None): + try: + shutil.copy(img_dest, self.config['extra_copy_path']) + except: + pass + return os.path.relpath(img_dest, os.getcwd()) + + except Exception as e: + return None + finally: + os.unlink(src_fname) + os.unlink(out_fname) + def run(self, lines): START_TAG = "```ditaa" END_TAG = "```" @@ -85,6 +95,7 @@ def run(self, lines): ditaa_prefix = "" ditaa_lines = [] in_diagram = False + path_override = None for ln in lines: if in_diagram: # lines of a diagram if ln == ditaa_prefix + END_TAG: @@ -92,9 +103,14 @@ def run(self, lines): plen = len(ditaa_prefix) ditaa_lines = [dln[plen:] for dln in ditaa_lines] ditaa_code = "\n".join(ditaa_lines) - filename = generate_diagram(ditaa_code, self.config['ditaa_cmd'], self.config['ditaa_image_dir']) + filename = self.generate_diagram(ditaa_code) if filename: - new_lines.append(ditaa_prefix + "![%s](%s)" % (filename, filename)) + if path_override: + mkdocs_path = os.path.join(path_override, os.path.basename(filename)) + else: + mkdocs_path = os.path.join(self.config['extra_copy_path'], os.path.basename(filename)) + new_lines.append(ditaa_prefix + "![%s](%s)" % (mkdocs_path, mkdocs_path)) + path_override = None else: md_code = [ditaa_prefix + " " + dln for dln in ditaa_lines] new_lines.extend([""] + md_code + [""]) @@ -105,6 +121,11 @@ def run(self, lines): else: # normal lines start = ln.find(START_TAG) prefix = ln[:start] if start >= 0 else "" + postfix = ln[start + len(START_TAG) + 1:] if start >= 0 else "" + if postfix and postfix.startswith('path='): + path_override = postfix[5:] + ln = ln[:-len(postfix) - 1] + # code block may be nested within a list item or a blockquote if start >= 0 and ln.endswith(START_TAG) and not prefix.strip(" \t>"): in_diagram = True @@ -121,6 +142,7 @@ class DitaaExtension(Extension): def __init__(self, **kwargs): ditaa_cmd = kwargs.get('ditaa_cmd', 'ditaa {infile} {outfile} --overwrite') ditaa_image_dir = kwargs.get('ditaa_image_dir', '.') + extra_copy_path = kwargs.get('extra_copy_path', None) if 'DITAA_CMD' in os.environ: ditaa_cmd = os.environ.get("DITAA_CMD") @@ -132,7 +154,10 @@ def __init__(self, **kwargs): "Full command line to launch ditaa. Defaults to:" "{}".format(ditaa_cmd)], 'ditaa_image_dir': [ditaa_image_dir, - "Full path to directory where images will be saved."] + "Full path to directory where images will be saved."], + 'extra_copy_path': [extra_copy_path, + "Set this path to save an extra copy into " + "the specified directory."] } super(DitaaExtension, self).__init__(**kwargs) @@ -141,7 +166,7 @@ def extendMarkdown(self, md, md_globals): ditaa_ext = self.PreprocessorClass(md, self.config) ditaa_ext.config = self.getConfigs() - #md.registerExtension(self) + md.registerExtension(self) location = "=2.5'], ) From 32ed0119c00139ed81814cc9936cf4d283a43cde Mon Sep 17 00:00:00 2001 From: Justin Kinney Date: Thu, 17 Sep 2015 22:08:24 -0700 Subject: [PATCH 9/9] Check for existing image prior to build --- mdx_ditaa.py | 4 ++++ setup.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/mdx_ditaa.py b/mdx_ditaa.py index 3012c83..d6ca8a1 100644 --- a/mdx_ditaa.py +++ b/mdx_ditaa.py @@ -65,6 +65,10 @@ def generate_diagram(self, plaintext): Return relative path to the generated image. """ img_dest = self.generate_image_path(plaintext) + + if os.path.exists(img_dest): + return os.path.relpath(img_dest, os.getcwd()) + src_fd, src_fname = tempfile.mkstemp(prefix="ditaasrc", text=True) out_fd, out_fname = tempfile.mkstemp(prefix="ditaaout", text=True) with os.fdopen(src_fd, "w") as src: diff --git a/setup.py b/setup.py index 9478ca3..11b27a8 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup setup( name='mdx_ditaa', - version='0.2', + version='0.3', py_modules=['mdx_ditaa'], install_requires = ['markdown>=2.5'], )