From 9756cb9f6014347179a9acf54a739aad5dda0c6d Mon Sep 17 00:00:00 2001 From: Tiago Serafim Date: Wed, 19 Sep 2012 18:37:02 -0300 Subject: More tests for the extension and new tests for the RE. It's now possible to use an explicit blank title to not have the

tag with the title rendered. --- markdown/extensions/admonition.py | 18 +++++++++--------- tests/extensions/admonition.html | 11 +++++++++++ tests/extensions/admonition.txt | 11 +++++++++++ tests/test_extensions.py | 30 +++++++++--------------------- 4 files changed, 40 insertions(+), 30 deletions(-) diff --git a/markdown/extensions/admonition.py b/markdown/extensions/admonition.py index 32f2038..0f6af80 100644 --- a/markdown/extensions/admonition.py +++ b/markdown/extensions/admonition.py @@ -7,7 +7,7 @@ Admonition extension for Python-Markdown Adds rST-style admonitions. Inspired by [rST][] feature with the same name. The syntax is (followed by an indented block with the contents): - !!! [title] + !!! [type] [optional explicit title] Where `type` is used as a CSS class name of the div. If not present, `title` defaults to the capitalized `type`, so "note" -> "Note". @@ -63,10 +63,7 @@ class AdmonitionProcessor(markdown.blockprocessors.BlockProcessor): CLASSNAME = 'admonition' CLASSNAME_TITLE = 'admonition-title' - RE = re.compile(r'(?:^|\n)!!!\ ?([\w\-]+)(?:\ "?([^"\n]+)"?)?') - - def __init__(self, parser): - markdown.blockprocessors.BlockProcessor.__init__(self, parser) + RE = re.compile(r'(?:^|\n)!!!\ ?([\w\-]+)(?:\ "(.*?)")?') def test(self, parent, block): sibling = self.lastChild(parent) @@ -88,9 +85,10 @@ class AdmonitionProcessor(markdown.blockprocessors.BlockProcessor): klass, title = self.get_class_and_title(m) div = etree.SubElement(parent, 'div') div.set('class', u'%s %s' % (self.CLASSNAME, klass)) - p = etree.SubElement(div, 'p') - p.text = title - p.set('class', self.CLASSNAME_TITLE) + if title: + p = etree.SubElement(div, 'p') + p.text = title + p.set('class', self.CLASSNAME_TITLE) else: div = sibling @@ -104,8 +102,10 @@ class AdmonitionProcessor(markdown.blockprocessors.BlockProcessor): def get_class_and_title(self, match): klass, title = match.group(1), match.group(2) - if not title: + if title is None: title = klass.capitalize() + elif title == '': + title = None return klass, title diff --git a/tests/extensions/admonition.html b/tests/extensions/admonition.html index f28bcfd..437cac6 100644 --- a/tests/extensions/admonition.html +++ b/tests/extensions/admonition.html @@ -6,6 +6,14 @@

  • first
  • second
  • +
    +

    Some important quote

    +

    another paragraph in the quote

    +
    +
    int main() {
    +    // insert some code
    +}
    +

    More text and stuff.

    @@ -16,4 +24,7 @@

    And now...

    For something completely different.

    You can also use a custom CSS class name.

    +
    +
    +

    An explicitly empty string prevents the title from being rendered.

    \ No newline at end of file diff --git a/tests/extensions/admonition.txt b/tests/extensions/admonition.txt index e8db239..fd18bd6 100644 --- a/tests/extensions/admonition.txt +++ b/tests/extensions/admonition.txt @@ -6,6 +6,14 @@ Some text 1. first 2. second + > Some important quote + + > another paragraph in the quote + + int main() { + // insert some code + } + More text and stuff. !!! note "Did you know?" @@ -15,3 +23,6 @@ More text and stuff. For something completely different. You can also use a custom CSS class name. + +!!! tip "" + An explicitly empty string prevents the title from being rendered. diff --git a/tests/test_extensions.py b/tests/test_extensions.py index 78c6d9d..7661347 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -278,25 +278,13 @@ class TestAdmonition(unittest.TestCase): def setUp(self): self.md = markdown.Markdown(extensions=['admonition']) - self.text = \ -'''!!! note - First line -!!! didyouknow "Did you know?" - Another text''' - - def testComplexSettings(self): - """ Test Complex Settings. """ - - md = markdown.Markdown( - extensions=['admonition'], - extension_configs={}, - safe_mode=True) - self.assertEqual(md.convert(self.text), - '
    \n' - '

    Note

    \n' - '

    First line

    \n' - '
    \n' - '
    \n' - '

    Did you know?

    \n' - '

    Another text

    \n
    ') + def testRE(self): + RE = self.md.parser.blockprocessors['admonition'].RE + tests = [ + ('!!! note', ('note', None)), + ('!!! note "Please Note"', ('note', 'Please Note')), + ('!!! note ""', ('note', '')), + ] + for test, expected in tests: + self.assertEqual(RE.match(test).groups(), expected) -- cgit v1.2.3