Skip to content

Commit

Permalink
Initial attempt to add TikZ
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-burridge-cfh committed Feb 24, 2022
1 parent e511cc7 commit d6386d9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
22 changes: 16 additions & 6 deletions sphinxcontrib/plantuml.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def _split_cmdargs(args):
'png': [],
'svg': ['-tsvg'],
'txt': ['-ttxt'],
'latex': ['-tlatex:nopreamble'],
}


Expand Down Expand Up @@ -541,6 +542,7 @@ def _convert_eps_to_pdf(self, refname, fname):
'eps': ('eps', lambda self, refname, fname: (refname, fname)),
'pdf': ('eps', _convert_eps_to_pdf),
'png': ('png', lambda self, refname, fname: (refname, fname)),
'tikz': ('latex', lambda self, refname, fname: (refname, fname)),
}


Expand Down Expand Up @@ -569,12 +571,20 @@ def latex_visit_plantuml(self, node):
logger.warning(str(err))
raise nodes.SkipNode

# put node representing rendered image
img_node = nodes.image(uri=refname, **node.attributes)
img_node.delattr('uml')
if not img_node.hasattr('alt'):
img_node['alt'] = node['uml']
node.append(img_node)
if fmt == 'tikz':
package = '\\usepackage{tikz}'
if package not in self.elements['preamble']:
self.elements['preamble'] += package + '\n'
base, ext = os.path.splitext(refname)

self.body.append('\\input{{%s}%s}' % (base, ext))
else:
# put node representing rendered image
img_node = nodes.image(uri=refname, **node.attributes)
img_node.delattr('uml')
if not img_node.hasattr('alt'):
img_node['alt'] = node['uml']
node.append(img_node)


def latex_depart_plantuml(self, node):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,24 @@ def test_buildlatex_simple_with_eps():
assert content[1][2:] == b'Hello'


@with_runsphinx('latex', plantuml_latex_output_format='tikz')
def test_buildlatex_simple_with_tikz():
"""Generate simple LaTeX with TikZ
.. uml::
Hello
"""
files = glob.glob(os.path.join(_outdir, 'plantuml-*.latex'))
assert len(files) == 1
assert re.search(br'\\(sphinx)?input\{+plantuml-',
readfile('plantuml_fixture.tex'))

content = readfile(files[0]).splitlines()
assert b'-tlatex:nopreamble' in content[0]
assert content[1][2:] == b'Hello'


@with_runsphinx('latex', plantuml_latex_output_format='pdf')
def test_buildlatex_simple_with_pdf():
"""Generate simple LaTeX with PDF
Expand Down

0 comments on commit d6386d9

Please sign in to comment.