Marking hyperlinks in a Text widget
François Pinard
pinard at iro.umontreal.ca
Tue May 30 09:11:04 EDT 2000
"André Dahlqvist" <andre at beta.telenordia.se> écrit:
> I am trying to figure out how to mark text starting with http:// and ftp://
> in a Text widget as hyperlinks. I know how to mark all the text I am
> inserting as hyperlinks:
> text.tag_config("hyperlink", foreground="blue", underline=1)
> text.insert(END, "http://www.python.org", "hyperlink")
> But what if I am inserting a large chunk of text that can contain links
> anywhere in it? How do I mark the sentences starting with http:// and
> ftp:// in that text as hyperlinks?
Hello, André. Not so long ago, I posted the following bits to this list.
I revised them since, because of a tiny bug. Extracted from:
http://www.iro.umontreal.ca/contrib/recode/lib/htmlpage.py
I use that code to enhance some TEXT before inserting it into a Web page.
If VERBATIM is true, extra care will be taken for not removing `http://',
and other marks, from the generated HTML. The default is to clean up a bit.
def enhance(text, verbatim=0):
text = string.replace(text, '&', '&')
text = string.replace(text, '<', '<')
text = string.replace(text, '>', '>')
text = string.replace(text, '\f', '')
if verbatim:
text = re.sub(
r'((mailto:|http://|ftp://)[-_.@~/a-zA-Z0-9]*[~/a-zA-Z0-9])',
r'<a href="\1">\1</a>', text)
else:
text = re.sub(
r'((mailto:|http://|ftp://)([-_.@~/a-zA-Z0-9]*[~/a-zA-Z0-9]))',
r'<a href="\1">\3</a>', text)
text = re.sub(
(r'(^|[^-_%+./a-zA-Z0-9:])'
r'([-_%+./a-zA-Z0-9]+@[-a-zA-Z0-9]+\.[-.a-zA-Z0-9]*[a-zA-Z0-9])'),
r'\1<a href="mailto:\2">\2</a>', text)
if verbatim:
text = re.sub(
r'_([-_ at ./a-zA-Z0-9]*[/a-zA-Z0-9])_', r'_<i>\1</i>_', text)
text = re.sub(
r'\*([-_ at ./a-zA-Z0-9]*[/a-zA-Z0-9])\*', r'*<b>\1</b>*', text)
text = re.sub(
r"`([-_ at ./a-zA-Z0-9]*[/a-zA-Z0-9])'", r"`<tt><b>\1</b></tt>'",
text)
else:
text = re.sub(
r'_([-_ at ./a-zA-Z0-9]*[/a-zA-Z0-9])_', r'<i>\1</i>', text)
text = re.sub(
r'\*([-_ at ./a-zA-Z0-9]*[/a-zA-Z0-9])\*', r'<b>\1</b>', text)
text = re.sub(
r"`([-_ at ./a-zA-Z0-9]*[/a-zA-Z0-9])'", r'<tt><b>\1</b></tt>', text)
return text
--
François Pinard http://www.iro.umontreal.ca/~pinard
More information about the Python-list
mailing list