text representation of HTML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ksenia Marasanova

    #1

    text representation of HTML

    Hi,

    I am looking for a library that will give me very simple text
    representation of HTML.
    For example
    <div><h1>Titl e</h1><p>This is a <br />test</p></div>

    will be transformed to:

    Title

    This is a
    test


    i want to send plain text alternative of html email, and would prefer
    to do it automatically from HTML source.
    Any hints?

    Thanks!
    Ksenia.
  • Diez B. Roggisch

    #2
    Re: text representation of HTML

    Ksenia Marasanova wrote:
    Hi,
    >
    I am looking for a library that will give me very simple text
    representation of HTML.
    For example
    <div><h1>Titl e</h1><p>This is a <br />test</p></div>
    >
    will be transformed to:
    >
    Title
    >
    This is a
    test
    >
    >
    i want to send plain text alternative of html email, and would prefer
    to do it automatically from HTML source.
    Any hints?
    html2text is a commandline tool. You can invoke it from python using
    subprocess.

    Diez

    Comment

    • Laurent Rahuel

      #3
      Re: text representation of HTML

      Hi,

      I guess stripogram would be more pythonic :
      http://sourceforge.net/project/showf...?group_id=1083

      Regards,

      Laurent

      Diez B. Roggisch wrote:
      Ksenia Marasanova wrote:
      >
      >Hi,
      >>
      >I am looking for a library that will give me very simple text
      >representati on of HTML.
      >For example
      ><div><h1>Title </h1><p>This is a <br />test</p></div>
      >>
      >will be transformed to:
      >>
      >Title
      >>
      >This is a
      >test
      >>
      >>
      >i want to send plain text alternative of html email, and would prefer
      >to do it automatically from HTML source.
      >Any hints?
      >
      html2text is a commandline tool. You can invoke it from python using
      subprocess.
      >
      Diez

      Comment

      • garabik-news-2005-05@kassiopeia.juls.savba.sk

        #4
        Re: text representation of HTML

        Ksenia Marasanova <ksenia.marasan ova@gmail.comwr ote:
        Hi,
        >
        I am looking for a library that will give me very simple text
        representation of HTML.
        For example
        <div><h1>Titl e</h1><p>This is a <br />test</p></div>
        >
        will be transformed to:
        >
        Title
        >
        This is a
        test
        >
        >
        i want to send plain text alternative of html email, and would prefer
        to do it automatically from HTML source.
        something like this:

        import re
        text = '<div><h1>Title </h1><p>This is a <br />test</p></div>'
        text = re.sub(r'[\n\ \t]+', ' ', text)
        text = re.sub(r'(?i)(\ <p\>|\<br\>|\ <h[1-6]\>)', '\n', text)
        result = re.sub('<.+?>', '', text)
        print result

        --
        -----------------------------------------------------------
        | Radovan Garabík http://kassiopeia.juls.savba.sk/~garabik/ |
        | __..--^^^--..__ garabik @ kassiopeia.juls .savba.sk |
        -----------------------------------------------------------
        Antivirus alert: file .signature infected by signature virus.
        Hi! I'm a signature virus! Copy me into your signature file to help me spread!

        Comment

        • Duncan Booth

          #5
          Re: text representation of HTML

          Ksenia Marasanova wrote:
          I am looking for a library that will give me very simple text
          representation of HTML.
          For example
          ><div><h1>Title </h1><p>This is a <br />test</p></div>
          >
          will be transformed to:
          >
          Title
          >
          This is a
          test
          >
          >
          i want to send plain text alternative of html email, and would prefer
          to do it automatically from HTML source.
          Any hints?
          Use htmllib:
          >>import htmllib, formatter, StringIO
          >>def cleanup(s):
          out = StringIO.String IO()
          p = htmllib.HTMLPar ser(
          formatter.Abstr actFormatter(fo rmatter.DumbWri ter(out)))
          p.feed(s)
          p.close()
          if p.anchorlist:
          print >>out
          for idx,anchor in enumerate(p.anc horlist):
          print >>out, "\n[%d]: %s" % (idx+1,anchor)
          return out.getvalue()
          >>print cleanup('''<div ><h1>Title</h1><p>This is a <br
          />test</p></div>''')

          Title

          This is a
          test
          >>print cleanup('''<div ><h1>Title</h1><p>This is a <br />test with <a
          href="http://python.org">a link</ato the Python homepage</p></div>''')

          Title

          This is a
          test with a link[1] to the Python homepage

          [1]: http://python.org


          Comment

          • Tim Williams

            #6
            Re: text representation of HTML

            On 20 Jul 2006 15:12:27 GMT, Duncan Booth <duncan.booth@i nvalid.invalidw rote:
            Ksenia Marasanova wrote:
            i want to send plain text alternative of html email, and would prefer
            to do it automatically from HTML source.
            Any hints?
            >
            Use htmllib:
            >
            >import htmllib, formatter, StringIO
            >def cleanup(s):
            out = StringIO.String IO()
            p = htmllib.HTMLPar ser(
            formatter.Abstr actFormatter(fo rmatter.DumbWri ter(out)))
            p.feed(s)
            p.close()
            if p.anchorlist:
            print >>out
            for idx,anchor in enumerate(p.anc horlist):
            print >>out, "\n[%d]: %s" % (idx+1,anchor)
            return out.getvalue()
            >
            >print cleanup('''<div ><h1>Title</h1><p>This is a <br
            />test</p></div>''')
            >
            Title
            >
            This is a
            test
            >print cleanup('''<div ><h1>Title</h1><p>This is a <br />test with <a
            href="http://python.org">a link</ato the Python homepage</p></div>''')
            >
            Title
            >
            This is a
            test with a link[1] to the Python homepage
            >
            [1]: http://python.org
            >
            cleanup() doesn't handle script and styles too well. html2text will
            do a much better job of these and give a more structured output
            (compatible with Markdown)


            >>import html2text
            >>print html2text.html2 text('''<div><h 1>Title</h1><p>This is a <br
            />test with <a href="http://python.org">a link</ato the Python
            homepage</p></div>''')

            # Title

            This is a
            test with [a link][1] to the Python homepage

            [1]: http://python.org


            HTH :)

            Comment

            • Ksenia Marasanova

              #7
              Re: text representation of HTML

              Sorry for the late reply... better too late than never :)
              Thanks to all for the tips. Stripogram is the winner, since it is the
              most configurable and accept line-length parameter, which is handy for
              email...

              Ksenia.

              On 7/19/06, Laurent Rahuel <lrahuel.notgoo d@voila.frwrote :
              Hi,
              >
              I guess stripogram would be more pythonic :
              http://sourceforge.net/project/showf...?group_id=1083
              >
              Regards,
              >
              Laurent
              >
              Diez B. Roggisch wrote:
              >
              Ksenia Marasanova wrote:
              Hi,
              >
              I am looking for a library that will give me very simple text
              representation of HTML.
              For example
              <div><h1>Titl e</h1><p>This is a <br />test</p></div>
              >
              will be transformed to:
              >
              Title
              >
              This is a
              test
              >
              >
              i want to send plain text alternative of html email, and would prefer
              to do it automatically from HTML source.
              Any hints?
              html2text is a commandline tool. You can invoke it from python using
              subprocess.

              Diez
              >
              --

              >

              Comment

              Working...