http://hg.thadeusb.com/Web/web2py_utils/file/697470f78d16/web2py_utils/output.py
Line 20 - 48 could possibly be adapted.
Basicly use a DOTALL regex to find \n (what about \r too, if your on
windows), you might want to use os.newline, and then a dotall regex
for the pre code blockqoute etc.
Then doing the regex match, you look and see if the line starts with
something you don't want to alter, if it does then don't do anything,
otherwise return '', or in your case <br />
This should do it.... Just remove the regex looking for multiple
spaces, and set to return <br />
def compress_output(response,
21 startswith = [
22 '<pre',
23 '<textarea',
24 '<blockquote',
25 ],
26 funcs=[],
27 debug=False,):
28
29 def save_pre(match):
30 s = match.group()
31 for sw in startswith:
32 if s.startswith(sw):
33 return s
34 return '<br />' # this turns whitespace into nothing
35
36 def compress_response(d):
37 if callable(d):
38 d = d()
39 if isinstance(d, dict):
40 cpat =
re.compile(r'[\n\t\r\f\v]|(?s)<pre(.*?)</pre>|(?s)<blockquote(.*?)</blockquote>|(?s)<textarea(.*?)</textarea>')
41 d = cpat.sub(save_pre, response.render(d))
42 for f in funcs:
43 if callable(f):
44 f(d)
45 return d
46
47 if not debug:
48 response._caller = compress_response
--
Thadeus
On Sat, Jun 5, 2010 at 12:14 PM, mr.freeze <[email protected]> wrote:
> I'm trying to replace all newline characters in a chunk of text with
> BRs except for those inside PRE tags. I can't seem to make it work.
> Any regex gurus out there?
>
>