Skip to content

Commit

Permalink
Improve sphinx warnings
Browse files Browse the repository at this point in the history
This commit intends to improve the warning emitted by the extension; to include
source mapping to the error origin, and set the type for all warning (this means
e.g. you can suppress them:
https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-suppress_warnings)
  • Loading branch information
chrisjsewell authored and yuja committed Aug 25, 2023
1 parent 8eda178 commit 299b01b
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions sphinxcontrib/plantuml.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ def _render_files(self, keys, fileformat):
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)
logger.warning(
'error while running plantuml\n\n%s' % serr, type='plantuml'
)
else:
raise PlantUmlError('error while running plantuml\n\n%s' % serr)

Expand Down Expand Up @@ -384,7 +386,11 @@ def render(self, node, fileformat):
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)
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)

Expand All @@ -408,7 +414,9 @@ def _get_png_tag(self, fnames, node):
(
'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))
Expand Down Expand Up @@ -441,7 +449,11 @@ def _get_png_tag(self, fnames, node):
for a, w in zip(['width', 'height'], im.size)
)
except (IOError, OSError) as err:
logger.warning('plantuml: failed to get image size: %s' % 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),
Expand Down Expand Up @@ -533,14 +545,14 @@ def _lookup_html_format(fmt):


@contextmanager
def _prepare_html_render(self, fmt):
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))
logger.warning(str(err), location=node, type='plantuml')
raise nodes.SkipNode


Expand All @@ -551,7 +563,7 @@ def html_visit_plantuml(self, node):
else:
fmt = self.builder.config.plantuml_output_format

with _prepare_html_render(self, fmt) as (fileformats, gettag):
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)

Expand Down Expand Up @@ -647,7 +659,7 @@ def latex_visit_plantuml(self, node):
refname, outfname = render_plantuml(self, node, fileformat)
refname, outfname = postproc(self, refname, outfname)
except PlantUmlError as err:
logger.warning(str(err))
logger.warning(str(err), location=node, type='plantuml')
raise nodes.SkipNode

if fmt == 'tikz':
Expand Down Expand Up @@ -711,7 +723,7 @@ def text_visit_plantuml(self, node):
try:
text = render_plantuml_inline(self, node, 'txt')
except PlantUmlError as err:
logger.warning(str(err))
logger.warning(str(err), location=node, type='plantuml')
text = node['uml'] # fall back to uml text, which is still readable

self.new_state()
Expand All @@ -726,14 +738,18 @@ def pdf_visit_plantuml(self, node):
refname, outfname = render_plantuml(self, node, 'eps')
refname, outfname = _convert_eps_to_pdf(self, refname, outfname)
except PlantUmlError as err:
logger.warning(str(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)')
logger.warning(
'plantuml: unsupported output format (node skipped)',
location=node,
type='plantuml',
)
raise nodes.SkipNode


Expand Down

0 comments on commit 299b01b

Please sign in to comment.