Skip to content

Commit

Permalink
1.7.1dev: merge [17873,17876-17878] from 1.6-stable (fix for #13864,1…
Browse files Browse the repository at this point in the history
…3865,13866)

git-svn-id: http://trac.edgewall.org/intertrac/log:/trunk@17879 af82e41b-90c4-0310-8c96-b1721e28e2e2
  • Loading branch information
jomae committed Feb 5, 2025
1 parent 4e4d052 commit d90d2d8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 31 deletions.
45 changes: 33 additions & 12 deletions trac/mimeview/pygments.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@
__all__ = ['PygmentsRenderer']


if hasattr(HtmlFormatter, 'get_background_style_defs') and \
hasattr(HtmlFormatter, 'get_token_style_defs'):
def _get_style_defs(style, arg):
lines = []
formatter = HtmlFormatter(style=style)
lines.extend(formatter.get_background_style_defs(arg))
lines.extend(formatter.get_token_style_defs(arg))
return '\n'.join(lines)
else:
def _get_style_defs(style, arg):
formatter = HtmlFormatter(style=style)
return formatter.get_style_defs(arg)


class PygmentsRenderer(Component):
"""HTML renderer for syntax highlighting based on Pygments."""

Expand Down Expand Up @@ -140,7 +154,7 @@ def get_preference_panels(self, req):
yield 'pygments', _('Syntax Highlighting')

def render_preference_panel(self, req, panel):
styles = list(get_all_styles())
styles = set(get_all_styles())

if req.method == 'POST':
style = req.args.get('style')
Expand All @@ -151,15 +165,25 @@ def render_preference_panel(self, req, panel):
add_notice(req, _("Your preferences have been saved."))
req.redirect(req.href.prefs(panel or None))

for style in sorted(styles):
add_stylesheet(req, '/pygments/%s.css' % style, title=style.title())
def style_defs(style):
cls = get_style_by_name(style)
selector = '.trac-pygments-%s div.code pre' % style
return _get_style_defs(cls, selector)

default_style = self.default_style
if default_style not in styles:
default_style = 'trac'
selection = req.session.get('pygments_style')
if selection not in styles:
selection = default_style
output = self._generate('html', self.EXAMPLE)
add_script_data(req, default_style=self.default_style.title())
add_script_data(req, default_style=default_style, selection=selection)
return 'prefs_pygments.html', {
'output': output,
'selection': req.session.get('pygments_style'),
'default_style': self.default_style,
'styles': styles
'selection': selection,
'default_style': default_style,
'styles': sorted(styles),
'style_defs': style_defs,
}

# IRequestHandler methods
Expand All @@ -186,11 +210,8 @@ def process_request(self, req):
req.end_headers()
return

formatter = HtmlFormatter(style=style_cls)
content = '\n\n'.join([
formatter.get_style_defs('div.code pre'),
formatter.get_style_defs('table.code td')
]).encode('utf-8')
content = _get_style_defs(style_cls, ['div.code pre', 'table.code td'])
content = content.encode('utf-8')

req.send_response(200)
req.send_header('Content-Type', 'text/css; charset=utf-8')
Expand Down
29 changes: 14 additions & 15 deletions trac/mimeview/templates/prefs_pygments.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,21 @@
div.code pre { border: 1px solid #999; font-size: 90%; margin: 1em 2em;
padding: 5px; width: 60%;
}
# for style_ in styles:
${- style_defs(style_)|safe}
# endfor
</style>
<script>
(function($){
window.switchStyleSheet = function(title) {
$('link[rel="stylesheet"][title]').each(function() {
this.disabled = true; // needed to force correct behavior for WebKit (see #10923)
this.disabled = this.getAttribute('title') != title;
});
}
})(jQuery);
jQuery(function($) {
$("#pygment_theme").attr("autocomplete", "off").on('change keypress', function() {
var idx = this.selectedIndex;
var text = this.options[idx].text;
var stylesheet = idx > 0 ? text : default_style;
switchStyleSheet(stylesheet);
var selection = window.selection;
var default_style = window.default_style;
var $theme = $('#pygment_theme');
var $field = $theme.closest('div.field');
$theme.attr("autocomplete", "off").on('change keypress', function() {
var style = this.value || default_style;
$field.removeClass('trac-pygments-' + selection)
.addClass('trac-pygments-' + style);
selection = style;
}).change();
});
</script>
Expand All @@ -48,12 +47,12 @@

<body>
# block prefpanel
<div class="field">
<div class="field trac-pygments-${selection}">
<label>${_("Style:")}
<select id="pygment_theme" name="style">
<option value="">${_("Default: %(default)s",
default=default_style.title())}</option>
# for style in sorted(styles):
# for style in styles:
<option${{'value': style,
'selected': selection == style
}|htmlattr}>${style.title()}</option>
Expand Down
8 changes: 8 additions & 0 deletions trac/mimeview/tests/pygments.data
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
# python_hello_pygments_2.1plus
<span class="k">def</span> <span class="nf">hello</span><span class="p">():</span>
<span class="k">return</span> <span class="s2">&quot;Hello World!&quot;</span>
# python_hello_pygments_2.19plus
<span class="k">def</span><span class="w"> </span><span class="nf">hello</span><span class="p">():</span>
<span class="k">return</span> <span class="s2">&quot;Hello World!&quot;</span>
# python_hello_mimeview
<div class="code"><pre>
<span class="k">def</span> <span class="nf">hello</span><span class="p">():</span>
Expand All @@ -14,6 +17,11 @@
<span class="k">def</span> <span class="nf">hello</span><span class="p">():</span>
<span class="k">return</span> <span class="s2">&quot;Hello World!&quot;</span>
</pre></div>
# python_hello_mimeview_pygments_2.19plus
<div class="code"><pre>
<span class="k">def</span><span class="w"> </span><span class="nf">hello</span><span class="p">():</span>
<span class="k">return</span> <span class="s2">&quot;Hello World!&quot;</span>
</pre></div>
# python_with_lineno_1
<div class="wiki-code"><table class="code"><thead><tr><th class="lineno" title="Line numbers">Line</th><th class="content"> </th></tr></thead><tbody><tr><th id="a-L1"><a href="#a-L1">1</a></th><td><span class="k">print</span> <span class="s">&#39;this is a python sample&#39;</span>
</td></tr><tr><th id="a-L2"><a href="#a-L2">2</a></th><td><span class="n">a</span> <span class="o">=</span> <span class="n">b</span><span class="o">+</span><span class="mi">3</span>
Expand Down
14 changes: 10 additions & 4 deletions trac/mimeview/tests/pygments.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ def hello():
"""))
self.assertTrue(result)
if pygments_version < parse_version('2.1'):
self._test('python_hello', result)
expected_id = 'python_hello'
elif pygments_version < parse_version('2.19'):
expected_id = 'python_hello_pygments_2.1plus'
else:
self._test('python_hello_pygments_2.1plus', result)
expected_id = 'python_hello_pygments_2.19plus'
self._test(expected_id, result)

def test_python_hello_mimeview(self):
"""
Expand All @@ -102,9 +105,12 @@ def hello():
"""))
self.assertTrue(result)
if pygments_version < parse_version('2.1'):
self._test('python_hello_mimeview', result)
expected_id = 'python_hello_mimeview'
elif pygments_version < parse_version('2.19'):
expected_id = 'python_hello_mimeview_pygments_2.1plus'
else:
self._test('python_hello_mimeview_pygments_2.1plus', result)
expected_id = 'python_hello_mimeview_pygments_2.19plus'
self._test(expected_id, result)

def test_python_with_lineno(self):
result = format_to_html(self.env, self.context, textwrap.dedent("""\
Expand Down

0 comments on commit d90d2d8

Please sign in to comment.