Django Spaceless Middleware Hack Around Pygments To Avoid One-Liners
Efficiency is key - Spaceless Middleware is a great help. However, Pygments does not play nicely. A quick hack allows specific request URL's to be untouched by your SpacelessMiddleware. This got me half way finished. Thanks to the Code Warrior Luke Stamm over at SOEKUL LLC the notion of controlling spaceless middleware has been greatly improved upon. SOEKUL contrived the awesome_strip_space_between_tags function enhanced with pre tag awesomeness allowing a set of HTML comments to escape your SpacelessMiddleware. Now you can save white space only where you need to without extra hassle of digging through existing templates!
middleware.SpacelessMiddleware
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | from django.utils.html import strip_spaces_between_tags as short
from django.conf import settings
from functions import awesome_strip_spaces_between_tags
class SpacelessMiddleware(object):
def process_response(self, request, response):
if 'text/html' in response['Content-Type']:
dospaceless = True
spacelessurl = request.get_full_path()
for exp in settings.SPACELESS_IGNORE_REGEXPS:
if re.match(exp,spacelessurl):
dospaceless = False
break
if dospaceless:
response.content = awesome_strip_spaces_between_tags(response.content)
return response
|
settings.py
1 2 3 | SPACELESS_IGNORE_REGEXPS = (
r'/resources/software/.*',
)
|
awesome_strip_spaces_between_tags()
Without resorting to {% spaceless %} scattered throughout your templates, you can use the following function instead of django.utils.html.strip_spaces_between_tags directly in conjunction with HTML comments marking the beginning and end of your white space friendly zone in your template. For use in clearing up the Pygments one-line issue, place the HTML comments just around pygments output in your custom template and the rest of your site will remain spaceless!
If you are using cmsplugin_pygments you will want to include sekizai_tags {% addtoblock %} functionality so the required style for pretty syntax highlighting is thrown into the head were it belongs (and only once)!
templates/pygments_custom.html
1 2 3 4 5 6 7 8 9 10 11 | {% load sekizai_tags %}
{% addtoblock "css" %}
<style type="text/css">
{{ css|safe }}
</style>
{% endaddtoblock %}
<!-- BEGIN NO SPACELESS -->
{{ pygments_html|safe }}
<!-- END NO SPACELESS -->
|
functions.awesome_strip_spaces_between_tags
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import re
from django.utils.encoding import force_unicode
from django.utils.html import strip_spaces_between_tags as short
_RE_NO_SPACELESS_BDELIM = re.compile(u"<!-- BEGIN NO SPACELESS -->")
_RE_NO_SPACELESS_EDELIM = re.compile(u"<!-- END NO SPACELESS -->")
def awesome_strip_spaces_between_tags(value, _bre = _RE_NO_SPACELESS_BDELIM, _ere = _RE_NO_SPACELESS_EDELIM):
"""Returns the given HTML with spaces between tags removed.
Enhanced With Pre tag Awesomeness """
_val = force_unicode(value)
index = 0
rejoin = []
while index < len(_val):
_begin_match = _bre.search(_val, index)
if _begin_match is None:
break
begin_index = _begin_match.start()
begin_length = _begin_match.end() - begin_index
_end_match = _ere.search(_val, begin_index+begin_length)
if _end_match is None:
break
end_index = _end_match.start()
end_length = _end_match.end() - end_index
if begin_index > index:
rejoin.append(short(_val[index:begin_index]))
rejoin.append(_val[begin_index + begin_length:end_index])
index = end_index + end_length
if index < len(_val):
rejoin.append(short(_val[index:]))
return u''.join(rejoin)
|
Conclusion
Viewing the source of this page will give you an idea of what we are dealing with after everything is said and done. Preserving the pygments line numbers whitespace is ugly - as well as the pretty code whitespace - but somebody has to do it. This beats manually updating existing templates and is extremely easy to integrate. Enjoy!
