[Baypiggies] Html encoding question
kelly at nttmcl.com
Thu Jun 7 20:54:19 CEST 2007
Joshua Gallagher wrote::
> Hi Ken,
> If you want the string 'where x < z*5, and x > z+2' to show up as
> text, you could also put it within the <pre></pre> html tags.
> For example:
> sampleCode = '<pre>' + 'where x < z*5, and x > z+2' + '</pre>'
> All the best,
Unfortunately, you still need to convert the '<' and '>' characters
to HTML entities (as JJ's snippet did). Somewhere I seem to recall
having seen a web browser that did not interpret '<', '>', etc. in PRE
tags, but a quick test with both Firefox and IE7 shows that both of the
mainstream browsers will interpret them. Hence the need to convert them
to entities using cgi.escape().
As a quick demo, consider the following HTML document:
In both Firefox and IE7, Test 2 is rendered in bold.
For this reason, I have to run all code samples through the following
filter before posting them to my blog:
python -c 'import cgi; import sys; print cgi.escape(sys.stdin.read());'
Of course, as you mention, the code looks a lot better if I put it
inside a PRE tag. :)
> On 6/7/07, Shannon -jj Behrens <jjinux at gmail.com> wrote:
>> On 6/7/07, Ken Seehart <ken at seehart.com> wrote:
>>> Is there a handy function somewhere that converts a string so that the
>>> result will display as plain text in an html document? Somewhat
>>> analogous to urlencode but not exactly.
>>> So for example
>>> >>> html_str_encode('where x < z*5, and x > z+2')
>>> 'where x < z*5, and x > z+2'
>>> I'm pretty sure I've seen this before, but I just can't seem to find it
>>> today. Maybe I just need more coffee.
>>> - Ken
>>>>> import cgi
>>>>> cgi.escape('where x < z*5, and x > z+2')
>> 'where x < z*5, and x > z+2'
>> It's more simple than complete, but it works for the standard stuff.
>> Best Regards,
>> Baypiggies mailing list
>> Baypiggies at python.org
>> To change your subscription options or unsubscribe:
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
More information about the Baypiggies