def parseUrls(rtext):
def findUrls(s):
# yes, this is faster than regex and less complex to read
urls = []
prefixes = ("http://", "https://")
for prefix in prefixes:
start = 0
while start >= 0:
start = s.find(prefix, start)
if start >= 0:
stop = s.find(" ", start)
if stop < 0:
stop = len(s)
urls.append(s[start:stop])
start += 1
return urls
def formatUrl(text, url):
newText = u"""<a style="color:#28a5ed;font-weight:bold" href="link/{0}">{0}</a>"""
text = text.replace(url, newText.format(url))
return text
texts = [t for t in rtext.contents if isinstance(t, NavigableString)]
for text in texts:
urls = findUrls(text)
for url in urls:
textReplace(text, formatUrl(text, url))
return True
评论列表
文章目录