如何在rebol VID中创建可点击的链接?

时间:2010-08-07 12:09:28

标签: rebol

假设我想创建一个列出推文的推特客户端。如何在文本区域中创建和检测可点击链接?

更新:我的意思是在rebol VID

2 个答案:

答案 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使用与下面的代码非常相似的代码来执行此操作。请注意,实现有三个要素:

  1. 一组定义URL及其组件的解析定义
  2. 解析字符串的函数。每次在字符串中找到URL时,它都会调用外部函数。它将原始字符串的URL替换为外部函数返回的任何内容
  3. 一个简单地将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>}
    
  4. 备注

    1. 您应该能够轻松地了解如何将 find-urls 改为查找电子邮件地址以进行搜查和/或点击;查找电子邮件地址的所有解析定义都在上面的示例中
    2. 您可以在此处查看此代码的REBOL.org版本,例如:http://www.rebol.org/aga-display-posts.r?offset=0&post=r3wp157x17091
    3. 如果网址已经在锚标记中,我会留下绕过让它可点击的练习
    4. 同样遗漏:任何需要在URL中转义字符(例如&amp; ==&gt; amp;)
    5. 感谢REBOL先驱 Andrew Martin 提供的原始代码。