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

发布于 2024-09-13 10:49:54 字数 82 浏览 6 评论 0原文

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

更新:我的意思是在 rebol VID 中

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你只要分分秒秒 2024-09-20 10:49:54

这是一个检测 face/text 中的 URL 并覆盖超链接的脚本: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 相同

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

年少掌心 2024-09-20 10:49:54

原则上,您希望:

  • 解析字符串以识别 URL
  • 将每个 URL 替换为 锚标记

REBOL.org 使用与以下代码非常相似的代码来执行此操作。请注意,实现包含三个元素:

  1. 一组定义 URL 的解析定义及其组件、
  2. 解析字符串的函数。每次它在字符串中找到 URL 时,都会调用一个外部函数。它将原始字符串的 URL 替换为外部函数返回的任何内容
  3. 简单地将 URL 包装在锚标记中的外部函数

    <前><代码>;; =======================================
    ;;定义由提供
    ;;安德鲁·马丁,2004 年 6 月 15 日
    ;; ....并非所有内容都需要用于定位 URL ...所以
    ;;随意删除不必要的物品

    八位字节:字符集 [#"^(00)" - #"^(FF)"]
    数字:字符集“0123456789”
    数字:[一些数字]
    上:字符集 [#"A" - #"Z"]
    下:字符集 [#"a" - #"z"]
    Alpha:联合上下
    Alpha:[一些Alpha]
    AlphaDigit:联合 Alpha 数字
    AlphaDigits:[一些 AlphaDigit]
    十六进制:字符集“0123456789ABCDEFabcdef”
    字符:联合 AlphaDigit 字符集“-_~+*'”
    字符:[一些[字符|逃脱]]
    转义:[#"%" 十六进制十六进制]
    路径:union AlphaDigit 字符集“-_~+*'/.?=&{}#”
    域标签:Chars
    域:[域-标签任意 [#"."域标签]]
    IP 地址:[数字 #"."数字#“。”数字#“。”数字]
    用户:[一些[Char |逃亡| #"."]]
    主持人:[域名| IP地址]
    电子邮件^:[用户#“@”主机]
    网址^:[[“http://”| “ftp://”| “https://”]一些路径]

    ;;在字符串中定位 URL 的函数
    ;;并在找到每个时调用操作函数
    ;; ===========================================

    查找 url:func [
    字符串[字符串!]
    动作-函数 [函数!]
    /本地 开始 停止
    ][
    解析/所有字符串[
    任何 [
    开始:复制 url url^ 停止:(
    停止:更改/部分 开始操作-func url 停止
    打印开始

    通过 ;;这取决于动作功能设置作为结束标记
    |跳过
    ]
    结尾
    ]
    返回字符串
    ]

    ;;动作函数的用法示例
    ;;用锚标记替换 url 引用
    ;; =============================================

    target-string: {这个字符串中有这个url http://www.test.com/path
    还有这个:https://www.test.com/example.php}

    查找 url 目标字符串
    func [url][打印 url return rejoin [{} url ]]
    探测目标字符串

    {此字符串具有此 url http://www.test.com/path;在其中
    还有这个:https://www.test.com/example.php}

注释

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

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">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

  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&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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文