From d6386d961cc20ce6e6accd1c93ec9311a277bc4e Mon Sep 17 00:00:00 2001 From: "jack.burridge" Date: Thu, 24 Feb 2022 19:41:11 +0000 Subject: [PATCH 1/2] Initial attempt to add TikZ --- sphinxcontrib/plantuml.py | 22 ++++++++++++++++------ tests/test_functional.py | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/sphinxcontrib/plantuml.py b/sphinxcontrib/plantuml.py index b3f891e..a20481a 100644 --- a/sphinxcontrib/plantuml.py +++ b/sphinxcontrib/plantuml.py @@ -199,6 +199,7 @@ def _split_cmdargs(args): 'png': [], 'svg': ['-tsvg'], 'txt': ['-ttxt'], + 'latex': ['-tlatex:nopreamble'], } @@ -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)), } @@ -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): diff --git a/tests/test_functional.py b/tests/test_functional.py index 6eeca75..587091e 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -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 From dd941835aeaf146a28e6c48008fa4e65f2604124 Mon Sep 17 00:00:00 2001 From: "jack.burridge" Date: Fri, 25 Feb 2022 11:06:04 +0000 Subject: [PATCH 2/2] Add adjustbox to change width, height and scale --- README.rst | 1 + sphinxcontrib/plantuml.py | 44 +++++++++++++++++++++++++++++++++++---- tests/test_functional.py | 41 +++++++++++++++++++++++++++++++++++- 3 files changed, 81 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 461509e..2fdb69c 100644 --- a/README.rst +++ b/README.rst @@ -92,6 +92,7 @@ plantuml_latex_output_format :eps: generate .eps (not supported by `pdflatex`) :pdf: generate .eps and convert it to .pdf (requires `epstopdf`) :png: generate .png + :tikz: generate .latex in the TikZ format :none: do not generate any images (ignore uml directive) Because embedded png looks pretty bad, it is recommended to choose `pdf` diff --git a/sphinxcontrib/plantuml.py b/sphinxcontrib/plantuml.py index a20481a..7766494 100644 --- a/sphinxcontrib/plantuml.py +++ b/sphinxcontrib/plantuml.py @@ -555,6 +555,36 @@ def _lookup_latex_format(fmt): % (', '.join(map(repr, _KNOWN_LATEX_FORMATS)), fmt)) +def _latex_adjustbox_options(self, node): + adjustbox_options = [] + if 'width' in node: + if 'scale' in node: + w = self.latex_image_length(node['width'], node['scale']) + else: + w = self.latex_image_length(node['width']) + if w: + adjustbox_options.append('width=%s' % w) + if 'height' in node: + if 'scale' in node: + h = self.latex_image_length(node['height'], node['scale']) + else: + h = self.latex_image_length(node['height']) + if h: + adjustbox_options.append('height=%s' % h) + if 'scale' in node: + if not adjustbox_options: + adjustbox_options.append('scale=%s' + % (float(node['scale']) / 100.0)) + return adjustbox_options + + +def _latex_add_package(self, package): + # TODO: Currently modifying the preamble to add a package, there may be a cleaner solution + package = '\\usepackage{%s}' % (package,) + if package not in self.elements['preamble']: + self.elements['preamble'] += package + '\n' + + def latex_visit_plantuml(self, node): _render_batches_on_vist(self) if 'latex_format' in node: @@ -572,12 +602,18 @@ def latex_visit_plantuml(self, node): raise nodes.SkipNode if fmt == 'tikz': - package = '\\usepackage{tikz}' - if package not in self.elements['preamble']: - self.elements['preamble'] += package + '\n' + _latex_add_package(self, 'tikz') + base, ext = os.path.splitext(refname) + input_macro = '\\input{{%s}%s}' % (base, ext) - self.body.append('\\input{{%s}%s}' % (base, ext)) + adjustbox_options = _latex_adjustbox_options(self, node) + if adjustbox_options: + _latex_add_package(self, 'adjustbox') + options = ','.join(adjustbox_options) + self.body.append('\\adjustbox{%s}{%s}' % (options, input_macro)) + else: + self.body.append(input_macro) else: # put node representing rendered image img_node = nodes.image(uri=refname, **node.attributes) diff --git a/tests/test_functional.py b/tests/test_functional.py index 587091e..77cce9b 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -262,7 +262,7 @@ def test_buildlatex_simple_with_tikz(): """ files = glob.glob(os.path.join(_outdir, 'plantuml-*.latex')) assert len(files) == 1 - assert re.search(br'\\(sphinx)?input\{+plantuml-', + assert re.search(br'\\input\{+plantuml-', readfile('plantuml_fixture.tex')) content = readfile(files[0]).splitlines() @@ -270,6 +270,45 @@ def test_buildlatex_simple_with_tikz(): assert content[1][2:] == b'Hello' +@with_runsphinx('latex', plantuml_latex_output_format='tikz') +def test_buildlatex_simple_scale_with_tikz(): + """Generate simple LaTeX with TikZ + + .. uml:: + :scale: 20% + + Hello + """ + assert re.search(br'\\adjustbox\{scale=0.2\}\{\\input\{+plantuml-', + readfile('plantuml_fixture.tex')) + + +@with_runsphinx('latex', plantuml_latex_output_format='tikz') +def test_buildlatex_simple_width_with_tikz(): + """Generate simple LaTeX with TikZ + + .. uml:: + :width: 50mm + + Hello + """ + assert re.search(br'\\adjustbox\{width=50mm\}\{\\input\{+plantuml-', + readfile('plantuml_fixture.tex')) + + +@with_runsphinx('latex', plantuml_latex_output_format='tikz') +def test_buildlatex_simple_height_with_tikz(): + """Generate simple LaTeX with TikZ + + .. uml:: + :height: 50mm + + Hello + """ + assert re.search(br'\\adjustbox\{height=50mm\}\{\\input\{+plantuml-', + readfile('plantuml_fixture.tex')) + + @with_runsphinx('latex', plantuml_latex_output_format='pdf') def test_buildlatex_simple_with_pdf(): """Generate simple LaTeX with PDF