Show More
@@ -40,7 +40,7 b' class MarkupRenderer(object):' | |||||
40 | RST_PAT = re.compile(r're?st', re.IGNORECASE) |
|
40 | RST_PAT = re.compile(r're?st', re.IGNORECASE) | |
41 | PLAIN_PAT = re.compile(r'readme', re.IGNORECASE) |
|
41 | PLAIN_PAT = re.compile(r'readme', re.IGNORECASE) | |
42 |
|
42 | |||
43 |
def |
|
43 | def _detect_renderer(self, source, filename=None): | |
44 | """ |
|
44 | """ | |
45 | runs detection of what renderer should be used for generating html |
|
45 | runs detection of what renderer should be used for generating html | |
46 | from a markup language |
|
46 | from a markup language | |
@@ -62,6 +62,49 b' class MarkupRenderer(object):' | |||||
62 |
|
62 | |||
63 | return getattr(MarkupRenderer, detected_renderer) |
|
63 | return getattr(MarkupRenderer, detected_renderer) | |
64 |
|
64 | |||
|
65 | @classmethod | |||
|
66 | def _flavored_markdown(cls, text): | |||
|
67 | """ | |||
|
68 | Github style flavored markdown | |||
|
69 | ||||
|
70 | :param text: | |||
|
71 | """ | |||
|
72 | from hashlib import md5 | |||
|
73 | ||||
|
74 | # Extract pre blocks. | |||
|
75 | extractions = {} | |||
|
76 | def pre_extraction_callback(matchobj): | |||
|
77 | digest = md5(matchobj.group(0)).hexdigest() | |||
|
78 | extractions[digest] = matchobj.group(0) | |||
|
79 | return "{gfm-extraction-%s}" % digest | |||
|
80 | pattern = re.compile(r'<pre>.*?</pre>', re.MULTILINE | re.DOTALL) | |||
|
81 | text = re.sub(pattern, pre_extraction_callback, text) | |||
|
82 | ||||
|
83 | # Prevent foo_bar_baz from ending up with an italic word in the middle. | |||
|
84 | def italic_callback(matchobj): | |||
|
85 | s = matchobj.group(0) | |||
|
86 | if list(s).count('_') >= 2: | |||
|
87 | return s.replace('_', '\_') | |||
|
88 | return s | |||
|
89 | text = re.sub(r'^(?! {4}|\t)\w+_\w+_\w[\w_]*', italic_callback, text) | |||
|
90 | ||||
|
91 | # In very clear cases, let newlines become <br /> tags. | |||
|
92 | def newline_callback(matchobj): | |||
|
93 | if len(matchobj.group(1)) == 1: | |||
|
94 | return matchobj.group(0).rstrip() + ' \n' | |||
|
95 | else: | |||
|
96 | return matchobj.group(0) | |||
|
97 | pattern = re.compile(r'^[\w\<][^\n]*(\n+)', re.MULTILINE) | |||
|
98 | text = re.sub(pattern, newline_callback, text) | |||
|
99 | ||||
|
100 | # Insert pre block extractions. | |||
|
101 | def pre_insert_callback(matchobj): | |||
|
102 | return '\n\n' + extractions[matchobj.group(1)] | |||
|
103 | text = re.sub(r'{gfm-extraction-([0-9a-f]{32})\}', | |||
|
104 | pre_insert_callback, text) | |||
|
105 | ||||
|
106 | return text | |||
|
107 | ||||
65 | def render(self, source, filename=None): |
|
108 | def render(self, source, filename=None): | |
66 | """ |
|
109 | """ | |
67 | Renders a given filename using detected renderer |
|
110 | Renders a given filename using detected renderer | |
@@ -72,7 +115,7 b' class MarkupRenderer(object):' | |||||
72 | :param source: |
|
115 | :param source: | |
73 | """ |
|
116 | """ | |
74 |
|
117 | |||
75 |
renderer = self. |
|
118 | renderer = self._detect_renderer(source, filename) | |
76 | readme_data = renderer(source) |
|
119 | readme_data = renderer(source) | |
77 | return readme_data |
|
120 | return readme_data | |
78 |
|
121 | |||
@@ -94,10 +137,12 b' class MarkupRenderer(object):' | |||||
94 | return '<br />' + source.replace("\n", '<br />') |
|
137 | return '<br />' + source.replace("\n", '<br />') | |
95 |
|
138 | |||
96 | @classmethod |
|
139 | @classmethod | |
97 | def markdown(cls, source, safe=True): |
|
140 | def markdown(cls, source, safe=True, flavored=False): | |
98 | source = safe_unicode(source) |
|
141 | source = safe_unicode(source) | |
99 | try: |
|
142 | try: | |
100 | import markdown as __markdown |
|
143 | import markdown as __markdown | |
|
144 | if flavored: | |||
|
145 | source = cls._flavored_markdown(source) | |||
101 | return __markdown.markdown(source, ['codehilite', 'extra']) |
|
146 | return __markdown.markdown(source, ['codehilite', 'extra']) | |
102 | except ImportError: |
|
147 | except ImportError: | |
103 | log.warning('Install markdown to use this function') |
|
148 | log.warning('Install markdown to use this function') |
General Comments 0
You need to be logged in to leave comments.
Login now