##// END OF EJS Templates
activate codehilite extension on markdown renderer, inspired by bitbucket blog ;)
marcink -
r1996:05929879 beta
parent child Browse files
Show More
@@ -31,12 +31,13 b' from rhodecode.lib import safe_unicode'
31
31
32 log = logging.getLogger(__name__)
32 log = logging.getLogger(__name__)
33
33
34
34 class MarkupRenderer(object):
35 class MarkupRenderer(object):
35 RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES = ['include', 'meta', 'raw']
36 RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES = ['include', 'meta', 'raw']
36
37
37 MARKDOWN_PAT = re.compile(r'md|mkdn?|mdown|markdown',re.IGNORECASE)
38 MARKDOWN_PAT = re.compile(r'md|mkdn?|mdown|markdown', re.IGNORECASE)
38 RST_PAT = re.compile(r're?st',re.IGNORECASE)
39 RST_PAT = re.compile(r're?st', re.IGNORECASE)
39 PLAIN_PAT = re.compile(r'readme',re.IGNORECASE)
40 PLAIN_PAT = re.compile(r'readme', re.IGNORECASE)
40
41
41 def __detect_renderer(self, source, filename=None):
42 def __detect_renderer(self, source, filename=None):
42 """
43 """
@@ -60,7 +61,6 b' class MarkupRenderer(object):'
60
61
61 return getattr(MarkupRenderer, detected_renderer)
62 return getattr(MarkupRenderer, detected_renderer)
62
63
63
64 def render(self, source, filename=None):
64 def render(self, source, filename=None):
65 """
65 """
66 Renders a given filename using detected renderer
66 Renders a given filename using detected renderer
@@ -78,31 +78,30 b' class MarkupRenderer(object):'
78 @classmethod
78 @classmethod
79 def plain(cls, source):
79 def plain(cls, source):
80 source = safe_unicode(source)
80 source = safe_unicode(source)
81
81 def urlify_text(text):
82 def urlify_text(text):
82 url_pat = re.compile(r'(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]'
83 url_pat = re.compile(r'(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]'
83 '|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)')
84 '|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)')
84
85
85 def url_func(match_obj):
86 def url_func(match_obj):
86 url_full = match_obj.groups()[0]
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 return url_pat.sub(url_func, text)
90 return url_pat.sub(url_func, text)
90
91
91 source = urlify_text(source)
92 source = urlify_text(source)
92 return '<br />' + source.replace("\n", '<br />')
93 return '<br />' + source.replace("\n", '<br />')
93
94
94
95 @classmethod
95 @classmethod
96 def markdown(cls, source):
96 def markdown(cls, source):
97 source = safe_unicode(source)
97 source = safe_unicode(source)
98 try:
98 try:
99 import markdown as __markdown
99 import markdown as __markdown
100 return __markdown.markdown(source)
100 return __markdown.markdown(source, ['codehilite'])
101 except ImportError:
101 except ImportError:
102 log.warning('Install markdown to use this function')
102 log.warning('Install markdown to use this function')
103 return cls.plain(source)
103 return cls.plain(source)
104
104
105
106 @classmethod
105 @classmethod
107 def rst(cls, source):
106 def rst(cls, source):
108 source = safe_unicode(source)
107 source = safe_unicode(source)
@@ -113,7 +112,7 b' class MarkupRenderer(object):'
113 cls.RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES])
112 cls.RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES])
114
113
115 docutils_settings.update({'input_encoding': 'unicode',
114 docutils_settings.update({'input_encoding': 'unicode',
116 'report_level':4})
115 'report_level': 4})
117
116
118 for k, v in docutils_settings.iteritems():
117 for k, v in docutils_settings.iteritems():
119 directives.register_directive(k, v)
118 directives.register_directive(k, v)
@@ -106,64 +106,64 b' div.annotatediv{'
106 .linenos a { text-decoration: none; }
106 .linenos a { text-decoration: none; }
107
107
108 .code { display: block; }
108 .code { display: block; }
109 .code-highlight .hll { background-color: #ffffcc }
109 .code-highlight .hll, .codehilite .hll { background-color: #ffffcc }
110 .code-highlight .c { color: #408080; font-style: italic } /* Comment */
110 .code-highlight .c, .codehilite .c { color: #408080; font-style: italic } /* Comment */
111 .code-highlight .err { border: 1px solid #FF0000 } /* Error */
111 .code-highlight .err, .codehilite .err { border: 1px solid #FF0000 } /* Error */
112 .code-highlight .k { color: #008000; font-weight: bold } /* Keyword */
112 .code-highlight .k, .codehilite .k { color: #008000; font-weight: bold } /* Keyword */
113 .code-highlight .o { color: #666666 } /* Operator */
113 .code-highlight .o, .codehilite .o { color: #666666 } /* Operator */
114 .code-highlight .cm { color: #408080; font-style: italic } /* Comment.Multiline */
114 .code-highlight .cm, .codehilite .cm { color: #408080; font-style: italic } /* Comment.Multiline */
115 .code-highlight .cp { color: #BC7A00 } /* Comment.Preproc */
115 .code-highlight .cp, .codehilite .cp { color: #BC7A00 } /* Comment.Preproc */
116 .code-highlight .c1 { color: #408080; font-style: italic } /* Comment.Single */
116 .code-highlight .c1, .codehilite .c1 { color: #408080; font-style: italic } /* Comment.Single */
117 .code-highlight .cs { color: #408080; font-style: italic } /* Comment.Special */
117 .code-highlight .cs, .codehilite .cs { color: #408080; font-style: italic } /* Comment.Special */
118 .code-highlight .gd { color: #A00000 } /* Generic.Deleted */
118 .code-highlight .gd, .codehilite .gd { color: #A00000 } /* Generic.Deleted */
119 .code-highlight .ge { font-style: italic } /* Generic.Emph */
119 .code-highlight .ge, .codehilite .ge { font-style: italic } /* Generic.Emph */
120 .code-highlight .gr { color: #FF0000 } /* Generic.Error */
120 .code-highlight .gr, .codehilite .gr { color: #FF0000 } /* Generic.Error */
121 .code-highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
121 .code-highlight .gh, .codehilite .gh { color: #000080; font-weight: bold } /* Generic.Heading */
122 .code-highlight .gi { color: #00A000 } /* Generic.Inserted */
122 .code-highlight .gi, .codehilite .gi { color: #00A000 } /* Generic.Inserted */
123 .code-highlight .go { color: #808080 } /* Generic.Output */
123 .code-highlight .go, .codehilite .go { color: #808080 } /* Generic.Output */
124 .code-highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
124 .code-highlight .gp, .codehilite .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
125 .code-highlight .gs { font-weight: bold } /* Generic.Strong */
125 .code-highlight .gs, .codehilite .gs { font-weight: bold } /* Generic.Strong */
126 .code-highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
126 .code-highlight .gu, .codehilite .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
127 .code-highlight .gt { color: #0040D0 } /* Generic.Traceback */
127 .code-highlight .gt, .codehilite .gt { color: #0040D0 } /* Generic.Traceback */
128 .code-highlight .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
128 .code-highlight .kc, .codehilite .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
129 .code-highlight .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
129 .code-highlight .kd, .codehilite .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
130 .code-highlight .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
130 .code-highlight .kn, .codehilite .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
131 .code-highlight .kp { color: #008000 } /* Keyword.Pseudo */
131 .code-highlight .kp, .codehilite .kp { color: #008000 } /* Keyword.Pseudo */
132 .code-highlight .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
132 .code-highlight .kr, .codehilite .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
133 .code-highlight .kt { color: #B00040 } /* Keyword.Type */
133 .code-highlight .kt, .codehilite .kt { color: #B00040 } /* Keyword.Type */
134 .code-highlight .m { color: #666666 } /* Literal.Number */
134 .code-highlight .m, .codehilite .m { color: #666666 } /* Literal.Number */
135 .code-highlight .s { color: #BA2121 } /* Literal.String */
135 .code-highlight .s, .codehilite .s { color: #BA2121 } /* Literal.String */
136 .code-highlight .na { color: #7D9029 } /* Name.Attribute */
136 .code-highlight .na, .codehilite .na { color: #7D9029 } /* Name.Attribute */
137 .code-highlight .nb { color: #008000 } /* Name.Builtin */
137 .code-highlight .nb, .codehilite .nb { color: #008000 } /* Name.Builtin */
138 .code-highlight .nc { color: #0000FF; font-weight: bold } /* Name.Class */
138 .code-highlight .nc, .codehilite .nc { color: #0000FF; font-weight: bold } /* Name.Class */
139 .code-highlight .no { color: #880000 } /* Name.Constant */
139 .code-highlight .no, .codehilite .no { color: #880000 } /* Name.Constant */
140 .code-highlight .nd { color: #AA22FF } /* Name.Decorator */
140 .code-highlight .nd, .codehilite .nd { color: #AA22FF } /* Name.Decorator */
141 .code-highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */
141 .code-highlight .ni, .codehilite .ni { color: #999999; font-weight: bold } /* Name.Entity */
142 .code-highlight .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
142 .code-highlight .ne, .codehilite .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
143 .code-highlight .nf { color: #0000FF } /* Name.Function */
143 .code-highlight .nf, .codehilite .nf { color: #0000FF } /* Name.Function */
144 .code-highlight .nl { color: #A0A000 } /* Name.Label */
144 .code-highlight .nl, .codehilite .nl { color: #A0A000 } /* Name.Label */
145 .code-highlight .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
145 .code-highlight .nn, .codehilite .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
146 .code-highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */
146 .code-highlight .nt, .codehilite .nt { color: #008000; font-weight: bold } /* Name.Tag */
147 .code-highlight .nv { color: #19177C } /* Name.Variable */
147 .code-highlight .nv, .codehilite .nv { color: #19177C } /* Name.Variable */
148 .code-highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
148 .code-highlight .ow, .codehilite .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
149 .code-highlight .w { color: #bbbbbb } /* Text.Whitespace */
149 .code-highlight .w, .codehilite .w { color: #bbbbbb } /* Text.Whitespace */
150 .code-highlight .mf { color: #666666 } /* Literal.Number.Float */
150 .code-highlight .mf, .codehilite .mf { color: #666666 } /* Literal.Number.Float */
151 .code-highlight .mh { color: #666666 } /* Literal.Number.Hex */
151 .code-highlight .mh, .codehilite .mh { color: #666666 } /* Literal.Number.Hex */
152 .code-highlight .mi { color: #666666 } /* Literal.Number.Integer */
152 .code-highlight .mi, .codehilite .mi { color: #666666 } /* Literal.Number.Integer */
153 .code-highlight .mo { color: #666666 } /* Literal.Number.Oct */
153 .code-highlight .mo, .codehilite .mo { color: #666666 } /* Literal.Number.Oct */
154 .code-highlight .sb { color: #BA2121 } /* Literal.String.Backtick */
154 .code-highlight .sb, .codehilite .sb { color: #BA2121 } /* Literal.String.Backtick */
155 .code-highlight .sc { color: #BA2121 } /* Literal.String.Char */
155 .code-highlight .sc, .codehilite .sc { color: #BA2121 } /* Literal.String.Char */
156 .code-highlight .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
156 .code-highlight .sd, .codehilite .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
157 .code-highlight .s2 { color: #BA2121 } /* Literal.String.Double */
157 .code-highlight .s2, .codehilite .s2 { color: #BA2121 } /* Literal.String.Double */
158 .code-highlight .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
158 .code-highlight .se, .codehilite .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
159 .code-highlight .sh { color: #BA2121 } /* Literal.String.Heredoc */
159 .code-highlight .sh, .codehilite .sh { color: #BA2121 } /* Literal.String.Heredoc */
160 .code-highlight .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
160 .code-highlight .si, .codehilite .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
161 .code-highlight .sx { color: #008000 } /* Literal.String.Other */
161 .code-highlight .sx, .codehilite .sx { color: #008000 } /* Literal.String.Other */
162 .code-highlight .sr { color: #BB6688 } /* Literal.String.Regex */
162 .code-highlight .sr, .codehilite .sr { color: #BB6688 } /* Literal.String.Regex */
163 .code-highlight .s1 { color: #BA2121 } /* Literal.String.Single */
163 .code-highlight .s1, .codehilite .s1 { color: #BA2121 } /* Literal.String.Single */
164 .code-highlight .ss { color: #19177C } /* Literal.String.Symbol */
164 .code-highlight .ss, .codehilite .ss { color: #19177C } /* Literal.String.Symbol */
165 .code-highlight .bp { color: #008000 } /* Name.Builtin.Pseudo */
165 .code-highlight .bp, .codehilite .bp { color: #008000 } /* Name.Builtin.Pseudo */
166 .code-highlight .vc { color: #19177C } /* Name.Variable.Class */
166 .code-highlight .vc, .codehilite .vc { color: #19177C } /* Name.Variable.Class */
167 .code-highlight .vg { color: #19177C } /* Name.Variable.Global */
167 .code-highlight .vg, .codehilite .vg { color: #19177C } /* Name.Variable.Global */
168 .code-highlight .vi { color: #19177C } /* Name.Variable.Instance */
168 .code-highlight .vi, .codehilite .vi { color: #19177C } /* Name.Variable.Instance */
169 .code-highlight .il { color: #666666 } /* Literal.Number.Integer.Long */
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