Connecting Tech Pros Worldwide Help | Site Map

printing indented html code

  #1  
Old July 19th, 2005, 03:27 AM
Lowell Kirsh
Guest
 
Posts: 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
  #2  
Old July 19th, 2005, 03:27 AM
TechBookReport
Guest
 
Posts: n/a

re: printing indented html code


Lowell Kirsh wrote:[color=blue]
> 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[/color]

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
  #3  
Old July 19th, 2005, 03:27 AM
Lowell Kirsh
Guest
 
Posts: n/a

re: printing indented html code


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

Lowell

TechBookReport wrote:[color=blue]
> Lowell Kirsh wrote:
>[color=green]
>> 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[/color]
>
>
> 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[/color]
  #4  
Old July 19th, 2005, 03:27 AM
Konstantin Veretennicov
Guest
 
Posts: n/a

re: printing indented html code


On 6/24/05, Lowell Kirsh <lkirsh@cs.ubc.ca> wrote:[color=blue]
> Is there a module or library anyone knows of that will print html code
> indented?[/color]

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
  #5  
Old July 19th, 2005, 03:27 AM
Lowell Kirsh
Guest
 
Posts: n/a

re: printing indented html code


Looks good. I'll give it a try.

Konstantin Veretennicov wrote:[color=blue]
> On 6/24/05, Lowell Kirsh <lkirsh@cs.ubc.ca> wrote:
>[color=green]
>>Is there a module or library anyone knows of that will print html code
>>indented?[/color]
>
>
> 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[/color]
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hows my code saraca means ashoka tree answers 10 November 14th, 2005 04:42 PM
Python/IDLE - text in different colours Bill Davy answers 4 July 19th, 2005 03:29 AM
pre-PEP: Print Without Intervening Space Marcin Ciura answers 14 July 18th, 2005 11:39 PM
doctest bug? Edward K. Ream answers 3 July 18th, 2005 01:57 PM