From 67bb12b3e508b389f18efb6a992cc390680a7235 Mon Sep 17 00:00:00 2001 From: WhiteWinterWolf Date: Fri, 8 Sep 2017 13:45:19 +0200 Subject: Add the possibility to set additional classes Additional CSS classes names can be appended to the admonition name using spaces as separators. The following markdown: !!! note floatright This is a floating note. Generates the following HTML code:

Note

This is a floating note.

--- docs/extensions/admonition.md | 22 ++++++++++++++++++++-- markdown/extensions/admonition.py | 6 ++++-- tests/extensions/admonition.html | 9 +++++++++ tests/extensions/admonition.txt | 8 ++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/docs/extensions/admonition.md b/docs/extensions/admonition.md index 2d2634d..22a6aea 100644 --- a/docs/extensions/admonition.md +++ b/docs/extensions/admonition.md @@ -72,8 +72,26 @@ results in: ``` -rST suggests the following `types`, but you're free to use whatever you want: - attention, caution, danger, error, hint, important, note, tip, warning. +You can also provide additional CSS class names separated by spaces. The first +class should be the "type." For example: + +```md +!!! danger highlight blink "Don't try this at home" + ... +``` + +will render: + +```html + +``` + +rST suggests the following "types": `attention`, `caution`, `danger`, `error`, +`hint`, `important`, `note`, `tip`, and `warning`; however, you're free to use +whatever you want. Styling ------- diff --git a/markdown/extensions/admonition.py b/markdown/extensions/admonition.py index 8576638..b001957 100644 --- a/markdown/extensions/admonition.py +++ b/markdown/extensions/admonition.py @@ -41,7 +41,8 @@ class AdmonitionProcessor(BlockProcessor): CLASSNAME = 'admonition' CLASSNAME_TITLE = 'admonition-title' - RE = re.compile(r'(?:^|\n)!!! ?([\w\-]+)(?: +"(.*?)")? *(?:\n|$)') + RE = re.compile(r'(?:^|\n)!!! ?([\w\-]+(?: +[\w\-]+)*)(?: +"(.*?)")? *(?:\n|$)') + RE_SPACES = re.compile(' +') def test(self, parent, block): sibling = self.lastChild(parent) @@ -80,11 +81,12 @@ class AdmonitionProcessor(BlockProcessor): def get_class_and_title(self, match): klass, title = match.group(1).lower(), match.group(2) + klass = self.RE_SPACES.sub(' ', klass) if title is None: # no title was provided, use the capitalized classname as title # e.g.: `!!! note` will render # `

Note

` - title = klass.capitalize() + title = klass.split(' ', 1)[0].capitalize() elif title == '': # an explicit blank title should not be rendered # e.g.: `!!! warning ""` will *not* render `p` with a title diff --git a/tests/extensions/admonition.html b/tests/extensions/admonition.html index 511d71a..8812dcb 100644 --- a/tests/extensions/admonition.html +++ b/tests/extensions/admonition.html @@ -26,6 +26,15 @@

For something completely different.

You can also use a custom CSS class name.

+
+

And now...

+

For something completely different.

+

Several class names can be separated by space chars.

+
+
+

Note

+

The default title is the capitalized first class name.

+

An explicitly empty string prevents the title from being rendered.

diff --git a/tests/extensions/admonition.txt b/tests/extensions/admonition.txt index ab8434c..03ff4e9 100644 --- a/tests/extensions/admonition.txt +++ b/tests/extensions/admonition.txt @@ -25,6 +25,14 @@ Not part of an Admonition! You can also use a custom CSS class name. +!!! class1 class2 class3 "And now..." + For something completely different. + + Several class names can be separated by space chars. + +!!! note anotherclass + The default title is the capitalized first class name. + !!! tip "" An explicitly empty string prevents the title from being rendered. -- cgit v1.2.3