Mercurial > kallithea
annotate rhodecode/lib/markup_renderer.py @ 4115:8b7294a804a0 rhodecode-2.2.5-gpl
Update CONTRIBUTORS with new contributors from RhodeCode v2.2.5
author | Bradley M. Kuhn <bkuhn@sfconservancy.org> |
---|---|
date | Wed, 02 Jul 2014 19:03:10 -0400 |
parents | 7563624e712c |
children | ffd45b185016 |
rev | line source |
---|---|
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
2 """ |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
3 rhodecode.lib.markup_renderer |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
5 |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1769
diff
changeset
|
6 |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
7 Renderer for markup languages with ability to parse using rst or markdown |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1769
diff
changeset
|
8 |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
9 :created_on: Oct 27, 2011 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
10 :author: marcink |
1824
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1818
diff
changeset
|
11 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
12 :license: GPLv3, see COPYING for more details. |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
13 """ |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
14 # This program is free software: you can redistribute it and/or modify |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
15 # it under the terms of the GNU General Public License as published by |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
16 # the Free Software Foundation, either version 3 of the License, or |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
17 # (at your option) any later version. |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
18 # |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
19 # This program is distributed in the hope that it will be useful, |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
22 # GNU General Public License for more details. |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
23 # |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
24 # You should have received a copy of the GNU General Public License |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
25 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
26 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
27 import re |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
28 import logging |
2747
cdce3d7282b2
catch errors on renderers, and display plain if critical rendering error is present
Marcin Kuzminski <marcin@python-works.com>
parents:
2201
diff
changeset
|
29 import traceback |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
30 |
2201
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
31 from rhodecode.lib.utils2 import safe_unicode, MENTIONS_REGEX |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
32 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 log = logging.getLogger(__name__) |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
34 |
1996
0592987973eb
activate codehilite extension on markdown renderer, inspired by bitbucket blog ;)
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
35 |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
36 class MarkupRenderer(object): |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES = ['include', 'meta', 'raw'] |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1769
diff
changeset
|
38 |
1996
0592987973eb
activate codehilite extension on markdown renderer, inspired by bitbucket blog ;)
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
39 MARKDOWN_PAT = re.compile(r'md|mkdn?|mdown|markdown', re.IGNORECASE) |
0592987973eb
activate codehilite extension on markdown renderer, inspired by bitbucket blog ;)
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
40 RST_PAT = re.compile(r're?st', re.IGNORECASE) |
0592987973eb
activate codehilite extension on markdown renderer, inspired by bitbucket blog ;)
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
41 PLAIN_PAT = re.compile(r'readme', re.IGNORECASE) |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1769
diff
changeset
|
42 |
4009
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
43 def _detect_renderer(self, source, filename=None): |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
44 """ |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
45 runs detection of what renderer should be used for generating html |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
46 from a markup language |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1769
diff
changeset
|
47 |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
48 filename can be also explicitly a renderer name |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1769
diff
changeset
|
49 |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 :param source: |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 :param filename: |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 """ |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
54 if MarkupRenderer.MARKDOWN_PAT.findall(filename): |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
55 detected_renderer = 'markdown' |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
56 elif MarkupRenderer.RST_PAT.findall(filename): |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
57 detected_renderer = 'rst' |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
58 elif MarkupRenderer.PLAIN_PAT.findall(filename): |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
59 detected_renderer = 'rst' |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 else: |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
61 detected_renderer = 'plain' |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
62 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
63 return getattr(MarkupRenderer, detected_renderer) |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
64 |
4009
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
65 @classmethod |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
66 def _flavored_markdown(cls, text): |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
67 """ |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
68 Github style flavored markdown |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
69 |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
70 :param text: |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
71 """ |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
72 from hashlib import md5 |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
73 |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
74 # Extract pre blocks. |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
75 extractions = {} |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
76 def pre_extraction_callback(matchobj): |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
77 digest = md5(matchobj.group(0)).hexdigest() |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
78 extractions[digest] = matchobj.group(0) |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
79 return "{gfm-extraction-%s}" % digest |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
80 pattern = re.compile(r'<pre>.*?</pre>', re.MULTILINE | re.DOTALL) |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
81 text = re.sub(pattern, pre_extraction_callback, text) |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
82 |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
83 # Prevent foo_bar_baz from ending up with an italic word in the middle. |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
84 def italic_callback(matchobj): |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
85 s = matchobj.group(0) |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
86 if list(s).count('_') >= 2: |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
87 return s.replace('_', '\_') |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
88 return s |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
89 text = re.sub(r'^(?! {4}|\t)\w+_\w+_\w[\w_]*', italic_callback, text) |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
90 |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
91 # In very clear cases, let newlines become <br /> tags. |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
92 def newline_callback(matchobj): |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
93 if len(matchobj.group(1)) == 1: |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
94 return matchobj.group(0).rstrip() + ' \n' |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
95 else: |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
96 return matchobj.group(0) |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
97 pattern = re.compile(r'^[\w\<][^\n]*(\n+)', re.MULTILINE) |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
98 text = re.sub(pattern, newline_callback, text) |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
99 |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
100 # Insert pre block extractions. |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
101 def pre_insert_callback(matchobj): |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
102 return '\n\n' + extractions[matchobj.group(1)] |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
103 text = re.sub(r'{gfm-extraction-([0-9a-f]{32})\}', |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
104 pre_insert_callback, text) |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
105 |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
106 return text |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
107 |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
108 def render(self, source, filename=None): |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
109 """ |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
110 Renders a given filename using detected renderer |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
111 it detects renderers based on file extension or mimetype. |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
112 At last it will just do a simple html replacing new lines with <br/> |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1769
diff
changeset
|
113 |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
114 :param file_name: |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
115 :param source: |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
116 """ |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
117 |
4009
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
118 renderer = self._detect_renderer(source, filename) |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 readme_data = renderer(source) |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 return readme_data |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
121 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
122 @classmethod |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
123 def plain(cls, source): |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
124 source = safe_unicode(source) |
1996
0592987973eb
activate codehilite extension on markdown renderer, inspired by bitbucket blog ;)
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
125 |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
126 def urlify_text(text): |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
127 url_pat = re.compile(r'(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]' |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
128 '|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)') |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
129 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
130 def url_func(match_obj): |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
131 url_full = match_obj.groups()[0] |
1996
0592987973eb
activate codehilite extension on markdown renderer, inspired by bitbucket blog ;)
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
132 return '<a href="%(url)s">%(url)s</a>' % ({'url': url_full}) |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
133 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
134 return url_pat.sub(url_func, text) |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
135 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
136 source = urlify_text(source) |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
137 return '<br />' + source.replace("\n", '<br />') |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
138 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
139 @classmethod |
4009
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
140 def markdown(cls, source, safe=True, flavored=False): |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
141 source = safe_unicode(source) |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
142 try: |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
143 import markdown as __markdown |
4009
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
144 if flavored: |
7563624e712c
Added github flavored markdown style rendering into markdown
Marcin Kuzminski <marcin@python-works.com>
parents:
3164
diff
changeset
|
145 source = cls._flavored_markdown(source) |
3164
01cb7df198ae
Enable all Markdown Extra in Python markdown library.
xpol <xpolife@gmail.com>
parents:
3019
diff
changeset
|
146 return __markdown.markdown(source, ['codehilite', 'extra']) |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
147 except ImportError: |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
148 log.warning('Install markdown to use this function') |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
149 return cls.plain(source) |
2747
cdce3d7282b2
catch errors on renderers, and display plain if critical rendering error is present
Marcin Kuzminski <marcin@python-works.com>
parents:
2201
diff
changeset
|
150 except Exception: |
cdce3d7282b2
catch errors on renderers, and display plain if critical rendering error is present
Marcin Kuzminski <marcin@python-works.com>
parents:
2201
diff
changeset
|
151 log.error(traceback.format_exc()) |
cdce3d7282b2
catch errors on renderers, and display plain if critical rendering error is present
Marcin Kuzminski <marcin@python-works.com>
parents:
2201
diff
changeset
|
152 if safe: |
cdce3d7282b2
catch errors on renderers, and display plain if critical rendering error is present
Marcin Kuzminski <marcin@python-works.com>
parents:
2201
diff
changeset
|
153 return source |
cdce3d7282b2
catch errors on renderers, and display plain if critical rendering error is present
Marcin Kuzminski <marcin@python-works.com>
parents:
2201
diff
changeset
|
154 else: |
cdce3d7282b2
catch errors on renderers, and display plain if critical rendering error is present
Marcin Kuzminski <marcin@python-works.com>
parents:
2201
diff
changeset
|
155 raise |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
156 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
157 @classmethod |
2747
cdce3d7282b2
catch errors on renderers, and display plain if critical rendering error is present
Marcin Kuzminski <marcin@python-works.com>
parents:
2201
diff
changeset
|
158 def rst(cls, source, safe=True): |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
159 source = safe_unicode(source) |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
160 try: |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
161 from docutils.core import publish_parts |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
162 from docutils.parsers.rst import directives |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
163 docutils_settings = dict([(alias, None) for alias in |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
164 cls.RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES]) |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
165 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
166 docutils_settings.update({'input_encoding': 'unicode', |
1996
0592987973eb
activate codehilite extension on markdown renderer, inspired by bitbucket blog ;)
Marcin Kuzminski <marcin@python-works.com>
parents:
1824
diff
changeset
|
167 'report_level': 4}) |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
168 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
169 for k, v in docutils_settings.iteritems(): |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
170 directives.register_directive(k, v) |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
171 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
172 parts = publish_parts(source=source, |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
173 writer_name="html4css1", |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
174 settings_overrides=docutils_settings) |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
175 |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
176 return parts['html_title'] + parts["fragment"] |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
177 except ImportError: |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
178 log.warning('Install docutils to use this function') |
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
179 return cls.plain(source) |
2747
cdce3d7282b2
catch errors on renderers, and display plain if critical rendering error is present
Marcin Kuzminski <marcin@python-works.com>
parents:
2201
diff
changeset
|
180 except Exception: |
cdce3d7282b2
catch errors on renderers, and display plain if critical rendering error is present
Marcin Kuzminski <marcin@python-works.com>
parents:
2201
diff
changeset
|
181 log.error(traceback.format_exc()) |
cdce3d7282b2
catch errors on renderers, and display plain if critical rendering error is present
Marcin Kuzminski <marcin@python-works.com>
parents:
2201
diff
changeset
|
182 if safe: |
cdce3d7282b2
catch errors on renderers, and display plain if critical rendering error is present
Marcin Kuzminski <marcin@python-works.com>
parents:
2201
diff
changeset
|
183 return source |
cdce3d7282b2
catch errors on renderers, and display plain if critical rendering error is present
Marcin Kuzminski <marcin@python-works.com>
parents:
2201
diff
changeset
|
184 else: |
cdce3d7282b2
catch errors on renderers, and display plain if critical rendering error is present
Marcin Kuzminski <marcin@python-works.com>
parents:
2201
diff
changeset
|
185 raise |
1604
bb3c2111bf92
initial version of markup renderer
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
186 |
1769
025f3333c769
@mention highlighting
Marcin Kuzminski <marcin@python-works.com>
parents:
1604
diff
changeset
|
187 @classmethod |
025f3333c769
@mention highlighting
Marcin Kuzminski <marcin@python-works.com>
parents:
1604
diff
changeset
|
188 def rst_with_mentions(cls, source): |
2201
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
189 mention_pat = re.compile(MENTIONS_REGEX) |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1769
diff
changeset
|
190 |
1769
025f3333c769
@mention highlighting
Marcin Kuzminski <marcin@python-works.com>
parents:
1604
diff
changeset
|
191 def wrapp(match_obj): |
025f3333c769
@mention highlighting
Marcin Kuzminski <marcin@python-works.com>
parents:
1604
diff
changeset
|
192 uname = match_obj.groups()[0] |
2201
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
193 return ' **@%(uname)s** ' % {'uname': uname} |
1769
025f3333c769
@mention highlighting
Marcin Kuzminski <marcin@python-works.com>
parents:
1604
diff
changeset
|
194 mention_hl = mention_pat.sub(wrapp, source).strip() |
025f3333c769
@mention highlighting
Marcin Kuzminski <marcin@python-works.com>
parents:
1604
diff
changeset
|
195 return cls.rst(mention_hl) |