Commits

Yuya Nishihara  committed cd1013a

plantuml: add option to generate pdf image for latex output

  • Participants
  • Parent commits 347a0de

Comments (0)

Files changed (4)

File plantuml/README

   Type of output image for LaTeX renderer. (default: 'png')
 
   :eps: generate .eps (not supported by `pdflatex`)
+  :pdf: generate .eps and convert it to .pdf (requires `epstopdf`)
   :png: generate .png
+
+  Because embedded png looks pretty bad, it is recommended to choose `pdf`
+  for `pdflatex` or `eps` for `platex`.
+
+plantuml_epstopdf
+  Path to epstopdf executable. (default: 'epstopdf')

File plantuml/sphinxcontrib/plantuml.py

     self.body.append('</p>\n')
     raise nodes.SkipNode
 
+def _convert_eps_to_pdf(self, refname, fname):
+    if isinstance(self.builder.config.plantuml_epstopdf, basestring):
+        args = shlex.split(self.builder.config.plantuml_epstopdf)
+    else:
+        args = list(self.builder.config.plantuml_epstopdf)
+    args.append(fname)
+    try:
+        p = subprocess.Popen(args, stdout=subprocess.PIPE,
+                             stderr=subprocess.PIPE)
+    except OSError, err:
+        if err.errno != ENOENT:
+            raise
+        raise PlantUmlError('epstopdf command %r cannot be run'
+                            % self.builder.config.plantuml_epstopdf)
+    serr = p.communicate()[1]
+    if p.returncode != 0:
+        raise PlantUmlError('error while running epstopdf\n\n' + serr)
+    return refname[:-4] + '.pdf', fname[:-4] + '.pdf'
+
 _KNOWN_LATEX_FORMATS = {
-    'eps': ('eps',),
-    'png': ('png',),
+    'eps': ('eps', lambda self, refname, fname: (refname, fname)),
+    'pdf': ('eps', _convert_eps_to_pdf),
+    'png': ('png', lambda self, refname, fname: (refname, fname)),
     }
 
 def latex_visit_plantuml(self, node):
     try:
         format = self.builder.config.plantuml_latex_output_format
         try:
-            fileformat, = _KNOWN_LATEX_FORMATS[format]
+            fileformat, postproc = _KNOWN_LATEX_FORMATS[format]
         except KeyError:
             raise PlantUmlError(
                 'plantuml_latex_output_format must be one of %s, but is %r'
                 % (', '.join(map(repr, _KNOWN_LATEX_FORMATS)), format))
-        refname, _outfname = render_plantuml(self, node, fileformat)
+        refname, outfname = render_plantuml(self, node, fileformat)
+        refname, outfname = postproc(self, refname, outfname)
     except PlantUmlError, err:
         self.builder.warn(str(err))
         raise nodes.SkipNode
     app.add_directive('uml', UmlDirective)
     app.add_config_value('plantuml', 'plantuml', 'html')
     app.add_config_value('plantuml_output_format', 'png', 'html')
+    app.add_config_value('plantuml_epstopdf', 'epstopdf', '')
     app.add_config_value('plantuml_latex_output_format', 'png', '')

File plantuml/tests/fakeepstopdf.py

+#!/usr/bin/env python
+import sys
+assert len(sys.argv) > 1
+f = open(sys.argv[-1][:-4] + '.pdf', 'w')
+try:
+    f.write(' '.join(sys.argv[1:]))
+finally:
+    f.close()

File plantuml/tests/test_functional.py

 
 _fixturedir = os.path.join(os.path.dirname(__file__), 'fixture')
 _fakecmd = os.path.join(os.path.dirname(__file__), 'fakecmd.py')
+_fakeepstopdf = os.path.join(os.path.dirname(__file__), 'fakeepstopdf.py')
 
 def setup():
     global _tempdir, _srcdir, _outdir
     app.build()
 
 def with_runsphinx(builder, **kwargs):
-    confoverrides = {'plantuml': _fakecmd}
+    confoverrides = {'plantuml': _fakecmd, 'plantuml_epstopdf': _fakeepstopdf}
     confoverrides.update(kwargs)
     def wrapfunc(func):
         def test():
     content = readfile(files[0]).splitlines()
     assert '-teps' in content[0]
     assert_equals('Hello', content[1])
+
+@with_runsphinx('latex', plantuml_latex_output_format='pdf')
+def test_buildlatex_simple_with_pdf():
+    """Generate simple LaTeX with PDF
+
+    .. uml::
+
+       Hello
+    """
+    epsfiles = glob.glob(os.path.join(_outdir, 'plantuml-*.eps'))
+    pdffiles = glob.glob(os.path.join(_outdir, 'plantuml-*.pdf'))
+    assert len(epsfiles) == 1
+    assert len(pdffiles) == 1
+    assert r'\includegraphics{plantuml-' in readfile('plantuml_fixture.tex')
+
+    epscontent = readfile(epsfiles[0]).splitlines()
+    assert '-teps' in epscontent[0]
+    assert_equals('Hello', epscontent[1])
+    pdfcontent = readfile(pdffiles[0]).splitlines()
+    assert os.path.basename(epsfiles[0]) in pdfcontent[0]