Mercurial > libervia-backend
diff frontends/src/tools/strings.py @ 1801:07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
author | souliane <souliane@mailoo.org> |
---|---|
date | Fri, 15 Jan 2016 12:27:34 +0100 |
parents | bf7e468fe440 |
children | b43ee22eac98 |
line wrap: on
line diff
--- a/frontends/src/tools/strings.py Wed Jan 13 22:10:06 2016 +0100 +++ b/frontends/src/tools/strings.py Fri Jan 15 12:27:34 2016 +0100 @@ -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 @@ 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 +