allura
Révision | 252cf5408438e1b4d53b63e88c832309595b2260 (tree) |
---|---|
l'heure | 2012-05-11 04:33:32 |
Auteur | bolkimen <bolkimen@yaho...> |
Commiter | Yaroslav Luzin |
[#4115] ticket:21 add code line link and highlight
@@ -75,7 +75,7 @@ class Globals(object): | ||
75 | 75 | self.oid_store = M.OpenIdStore() |
76 | 76 | |
77 | 77 | # Setup pygments |
78 | - self.pygments_formatter = pygments.formatters.HtmlFormatter( | |
78 | + self.pygments_formatter = utils.LineAnchorCodeHtmlFormatter( | |
79 | 79 | cssclass='codehilite', |
80 | 80 | linenos='inline') |
81 | 81 |
@@ -20,6 +20,7 @@ from pylons import tmpl_context as c | ||
20 | 20 | from paste.httpheaders import CACHE_CONTROL, EXPIRES |
21 | 21 | from webhelpers.html import literal |
22 | 22 | from webob import exc |
23 | +from pygments.formatters import HtmlFormatter | |
23 | 24 | |
24 | 25 | from ew import jinja2_ew as ew |
25 | 26 | from ming.utils import LazyProperty |
@@ -393,3 +394,20 @@ def postmortem_hook(etype, value, tb): # pragma no cover | ||
393 | 394 | sys.stderr.write('Entering post-mortem PDB shell\n') |
394 | 395 | traceback.print_exception(etype, value, tb) |
395 | 396 | pdb.post_mortem(tb) |
397 | + | |
398 | +class LineAnchorCodeHtmlFormatter(HtmlFormatter): | |
399 | + def _wrap_pre(self, inner): | |
400 | + style = [] | |
401 | + if self.prestyles: | |
402 | + style.append(self.prestyles) | |
403 | + if self.noclasses: | |
404 | + style.append('line-height: 125%') | |
405 | + style = '; '.join(style) | |
406 | + | |
407 | + num = self.linenostart | |
408 | + yield 0, ('<pre' + (style and ' style="%s"' % style) + '>') | |
409 | + for tup in inner: | |
410 | + yield (tup[0], '<a class="linelink" href="#codeline_%s">'\ | |
411 | + '<div id="codeline_%s" class="code_block">%s</div></a>' % (num, num, tup[1])) | |
412 | + num += 1 | |
413 | + yield 0, '</pre>' |
@@ -63,4 +63,8 @@ | ||
63 | 63 | .codehilite .il { color: #666666 } /* Literal.Number.Integer.Long */ |
64 | 64 | |
65 | 65 | .codehilite pre { margin:0; } |
66 | +.codehilite div { margin:0; padding: 0; } | |
67 | +.codehilite a.linelink { text-decoration: none; } | |
68 | +.codehilite a.linelink .code_block { width:100%; } | |
69 | +.codehilite a.linelink .code_block:hover { background-color: #ffff99; } | |
66 | 70 | .codehilite .lineno { background-color: #ebebeb; display:inline-block; padding:0 .5em; border-width: 0 1px 0 0; border-style: solid; border-color: #ddd; } |
@@ -18,6 +18,27 @@ | ||
18 | 18 | </a> |
19 | 19 | {% endblock %} |
20 | 20 | |
21 | +{% block extra_js %} | |
22 | +{{ super() }} | |
23 | +<script type="text/javascript">(function() { | |
24 | + var hash = window.location.hash.substring(1); | |
25 | + if (hash != '' && hash.substring(0, 9) == 'codeline_') { | |
26 | + $('#' + hash).css('background-color', '#ffff99'); | |
27 | + } | |
28 | + | |
29 | + $('.code_block').each(function(index, element) { | |
30 | + $(element).bind('click', function() { | |
31 | + var hash = window.location.hash.substring(1); | |
32 | + if (hash != '' && hash.substring(0, 9) == 'codeline_') { | |
33 | + $('#' + hash).css('background-color', 'transparent'); | |
34 | + } | |
35 | + $(element).css('background-color', '#ffff99'); | |
36 | + }); | |
37 | + }); | |
38 | +}()); | |
39 | +</script> | |
40 | +{% endblock %} | |
41 | + | |
21 | 42 | {% block content %} |
22 | 43 | {{ clone_info(c.app.repo) }} |
23 | 44 | {% if prev %} |