2003-07-04 14:24:44 +00:00
|
|
|
# -*- coding: iso-8859-1 -*-
|
2010-03-26 19:51:59 +00:00
|
|
|
# Copyright (C) 2001-2010 Bastian Kleineidam
|
2001-05-23 21:20:44 +00:00
|
|
|
#
|
|
|
|
|
# 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
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
2009-07-24 21:58:20 +00:00
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2005-01-19 15:08:02 +00:00
|
|
|
"""
|
2008-06-07 13:07:48 +00:00
|
|
|
Parse names of title tags and link types.
|
2005-01-19 15:08:02 +00:00
|
|
|
"""
|
2001-05-23 21:20:44 +00:00
|
|
|
|
2004-07-07 18:04:40 +00:00
|
|
|
import re
|
2008-06-07 13:07:48 +00:00
|
|
|
from .. import HtmlParser, strformat
|
2004-07-07 18:04:40 +00:00
|
|
|
|
2001-04-28 18:37:10 +00:00
|
|
|
|
2004-08-16 19:20:53 +00:00
|
|
|
imgtag_re = re.compile(r"(?i)\s+alt\s*=\s*"+\
|
|
|
|
|
r"""(?P<name>("[^"\n]*"|'[^'\n]*'|[^\s>]+))""")
|
2004-11-06 12:43:48 +00:00
|
|
|
img_re = re.compile(r"""(?i)<\s*img\s+("[^"\n]*"|'[^'\n]*'|[^>])+>""")
|
2008-06-07 13:07:48 +00:00
|
|
|
|
2010-03-13 07:47:12 +00:00
|
|
|
|
2008-06-07 13:07:48 +00:00
|
|
|
def endtag_re (tag):
|
|
|
|
|
"""Return matcher for given end tag"""
|
|
|
|
|
return re.compile(r"(?i)</%s\s*>" % tag)
|
|
|
|
|
|
|
|
|
|
a_end_search = endtag_re("a").search
|
|
|
|
|
title_end_search = endtag_re("title").search
|
2001-04-28 18:37:10 +00:00
|
|
|
|
2010-03-13 07:47:12 +00:00
|
|
|
|
2004-08-16 19:20:53 +00:00
|
|
|
def _unquote (txt):
|
2008-06-07 13:07:48 +00:00
|
|
|
"""Resolve entities and remove markup from txt."""
|
2008-04-27 11:39:21 +00:00
|
|
|
return HtmlParser.resolve_entities(strformat.remove_markup(txt))
|
2004-08-16 19:20:53 +00:00
|
|
|
|
2008-06-07 13:07:48 +00:00
|
|
|
|
2004-08-16 19:20:53 +00:00
|
|
|
def image_name (txt):
|
2008-04-27 11:39:21 +00:00
|
|
|
"""Return the alt part of the first <img alt=""> tag in txt."""
|
2001-04-28 18:37:10 +00:00
|
|
|
mo = imgtag_re.search(txt)
|
|
|
|
|
if mo:
|
2008-04-27 11:39:21 +00:00
|
|
|
name = strformat.unquote(mo.group('name').strip())
|
2004-08-16 19:20:53 +00:00
|
|
|
return _unquote(name)
|
2004-11-03 21:29:25 +00:00
|
|
|
return u''
|
2001-04-28 18:37:10 +00:00
|
|
|
|
|
|
|
|
|
2004-08-16 19:20:53 +00:00
|
|
|
def href_name (txt):
|
2008-04-27 11:39:21 +00:00
|
|
|
"""Return the name part of the first <a href="">name</a> link in txt."""
|
2004-11-03 21:29:25 +00:00
|
|
|
name = u""
|
2008-06-07 13:07:48 +00:00
|
|
|
endtag = a_end_search(txt)
|
2004-08-16 19:20:53 +00:00
|
|
|
if not endtag:
|
|
|
|
|
return name
|
2002-06-09 15:32:14 +00:00
|
|
|
name = txt[:endtag.start()]
|
|
|
|
|
if img_re.search(name):
|
|
|
|
|
return image_name(name)
|
2004-08-16 19:20:53 +00:00
|
|
|
return _unquote(name)
|
2008-06-07 13:07:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def title_name (txt):
|
|
|
|
|
"""Return the part of the first <title>name</title> in txt."""
|
|
|
|
|
name = u""
|
|
|
|
|
endtag = title_end_search(txt)
|
|
|
|
|
if not endtag:
|
|
|
|
|
return name
|
|
|
|
|
name = txt[:endtag.start()]
|
|
|
|
|
return _unquote(name)
|