Skip to content

Commit

Permalink
1.6.1dev: use the inline style element in prefs/pygments panel in ord…
Browse files Browse the repository at this point in the history
…er to prevent many requests on page load (closes #13864)

git-svn-id: http://trac.edgewall.org/intertrac/log:/branches/1.6-stable@17878 af82e41b-90c4-0310-8c96-b1721e28e2e2
  • Loading branch information
jomae committed Feb 5, 2025
1 parent 528870b commit ed4c150
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
24 changes: 17 additions & 7 deletions trac/mimeview/pygments.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,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 @@ -165,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 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

0 comments on commit ed4c150

Please sign in to comment.