tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
This commit is contained in:
parent
e3144d93c2
commit
aa1e0714dd
1 changed files with 22 additions and 0 deletions
|
|
@ -18,6 +18,7 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import re
|
||||
from feed.atom import Content
|
||||
|
||||
|
||||
def getURLParams(url):
|
||||
|
|
@ -72,3 +73,24 @@ def addURLToImage(string):
|
|||
return '<a href="%s" target="_blank">%s</a>' % (url, match.group(0))
|
||||
pattern = r"""<img[^>]* src="([^"]+)"[^>]*>"""
|
||||
return re.sub(pattern, repl, string)
|
||||
|
||||
|
||||
def fixXHTMLLinks(xhtml):
|
||||
"""Add http:// if the scheme is missing and force opening in a new window.
|
||||
|
||||
@param string (unicode): XHTML Content
|
||||
"""
|
||||
subs = []
|
||||
for match in re.finditer(r'<a( \w+="[^"]*")* ?/?>', xhtml):
|
||||
tag = match.group(0)
|
||||
url = re.search(r'href="([^"]*)"', tag)
|
||||
if url:
|
||||
if not re.search(r'target="([^"]*)"', tag): # no target
|
||||
subs.append((tag, '<a target="_blank"%s' % tag[2:]))
|
||||
if not re.match(r"^\w+://", url.group(1)): # no scheme
|
||||
subs.append((url.group(0), 'href="http://%s"' % url.group(1)))
|
||||
|
||||
for url, new_url in subs:
|
||||
xhtml = xhtml.replace(url, new_url)
|
||||
return xhtml
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue