0

I've created a sample blog application using webpy. I used some code here(Replace URL with a link using regex in python) to replace the URL but when I render the page it won't show the hyperlink but just a plain text

In my template:

    <p>
    <i>$datestr(post.posted_on) by $user</i><br/><br/>
    $urlify.formatstring(post.content)
    <p>

urlify.py

    def formatstring(self, value):
    pat1 = re.compile(r"(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)", re.IGNORECASE | re.DOTALL)

    pat2 = re.compile(r"(^|[\n ])(((www|ftp)\.[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)", re.IGNORECASE | re.DOTALL)
    value = pat1.sub(r'\1<a href="\2" target="_blank">\3</a>', value)
    value = pat2.sub(r'\1<a href="http://\2" target="_blank">\3</a>', value)

    return value

Example text:

This url will make your life sh!t so don't click this one.<a href="http://www.someurl.com">some url</a>.

Expected after the render of the page shoud be:

This url will make your life sh!t so don't click this one.some url.

But the actual is:

This url will make your life sh!t so don't click this one.<a href="http://www.someurl.com">some url</a>.

Url was not change to hyperlink.

EDIT: In my template: Instead of $content, it should be $:content to treat the url as hyperlink.

Community
  • 1
  • 1
marvinc
  • 51
  • 7

1 Answers1

0

Code seems to be ok, can you post the arguments you're passing to the formatstring method

  • I've edited my post to add some details to make my problem more clearer. – marvinc Jan 16 '14 at 14:26
  • 1
    I think you have misunderstood what the code does. It takes a url, say `http://www.example.com/foo/bar` and displays a link with only the domain name as text, so in html that would be `http://www.example.com` –  Jan 16 '14 at 15:03
  • BTW, getting the domain name (the string `"example"` from `http://www.example.com/foo/bar`) is not trivial you can look at this related question http://stackoverflow.com/questions/569137/how-to-get-domain-name-from-url –  Jan 16 '14 at 15:08
  • Yes, you're right I think I misunderstood the function. I already solve my problem. Instead of in my template: $context, it should be $:content to treat the url as hyperlink. BTW thanks for your time. – marvinc Jan 17 '14 at 07:14