tags:

views:

61

answers:

2

Let's say I want to create a twitter client which list tweets. How would I create and detect a clickable link within a text zone ?

Update: I mean in rebol VID

+1  A: 

In principle, you want to:

  • parse your string to identify URLs
  • replace each URL with an anchor tag

REBOL.org uses code very similar to the code below to do that. Note there are three elements to the implementation:

  1. a set of parse definitions that define a URL and its components
  2. a function that parses a string. Each time it finds a URL in the string, it calls an external function. It replaces the original string's URL with whatever that external function returns to it
  3. an external function that simply wraps a URL in an anchor tag

    ;;   ======================================
    ;;   Definitions provided by
    ;;   Andrew Martin, 15-June-2004
    ;;   ....not all are needed for locating URLs ... so
    ;;   feel free to remove unnecessary items
    
    
    Octet: charset [#"^(00)" - #"^(FF)"]
    Digit: charset "0123456789"
    Digits: [some Digit]
    Upper: charset [#"A" - #"Z"]
    Lower: charset [#"a" - #"z"]
    Alpha: union Upper Lower
    Alphas: [some Alpha]
    AlphaDigit: union Alpha Digit
    AlphaDigits: [some AlphaDigit]
    Hex: charset "0123456789ABCDEFabcdef"
    Char: union AlphaDigit charset "-_~+*'"
    Chars: [some [Char | Escape]]
    Escape: [#"%" Hex Hex]
    Path: union AlphaDigit charset "-_~+*'/.?=&;{}#"
    Domain-Label: Chars
    Domain: [Domain-Label any [#"." Domain-Label]]
    IP-Address: [Digits #"." Digits #"." Digits #"." Digits]
    User: [some [Char | Escape | #"."]]
    Host: [Domain | IP-Address]
    Email^: [User #"@" Host]
    Url^: [["http://" | "ftp://" | "https://"] some Path] 
    
    
    ;; function to locate URLs in a string
    ;; and call an action func when each is found
    ;; ==========================================
    
    
    find-urls:  func [
        String [string!]
        action-func [function!]
       /local Start Stop
     ][
      parse/all String [
         any [
            Start: copy url url^  Stop: (
               Stop: change/part Start action-func url Stop
               print start 
               )
               thru </a>    ;; this is dependent on the action-func setting </a> as an end marker
            | skip
            ]
         end
         ]
        return String
          ]
    
    
    
    ;; example of usage with an action-func that
    ;; replaces url references with an anchor tag
    ;; ===========================================
    
    target-string: {this string has this url http://www.test.com/path in it and also this one: https://www.test.com/example.php} find-urls target-string func [url][print url return rejoin [{<a href="} url {">} url </a>]] probe target-string {this string has this url <a href="http://www.test.com/path"&gt;http://www.test.com/path&lt;/a&gt; in it and also this one: <a href="https://www.test.com/example.php"&gt;https://www.test.com/example.php&lt;/a&gt;}

Notes

  1. You should easily be able to see how to adapt find-urls into, say, find-email-addresses for obfucation and/or clickability; all the parse definitions for finding email addresses are in the sample above
  2. You can see REBOL.org's version of this code in operation here, for example: http://www.rebol.org/aga-display-posts.r?offset=0&amp;post=r3wp157x17091
  3. I'll leave you the exercise of bypassing making it clickable if the URL is already in an anchor tag
  4. Also left out: any need to escape chars in the URL (eg & ==> amp;)
  5. Thanks to REBOL pioneer Andrew Martin for the original code that this is based on.
Sunanda
Sorry I realized I wasn't clear in my question: I mean in Rebol VID box
Rebol Tutorial
+1  A: 

This is a script that detects URLs in face/text and overlays hyperlinks: http://www.ross-gill.com/r/link-up.html

view layout [
    my-text: text read %some.txt
    do [link-up my-text]
]

It's based on the pattern in the article below, so you may need to adapt the recognition pattern to your specifications. The links are passed through a to-link function which by default is the same as to-url

rgchris
Amazing I wasn't sure it was even possible : thanks !!!
Rebol Tutorial
There are some limitations for sure, but it works as well as many others - for example, certain iPhone Twitter clients I've used...
rgchris