printing indented html code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lowell Kirsh

    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>fo obar</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
  • TechBookReport

    #2
    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>fo obar</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

    Comment

    • Lowell Kirsh

      #3
      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>fo obar</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]

      Comment

      • Konstantin Veretennicov

        #4
        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(HTMLPa rser):

        def __init__(self, out):
        HTMLParser.__in it__(self)
        self.out = out
        self.indent_lev el = 0

        def write_line(self , text):
        print >> self.out, '\t' * self.indent_lev el + 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_lev el += 1

        def handle_endtag(s elf, tag):
        self.indent_lev el -= 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.st dout)
        i.feed('<html>< head>foobar</head><body color=0>body</body></html>')
        i.close()

        - kv

        Comment

        • Lowell Kirsh

          #5
          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(HTMLPa rser):
          >
          > def __init__(self, out):
          > HTMLParser.__in it__(self)
          > self.out = out
          > self.indent_lev el = 0
          >
          > def write_line(self , text):
          > print >> self.out, '\t' * self.indent_lev el + 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_lev el += 1
          >
          > def handle_endtag(s elf, tag):
          > self.indent_lev el -= 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.st dout)
          > i.feed('<html>< head>foobar</head><body color=0>body</body></html>')
          > i.close()
          >
          > - kv[/color]

          Comment

          Working...