By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,768 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,768 IT Pros & Developers. It's quick & easy.

printing indented html code

P: n/a
Is there a module or library anyone knows of that will print html code
indented? What I'd like would be for a function or class which works
like this:

htmlIndent(sys.stdout, '<html><head>foobar</head>...')

and will print somethinkg like this to stdout:

<html>
<head>
foobar
</head>
...

My current way of doing this kind of stuff is less than ideal and will
write such a function if it doesn't exist.

Thanks,
Lowell
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Lowell Kirsh wrote:
Is there a module or library anyone knows of that will print html code
indented? What I'd like would be for a function or class which works
like this:

htmlIndent(sys.stdout, '<html><head>foobar</head>...')

and will print somethinkg like this to stdout:

<html>
<head>
foobar
</head>
...

My current way of doing this kind of stuff is less than ideal and will
write such a function if it doesn't exist.

Thanks,
Lowell


There are lots of HTML pretty printers around, but for starters take a
look at this: http://www.oreilly.com/catalog/pytho...pter/ch05.html

HTH
================================================== ========================
TechBookReport - http://www.techbookreport.com
Jul 19 '05 #2

P: n/a
Thanks. At a glance, that looks like it's what I'm looking for.

Lowell

TechBookReport wrote:
Lowell Kirsh wrote:
Is there a module or library anyone knows of that will print html code
indented? What I'd like would be for a function or class which works
like this:

htmlIndent(sys.stdout, '<html><head>foobar</head>...')

and will print somethinkg like this to stdout:

<html>
<head>
foobar
</head>
...

My current way of doing this kind of stuff is less than ideal and will
write such a function if it doesn't exist.

Thanks,
Lowell

There are lots of HTML pretty printers around, but for starters take a
look at this: http://www.oreilly.com/catalog/pytho...pter/ch05.html

HTH
================================================== ========================
TechBookReport - http://www.techbookreport.com

Jul 19 '05 #3

P: n/a
On 6/24/05, Lowell Kirsh <lk****@cs.ubc.ca> wrote:
Is there a module or library anyone knows of that will print html code
indented?


Depends on whether you have to deal with xhtml, valid html or just
any, possibly invalid, "pseudo-html" that abounds on the net.

Here's an example of (too) simple html processing using standard
HTMLParser module:

from HTMLParser import HTMLParser

class Indenter(HTMLParser):

def __init__(self, out):
HTMLParser.__init__(self)
self.out = out
self.indent_level = 0

def write_line(self, text):
print >> self.out, '\t' * self.indent_level + text

def handle_starttag(self, tag, attrs):
self.write_line(
'<%s %s>' % (tag, ' '.join('%s=%s' % (k, v) for k, v in attrs))
)
self.indent_level += 1

def handle_endtag(self, tag):
self.indent_level -= 1
self.write_line('</%s>' % tag)

handle_data = write_line

# handle everything else...
# http://docs.python.org/lib/module-HTMLParser.html

if __name__ == '__main__':
import sys
i = Indenter(sys.stdout)
i.feed('<html><head>foobar</head><body color=0>body</body></html>')
i.close()

- kv
Jul 19 '05 #4

P: n/a
Looks good. I'll give it a try.

Konstantin Veretennicov wrote:
On 6/24/05, Lowell Kirsh <lk****@cs.ubc.ca> wrote:
Is there a module or library anyone knows of that will print html code
indented?

Depends on whether you have to deal with xhtml, valid html or just
any, possibly invalid, "pseudo-html" that abounds on the net.

Here's an example of (too) simple html processing using standard
HTMLParser module:

from HTMLParser import HTMLParser

class Indenter(HTMLParser):

def __init__(self, out):
HTMLParser.__init__(self)
self.out = out
self.indent_level = 0

def write_line(self, text):
print >> self.out, '\t' * self.indent_level + text

def handle_starttag(self, tag, attrs):
self.write_line(
'<%s %s>' % (tag, ' '.join('%s=%s' % (k, v) for k, v in attrs))
)
self.indent_level += 1

def handle_endtag(self, tag):
self.indent_level -= 1
self.write_line('</%s>' % tag)

handle_data = write_line

# handle everything else...
# http://docs.python.org/lib/module-HTMLParser.html

if __name__ == '__main__':
import sys
i = Indenter(sys.stdout)
i.feed('<html><head>foobar</head><body color=0>body</body></html>')
i.close()

- kv

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.