comparison 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
comparison
equal deleted inserted replaced
1800:5f4d688d8b6e 1801:07390a9d1c09
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 import re 20 import re
21 from feed.atom import Content
21 22
22 23
23 def getURLParams(url): 24 def getURLParams(url):
24 """This comes from pyjamas.Location.makeUrlDict with a small change 25 """This comes from pyjamas.Location.makeUrlDict with a small change
25 to also parse full URLs, and parameters with no value specified 26 to also parse full URLs, and parameters with no value specified
70 def repl(match): 71 def repl(match):
71 url = match.group(1) 72 url = match.group(1)
72 return '<a href="%s" target="_blank">%s</a>' % (url, match.group(0)) 73 return '<a href="%s" target="_blank">%s</a>' % (url, match.group(0))
73 pattern = r"""<img[^>]* src="([^"]+)"[^>]*>""" 74 pattern = r"""<img[^>]* src="([^"]+)"[^>]*>"""
74 return re.sub(pattern, repl, string) 75 return re.sub(pattern, repl, string)
76
77
78 def fixXHTMLLinks(xhtml):
79 """Add http:// if the scheme is missing and force opening in a new window.
80
81 @param string (unicode): XHTML Content
82 """
83 subs = []
84 for match in re.finditer(r'<a( \w+="[^"]*")* ?/?>', xhtml):
85 tag = match.group(0)
86 url = re.search(r'href="([^"]*)"', tag)
87 if url:
88 if not re.search(r'target="([^"]*)"', tag): # no target
89 subs.append((tag, '<a target="_blank"%s' % tag[2:]))
90 if not re.match(r"^\w+://", url.group(1)): # no scheme
91 subs.append((url.group(0), 'href="http://%s"' % url.group(1)))
92
93 for url, new_url in subs:
94 xhtml = xhtml.replace(url, new_url)
95 return xhtml
96