Let's say I want to create a twitter c开发者_StackOverflow社区lient which list tweets. How would I create and detect a clickable link within a text zone ?
Update: I mean in rebol VID
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
- http://www.ross-gill.com/page/Beyond_Regular_Expressions
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:
- a set of parse definitions that define a URL and its components
- 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
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">http://www.test.com/path</a> in it and also this one: <a href="https://www.test.com/example.php">https://www.test.com/example.php</a>}
Notes
- 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
- 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&post=r3wp157x17091
- I'll leave you the exercise of bypassing making it clickable if the URL is already in an anchor tag
- Also left out: any need to escape chars in the URL (eg & ==> amp;)
- Thanks to REBOL pioneer Andrew Martin for the original code that this is based on.
精彩评论