SGMLParser eats ä etc

A

Anders Eriksson

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
 
J

John J. Lee

Anders Eriksson said:
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?

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

import htmlentitydefs

class MyParser(sgmllib.SGMLParser):
entitydefs = htmlentitydefs.entitydefs

def unknown_entityref(self, ref):
...

...


John
 
E

Eric Brunel

Anders said:
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?

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 å, ä, 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 ä>
(if you use the iso8859-1 encoding)

HTH
 
J

John J. Lee

Eric Brunel said:
Anders said:
Hello!
I'm using smgllib (ActivePython 2.3.2, build 230) and I have some
[...]
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:
[...]

sgmllib only really does HTML.


John
 
A

Anders Eriksson

You probably want to use HTMLParser.HTMLParser instead (NOT the same
thing as htmllib.HTMLParser, note). It knows about XHTML, sgmllib &
htmllib don't.
&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
 
P

Peter Hansen

Anders said:
&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...

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
 
J

John J. Lee

Anders Eriksson said:
&aring; etc isn't XHTML, is it? AFAIK it is defined in HTML 4.

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

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.

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

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

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


John
 
D

ddubin

Anders Eriksson said:
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.

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

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
 
J

John J. Lee

ddubin said:
disappears. The question is what replacement text has been put in its
place, and why can't you see it?

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

# To be overridden -- handlers for unknown objects
def unknown_starttag(self, tag, attrs): pass
def unknown_endtag(self, tag): pass
def unknown_charref(self, ref): pass
def unknown_entityref(self, ref): pass


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top