plantuml_rst.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python
  2. """Custom reST_ directive for plantuml_ integration.
  3. Adapted from ditaa_rst plugin.
  4. .. _reST: http://docutils.sourceforge.net/rst.html
  5. .. _plantuml: http://plantuml.sourceforge.net/
  6. """
  7. import sys
  8. import os
  9. from docutils.nodes import image, literal_block
  10. from docutils.parsers.rst import Directive, directives
  11. from pelican import signals, logger
  12. from .generateUmlDiagram import generate_uml_image
  13. global_siteurl = "" # URL of the site, filled on plugin initialization
  14. class PlantUML_rst(Directive):
  15. """ reST directive for PlantUML """
  16. required_arguments = 0
  17. optional_arguments = 0
  18. has_content = True
  19. global global_siteurl
  20. option_spec = {
  21. 'class' : directives.class_option,
  22. 'alt' : directives.unchanged,
  23. 'format': directives.unchanged,
  24. }
  25. def run(self):
  26. path = os.path.abspath(os.path.join('output', 'images'))
  27. if not os.path.exists(path):
  28. os.makedirs(path)
  29. nodes = []
  30. body = '\n'.join(self.content)
  31. try:
  32. url = global_siteurl+'/'+generate_uml_image(path, body, "png")
  33. except Exception as exc:
  34. error = self.state_machine.reporter.error(
  35. 'Failed to run plantuml: %s' % exc,
  36. literal_block(self.block_text, self.block_text),
  37. line=self.lineno)
  38. nodes.append(error)
  39. else:
  40. alt = self.options.get('alt', 'uml diagram')
  41. classes = self.options.pop('class', ['uml'])
  42. imgnode = image(uri=url, classes=classes, alt=alt)
  43. nodes.append(imgnode)
  44. return nodes
  45. def custom_url(generator, metadata):
  46. """ Saves globally the value of SITEURL configuration parameter """
  47. global global_siteurl
  48. global_siteurl = generator.settings['SITEURL']
  49. def pelican_init(pelicanobj):
  50. """ Prepare configurations for the MD plugin """
  51. try:
  52. import markdown
  53. from .plantuml_md import PlantUMLMarkdownExtension
  54. except:
  55. # Markdown not available
  56. logger.debug("[plantuml] Markdown support not available")
  57. return
  58. # Register the Markdown plugin
  59. config = { 'siteurl': pelicanobj.settings['SITEURL'] }
  60. try:
  61. if 'MD_EXTENSIONS' in pelicanobj.settings.keys(): # pre pelican 3.7.0
  62. pelicanobj.settings['MD_EXTENSIONS'].append(PlantUMLMarkdownExtension(config))
  63. elif 'MARKDOWN' in pelicanobj.settings.keys() and \
  64. not ('extension_configs' in pelicanobj.settings['MARKDOWN']['extension_configs']): # from pelican 3.7.0
  65. pelicanobj.settings['MARKDOWN']['extension_configs']['plantuml.plantuml_md'] = {}
  66. except:
  67. logger.error("[plantuml] Unable to configure plantuml markdown extension")
  68. def register():
  69. """Plugin registration."""
  70. signals.initialized.connect(pelican_init)
  71. signals.article_generator_context.connect(custom_url)
  72. directives.register_directive('uml', PlantUML_rst)