OLD | NEW |
| (Empty) |
1 # -*- coding: utf-8 -*- | |
2 from __future__ import unicode_literals | |
3 import re | |
4 import sys | |
5 | |
6 | |
7 """ | |
8 Python 3 Stuff | |
9 ============================================================================= | |
10 """ | |
11 PY3 = sys.version_info[0] == 3 | |
12 | |
13 if PY3: | |
14 string_type = str | |
15 text_type = str | |
16 int2str = chr | |
17 else: | |
18 string_type = basestring | |
19 text_type = unicode | |
20 int2str = unichr | |
21 | |
22 | |
23 """ | |
24 Constants you might want to modify | |
25 ----------------------------------------------------------------------------- | |
26 """ | |
27 | |
28 BLOCK_LEVEL_ELEMENTS = re.compile("^(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul" | |
29 "|script|noscript|form|fieldset|iframe|math" | |
30 "|hr|hr/|style|li|dt|dd|thead|tbody" | |
31 "|tr|th|td|section|footer|header|group|figure" | |
32 "|figcaption|aside|article|canvas|output" | |
33 "|progress|video)$", re.IGNORECASE) | |
34 # Placeholders | |
35 STX = '\u0002' # Use STX ("Start of text") for start-of-placeholder | |
36 ETX = '\u0003' # Use ETX ("End of text") for end-of-placeholder | |
37 INLINE_PLACEHOLDER_PREFIX = STX+"klzzwxh:" | |
38 INLINE_PLACEHOLDER = INLINE_PLACEHOLDER_PREFIX + "%s" + ETX | |
39 INLINE_PLACEHOLDER_RE = re.compile(INLINE_PLACEHOLDER % r'([0-9]{4})') | |
40 AMP_SUBSTITUTE = STX+"amp"+ETX | |
41 | |
42 """ | |
43 Constants you probably do not need to change | |
44 ----------------------------------------------------------------------------- | |
45 """ | |
46 | |
47 RTL_BIDI_RANGES = ( ('\u0590', '\u07FF'), | |
48 # Hebrew (0590-05FF), Arabic (0600-06FF), | |
49 # Syriac (0700-074F), Arabic supplement (0750-077F), | |
50 # Thaana (0780-07BF), Nko (07C0-07FF). | |
51 ('\u2D30', '\u2D7F'), # Tifinagh | |
52 ) | |
53 | |
54 # Extensions should use "markdown.util.etree" instead of "etree" (or do `from | |
55 # markdown.util import etree`). Do not import it by yourself. | |
56 | |
57 try: # Is the C implemenation of ElementTree available? | |
58 import xml.etree.cElementTree as etree | |
59 from xml.etree.ElementTree import Comment | |
60 # Serializers (including ours) test with non-c Comment | |
61 etree.test_comment = Comment | |
62 if etree.VERSION < "1.0.5": | |
63 raise RuntimeError("cElementTree version 1.0.5 or higher is required.") | |
64 except (ImportError, RuntimeError): | |
65 # Use the Python implementation of ElementTree? | |
66 import xml.etree.ElementTree as etree | |
67 if etree.VERSION < "1.1": | |
68 raise RuntimeError("ElementTree version 1.1 or higher is required") | |
69 | |
70 | |
71 """ | |
72 AUXILIARY GLOBAL FUNCTIONS | |
73 ============================================================================= | |
74 """ | |
75 | |
76 | |
77 def isBlockLevel(tag): | |
78 """Check if the tag is a block level HTML tag.""" | |
79 if isinstance(tag, string_type): | |
80 return BLOCK_LEVEL_ELEMENTS.match(tag) | |
81 # Some ElementTree tags are not strings, so return False. | |
82 return False | |
83 | |
84 """ | |
85 MISC AUXILIARY CLASSES | |
86 ============================================================================= | |
87 """ | |
88 | |
89 class AtomicString(text_type): | |
90 """A string which should not be further processed.""" | |
91 pass | |
92 | |
93 | |
94 class Processor(object): | |
95 def __init__(self, markdown_instance=None): | |
96 if markdown_instance: | |
97 self.markdown = markdown_instance | |
98 | |
99 | |
100 class HtmlStash(object): | |
101 """ | |
102 This class is used for stashing HTML objects that we extract | |
103 in the beginning and replace with place-holders. | |
104 """ | |
105 | |
106 def __init__ (self): | |
107 """ Create a HtmlStash. """ | |
108 self.html_counter = 0 # for counting inline html segments | |
109 self.rawHtmlBlocks=[] | |
110 | |
111 def store(self, html, safe=False): | |
112 """ | |
113 Saves an HTML segment for later reinsertion. Returns a | |
114 placeholder string that needs to be inserted into the | |
115 document. | |
116 | |
117 Keyword arguments: | |
118 | |
119 * html: an html segment | |
120 * safe: label an html segment as safe for safemode | |
121 | |
122 Returns : a placeholder string | |
123 | |
124 """ | |
125 self.rawHtmlBlocks.append((html, safe)) | |
126 placeholder = self.get_placeholder(self.html_counter) | |
127 self.html_counter += 1 | |
128 return placeholder | |
129 | |
130 def reset(self): | |
131 self.html_counter = 0 | |
132 self.rawHtmlBlocks = [] | |
133 | |
134 def get_placeholder(self, key): | |
135 return "%swzxhzdk:%d%s" % (STX, key, ETX) | |
136 | |
OLD | NEW |