1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
|
# -*- coding: utf-8 -*-
"""
sphinxcontrib.plantuml
~~~~~~~~~~~~~~~~~~~~~~
Embed PlantUML diagrams on your documentation.
:copyright: Copyright 2010 by Yuya Nishihara <yuya@tcha.org>.
:license: BSD, see LICENSE for details.
"""
import codecs
import errno
import hashlib
import os
import re
import shlex
import shutil
import subprocess
import tempfile
from contextlib import contextmanager
from docutils import nodes
from docutils.parsers.rst import directives
from docutils.parsers.rst import Directive
import sphinx
from sphinx.errors import SphinxError
from sphinx.util import (
i18n,
logging,
)
from sphinx.util.nodes import set_source_info
from sphinx.util.osutil import (
ensuredir,
)
from sphinx.transforms.post_transforms.images import ImageConverter
try:
from PIL import Image
except ImportError:
Image = None
logger = logging.getLogger(__name__)
if os.name == 'nt':
def rename(src, dst):
try:
os.rename(src, dst)
except OSError as err:
if err.errno != errno.EEXIST:
raise
os.unlink(dst)
os.rename(src, dst)
else:
rename = os.rename
class PlantUmlError(SphinxError):
pass
class plantuml(nodes.General, nodes.Element):
pass
def align(argument):
align_values = ('left', 'center', 'right')
return directives.choice(argument, align_values)
def html_format(argument):
format_values = list(_KNOWN_HTML_FORMATS.keys())
return directives.choice(argument, format_values)
def latex_format(argument):
format_values = list(_KNOWN_LATEX_FORMATS.keys())
return directives.choice(argument, format_values)
class UmlDirective(Directive):
"""Directive to insert PlantUML markup
Example::
.. uml::
:alt: Alice and Bob
Alice -> Bob: Hello
Alice <- Bob: Hi
"""
has_content = True
required_arguments = 0
optional_arguments = 1
final_argument_whitespace = True # allow whitespace in arguments[-1]
option_spec = {
'alt': directives.unchanged,
'align': align,
'caption': directives.unchanged,
'height': directives.length_or_unitless,
'html_format': html_format,
'latex_format': latex_format,
'name': directives.unchanged,
'scale': directives.percentage,
'width': directives.length_or_percentage_or_unitless,
'max-width': directives.length_or_percentage_or_unitless,
}
def run(self):
warning = self.state.document.reporter.warning
env = self.state.document.settings.env
if self.arguments and self.content:
return [
warning(
'uml directive cannot have both content and ' 'a filename argument',
line=self.lineno,
)
]
if self.arguments:
fn = i18n.search_image_for_language(self.arguments[0], env)
relfn, absfn = env.relfn2path(fn)
env.note_dependency(relfn)
try:
umlcode = _read_utf8(absfn)
except (IOError, UnicodeDecodeError) as err:
return [
warning(
'PlantUML file "%s" cannot be read: %s' % (fn, err),
line=self.lineno,
)
]
else:
relfn = env.doc2path(env.docname, base=None)
umlcode = '\n'.join(self.content)
node = plantuml(self.block_text, **self.options)
node['uml'] = umlcode
node['incdir'] = os.path.dirname(relfn)
node['filename'] = os.path.split(relfn)[1]
# XXX maybe this should be moved to _visit_plantuml functions. it
# seems wrong to insert "figure" node by "plantuml" directive.
if 'caption' in self.options or 'align' in self.options:
node = nodes.figure('', node)
if 'align' in self.options:
node['align'] = self.options['align']
if 'caption' in self.options:
inodes, messages = self.state.inline_text(
self.options['caption'], self.lineno
)
caption_node = nodes.caption(self.options['caption'], '', *inodes)
caption_node.extend(messages)
set_source_info(self, caption_node)
node += caption_node
self.add_name(node)
if 'html_format' in self.options:
node['html_format'] = self.options['html_format']
if 'latex_format' in self.options:
node['latex_format'] = self.options['latex_format']
return [node]
def _read_utf8(filename):
fp = codecs.open(filename, 'rb', 'utf-8')
try:
return fp.read()
finally:
fp.close()
def hash_plantuml_node(node):
h = hashlib.sha1()
# may include different file relative to doc
h.update(node['incdir'].encode('utf-8'))
h.update(b'\0')
h.update(node['uml'].encode('utf-8'))
return h.hexdigest()
def generate_name(self, node, fileformat):
key = hash_plantuml_node(node)
fname = 'plantuml-%s.%s' % (key, fileformat)
imgpath = getattr(self.builder, 'imgpath', None)
if imgpath:
return (
'/'.join((self.builder.imgpath, fname)),
os.path.join(self.builder.outdir, '_images', fname),
)
else:
return fname, os.path.join(self.builder.outdir, fname)
def _ntunquote(s):
if s.startswith('"') and s.endswith('"'):
return s[1:-1]
return s
def _split_cmdargs(args):
if isinstance(args, (tuple, list)):
return list(args)
if os.name == 'nt':
return list(map(_ntunquote, shlex.split(args, posix=False)))
else:
return shlex.split(args, posix=True)
_ARGS_BY_FILEFORMAT = {
'eps': ['-teps'],
'png': [],
'svg': ['-tsvg'],
'txt': ['-ttxt'],
'latex': ['-tlatex:nopreamble'],
}
def generate_plantuml_args(self, node, fileformat):
args = _split_cmdargs(self.builder.config.plantuml)
args.extend(['-pipe', '-charset', 'utf-8'])
args.extend(['-filename', node['filename']])
args.extend(_ARGS_BY_FILEFORMAT[fileformat])
return args
def render_plantuml(self, node, fileformat):
refname, outfname = generate_name(self, node, fileformat)
if os.path.exists(outfname):
return refname, outfname # don't regenerate
cachefname = self.builder.plantuml_builder.render(node, fileformat)
ensuredir(os.path.dirname(outfname))
# TODO: optionally do symlink/link
shutil.copyfile(cachefname, outfname)
return refname, outfname
def render_plantuml_inline(self, node, fileformat):
absincdir = os.path.join(self.builder.srcdir, node['incdir'])
try:
p = subprocess.Popen(
generate_plantuml_args(self, node, fileformat),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=absincdir,
)
except OSError as err:
if err.errno != errno.ENOENT:
raise
raise PlantUmlError(
'plantuml command %r cannot be run' % self.builder.config.plantuml
)
sout, serr = p.communicate(node['uml'].encode('utf-8'))
if p.returncode != 0:
raise PlantUmlError('error while running plantuml\n\n%s' % serr)
return sout.decode('utf-8')
class PlantumlBuilder(object):
def __init__(self, builder):
# for compatibility with existing functions which expect self.builder
# TODO: remove self.builder
self.builder = builder
self.batch_size = builder.config.plantuml_batch_size
self.cache_dir = os.path.join(
builder.outdir, builder.config.plantuml_cache_path
)
self._base_cmdargs = _split_cmdargs(builder.config.plantuml)
self._base_cmdargs.extend(['-charset', 'utf-8'])
self.image_formats = []
if builder.format == 'html':
fmt = builder.config.plantuml_output_format
if fmt != 'none':
fileformats, _gettag = _lookup_html_format(fmt)
self.image_formats = list(fileformats)
elif builder.format == 'latex':
fmt = builder.config.plantuml_latex_output_format
if fmt != 'none':
fileformat, _postproc = _lookup_latex_format(fmt)
self.image_formats = [fileformat]
self._known_keys = set()
self._pending_keys = []
def collect_nodes(self, doctree):
for node in doctree.traverse(plantuml):
key = hash_plantuml_node(node)
if key in self._known_keys:
continue
self._known_keys.add(key)
doc = node['uml'].encode('utf-8')
if b'!include' in doc or b'%filename' in doc:
# Heuristic to work around the path/filename issue. There's no
# easy way to specify the cwd of the doc without using -pipe.
continue
outdir = os.path.join(self.cache_dir, key[:2])
outfbase = os.path.join(outdir, key)
if all(
os.path.exists('%s.%s' % (outfbase, sfx))
for sfx in ['puml'] + self.image_formats
):
continue
ensuredir(outdir)
with open(outfbase + '.puml', 'wb') as f:
# @startuml/enduml is mandatory in non-pipe mode. For backward
# compatibility, we do wrap the document only if @startXYZ is
# not specified.
started = doc.lstrip().startswith(b'@start')
if not started:
f.write(b'@startuml\n')
f.write(doc)
if not started:
f.write(b'\n@enduml\n')
self._pending_keys.append(key)
def render_batches(self):
if sphinx.version_info[:2] >= (6, 1):
from sphinx.util.display import progress_message
else:
from sphinx.util import progress_message
pending_keys = sorted(self._pending_keys)
for fileformat in self.image_formats:
for i in range(0, len(pending_keys), self.batch_size):
keys = pending_keys[i : i + self.batch_size]
with progress_message(
'rendering plantuml diagrams [%d..%d/%d]'
% (i, i + len(keys), len(pending_keys))
):
self._render_files(keys, fileformat)
del self._pending_keys[:]
def _render_files(self, keys, fileformat):
cmdargs = self._base_cmdargs[:]
cmdargs.extend(_ARGS_BY_FILEFORMAT[fileformat])
cmdargs.extend(os.path.join(k[:2], '%s.puml' % k) for k in keys)
try:
p = subprocess.Popen(cmdargs, stderr=subprocess.PIPE, cwd=self.cache_dir)
except OSError as err:
if err.errno != errno.ENOENT:
raise
raise PlantUmlError(
'plantuml command %r cannot be run' % self.builder.config.plantuml
)
serr = p.communicate()[1]
if p.returncode != 0:
if self.builder.config.plantuml_syntax_error_image:
logger.warning(
'error while running plantuml\n\n%s' % serr, type='plantuml'
)
else:
raise PlantUmlError('error while running plantuml\n\n%s' % serr)
def render(self, node, fileformat):
key = hash_plantuml_node(node)
outdir = os.path.join(self.cache_dir, key[:2])
basename = '%s.%s' % (key, fileformat)
outfname = os.path.join(outdir, basename)
if os.path.exists(outfname):
return outfname
ensuredir(outdir)
absincdir = os.path.join(self.builder.srcdir, node['incdir'])
# TODO: delete_on_close can be used on Python 3.12+
with tempfile.NamedTemporaryFile(
prefix=basename + '.new', dir=outdir, delete=False
) as f:
try:
p = subprocess.Popen(
generate_plantuml_args(self, node, fileformat),
stdout=f,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=absincdir,
)
except OSError as err:
if err.errno != errno.ENOENT:
raise
raise PlantUmlError(
'plantuml command %r cannot be run' % self.builder.config.plantuml
)
serr = p.communicate(node['uml'].encode('utf-8'))[1]
if p.returncode != 0:
if self.builder.config.plantuml_syntax_error_image:
logger.warning(
'error while running plantuml\n\n%s' % serr,
location=node,
type='plantuml',
)
else:
raise PlantUmlError('error while running plantuml\n\n%s' % serr)
# inherit dir mode since temp file isn't world-readable by default.
if os.name == 'posix':
os.fchmod(f.fileno(), os.lstat(outdir).st_mode & 0o666)
f.close()
rename(f.name, outfname)
return outfname
def _render_batches_on_vist(self):
self.builder.plantuml_builder.render_batches()
def _get_png_tag(self, fnames, node):
refname, outfname = fnames['png']
alt = node.get('alt', node['uml'])
# mimic StandaloneHTMLBuilder.post_process_images(). maybe we should
# process images prior to html_vist.
scale_attrs = [k for k in ('scale', 'width', 'height') if k in node]
if scale_attrs and Image is None:
logger.warning(
(
'plantuml: unsupported scaling attributes: %s '
'(install PIL or Pillow)' % ', '.join(scale_attrs)
),
location=node,
type='plantuml',
)
if not scale_attrs or Image is None:
return '<img src="%s" alt="%s"/>\n' % (self.encode(refname), self.encode(alt))
scale = node.get('scale', 100)
styles = []
# Width/Height
vu = re.compile(r"(?P<value>\d+)\s*(?P<units>[a-zA-Z%]+)?")
for a in ['width', 'height']:
if a not in node:
continue
m = vu.match(node[a])
if not m:
raise PlantUmlError('Invalid %s' % a)
m = m.groupdict()
w = int(m['value'])
wu = m['units'] if m['units'] else 'px'
styles.append('%s: %s%s' % (a, w * scale / 100, wu))
# Add physical size to assist rendering (defaults)
if not styles:
# the image may be corrupted if platuml isn't configured correctly,
# which isn't a hard error.
try:
im = Image.open(outfname)
im.load()
styles.extend(
'%s: %s%s' % (a, w * scale / 100, 'px')
for a, w in zip(['width', 'height'], im.size)
)
except (IOError, OSError) as err:
logger.warning(
'plantuml: failed to get image size: %s' % err,
location=node,
type='plantuml',
)
return '<a href="%s"><img src="%s" alt="%s" style="%s"/>' '</a>\n' % (
self.encode(refname),
self.encode(refname),
self.encode(alt),
self.encode('; '.join(styles)),
)
def _get_svg_style(fname):
f = codecs.open(fname, 'r', 'utf-8')
try:
for l in f:
m = re.search(r'<svg\b([^<>]+)', l)
if m:
attrs = m.group(1)
break
else:
return
finally:
f.close()
m = re.search(r'\bstyle=[\'"]([^\'"]+)', attrs)
if not m:
return
return m.group(1)
def _svg_get_style_str(node, outfname):
width_height_styles = [
"%s:%s" % (key, val)
for key, val in node.attributes.items()
if key in ['width', 'height', 'max-width']
]
if width_height_styles:
style_str = '; '.join(width_height_styles)
else:
style_str = _get_svg_style(outfname) or ''
return style_str
def _get_svg_tag(self, fnames, node):
refname, outfname = fnames['svg']
style_str = _svg_get_style_str(node, outfname)
return '\n'.join(
[
# copy width/height style from <svg> tag, so that <object> area
# has enough space.
'<object data="%s" type="image/svg+xml" style="%s">'
% (self.encode(refname), style_str),
_get_png_tag(self, fnames, node),
'</object>',
]
)
def _get_svg_img_tag(self, fnames, node):
refname, outfname = fnames['svg']
alt = node.get('alt', node['uml'])
return '<img src="%s" alt="%s"/>' % (self.encode(refname), self.encode(alt))
def _get_svg_obj_tag(self, fnames, node):
refname, outfname = fnames['svg']
# copy width/height style from <svg> tag, so that <object> area
# has enough space.
return '<object data="%s" type="image/svg+xml" style="%s"></object>' % (
self.encode(refname),
_get_svg_style(outfname) or '',
)
_KNOWN_HTML_FORMATS = {
'png': (('png',), _get_png_tag),
'svg': (('png', 'svg'), _get_svg_tag),
'svg_img': (('svg',), _get_svg_img_tag),
'svg_obj': (('svg',), _get_svg_obj_tag),
}
def _lookup_html_format(fmt):
try:
return _KNOWN_HTML_FORMATS[fmt]
except KeyError:
raise PlantUmlError(
'plantuml_output_format must be one of %s, but is %r'
% (', '.join(map(repr, _KNOWN_HTML_FORMATS)), fmt)
)
@contextmanager
def _prepare_html_render(self, fmt, node):
if fmt == 'none':
raise nodes.SkipNode
try:
yield _lookup_html_format(fmt)
except PlantUmlError as err:
logger.warning(str(err), location=node, type='plantuml')
raise nodes.SkipNode
def html_visit_plantuml(self, node):
_render_batches_on_vist(self)
if 'html_format' in node:
fmt = node['html_format']
else:
fmt = self.builder.config.plantuml_output_format
with _prepare_html_render(self, fmt, node) as (fileformats, gettag):
# fnames: {fileformat: (refname, outfname), ...}
fnames = dict((e, render_plantuml(self, node, e)) for e in fileformats)
self.body.append(self.starttag(node, 'p', CLASS='plantuml'))
self.body.append(gettag(self, fnames, node))
self.body.append('</p>\n')
raise nodes.SkipNode
def _convert_eps_to_pdf(self, refname, fname):
args = _split_cmdargs(self.builder.config.plantuml_epstopdf)
args.append(fname)
try:
try:
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError as err:
# workaround for missing shebang of epstopdf script
if err.errno != getattr(errno, 'ENOEXEC', 0):
raise
p = subprocess.Popen(
['bash'] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
except OSError as err:
if err.errno != 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%s' % serr)
return refname[:-4] + '.pdf', fname[:-4] + '.pdf'
def _convert_svg_to_pdf_default(node, refname, fname):
raise PlantUmlError('no ImageConverter for svg to pdf was found')
def _svg_to_pdf_init(app):
for transform in app.registry.get_post_transforms():
if issubclass(transform, ImageConverter):
for source_type, target_type in transform.conversion_rules:
if 'svg' in source_type and 'pdf' in target_type:
def _convert_svg_to_pdf(node, refname, fname, transform=transform):
transform(node.document).convert(fname, fname[:-4] + '.pdf')
return (refname[:-4] + '.pdf', fname[:-4] + '.pdf')
_KNOWN_LATEX_FORMATS['pdf'] = ('svg', _convert_svg_to_pdf)
_KNOWN_LATEX_FORMATS['svg_pdf'] = ('svg', _convert_svg_to_pdf)
return
_KNOWN_LATEX_FORMATS = {
'eps': ('eps', lambda self, refname, fname: (refname, fname)),
'pdf': ('eps', _convert_eps_to_pdf),
'eps_pdf': ('eps', _convert_eps_to_pdf),
'svg_pdf': ('svg', _convert_svg_to_pdf_default),
'png': ('png', lambda self, refname, fname: (refname, fname)),
'tikz': ('latex', lambda self, refname, fname: (refname, fname)),
}
def _lookup_latex_format(fmt):
try:
return _KNOWN_LATEX_FORMATS[fmt]
except KeyError:
raise PlantUmlError(
'plantuml_latex_output_format must be one of %s, but is %r'
% (', '.join(map(repr, _KNOWN_LATEX_FORMATS)), fmt)
)
def _latex_adjustbox_options(self, node): # noqa: C901
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))
if 'max-width' in node:
if 'scale' in node:
w = self.latex_image_length(node['max-width'], node['scale'])
else:
w = self.latex_image_length(node['max-width'])
if w:
adjustbox_options.append('max width=%s' % w)
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:
fmt = node['latex_format']
else:
fmt = self.builder.config.plantuml_latex_output_format
if fmt == 'none':
raise nodes.SkipNode
try:
fileformat, postproc = _lookup_latex_format(fmt)
refname, outfname = render_plantuml(self, node, fileformat)
refname, outfname = postproc(self, refname, outfname)
except PlantUmlError as err:
logger.warning(str(err), location=node, type='plantuml')
raise nodes.SkipNode
if fmt == 'tikz':
_latex_add_package(self, 'tikz')
base, ext = os.path.splitext(refname)
input_macro = '\\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)
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):
pass
_KNOWN_CONFLUENCE_FORMATS = [
'png',
'svg',
]
def confluence_visit_plantuml(self, node):
_render_batches_on_vist(self)
fmt = self.builder.config.plantuml_output_format
if fmt == 'none':
raise nodes.SkipNode
if fmt not in _KNOWN_CONFLUENCE_FORMATS:
raise PlantUmlError(
'plantuml_output_format must be one of %s, but is %r'
% (', '.join(map(repr, _KNOWN_CONFLUENCE_FORMATS)), fmt)
)
_, outfname = render_plantuml(self, node, fmt)
# put node representing rendered image
img_node = nodes.image(uri=outfname, alt=node.get('alt', node['uml']))
node.replace_self(img_node)
self.visit_image(img_node)
def confluence_depart_plantuml(self, node):
pass
def text_visit_plantuml(self, node):
_render_batches_on_vist(self)
try:
text = render_plantuml_inline(self, node, 'txt')
except PlantUmlError as err:
logger.warning(str(err), location=node, type='plantuml')
text = node['uml'] # fall back to uml text, which is still readable
self.new_state()
self.add_text(text)
self.end_state()
raise nodes.SkipNode
def pdf_visit_plantuml(self, node):
_render_batches_on_vist(self)
try:
_, outfname = render_plantuml(self, node, 'svg')
except PlantUmlError as err:
logger.warning(str(err), location=node, type='plantuml')
raise nodes.SkipNode
rep = nodes.image(uri=outfname, alt=node.get('alt', node['uml']))
node.parent.replace(node, rep)
def unsupported_visit_plantuml(self, node):
logger.warning(
'plantuml: unsupported output format (node skipped)',
location=node,
type='plantuml',
)
raise nodes.SkipNode
_NODE_VISITORS = {
'html': (html_visit_plantuml, None),
'latex': (latex_visit_plantuml, latex_depart_plantuml),
'man': (unsupported_visit_plantuml, None), # TODO
'texinfo': (unsupported_visit_plantuml, None), # TODO
'text': (text_visit_plantuml, None),
'confluence': (confluence_visit_plantuml, confluence_depart_plantuml),
'singleconfluence': (confluence_visit_plantuml, confluence_depart_plantuml),
}
def _on_builder_inited(app):
app.builder.plantuml_builder = PlantumlBuilder(app.builder)
def _on_doctree_read(app, doctree):
# Collect as many static nodes as possible prior to start building.
if app.builder.plantuml_builder.batch_size > 1:
app.builder.plantuml_builder.collect_nodes(doctree)
def _on_doctree_resolved(app, doctree, docname):
# Dynamically generated nodes will be collected here, which will be
# batched at node visitor. Since 'doctree-resolved' and node visits
# can be intermixed, there's no way to batch rendering of dynamic nodes
# at once.
if app.builder.plantuml_builder.batch_size > 1:
app.builder.plantuml_builder.collect_nodes(doctree)
def setup(app):
app.add_node(plantuml, **_NODE_VISITORS)
app.add_directive('uml', UmlDirective)
app.add_directive('plantuml', UmlDirective)
try:
app.add_config_value('plantuml', 'plantuml', 'html', types=(str, tuple, list))
except TypeError:
# Sphinx < 1.4?
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', '')
app.add_config_value('plantuml_syntax_error_image', False, '')
app.add_config_value('plantuml_cache_path', '_plantuml', '')
app.add_config_value('plantuml_batch_size', 1, '')
app.connect('builder-inited', _on_builder_inited)
app.connect('builder-inited', _svg_to_pdf_init)
app.connect('doctree-read', _on_doctree_read)
app.connect('doctree-resolved', _on_doctree_resolved)
# imitate what app.add_node() does
if 'rst2pdf.pdfbuilder' in app.config.extensions:
from rst2pdf.pdfbuilder import PDFTranslator as translator
setattr(translator, 'visit_' + plantuml.__name__, pdf_visit_plantuml)
return {'parallel_read_safe': True}
|