27 Dec 09 edit to protect script blocks
When web2py renders a template, it leaves extra blank lines in the output, making the html harder to read, and looking, well, generated. Removing those lines improves the appearance of the html.
The following snippet filters out those excess lines. Place it in a controller.py file to filter the output of that controller, or in a models.py file to filter the entire application.
import re
def filter(d):
if not isinstance(d,dict): d = d()
return re.compile(r'\n([ \t]+\n)+').sub('\n',response.render(d))
response._caller=filter
A slightly more complex approach leaves <pre> and <script> blocks untouched:
import re
def save_block(match):
s = match.group()
if s.startswith('<'): return s
return '/n'
def filter(d):
if not isinstance(d,dict): d = d()
pat = '\n([ \t]+\n)+' # match empty lines that start with spaces
pat += '|(?s)<script.*?</script' # OR script blocks
pat += '|(?s)<pre.*?</pre' # OR pre blocks
cpat = re.compile(pat)
return cpat.sub(save_block, response.render(d))
response._caller=filter
Note: This is said to mess up flash.
Comments (9)
0
thadeusb 15 years ago
0
aleksdj 15 years ago
0
aleksdj 15 years ago
0
thadeusb 14 years ago
0
kbochert 14 years ago
0
kbochert 14 years ago
0
thadeusb 14 years ago
0
thadeusb 14 years ago
0
mrfreeze 14 years ago