stathissideris / ditaa

ditaa is a small command-line utility that can convert diagrams drawn using ascii art ('drawings' that contain characters that resemble lines like | / - ), into proper bitmap graphics.
GNU Lesser General Public License v3.0
924 stars 87 forks source link

Reproduce a package source tree for the Python Sphinx extension for ditaa #77

Open akaihola opened 1 year ago

akaihola commented 1 year ago

Since ypguo, the owner of sphinxcontrib-ditaa is not reachable, let's try to trace the origins of the code and reconstruct a source tree and repository for publishing the extension.

(Sorry for using the ditaa repository for this purpose, but since the discussion originated here, I thought it could continue here as well until a new repository is created as a result.)


Background

In #61, @Cilyan wrote:

I doubt that this is the real repository of the sphinx extension, nor the right place to report issues there. It seems that the PyPI user ypguo (behind sphinxcontrib-ditaa) had a bad habit to link his work to the website of the original software that his plugins wraps instead of the website that hosts his own code. Doing this, there is no way to trace where the code really comes from, and apparently this is not from here. We hit here a major issue of PyPI...

@akaihola responded:

I tried to find where the PyPI package comes from, and here are some traces:

  • sdelisle25/sphinxcontrib-ditaa on GitHub, last updated 7 Jul 2017, forked from baloo/sphinx-ditaa, and has this at the top:

    sphinx.ext.ditaa
    ~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Allow ditaa-formatted graphs to by included in Sphinx-generated
    documents inline.
    
    :copyright: Copyright 2011 by Arthur Gautier
    :copyright: Copyright 2011 by Zenexity
    :license: BSD, see LICENSE for details.
  • baloo/sphinx-ditaa on GitHub, last updated 15 Oct 2017 (for which I've opened PR baloo/sphinx-ditaa#15 back in Apr 2018), and has this at the top:
    sphinx.ext.ditaa
    ~~~~~~~~~~~~~~~~~~~~~~~~~
    Allow ditaa-formatted graphs to by included in Sphinx-generated
    documents inline.
    :copyright: Copyright 2011 by Arthur Gautier
    :copyright: Copyright 2011 by Zenexity
    :license: BSD, see LICENSE for details.
  • the ditaa.py file in the sphinxcontrib-ditaa PyPI package has this at the top:

    sphinxcontrib.ditaa
    ~~~~~~~~~~~~~~~~~~~~~
    
    Allow ditaa commands be rendered as nice looking images
    
    See the README file for details.
    
    :author: Vadim Gubergrits <vadim.gubergrits@gmail.com>
    :license: BSD, see LICENSE for details
    
    Inspired by ``sphinxcontrib-aafig`` by Leandro Lucarella.
  • in the sphinx-contrib GitHub organization, there's also a gnuplot extension from the same author
akaihola commented 1 year ago

sdelisle25/sphinxcontrib-ditaa

diff -U0 sphinxcontrib-ditaa/sphinxcontrib sphinxcontrib-ditaa-1.0.2/sphinxcontrib | grep "^+[^+]" | wc
    136     603    5907
diff -U0 sphinxcontrib-ditaa/sphinxcontrib/ditaa.py sphinxcontrib-ditaa-1.0.2/sphinxcontrib/ditaa.py
--- sphinxcontrib-ditaa/sphinxcontrib/ditaa.py  2023-03-02 14:46:11.725318617 +0200
+++ sphinxcontrib-ditaa-1.0.2/sphinxcontrib/ditaa.py    2022-12-17 05:25:02.000000000 +0200
@@ -3,2 +3,2 @@
-    sphinx.ext.ditaa
-    ~~~~~~~~~~~~~~~~~~~~~~~~~
+    sphinxcontrib.ditaa
+    ~~~~~~~~~~~~~~~~~~~~~
@@ -6,2 +6,2 @@
-    Allow ditaa-formatted graphs to by included in Sphinx-generated
-    documents inline.
+    Allow ditaa commands be rendered as nice looking images
+    
@@ -9,3 +9,6 @@
-    :copyright: Copyright 2011 by Arthur Gautier
-    :copyright: Copyright 2011 by Zenexity
-    :license: BSD, see LICENSE for details.
+    See the README file for details.
+
+    :author: Yongping Guo <guoyoooping@163.com>
+    :license: BSD, see LICENSE for details
+
+    Inspired by ``sphinxcontrib-aafig`` by Leandro Lucarella.
@@ -14 +17 @@
-import re
+import re, os
@@ -17,0 +21 @@
+from math import ceil
@@ -28,2 +32,2 @@
-from sphinx.util.osutil import ensuredir, ENOENT, EPIPE, EINVAL
-from docutils.parsers.rst import Directive
+from sphinx.util import ensuredir, relative_uri
+#from sphinx.util.compat import Directive
@@ -31,3 +35,4 @@
-
-mapname_re = re.compile(r'<map id="(.*?)"')
-svg_dim_re = re.compile(r'<svg\swidth="(\d+)pt"\sheight="(\d+)pt"', re.M)
+def get_hashid(text,options):
+    hashkey = text.encode('utf-8') + str(options).encode('utf-8')
+    hashid = sha(hashkey).hexdigest()
+    return hashid
@@ -37,2 +42 @@
-    category = 'Ditaa error'
-
+    category = 'ditaa error'
@@ -40,2 +43,0 @@
-class ditaa(nodes.General, nodes.Element):
-    pass
@@ -43,2 +45 @@
-
-class Ditaa(Directive):
+class DitaaDirective(directives.images.Image):
@@ -46 +47 @@
-    Directive to insert ditaa markup.
+    Directive that builds plots using ditaa.
@@ -50,10 +51,21 @@
-    optional_arguments = 0
-    final_argument_whitespace = False
-
-    option_spec = {
-        'alt': directives.unchanged,
-        'inline': directives.flag,
-        'caption': directives.unchanged,
-        'options': directives.unchanged,
-    }
-
+    own_option_spec = dict( {
+        '--no-antialias': str,
+        '--background': str,
+        '--no-separation': str,
+        '--encoding': str,
+        '--html': str,
+        '--overwrite': str,
+        '--round-corners': str,
+        '--no-shadows': str,
+        '--scale': str,
+        '--transparent': str,
+        '--tabs': str,
+        '--fixed-slope': str,
+        }
+
+    )
+
+    option_spec = directives.images.Image.option_spec.copy()
+    option_spec.update(own_option_spec)
+    #print(option_spec)
+  
@@ -61,39 +73,31 @@
-        if self.arguments:
-            if self.content:
-                return [self.state_machine.reporter.warning(
-                    'Ditaa directive cannot have both content and '
-                    'a filename argument', line=self.lineno)]
-            env = self.state.document.settings.env
-            rel_filename, filename = env.relfn2path(self.arguments[0])
-            env.note_dependency(rel_filename)
-            try:
-                fp = codecs.open(filename, 'r', 'utf-8')
-                try:
-                    code_block = fp.read()
-                finally:
-                    fp.close()
-            except (IOError, OSError):
-                return [self.state_machine.reporter.warning(
-                    'External Ditaa file %r not found or reading '
-                    'it failed' % filename, line=self.lineno)]
-        else:
-            code_block = '\n'.join(self.content)
-
-        # Verify valid content
-        if not code_block.strip():
-            return [self.state_machine.reporter.warning('Ignoring "ditaa" directive without content.', line=self.lineno)]
-
-        node = ditaa()
-        node['code'] = code_block
-        node['options'] = []
-
-        if 'alt' in self.options:
-            node['alt'] = self.options['alt']
-
-        if 'caption' in self.options:
-            node['caption'] = self.options['caption']
-        node['inline'] = 'inline' in self.options
-
-        if 'options' in self.options:
-            node['options'] = self.options['options'].split()
-        return [node]
+        self.arguments = ['']
+        #print("self.options: %s" %(self.options))
+        ditaa_options = dict([(k,v) for k,v in self.options.items() 
+                                  if k in self.own_option_spec])
+
+        (image_node,) = directives.images.Image.run(self)
+        if isinstance(image_node, nodes.system_message):
+            return [image_node]
+        text = '\n'.join(self.content)
+        image_node.ditaa = dict(text=text,options=ditaa_options)
+        return [image_node]
+
+
+def render_ditaa_images(app, doctree):
+    #print("app.builder.env.docname: %s" %(app.builder.env.docname))
+    #print("app.builder.format: %s" %(app.builder.format))
+    #print(nodes)
+    #print(dir(nodes))
+    for img in doctree.traverse(nodes.image):
+        if not hasattr(img, 'ditaa'):
+            continue
+
+        text = img.ditaa['text']
+        options = img.ditaa['options']
+        try:
+            relfn, outfn = render_ditaa(app, text, options, app.builder.format, "ditaa")
+            img['uri'] = relfn
+        except DitaaError:
+            #app.builder.warn('ditaa error: ')
+            img.replace_self(nodes.literal_block(text, text))
+            continue
@@ -101 +105 @@
-def render_ditaa(self, code, options, prefix='ditaa'):
+def render_ditaa(app, code, options, format, prefix='ditaa'):
@@ -102,0 +107,14 @@
+    hashkey = code + str(options) + \
+              str(app.builder.config.ditaa) + \
+              str(app.builder.config.ditaa_args)
+    hashkey = hashkey.encode('utf-8')
+    infname = '%s-%s.%s' % (prefix, sha(hashkey).hexdigest(), "ditaa")
+    outfname = '%s-%s.%s' % (prefix, sha(hashkey).hexdigest(), app.builder.config.ditaa_output_suffix)
+
+    #rel_imgpath = (format == "html") and relative_uri(app.builder.env.docname, app.builder.imagedir) or ''
+    if format == 'html':
+        rel_imgpath = relative_uri(app.builder.env.docname, app.builder.imagedir)
+    elif format == 'latex':
+        rel_imgpath = relative_uri(app.builder.env.docname, app.builder.imagedir)
+    else:
+        app.builder.warn('gnuplot: the builder format %s is not officially supported.' % format)
@@ -104,12 +122,4 @@
-    hashkey = "".join([str(code), str(options), str(self.builder.config.ditaa), str(self.builder.config.ditaa_args)])
-    hashkey = hashkey.encode('UTF-8')
-
-    digest = sha(hashkey).hexdigest()
-    infname = '%s-%s.%s' % (prefix, digest, "ditaa")
-    outfname = '%s-%s.%s' % (prefix, digest, "png")
-
-    imgpath = self.builder.imgpath if hasattr(self.builder, 'imgpath') else ''
-    inrelfn = posixpath.join(imgpath, infname)
-    infullfn = path.join(self.builder.outdir, '_images', infname)
-    outrelfn = posixpath.join(imgpath, outfname)
-    outfullfn = path.join(self.builder.outdir, '_images', outfname)
+    infullfn = path.join(app.builder.outdir, app.builder.imagedir, infname)
+    outrelfn = posixpath.join(rel_imgpath, outfname)
+    outfullfn = path.join(app.builder.outdir, app.builder.imagedir, outfname)
+    #inrelfn = posixpath.join(relative_uri(app.builder.env.docname, app.builder.imagedir), infname)
@@ -121 +130,0 @@
-
@@ -123,3 +132,2 @@
-    if not isinstance(code, str):
-        self.builder.warn("Invalid code block type expected str type='%s'" % type(code))
-        return None, None
+    #if isinstance(code, unicode): code = code.encode('utf-8')
+    code = code.encode('utf-8')
@@ -127,9 +135,14 @@
-    ditaa_args = [self.builder.config.ditaa]
-    ditaa_args.extend(self.builder.config.ditaa_args)
-    ditaa_args.extend(options)
-    ditaa_args.extend([infullfn])
-    ditaa_args.extend([outfullfn])
-
-    # TODO: verify code encoding bytes to string
-    with open(infullfn, 'wt') as fd:
-        fd.write(code)
+    ditaa_args = [app.builder.config.ditaa]
+    ditaa_args.extend(app.builder.config.ditaa_args)
+    #print(options)
+    for item in options.keys():
+        ditaa_args.append(item)  
+        if options[item] != "None":
+            ditaa_args.append(options[item])  
+        #ditaa_args.append(options)
+    ditaa_args.extend( [infname, outfname] ) # use relative path
+    f = open(infullfn, 'wb')
+    f.write(code)
+    f.close() 
+    currpath = os.getcwd()
+    os.chdir(path.join(app.builder.outdir, app.builder.imagedir))
@@ -137,0 +151,4 @@
+        if app.builder.config.ditaa_log_enable:
+            print("rending %s in %s.rst" %(outfullfn, app.builder.env.docname))
+        #app.builder.warn(ditaa_args)
+        #print(ditaa_args)
@@ -140,3 +157,3 @@
-        if err.errno != ENOENT:  # No such file or directory
-            raise
-        self.builder.warn('ditaa command %r cannot be run (needed for ditaa '
+        if err.errno != ENOENT:   # No such file or directory
+            raise DitaaError("error")
+        app.builder.warn('ditaa command %r cannot be run (needed for ditaa '
@@ -144,2 +161,3 @@
-                          self.builder.config.ditaa)
-        self.builder._ditaa_warned_dot = True
+                          app.builder.config.ditaa)
+        app.builder._ditaa_warned_dot = True
+        os.chdir(currpath)
@@ -147,0 +166,2 @@
+    os.chdir(currpath)
+    wentWrong = False
@@ -149,4 +169,15 @@
-        stdout, stderr = p.communicate()
-    except (OSError, IOError) as err:
-        if (err.errno != EPIPE) and (err.errno != EINVAL):
-            raise
+        # Ditaa may close standard input when an error occurs,
+        # resulting in a broken pipe on communicate()
+        stdout, stderr = p.communicate(code)
+        #print(stdout.decode("utf-8"))
+    except OSError as err:
+        if err.errno != EPIPE:
+            raise DitaaError("error")
+        wentWrong = True
+    except IOError as err:
+        if err.errno != EINVAL:
+            raise DitaaError("error")
+        wentWrong = True
+    if wentWrong:
+        # in this case, read the standard output and standard error streams
+        # directly, to get the error message(s)
@@ -155 +185,0 @@
-
@@ -159 +188,0 @@
-
@@ -162,47 +190,0 @@
-
-def render_ditaa_html(self, node, code, options, prefix='ditaa',
-                    imgcls=None, alt=None):
-    try:
-        fname, _ = render_ditaa(self, code, options, prefix)
-    except DitaaError as _:
-        raise nodes.SkipNode
-
-    inline = node.get('inline', False)
-    if inline:
-        wrapper = 'span'
-    else:
-        wrapper = 'p'
-
-    self.body.append(self.starttag(node, wrapper, CLASS='ditaa'))
-    if fname is None:
-        self.body.append(self.encode(code))
-    else:
-        # nothing in image map (the lines are <map> and </map>)
-        self.body.append('<img src="%s"/>\n' %
-                         fname)
-
-    self.body.append('</%s>\n' % wrapper)
-
-    raise nodes.SkipNode
-
-
-def html_visit_ditaa(self, node):
-    render_ditaa_html(self, node, node['code'], node['options'])
-
-
-def render_ditaa_latex(self, node, code, options, prefix='ditaa'):
-    try:
-        fname, outfn = render_ditaa(self, code, options, prefix)
-    except DitaaError as exc:
-        raise nodes.SkipNode
-
-    if fname is not None:
-        self.body.append('\\par\\includegraphics{%s}\\par' % outfn)
-
-    raise nodes.SkipNode
-
-
-def latex_visit_ditaa(self, node):
-    render_ditaa_latex(self, node, node['code'], node['options'])
-
-
@@ -210,4 +192,2 @@
-    app.add_node(ditaa,
-                 html=(html_visit_ditaa, None),
-                 latex=(latex_visit_ditaa, None))
-    app.add_directive('ditaa', Ditaa)
+    app.add_directive('ditaa', DitaaDirective)
+    app.connect('doctree-read', render_ditaa_images)
@@ -215,0 +196,2 @@
+    app.add_config_value('ditaa_output_suffix', 'png', 'html')
+    app.add_config_value('ditaa_log_enable', True, 'html')
diff -U0 sphinxcontrib-ditaa/sphinxcontrib/__init__.py sphinxcontrib-ditaa-1.0.2/sphinxcontrib/__init__.py
--- sphinxcontrib-ditaa/sphinxcontrib/__init__.py   2023-03-02 14:46:11.725318617 +0200
+++ sphinxcontrib-ditaa-1.0.2/sphinxcontrib/__init__.py 2022-12-17 05:30:22.000000000 +0200
@@ -3,2 +3,2 @@
-    sphinx-ditaa contribution
-    ~~~~~~~~~~~~~~~~~~~~~~~~~
+    sphinxcontrib
+    ~~~~~~~~~~~~~
@@ -6 +6,2 @@
-    Contains Ditaa specific sphinx features not activated by default
+    This package is a namespace package that contains all extensions
+    distributed in the ``sphinx-contrib`` distribution.
@@ -8,3 +9,2 @@
-    :copyright: Copyright Arthur Gautier 2011
-    :copyright: Copyright Zenxity 2011
-    :licence: BSD
+    :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+    :license: BSD, see LICENSE for details.
@@ -11,0 +12 @@
+
akaihola commented 1 year ago

baloo/sphinx-ditaa

diff -U0 sphinx-ditaa/sphinxcontrib sphinxcontrib-ditaa-1.0.2/sphinxcontrib | grep "^+[^+]" | wc
    113     455    4919
diff -U0 sphinx-ditaa/sphinxcontrib/ditaa.py sphinxcontrib-ditaa-1.0.2/sphinxcontrib/ditaa.py
--- sphinx-ditaa/sphinxcontrib/ditaa.py 2023-03-02 14:46:22.894107629 +0200
+++ sphinxcontrib-ditaa-1.0.2/sphinxcontrib/ditaa.py    2022-12-17 05:25:02.000000000 +0200
@@ -3,2 +3,2 @@
-    sphinx.ext.ditaa
-    ~~~~~~~~~~~~~~~~~~~~~~~~~
+    sphinxcontrib.ditaa
+    ~~~~~~~~~~~~~~~~~~~~~
@@ -6,2 +6,2 @@
-    Allow ditaa-formatted graphs to by included in Sphinx-generated
-    documents inline.
+    Allow ditaa commands be rendered as nice looking images
+    
@@ -9,4 +9,4 @@
-    :copyright: Copyright 2011 by Arthur Gautier
-    :copyright: Copyright 2011 by Zenexity
-    :license: BSD, see LICENSE for details.
-"""
+    See the README file for details.
+
+    :author: Yongping Guo <guoyoooping@163.com>
+    :license: BSD, see LICENSE for details
@@ -14,2 +14,2 @@
-# for python2, to behave like python3 (all str are unicode).
-from __future__ import unicode_literals
+    Inspired by ``sphinxcontrib-aafig`` by Leandro Lucarella.
+"""
@@ -17 +17 @@
-import re
+import re, os
@@ -32,4 +32,2 @@
-from sphinx.util.osutil import ensuredir, ENOENT, EPIPE
-from sphinx.util.compat import Directive
-
-import sys
+from sphinx.util import ensuredir, relative_uri
+#from sphinx.util.compat import Directive
@@ -37,2 +35,4 @@
-mapname_re = re.compile(r'<map id="(.*?)"')
-svg_dim_re = re.compile(r'<svg\swidth="(\d+)pt"\sheight="(\d+)pt"', re.M)
+def get_hashid(text,options):
+    hashkey = text.encode('utf-8') + str(options).encode('utf-8')
+    hashid = sha(hashkey).hexdigest()
+    return hashid
@@ -42 +42 @@
-    category = 'Ditaa error'
+    category = 'ditaa error'
@@ -45,5 +45 @@
-class ditaa(nodes.General, nodes.Element):
-    pass
-
-
-class Ditaa(Directive):
+class DitaaDirective(directives.images.Image):
@@ -51 +47 @@
-    Directive to insert ditaa markup.
+    Directive that builds plots using ditaa.
@@ -55,8 +51,21 @@
-    optional_arguments = 0
-    final_argument_whitespace = False
-    option_spec = {
-        'alt': directives.unchanged,
-        'inline': directives.flag,
-        'caption': directives.unchanged,
-    }
-
+    own_option_spec = dict( {
+        '--no-antialias': str,
+        '--background': str,
+        '--no-separation': str,
+        '--encoding': str,
+        '--html': str,
+        '--overwrite': str,
+        '--round-corners': str,
+        '--no-shadows': str,
+        '--scale': str,
+        '--transparent': str,
+        '--tabs': str,
+        '--fixed-slope': str,
+        }
+
+    )
+
+    option_spec = directives.images.Image.option_spec.copy()
+    option_spec.update(own_option_spec)
+    #print(option_spec)
+  
@@ -64,35 +73,31 @@
-        if self.arguments:
-            print(self.arguments)
-            document = self.state.document
-            if self.content:
-                return [document.reporter.warning(
-                    'Ditaa directive cannot have both content and '
-                    'a filename argument', line=self.lineno)]
-            env = self.state.document.settings.env
-            rel_filename, filename = env.relfn2path(self.arguments[0])
-            env.note_dependency(rel_filename)
-            try:
-                fp = codecs.open(filename, 'r', 'utf-8')
-                try:
-                    dotcode = fp.read()
-                finally:
-                    fp.close()
-            except (IOError, OSError):
-                return [document.reporter.warning(
-                    'External Ditaa file %r not found or reading '
-                    'it failed' % filename, line=self.lineno)]
-        else:
-            dotcode = '\n'.join(self.content)
-            if not dotcode.strip():
-                return [self.state_machine.reporter.warning(
-                    'Ignoring "ditaa" directive without content.',
-                    line=self.lineno)]
-        node = ditaa()
-        node['code'] = dotcode
-        node['options'] = []
-        if 'alt' in self.options:
-            node['alt'] = self.options['alt']
-        if 'caption' in self.options:
-            node['caption'] = self.options['caption']
-        node['inline'] = 'inline' in self.options
-        return [node]
+        self.arguments = ['']
+        #print("self.options: %s" %(self.options))
+        ditaa_options = dict([(k,v) for k,v in self.options.items() 
+                                  if k in self.own_option_spec])
+
+        (image_node,) = directives.images.Image.run(self)
+        if isinstance(image_node, nodes.system_message):
+            return [image_node]
+        text = '\n'.join(self.content)
+        image_node.ditaa = dict(text=text,options=ditaa_options)
+        return [image_node]
+
+
+def render_ditaa_images(app, doctree):
+    #print("app.builder.env.docname: %s" %(app.builder.env.docname))
+    #print("app.builder.format: %s" %(app.builder.format))
+    #print(nodes)
+    #print(dir(nodes))
+    for img in doctree.traverse(nodes.image):
+        if not hasattr(img, 'ditaa'):
+            continue
+
+        text = img.ditaa['text']
+        options = img.ditaa['options']
+        try:
+            relfn, outfn = render_ditaa(app, text, options, app.builder.format, "ditaa")
+            img['uri'] = relfn
+        except DitaaError:
+            #app.builder.warn('ditaa error: ')
+            img.replace_self(nodes.literal_block(text, text))
+            continue
@@ -100 +105 @@
-def render_ditaa(self, code, options, prefix='ditaa'):
+def render_ditaa(app, code, options, format, prefix='ditaa'):
@@ -102,3 +107,4 @@
-    hashkey = code.encode('utf-8') + str(options).encode('utf-8') + \
-              str(self.builder.config.ditaa).encode('utf-8') + \
-              str(self.builder.config.ditaa_args).encode('utf-8')
+    hashkey = code + str(options) + \
+              str(app.builder.config.ditaa) + \
+              str(app.builder.config.ditaa_args)
+    hashkey = hashkey.encode('utf-8')
@@ -106 +112 @@
-    outfname = '%s-%s.%s' % (prefix, sha(hashkey).hexdigest(), "png")
+    outfname = '%s-%s.%s' % (prefix, sha(hashkey).hexdigest(), app.builder.config.ditaa_output_suffix)
@@ -108,5 +114,12 @@
-    imgpath = self.builder.imgpath if hasattr(self.builder, 'imgpath') else ''
-    inrelfn = posixpath.join(imgpath, infname)
-    infullfn = path.join(self.builder.outdir, '_images', infname)
-    outrelfn = posixpath.join(imgpath, outfname)
-    outfullfn = path.join(self.builder.outdir, '_images', outfname)
+    #rel_imgpath = (format == "html") and relative_uri(app.builder.env.docname, app.builder.imagedir) or ''
+    if format == 'html':
+        rel_imgpath = relative_uri(app.builder.env.docname, app.builder.imagedir)
+    elif format == 'latex':
+        rel_imgpath = relative_uri(app.builder.env.docname, app.builder.imagedir)
+    else:
+        app.builder.warn('gnuplot: the builder format %s is not officially supported.' % format)
+
+    infullfn = path.join(app.builder.outdir, app.builder.imagedir, infname)
+    outrelfn = posixpath.join(rel_imgpath, outfname)
+    outfullfn = path.join(app.builder.outdir, app.builder.imagedir, outfname)
+    #inrelfn = posixpath.join(relative_uri(app.builder.env.docname, app.builder.imagedir), infname)
@@ -118 +130,0 @@
-
@@ -120,13 +132,2 @@
-    # In Python 3, all strings are sequences of Unicode characters.
-    if sys.version_info < (3, 0):
-        if isinstance(code, unicode):
-            code = code.encode('utf-8')
-    else:
-        if isinstance(code, str):
-            code = code.encode('utf-8')
-
-    ditaa_args = [self.builder.config.ditaa]
-    ditaa_args.extend(self.builder.config.ditaa_args)
-    ditaa_args.extend(options)
-    ditaa_args.extend( [infullfn] )
-    ditaa_args.extend( [outfullfn] )
+    #if isinstance(code, unicode): code = code.encode('utf-8')
+    code = code.encode('utf-8')
@@ -133,0 +135,9 @@
+    ditaa_args = [app.builder.config.ditaa]
+    ditaa_args.extend(app.builder.config.ditaa_args)
+    #print(options)
+    for item in options.keys():
+        ditaa_args.append(item)  
+        if options[item] != "None":
+            ditaa_args.append(options[item])  
+        #ditaa_args.append(options)
+    ditaa_args.extend( [infname, outfname] ) # use relative path
@@ -136 +146,3 @@
-    f.close()
+    f.close() 
+    currpath = os.getcwd()
+    os.chdir(path.join(app.builder.outdir, app.builder.imagedir))
@@ -138,0 +151,4 @@
+        if app.builder.config.ditaa_log_enable:
+            print("rending %s in %s.rst" %(outfullfn, app.builder.env.docname))
+        #app.builder.warn(ditaa_args)
+        #print(ditaa_args)
@@ -142,2 +158,2 @@
-            raise
-        self.builder.warn('ditaa command %r cannot be run (needed for ditaa '
+            raise DitaaError("error")
+        app.builder.warn('ditaa command %r cannot be run (needed for ditaa '
@@ -145,2 +161,3 @@
-                          self.builder.config.ditaa)
-        self.builder._ditaa_warned_dot = True
+                          app.builder.config.ditaa)
+        app.builder._ditaa_warned_dot = True
+        os.chdir(currpath)
@@ -147,0 +165,2 @@
+
+    os.chdir(currpath)
@@ -152,0 +172 @@
+        #print(stdout.decode("utf-8"))
@@ -155 +175 @@
-            raise
+            raise DitaaError("error")
@@ -159 +179 @@
-            raise
+            raise DitaaError("error")
@@ -171,45 +190,0 @@
-
-def render_ditaa_html(self, node, code, options, prefix='ditaa',
-                    imgcls=None, alt=None):
-    try:
-        fname, outfn = render_ditaa(self, code, options, prefix)
-    except DitaaError as exc:
-        raise nodes.SkipNode
-
-    inline = node.get('inline', False)
-    if inline:
-        wrapper = 'span'
-    else:
-        wrapper = 'p'
-
-    self.body.append(self.starttag(node, wrapper, CLASS='ditaa'))
-    if fname is None:
-        self.body.append(self.encode(code))
-    else:
-        # nothing in image map (the lines are <map> and </map>)
-        self.body.append('<img src="%s"/>\n' %
-                         fname)
-
-    self.body.append('</%s>\n' % wrapper)
-    raise nodes.SkipNode
-
-
-def html_visit_ditaa(self, node):
-    render_ditaa_html(self, node, node['code'], node['options'])
-
-
-def render_ditaa_latex(self, node, code, options, prefix='ditaa'):
-    try:
-        fname, outfn = render_ditaa(self, code, options, prefix)
-    except DitaaError as exc:
-        raise nodes.SkipNode
-
-    if fname is not None:
-        self.body.append('\\par\\includegraphics{%s}\\par' % outfn)
-    raise nodes.SkipNode
-
-
-def latex_visit_ditaa(self, node):
-    render_ditaa_latex(self, node, node['code'], node['options'])
-
-
@@ -217,4 +192,2 @@
-    app.add_node(ditaa,
-                 html=(html_visit_ditaa, None),
-                 latex=(latex_visit_ditaa, None))
-    app.add_directive('ditaa', Ditaa)
+    app.add_directive('ditaa', DitaaDirective)
+    app.connect('doctree-read', render_ditaa_images)
@@ -222,0 +196,2 @@
+    app.add_config_value('ditaa_output_suffix', 'png', 'html')
+    app.add_config_value('ditaa_log_enable', True, 'html')
pohutukawa commented 1 year ago

@akaihola Any success, yet, on tracking sources down? I've just been in the same pickle (I wanted to generate variable output formats, e.g. SVG for html, but stumbled).

Might be worth putting some things together.

akaihola commented 1 year ago

@pohutukawa I haven't worked on this investigation since March. I was hoping someone would pick up where I left off if I left my findings on the table... :)