假设我想创建一个列出推文的推特客户端。如何在文本区域中创建和检测可点击链接?
更新:我的意思是在rebol VID
答案 0 :(得分:2)
这是一个检测face/text
中的网址并覆盖超链接的脚本:http://www.ross-gill.com/r/link-up.html
view layout [
my-text: text read %some.txt
do [link-up my-text]
]
它基于下文中的模式,因此您可能需要根据您的规格调整识别模式。链接通过to-link
函数传递,默认情况下与to-url
答案 1 :(得分:1)
原则上,您希望:
REBOL.org使用与下面的代码非常相似的代码来执行此操作。请注意,实现有三个要素:
一个简单地将URL包装在锚标记中的外部函数
;; ======================================
;; 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>}
备注强>