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

Side by Side Diff: third_party/markdown/postprocessors.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/odict.py ('k') | third_party/markdown/preprocessors.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 POST-PROCESSORS
3 =============================================================================
4
5 Markdown also allows post-processors, which are similar to preprocessors in
6 that they need to implement a "run" method. However, they are run after core
7 processing.
8
9 """
10
11 from __future__ import absolute_import
12 from __future__ import unicode_literals
13 from . import util
14 from . import odict
15 import re
16
17
18 def build_postprocessors(md_instance, **kwargs):
19 """ Build the default postprocessors for Markdown. """
20 postprocessors = odict.OrderedDict()
21 postprocessors["raw_html"] = RawHtmlPostprocessor(md_instance)
22 postprocessors["amp_substitute"] = AndSubstitutePostprocessor()
23 postprocessors["unescape"] = UnescapePostprocessor()
24 return postprocessors
25
26
27 class Postprocessor(util.Processor):
28 """
29 Postprocessors are run after the ElementTree it converted back into text.
30
31 Each Postprocessor implements a "run" method that takes a pointer to a
32 text string, modifies it as necessary and returns a text string.
33
34 Postprocessors must extend markdown.Postprocessor.
35
36 """
37
38 def run(self, text):
39 """
40 Subclasses of Postprocessor should implement a `run` method, which
41 takes the html document as a single text string and returns a
42 (possibly modified) string.
43
44 """
45 pass
46
47
48 class RawHtmlPostprocessor(Postprocessor):
49 """ Restore raw html to the document. """
50
51 def run(self, text):
52 """ Iterate over html stash and restore "safe" html. """
53 for i in range(self.markdown.htmlStash.html_counter):
54 html, safe = self.markdown.htmlStash.rawHtmlBlocks[i]
55 if self.markdown.safeMode and not safe:
56 if str(self.markdown.safeMode).lower() == 'escape':
57 html = self.escape(html)
58 elif str(self.markdown.safeMode).lower() == 'remove':
59 html = ''
60 else:
61 html = self.markdown.html_replacement_text
62 if self.isblocklevel(html) and (safe or not self.markdown.safeMode):
63 text = text.replace("<p>%s</p>" %
64 (self.markdown.htmlStash.get_placeholder(i)),
65 html + "\n")
66 text = text.replace(self.markdown.htmlStash.get_placeholder(i),
67 html)
68 return text
69
70 def escape(self, html):
71 """ Basic html escaping """
72 html = html.replace('&', '&amp;')
73 html = html.replace('<', '&lt;')
74 html = html.replace('>', '&gt;')
75 return html.replace('"', '&quot;')
76
77 def isblocklevel(self, html):
78 m = re.match(r'^\<\/?([^ >]+)', html)
79 if m:
80 if m.group(1)[0] in ('!', '?', '@', '%'):
81 # Comment, php etc...
82 return True
83 return util.isBlockLevel(m.group(1))
84 return False
85
86
87 class AndSubstitutePostprocessor(Postprocessor):
88 """ Restore valid entities """
89
90 def run(self, text):
91 text = text.replace(util.AMP_SUBSTITUTE, "&")
92 return text
93
94
95 class UnescapePostprocessor(Postprocessor):
96 """ Restore escaped chars """
97
98 RE = re.compile('%s(\d+)%s' % (util.STX, util.ETX))
99
100 def unescape(self, m):
101 return util.int2str(int(m.group(1)))
102
103 def run(self, text):
104 return self.RE.sub(self.unescape, text)
OLDNEW
« no previous file with comments | « third_party/markdown/odict.py ('k') | third_party/markdown/preprocessors.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698