Skip to content

Commit

Permalink
new: usr: added priority option to change plugin priority (refs #38)
Browse files Browse the repository at this point in the history
Now the plugin execution priority can be changed if you don't like
default value.
The default should be the most reasonable with most plugins.
  • Loading branch information
Michele Tessaro committed Dec 8, 2019
1 parent 1d233eb commit 702d1a4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ For [Gentoo Linux][Gentoo] there is an ebuild at http://gpo.zugaina.org/dev-util
the ebuild and the `files` subfolder or you can add the `zugaina` repository with [layman][]
(recommended).
### Using a PlantUML server
### <a name="using-plantuml-server"></a>Using a PlantUML server
From version `2.0` a [PlantUML server](http://plantuml.com/server) can be used for rendering diagrams. This speedups a
lot the diagrams rendering but needs to send the diagram source to a server.
Expand All @@ -125,11 +125,30 @@ plantuml_markdown:
classes: class1,class2 # default diagram classes
title: UML diagram # default title (tooltip) for diagram images
alt: UML diagram image # default `alt` attribute for diagram images
priority: 30 # plugin priority; the higher, the sooner will be applied (default 30)
```

Then you need to specify the configuration file on the command line:

markdown_py -x plantuml_markdown -c myconfig.yml mydoc.md > out.html

Plugin options
--------------

The plugin has several configuration option:

* `classes`: space separated list of classes for the generated image. Defaults to `uml`
* `format`: format of image to generate (`png`, `svg` or `txt`). Defaults to `png`
* `alt`: text to show when image is not available. Defaults to `uml diagram`
* `title`: tooltip for the diagram
* `server`: PlantUML server url, for remote rendering. Defaults to `''`, use local command
* `cachedir`: directory for caching of diagrams. Defaults to `''`, no caching
* `priority`: extension priority. Higher values means the extension is applied sooner than others. Defaults to `30`

For passing options to the `plantuml_plugin` see the documentation of the tool you are using.

For `markdown_py`, simply write a YAML file with the configurations and use the `-c` option on the command line.
See the [Using a PlantUML server](#using-plantuml-server) section for an example.

Running tests
-------------
Expand Down
6 changes: 4 additions & 2 deletions plantuml_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ def __init__(self, **kwargs):
'format': ["png", "Format of image to generate (png, svg or txt). Defaults to 'png'."],
'title': ["", "Tooltip for the diagram"],
'server': ["", "PlantUML server url, for remote rendering. Defaults to '', use local command."],
'cachedir': ["", "Directory for caching of diagrams. Defaults to '', no caching"]
'cachedir': ["", "Directory for caching of diagrams. Defaults to '', no caching"],
'priority': ["30", "Extension priority. Higher values means the extension is applied sooner than others. "
"Defaults to 30"]
}

# Fix to make links navigable in SVG diagrams
Expand All @@ -272,7 +274,7 @@ def extendMarkdown(self, md, md_globals=None):
if markdown.version_info[0] < 3:
md.preprocessors.add('plantuml', blockprocessor, '_begin')
else:
md.preprocessors.register(blockprocessor, 'plantuml', 100)
md.preprocessors.register(blockprocessor, 'plantuml', int(blockprocessor.config['priority']))


def makeExtension(**kwargs):
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
unittest2
pymdown-extensions
58 changes: 54 additions & 4 deletions test/test_plantuml.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# -*- coding: utf-8 -*-
import os
import re
import unittest
#import unittest
import markdown
import tempfile
from unittest import TestCase, SkipTest, mock


class PlantumlTest(unittest.TestCase):
class PlantumlTest(TestCase):

@classmethod
def setUpClass(cls):
if cls is PlantumlTest:
raise unittest.SkipTest("Base class")
raise SkipTest("Base class")
super(PlantumlTest, cls).setUpClass()

def setUp(self):
self.md = markdown.Markdown(extensions=['markdown.extensions.fenced_code', 'plantuml_markdown'])
self.md = markdown.Markdown(extensions=['markdown.extensions.fenced_code',
'pymdownx.snippets', 'plantuml_markdown'])
self.text_builder = None

def _load_file(self, filename):
Expand Down Expand Up @@ -57,6 +60,53 @@ def sort_attributes(groups):

return cls.SVG_REGEX.sub(lambda x: sort_attributes(x.groups()), html)

def test_priority_after_snippets(self):
"""
Verifies the normal priority of the plantuml_markdown plugin: it must be execute before the fenced code
but after the snippets plugin.
"""
self._test_snippets(30, 'A --> B\n')

def test_priority_before_snippets(self):
"""
Verifies changing plugin priority: in must be execute even before the snippets plugin.
:return:
"""
# raising priority, so the plantuml plugin is executed before the snippet plugin
# expecting that the snippet is not inserted in the plantuml source code
self._test_snippets(40, '--8<-- "'+os.path.join(tempfile.gettempdir(), 'test-defs.puml')+'"\n')

def _test_snippets(self, priority, expected):
"""
Verifies the execution order with the snippets plugin.
If priority is lower than 32, the snippets plugin has priority; if greater, the
plantml_markdown plugin has priority over the snippets plugin.
:param priority: execution priority of the plantuml_markdown plugin
:param expected: expected generated plantuml source code
"""
self.md = markdown.Markdown(extensions=['markdown.extensions.fenced_code',
'pymdownx.snippets', 'plantuml_markdown'],
extension_configs={
'plantuml_markdown': {
'priority': priority
}
})
tempdir = tempfile.gettempdir()
defs_file = os.path.join(tempdir, 'test-defs.puml')
# preparing a file to include
with open(defs_file, 'w') as f:
f.write('A --> B')

from test.markdown_builder import MarkdownBuilder
from plantuml_markdown import PlantUMLPreprocessor

# mcking a method to capture the generated PlantUML source code
with mock.patch.object(PlantUMLPreprocessor, '_render_diagram',
return_value='testing'.encode('utf8')) as mocked_plugin:
text = self.text_builder.diagram("--8<-- \"" + defs_file + "\"").build()
self.md.convert(text)
mocked_plugin.assert_called_with(expected, 'png')

def test_arg_title(self):
"""
Test for the correct parsing of the title argument
Expand Down

0 comments on commit 702d1a4

Please sign in to comment.