Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: third_party/markdown/extensions/admonition.py

Issue 93743005: Support markdown template for html editor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix path without dir Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/markdown/extensions/abbr.py ('k') | third_party/markdown/extensions/attr_list.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 """
2 Admonition extension for Python-Markdown
3 ========================================
4
5 Adds rST-style admonitions. Inspired by [rST][] feature with the same name.
6
7 The syntax is (followed by an indented block with the contents):
8 !!! [type] [optional explicit title]
9
10 Where `type` is used as a CSS class name of the div. If not present, `title`
11 defaults to the capitalized `type`, so "note" -> "Note".
12
13 rST suggests the following `types`, but you're free to use whatever you want:
14 attention, caution, danger, error, hint, important, note, tip, warning
15
16
17 A simple example:
18 !!! note
19 This is the first line inside the box.
20
21 Outputs:
22 <div class="admonition note">
23 <p class="admonition-title">Note</p>
24 <p>This is the first line inside the box</p>
25 </div>
26
27 You can also specify the title and CSS class of the admonition:
28 !!! custom "Did you know?"
29 Another line here.
30
31 Outputs:
32 <div class="admonition custom">
33 <p class="admonition-title">Did you know?</p>
34 <p>Another line here.</p>
35 </div>
36
37 [rST]: http://docutils.sourceforge.net/docs/ref/rst/directives.html#specific-adm onitions
38
39 By [Tiago Serafim](http://www.tiagoserafim.com/).
40
41 """
42
43 from __future__ import absolute_import
44 from __future__ import unicode_literals
45 from . import Extension
46 from ..blockprocessors import BlockProcessor
47 from ..util import etree
48 import re
49
50
51 class AdmonitionExtension(Extension):
52 """ Admonition extension for Python-Markdown. """
53
54 def extendMarkdown(self, md, md_globals):
55 """ Add Admonition to Markdown instance. """
56 md.registerExtension(self)
57
58 md.parser.blockprocessors.add('admonition',
59 AdmonitionProcessor(md.parser),
60 '_begin')
61
62
63 class AdmonitionProcessor(BlockProcessor):
64
65 CLASSNAME = 'admonition'
66 CLASSNAME_TITLE = 'admonition-title'
67 RE = re.compile(r'(?:^|\n)!!!\ ?([\w\-]+)(?:\ "(.*?)")?')
68
69 def test(self, parent, block):
70 sibling = self.lastChild(parent)
71 return self.RE.search(block) or \
72 (block.startswith(' ' * self.tab_length) and sibling and \
73 sibling.get('class', '').find(self.CLASSNAME) != -1)
74
75 def run(self, parent, blocks):
76 sibling = self.lastChild(parent)
77 block = blocks.pop(0)
78 m = self.RE.search(block)
79
80 if m:
81 block = block[m.end() + 1:] # removes the first line
82
83 block, theRest = self.detab(block)
84
85 if m:
86 klass, title = self.get_class_and_title(m)
87 div = etree.SubElement(parent, 'div')
88 div.set('class', '%s %s' % (self.CLASSNAME, klass))
89 if title:
90 p = etree.SubElement(div, 'p')
91 p.text = title
92 p.set('class', self.CLASSNAME_TITLE)
93 else:
94 div = sibling
95
96 self.parser.parseChunk(div, block)
97
98 if theRest:
99 # This block contained unindented line(s) after the first indented
100 # line. Insert these lines as the first block of the master blocks
101 # list for future processing.
102 blocks.insert(0, theRest)
103
104 def get_class_and_title(self, match):
105 klass, title = match.group(1).lower(), match.group(2)
106 if title is None:
107 # no title was provided, use the capitalized classname as title
108 # e.g.: `!!! note` will render `<p class="admonition-title">Note</p> `
109 title = klass.capitalize()
110 elif title == '':
111 # an explicit blank title should not be rendered
112 # e.g.: `!!! warning ""` will *not* render `p` with a title
113 title = None
114 return klass, title
115
116
117 def makeExtension(configs={}):
118 return AdmonitionExtension(configs=configs)
OLDNEW
« no previous file with comments | « third_party/markdown/extensions/abbr.py ('k') | third_party/markdown/extensions/attr_list.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698