##// END OF EJS Templates
activate codehilite extension on markdown renderer, inspired by bitbucket blog ;)
marcink -
r1996:05929879 beta
parent child Browse files
Show More
@@ -1,138 +1,137 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 rhodecode.lib.markup_renderer
4 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 5
6 6
7 7 Renderer for markup languages with ability to parse using rst or markdown
8 8
9 9 :created_on: Oct 27, 2011
10 10 :author: marcink
11 11 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com>
12 12 :license: GPLv3, see COPYING for more details.
13 13 """
14 14 # This program is free software: you can redistribute it and/or modify
15 15 # it under the terms of the GNU General Public License as published by
16 16 # the Free Software Foundation, either version 3 of the License, or
17 17 # (at your option) any later version.
18 18 #
19 19 # This program is distributed in the hope that it will be useful,
20 20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 22 # GNU General Public License for more details.
23 23 #
24 24 # You should have received a copy of the GNU General Public License
25 25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 26
27 27 import re
28 28 import logging
29 29
30 30 from rhodecode.lib import safe_unicode
31 31
32 32 log = logging.getLogger(__name__)
33 33
34
34 35 class MarkupRenderer(object):
35 36 RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES = ['include', 'meta', 'raw']
36 37
37 MARKDOWN_PAT = re.compile(r'md|mkdn?|mdown|markdown',re.IGNORECASE)
38 RST_PAT = re.compile(r're?st',re.IGNORECASE)
39 PLAIN_PAT = re.compile(r'readme',re.IGNORECASE)
38 MARKDOWN_PAT = re.compile(r'md|mkdn?|mdown|markdown', re.IGNORECASE)
39 RST_PAT = re.compile(r're?st', re.IGNORECASE)
40 PLAIN_PAT = re.compile(r'readme', re.IGNORECASE)
40 41
41 42 def __detect_renderer(self, source, filename=None):
42 43 """
43 44 runs detection of what renderer should be used for generating html
44 45 from a markup language
45 46
46 47 filename can be also explicitly a renderer name
47 48
48 49 :param source:
49 50 :param filename:
50 51 """
51 52
52 53 if MarkupRenderer.MARKDOWN_PAT.findall(filename):
53 54 detected_renderer = 'markdown'
54 55 elif MarkupRenderer.RST_PAT.findall(filename):
55 56 detected_renderer = 'rst'
56 57 elif MarkupRenderer.PLAIN_PAT.findall(filename):
57 58 detected_renderer = 'rst'
58 59 else:
59 60 detected_renderer = 'plain'
60 61
61 62 return getattr(MarkupRenderer, detected_renderer)
62 63
63
64 64 def render(self, source, filename=None):
65 65 """
66 66 Renders a given filename using detected renderer
67 67 it detects renderers based on file extension or mimetype.
68 68 At last it will just do a simple html replacing new lines with <br/>
69 69
70 70 :param file_name:
71 71 :param source:
72 72 """
73 73
74 74 renderer = self.__detect_renderer(source, filename)
75 75 readme_data = renderer(source)
76 76 return readme_data
77 77
78 78 @classmethod
79 79 def plain(cls, source):
80 80 source = safe_unicode(source)
81
81 82 def urlify_text(text):
82 83 url_pat = re.compile(r'(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]'
83 84 '|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)')
84 85
85 86 def url_func(match_obj):
86 87 url_full = match_obj.groups()[0]
87 return '<a href="%(url)s">%(url)s</a>' % ({'url':url_full})
88 return '<a href="%(url)s">%(url)s</a>' % ({'url': url_full})
88 89
89 90 return url_pat.sub(url_func, text)
90 91
91 92 source = urlify_text(source)
92 93 return '<br />' + source.replace("\n", '<br />')
93 94
94
95 95 @classmethod
96 96 def markdown(cls, source):
97 97 source = safe_unicode(source)
98 98 try:
99 99 import markdown as __markdown
100 return __markdown.markdown(source)
100 return __markdown.markdown(source, ['codehilite'])
101 101 except ImportError:
102 102 log.warning('Install markdown to use this function')
103 103 return cls.plain(source)
104 104
105
106 105 @classmethod
107 106 def rst(cls, source):
108 107 source = safe_unicode(source)
109 108 try:
110 109 from docutils.core import publish_parts
111 110 from docutils.parsers.rst import directives
112 111 docutils_settings = dict([(alias, None) for alias in
113 112 cls.RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES])
114 113
115 114 docutils_settings.update({'input_encoding': 'unicode',
116 'report_level':4})
115 'report_level': 4})
117 116
118 117 for k, v in docutils_settings.iteritems():
119 118 directives.register_directive(k, v)
120 119
121 120 parts = publish_parts(source=source,
122 121 writer_name="html4css1",
123 122 settings_overrides=docutils_settings)
124 123
125 124 return parts['html_title'] + parts["fragment"]
126 125 except ImportError:
127 126 log.warning('Install docutils to use this function')
128 127 return cls.plain(source)
129 128
130 129 @classmethod
131 130 def rst_with_mentions(cls, source):
132 131 mention_pat = re.compile(r'(?:^@|\s@)(\w+)')
133 132
134 133 def wrapp(match_obj):
135 134 uname = match_obj.groups()[0]
136 135 return ' **@%(uname)s** ' % {'uname':uname}
137 136 mention_hl = mention_pat.sub(wrapp, source).strip()
138 137 return cls.rst(mention_hl)
@@ -1,169 +1,169 b''
1 1 div.codeblock {
2 2 overflow: auto;
3 3 padding: 0px;
4 4 border: 1px solid #ccc;
5 5 background: #f8f8f8;
6 6 font-size: 100%;
7 7 line-height: 100%;
8 8 /* new */
9 9 line-height: 125%;
10 10 -webkit-border-radius: 4px;
11 11 -moz-border-radius: 4px;
12 12 border-radius: 4px;
13 13 }
14 14 div.codeblock .code-header{
15 15 border-bottom: 1px solid #CCCCCC;
16 16 background: #EEEEEE;
17 17 padding:10px 0 10px 0;
18 18 }
19 19
20 20 div.codeblock .code-header .stats{
21 21 clear: both;
22 22 margin-top:-3px;
23 23 padding-left: 8px;
24 24 border-bottom: 1px solid rgb(204, 204, 204);
25 25 margin-bottom: 5px; height: 23px;
26 26 }
27 27
28 28 div.codeblock .code-header .stats .left{
29 29 float:left;
30 30 }
31 31 div.codeblock .code-header .stats .left.item{
32 32 float:left;
33 33 padding: 0 9px 0 9px;
34 34 border-right:1px solid #ccc;
35 35 }
36 36 div.codeblock .code-header .stats .left.item.last{
37 37 border-right:none;
38 38 }
39 39 div.codeblock .code-header .stats .buttons{
40 40 float:right;
41 41 padding-right:4px;
42 42 }
43 43
44 44 div.codeblock .code-header .author{
45 45 margin-left:25px;
46 46 font-weight: bold;
47 47 height: 25px;
48 48 }
49 49 div.codeblock .code-header .author .user{
50 50 padding-top:3px;
51 51 }
52 52 div.codeblock .code-header .commit{
53 53 margin-left:25px;
54 54 font-weight: normal;
55 55 white-space:pre;
56 56 }
57 57
58 58 div.codeblock .code-body table{
59 59 width: 0 !important;
60 60 border: 0px !important;
61 61 }
62 62 div.codeblock .code-body table td {
63 63 border: 0px !important;
64 64 }
65 65 div.code-body {
66 66 background-color: #FFFFFF;
67 67 }
68 68
69 69 div.codeblock .code-header .search-path {
70 70 padding: 0px 0px 0px 10px;
71 71 }
72 72
73 73 div.search-code-body {
74 74 background-color: #FFFFFF;
75 75 padding: 5px 0px 5px 10px;
76 76 }
77 77
78 78 div.search-code-body pre .match{
79 79 background-color: #FAFFA6;
80 80 }
81 81 div.search-code-body pre .break{
82 82 background-color: #DDE7EF;
83 83 width: 100%;
84 84 color: #747474;
85 85 display: block;
86 86
87 87 }
88 88 div.annotatediv{
89 89 margin-left:2px;
90 90 margin-right:4px;
91 91 }
92 92 .code-highlight {
93 93 padding: 0px;
94 94 margin-top: 5px;
95 95 margin-bottom: 5px;
96 96 border-left: 2px solid #ccc;
97 97 }
98 98 .code-highlight pre, .linenodiv pre {
99 99 padding: 5px;
100 100 margin: 0;
101 101 }
102 102 .code-highlight pre div:target {
103 103 background-color: #FFFFBE !important;
104 104 }
105 105
106 106 .linenos a { text-decoration: none; }
107 107
108 108 .code { display: block; }
109 .code-highlight .hll { background-color: #ffffcc }
110 .code-highlight .c { color: #408080; font-style: italic } /* Comment */
111 .code-highlight .err { border: 1px solid #FF0000 } /* Error */
112 .code-highlight .k { color: #008000; font-weight: bold } /* Keyword */
113 .code-highlight .o { color: #666666 } /* Operator */
114 .code-highlight .cm { color: #408080; font-style: italic } /* Comment.Multiline */
115 .code-highlight .cp { color: #BC7A00 } /* Comment.Preproc */
116 .code-highlight .c1 { color: #408080; font-style: italic } /* Comment.Single */
117 .code-highlight .cs { color: #408080; font-style: italic } /* Comment.Special */
118 .code-highlight .gd { color: #A00000 } /* Generic.Deleted */
119 .code-highlight .ge { font-style: italic } /* Generic.Emph */
120 .code-highlight .gr { color: #FF0000 } /* Generic.Error */
121 .code-highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
122 .code-highlight .gi { color: #00A000 } /* Generic.Inserted */
123 .code-highlight .go { color: #808080 } /* Generic.Output */
124 .code-highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
125 .code-highlight .gs { font-weight: bold } /* Generic.Strong */
126 .code-highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
127 .code-highlight .gt { color: #0040D0 } /* Generic.Traceback */
128 .code-highlight .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
129 .code-highlight .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
130 .code-highlight .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
131 .code-highlight .kp { color: #008000 } /* Keyword.Pseudo */
132 .code-highlight .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
133 .code-highlight .kt { color: #B00040 } /* Keyword.Type */
134 .code-highlight .m { color: #666666 } /* Literal.Number */
135 .code-highlight .s { color: #BA2121 } /* Literal.String */
136 .code-highlight .na { color: #7D9029 } /* Name.Attribute */
137 .code-highlight .nb { color: #008000 } /* Name.Builtin */
138 .code-highlight .nc { color: #0000FF; font-weight: bold } /* Name.Class */
139 .code-highlight .no { color: #880000 } /* Name.Constant */
140 .code-highlight .nd { color: #AA22FF } /* Name.Decorator */
141 .code-highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */
142 .code-highlight .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
143 .code-highlight .nf { color: #0000FF } /* Name.Function */
144 .code-highlight .nl { color: #A0A000 } /* Name.Label */
145 .code-highlight .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
146 .code-highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */
147 .code-highlight .nv { color: #19177C } /* Name.Variable */
148 .code-highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
149 .code-highlight .w { color: #bbbbbb } /* Text.Whitespace */
150 .code-highlight .mf { color: #666666 } /* Literal.Number.Float */
151 .code-highlight .mh { color: #666666 } /* Literal.Number.Hex */
152 .code-highlight .mi { color: #666666 } /* Literal.Number.Integer */
153 .code-highlight .mo { color: #666666 } /* Literal.Number.Oct */
154 .code-highlight .sb { color: #BA2121 } /* Literal.String.Backtick */
155 .code-highlight .sc { color: #BA2121 } /* Literal.String.Char */
156 .code-highlight .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
157 .code-highlight .s2 { color: #BA2121 } /* Literal.String.Double */
158 .code-highlight .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
159 .code-highlight .sh { color: #BA2121 } /* Literal.String.Heredoc */
160 .code-highlight .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
161 .code-highlight .sx { color: #008000 } /* Literal.String.Other */
162 .code-highlight .sr { color: #BB6688 } /* Literal.String.Regex */
163 .code-highlight .s1 { color: #BA2121 } /* Literal.String.Single */
164 .code-highlight .ss { color: #19177C } /* Literal.String.Symbol */
165 .code-highlight .bp { color: #008000 } /* Name.Builtin.Pseudo */
166 .code-highlight .vc { color: #19177C } /* Name.Variable.Class */
167 .code-highlight .vg { color: #19177C } /* Name.Variable.Global */
168 .code-highlight .vi { color: #19177C } /* Name.Variable.Instance */
169 .code-highlight .il { color: #666666 } /* Literal.Number.Integer.Long */
109 .code-highlight .hll, .codehilite .hll { background-color: #ffffcc }
110 .code-highlight .c, .codehilite .c { color: #408080; font-style: italic } /* Comment */
111 .code-highlight .err, .codehilite .err { border: 1px solid #FF0000 } /* Error */
112 .code-highlight .k, .codehilite .k { color: #008000; font-weight: bold } /* Keyword */
113 .code-highlight .o, .codehilite .o { color: #666666 } /* Operator */
114 .code-highlight .cm, .codehilite .cm { color: #408080; font-style: italic } /* Comment.Multiline */
115 .code-highlight .cp, .codehilite .cp { color: #BC7A00 } /* Comment.Preproc */
116 .code-highlight .c1, .codehilite .c1 { color: #408080; font-style: italic } /* Comment.Single */
117 .code-highlight .cs, .codehilite .cs { color: #408080; font-style: italic } /* Comment.Special */
118 .code-highlight .gd, .codehilite .gd { color: #A00000 } /* Generic.Deleted */
119 .code-highlight .ge, .codehilite .ge { font-style: italic } /* Generic.Emph */
120 .code-highlight .gr, .codehilite .gr { color: #FF0000 } /* Generic.Error */
121 .code-highlight .gh, .codehilite .gh { color: #000080; font-weight: bold } /* Generic.Heading */
122 .code-highlight .gi, .codehilite .gi { color: #00A000 } /* Generic.Inserted */
123 .code-highlight .go, .codehilite .go { color: #808080 } /* Generic.Output */
124 .code-highlight .gp, .codehilite .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
125 .code-highlight .gs, .codehilite .gs { font-weight: bold } /* Generic.Strong */
126 .code-highlight .gu, .codehilite .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
127 .code-highlight .gt, .codehilite .gt { color: #0040D0 } /* Generic.Traceback */
128 .code-highlight .kc, .codehilite .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
129 .code-highlight .kd, .codehilite .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
130 .code-highlight .kn, .codehilite .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
131 .code-highlight .kp, .codehilite .kp { color: #008000 } /* Keyword.Pseudo */
132 .code-highlight .kr, .codehilite .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
133 .code-highlight .kt, .codehilite .kt { color: #B00040 } /* Keyword.Type */
134 .code-highlight .m, .codehilite .m { color: #666666 } /* Literal.Number */
135 .code-highlight .s, .codehilite .s { color: #BA2121 } /* Literal.String */
136 .code-highlight .na, .codehilite .na { color: #7D9029 } /* Name.Attribute */
137 .code-highlight .nb, .codehilite .nb { color: #008000 } /* Name.Builtin */
138 .code-highlight .nc, .codehilite .nc { color: #0000FF; font-weight: bold } /* Name.Class */
139 .code-highlight .no, .codehilite .no { color: #880000 } /* Name.Constant */
140 .code-highlight .nd, .codehilite .nd { color: #AA22FF } /* Name.Decorator */
141 .code-highlight .ni, .codehilite .ni { color: #999999; font-weight: bold } /* Name.Entity */
142 .code-highlight .ne, .codehilite .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
143 .code-highlight .nf, .codehilite .nf { color: #0000FF } /* Name.Function */
144 .code-highlight .nl, .codehilite .nl { color: #A0A000 } /* Name.Label */
145 .code-highlight .nn, .codehilite .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
146 .code-highlight .nt, .codehilite .nt { color: #008000; font-weight: bold } /* Name.Tag */
147 .code-highlight .nv, .codehilite .nv { color: #19177C } /* Name.Variable */
148 .code-highlight .ow, .codehilite .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
149 .code-highlight .w, .codehilite .w { color: #bbbbbb } /* Text.Whitespace */
150 .code-highlight .mf, .codehilite .mf { color: #666666 } /* Literal.Number.Float */
151 .code-highlight .mh, .codehilite .mh { color: #666666 } /* Literal.Number.Hex */
152 .code-highlight .mi, .codehilite .mi { color: #666666 } /* Literal.Number.Integer */
153 .code-highlight .mo, .codehilite .mo { color: #666666 } /* Literal.Number.Oct */
154 .code-highlight .sb, .codehilite .sb { color: #BA2121 } /* Literal.String.Backtick */
155 .code-highlight .sc, .codehilite .sc { color: #BA2121 } /* Literal.String.Char */
156 .code-highlight .sd, .codehilite .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
157 .code-highlight .s2, .codehilite .s2 { color: #BA2121 } /* Literal.String.Double */
158 .code-highlight .se, .codehilite .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
159 .code-highlight .sh, .codehilite .sh { color: #BA2121 } /* Literal.String.Heredoc */
160 .code-highlight .si, .codehilite .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
161 .code-highlight .sx, .codehilite .sx { color: #008000 } /* Literal.String.Other */
162 .code-highlight .sr, .codehilite .sr { color: #BB6688 } /* Literal.String.Regex */
163 .code-highlight .s1, .codehilite .s1 { color: #BA2121 } /* Literal.String.Single */
164 .code-highlight .ss, .codehilite .ss { color: #19177C } /* Literal.String.Symbol */
165 .code-highlight .bp, .codehilite .bp { color: #008000 } /* Name.Builtin.Pseudo */
166 .code-highlight .vc, .codehilite .vc { color: #19177C } /* Name.Variable.Class */
167 .code-highlight .vg, .codehilite .vg { color: #19177C } /* Name.Variable.Global */
168 .code-highlight .vi, .codehilite .vi { color: #19177C } /* Name.Variable.Instance */
169 .code-highlight .il, .codehilite .il { color: #666666 } /* Literal.Number.Integer.Long */
General Comments 0
You need to be logged in to leave comments. Login now