SGMLParser eats ä etc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anders Eriksson

    SGMLParser eats ä etc

    Hello!

    I'm using smgllib (ActivePython 2.3.2, build 230) and I have some trouble
    with letters that has been coded, e.g. the letter å is coded å ä is
    coded ä and ö is coded ö all according to the html standard.

    I use the SGMLParser and when I feed method all the coded letter will be
    stripped/eaten.

    Why?
    How do I fix this?

    // Anders
  • John J. Lee

    #2
    Re: SGMLParser eats ä etc

    Anders Eriksson <ameLista@telia .com> writes:[color=blue]
    > I'm using smgllib (ActivePython 2.3.2, build 230) and I have some trouble
    > with letters that has been coded, e.g. the letter å is coded &aring; ä is
    > coded &auml; and ö is coded &ouml; all according to the html standard.
    >
    > I use the SGMLParser and when I feed method all the coded letter will be
    > stripped/eaten.
    >
    > Why?
    > How do I fix this?[/color]

    You probably want to use HTMLParser.HTML Parser instead (NOT the same
    thing as htmllib.HTMLPar ser, note). It knows about XHTML, sgmllib &
    htmllib don't. If you really want sgmllib, though (untested):

    import htmlentitydefs

    class MyParser(sgmlli b.SGMLParser):
    entitydefs = htmlentitydefs. entitydefs

    def unknown_entityr ef(self, ref):
    ...

    ...


    John

    Comment

    • Eric Brunel

      #3
      Re: SGMLParser eats &amp;auml; etc

      Anders Eriksson wrote:[color=blue]
      > Hello!
      >
      > I'm using smgllib (ActivePython 2.3.2, build 230) and I have some trouble
      > with letters that has been coded, e.g. the letter å is coded &aring; ä is
      > coded &auml; and ö is coded &ouml; all according to the html standard.
      >
      > I use the SGMLParser and when I feed method all the coded letter will be
      > stripped/eaten.
      >
      > Why?
      > How do I fix this?[/color]

      The &something; "coding" for accented characters is called an entity in SGML.
      These entities are all defined in the underlying DTD for your document. HTML
      defines the "standard" entities you describe, like &aring;, &auml;, etc... But
      if the DTD for the document you're parsing does not include these entity
      definitions, there's no reason why the parser should do anything with them, even
      if silently ignoring them seems strange to me (I'd have expected a parsing error).

      So there are two solutions:
      - either your document is HTML, and you should use an HTML parser as it was
      already suggested
      - or your document is not HTML, and you should define all entities you may use
      in your DTD. This is done for example with:
      <!ENTITY auml &#228;>
      (if you use the iso8859-1 encoding)

      HTH
      --
      - Eric Brunel <eric dot brunel at pragmadev dot com> -
      PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

      Comment

      • John J. Lee

        #4
        Re: SGMLParser eats &amp;auml; etc

        Eric Brunel <eric.brunel@N0 SP4M.com> writes:
        [color=blue]
        > Anders Eriksson wrote:[color=green]
        > > Hello!
        > > I'm using smgllib (ActivePython 2.3.2, build 230) and I have some[/color][/color]
        [...][color=blue]
        > So there are two solutions:
        > - either your document is HTML, and you should use an HTML parser as
        > it was already suggested
        > - or your document is not HTML, and you should define all entities you
        > may use in your DTD. This is done for example with:[/color]
        [...]

        sgmllib only really does HTML.


        John

        Comment

        • Anders Eriksson

          #5
          Re: SGMLParser eats &amp;auml; etc

          On 30 Nov 2003 00:53:28 +0000, John J. Lee wrote:
          [color=blue]
          > You probably want to use HTMLParser.HTML Parser instead (NOT the same
          > thing as htmllib.HTMLPar ser, note). It knows about XHTML, sgmllib &
          > htmllib don't.[/color]
          &aring; etc isn't XHTML, is it? AFAIK it is defined in HTML 4.

          the strange thing is that the Character entity (i.e. &aring;) is stripped
          from the text. I don't want to change it since I'm feeding the output to a
          browser.

          I will try the HTMLParser instead but it seems to me that there is a bug in
          SMGLParser...

          // Anders

          Comment

          • Peter Hansen

            #6
            Re: SGMLParser eats &amp;auml; etc

            Anders Eriksson wrote:[color=blue]
            >
            > On 30 Nov 2003 00:53:28 +0000, John J. Lee wrote:
            >[color=green]
            > > You probably want to use HTMLParser.HTML Parser instead (NOT the same
            > > thing as htmllib.HTMLPar ser, note). It knows about XHTML, sgmllib &
            > > htmllib don't.[/color]
            > &aring; etc isn't XHTML, is it? AFAIK it is defined in HTML 4.
            >
            > the strange thing is that the Character entity (i.e. &aring;) is stripped
            > from the text. I don't want to change it since I'm feeding the output to a
            > browser.
            >
            > I will try the HTMLParser instead but it seems to me that there is a bug in
            > SMGLParser...[/color]

            If it's anything like the expat parser, it munches any undefined character
            entity references (i.e. if there's a DTD but no definitions) unless you plug
            in an appropriate entity reference subparser.

            -Peter

            Comment

            • John J. Lee

              #7
              Re: SGMLParser eats &amp;auml; etc

              Anders Eriksson <ameLista@telia .com> writes:
              [color=blue]
              > On 30 Nov 2003 00:53:28 +0000, John J. Lee wrote:
              >[color=green]
              > > You probably want to use HTMLParser.HTML Parser instead (NOT the same
              > > thing as htmllib.HTMLPar ser, note). It knows about XHTML, sgmllib &
              > > htmllib don't.[/color]
              > &aring; etc isn't XHTML, is it? AFAIK it is defined in HTML 4.[/color]

              It'll cope with HTML too. It seems silly to be writing new code now
              that will choke on XHTML.

              [color=blue]
              > the strange thing is that the Character entity (i.e. &aring;) is stripped
              > from the text. I don't want to change it since I'm feeding the output to a
              > browser.[/color]

              Did you read my post? Read the docs on the stuff in my code snippet.

              [color=blue]
              > I will try the HTMLParser instead but it seems to me that there is a bug in
              > SMGLParser...[/color]

              It's not a bug. That's just what it does, but you can easily override it.


              John

              Comment

              • ddubin

                #8
                Re: SGMLParser eats &amp;auml; etc

                Anders Eriksson <ameLista@telia .com> writes:
                [color=blue]
                > the strange thing is that the Character entity (i.e. &aring;) is
                > stripped from the text. I don't want to change it since I'm feeding
                > the output to a browser.[/color]

                Inconvenient for you, but not strange. An SGML parser is supposed to
                expand general entity references.
                [color=blue]
                > I will try the HTMLParser instead but it seems to me that there is a
                > bug in SMGLParser...[/color]

                No, it's consistent with the standard that the entity reference
                disappears. The question is what replacement text has been put in its
                place, and why can't you see it?

                Dave Dubin

                Comment

                • John J. Lee

                  #9
                  Re: SGMLParser eats &amp;auml; etc

                  ddubin <ddubin@lindev. isrl.uiuc.edu> writes:
                  [color=blue]
                  > Anders Eriksson <ameLista@telia .com> writes:[/color]
                  [...][color=blue]
                  > disappears. The question is what replacement text has been put in its
                  > place, and why can't you see it?[/color]

                  He can't see it because it's not there. From sgmllib.py:

                  # To be overridden -- handlers for unknown objects
                  def unknown_startta g(self, tag, attrs): pass
                  def unknown_endtag( self, tag): pass
                  def unknown_charref (self, ref): pass
                  def unknown_entityr ef(self, ref): pass


                  John

                  Comment

                  Working...