From aa1e0714dd0f4deea52ced389f6fa7fbc2ea30c6 Mon Sep 17 00:00:00 2001 From: souliane Date: Fri, 15 Jan 2016 12:27:34 +0100 Subject: [PATCH] tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab --- frontends/src/tools/strings.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/frontends/src/tools/strings.py b/frontends/src/tools/strings.py index 400f855b..632b616d 100644 --- a/frontends/src/tools/strings.py +++ b/frontends/src/tools/strings.py @@ -18,6 +18,7 @@ # along with this program. If not, see . import re +from feed.atom import Content def getURLParams(url): @@ -72,3 +73,24 @@ def addURLToImage(string): return '%s' % (url, match.group(0)) pattern = r"""]* 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'', 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, '