Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an imgbasename option to the UML directive #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions sphinxcontrib/plantuml.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class UmlDirective(Directive):
optional_arguments = 1
option_spec = {'alt': directives.unchanged,
'caption': directives.unchanged,
'imgbasename': directives.unchanged,
'height': directives.length_or_unitless,
'width': directives.length_or_percentage_or_unitless,
'scale': directives.percentage,
Expand Down Expand Up @@ -108,6 +109,9 @@ def run(self):
caption = nodes.caption(self.options['caption'], '', *cnode)
node += caption

if 'imgbasename' in self.options:
node['imgbasename'] = self.options['imgbasename']

return [node]

def _read_utf8(filename):
Expand All @@ -118,13 +122,19 @@ def _read_utf8(filename):
fp.close()

def generate_name(self, node, fileformat):
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'))
key = h.hexdigest()
fname = 'plantuml-%s.%s' % (key, fileformat)
imgbasename = node.get('imgbasename', None)

if imgbasename is None:
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'))
key = h.hexdigest()
fname = 'plantuml-%s.%s' % (key, fileformat)
else:
fname = 'plantuml-%s.%s' % (imgbasename, fileformat)

imgpath = getattr(self.builder, 'imgpath', None)
if imgpath:
return ('/'.join((self.builder.imgpath, fname)),
Expand Down Expand Up @@ -161,7 +171,8 @@ def generate_plantuml_args(self, node, fileformat):

def render_plantuml(self, node, fileformat):
refname, outfname = generate_name(self, node, fileformat)
if os.path.exists(outfname):
if (not self.builder.config.plantuml_overwrite_output) and \
os.path.exists(outfname):
return refname, outfname # don't regenerate
absincdir = os.path.join(self.builder.srcdir, node['incdir'])
ensuredir(os.path.dirname(outfname))
Expand Down Expand Up @@ -396,6 +407,7 @@ def setup(app):
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_overwrite_output', True, 'html')

# imitate what app.add_node() does
if 'rst2pdf.pdfbuilder' in app.config.extensions:
Expand Down