#!/usr/bin/python
# -*- coding: iso8859-15 -*-
# -----------------------------------------------------------------------
# Copyright (C) 2003 Gustavo Sverzut Barbieri.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
# -----------------------------------------------------------------------
#
# This code is part of the pytvgrab project:
# http://pytvgrab.sourceforge.net
#
# -----------------------------------------------------------------------
# Subversion Information, do not edit
#
# $Rev: 255 $
# $LastChangedDate: 2004-11-12 13:35:28 +1100 (Fri, 12 Nov 2004) $
# $LastChangedRevision: 255 $
# $LastChangedBy: ottrey $
#
#
# $Log: customizedparser.py,v $
# Rev 80 - Fixed xx; and &blah; handling and spacing, thx to
# Bastiaan Van Eeckhoudt for this.
#
# Revision 1.3 2003/08/25 21:02:25 den_RDC
# fixed must_close_tag cdata parsing
#
# Revision 1.2 2003/08/24 14:19:08 ottrey
# *** empty log message ***
#
from tag import Tag
from HTMLParser import HTMLParser
from htmlentitydefs import entitydefs
import message
import i18n
from grabexceptions import ParseError
class CustomizedParser ( HTMLParser ):
"""
This class parse the HTML, but in a customized way: it only parse
the tags and attributes specified in 'parse_tags' and 'parse_attrs',
skipping others.
NOTE: This Class tries to parse broken HTML, but it's something difficult
due the number of non-capable people doing HTML. Maybe you need to correct
the HTML using regular expressions or something like that.
One thing is to close one tag when you expect another, in this case, the
parser assumes that there is a need to close that tag and goes down through
the last opened tags, closing them until it matches the wanted tags. This
can lead to a problem when you close a tag that was not opened (in my case,
it was
!), then you need to remove this tag.
"""
__author__ = "Gustavo Sverzut Barbieri "
__revision__ = "$Rev: 255 $"
verbose = 0
queue = None
text = None
skip = None
parse_tags = None
parse_attrs = None
keep_empty_tags = None
must_close_tags = None
def __init__( self, parse_tags=None, parse_attrs=None,
must_close_tags=None, keep_empty_tags=None,
verbose=0 ):
"""
parse_tags: a list of tag names to be parsed. Tags outside this
list will be skipped.
parse_attrs: a list of attributes to be parsed. Attributes outside
this list will be skipped.
must_close_tags: a list of tags that should be closed before other tag
start. Ie: many document doesn't close