Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 03:27 AM
Lowell Kirsh
Guest
 
Posts: n/a
Default printing indented html code

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
Default 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
Default 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
Default 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
Default 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]
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles