#!/usr/bin/python
#
# htmlp.py by Mark Paschal
# Slice HTML fragments from a document according to CSS selectors.
#
__author__ = "Mark Paschal "
__copyright__ = "Copyright 2003 Mark Paschal"
__version__ = "1.0"
__license__ = """MIT
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from sgmllib import SGMLParser
UNCLOSED_TAGS = ('img', 'br', 'link', 'meta')
class Tag(dict):
def __init__(self, tag, attrs):
self.tag = tag
self.matched = 0
for a in attrs:
self[a[0]] = a[1]
class htmlp(SGMLParser):
def __init__(self, selector):
import selectorp
self.selectors = tuple(selectorp.parse("selector", selector))
SGMLParser.__init__(self)
def reset(self, selector=None):
self.tags = []
self.curSel = self.saving = 0
self.savingStr = ""
self.saved = []
SGMLParser.reset(self)
def unknown_starttag(self, tag, attrs):
newtag = Tag(tag, attrs)
if not self.saving:
if self.selectors[self.curSel].matches(newtag):
newtag.matched = 1
self.curSel += 1
if self.curSel == len(self.selectors):
self.saving = 1
self.savingStr = ""
self.tags.append(newtag)
# If we're saving, add to the saving text.
if self.saving:
self.savingStr = self.savingStr + "<%s%s>" % (tag, "".join([' %s="%s"' % (key, value.replace('"', '"')) for key, value in attrs]))
if tag in UNCLOSED_TAGS:
self.unknown_endtag(tag)
def unknown_endtag(self, tag):
# Nest correctly.
try:
while self.tags[-1].tag <> tag:
self.unknown_endtag(self.tags[-1].tag)
except IndexError: return
# Close the string?
if self.saving and tag not in UNCLOSED_TAGS:
self.savingStr = self.savingStr + "%s>" % tag
if self.tags[-1].matched:
self.curSel -= 1
if self.saving:
self.saving = 0
self.saved.append(self.savingStr)
self.tags.pop()
def handle_charref(self, ref):
if self.saving: self.savingStr += "%s;" % ref
def handle_entityref(self, ref):
if self.saving: self.savingStr += "&%s;" % ref
def handle_data(self, text):
if self.saving: self.savingStr += text
def handle_comment(self, text):
if self.saving: self.savingStr += "" % text
def handle_pi(self, text):
if self.saving: self.savingStr += "%s>" % text
def handle_decl(self, text):
if self.saving: self.savingStr += "" % text
if '__main__' == __name__:
h = htmlp(file("moose.txt").read())
h.feed(file("moose.html").read())
print h.saved