Rebol Talk Forum  |  REBOL Discussions  |  REBOL Core  |  Topic: Parsing text urls to HTML urls
Pages: [1] Print
Author Topic: Parsing text urls to HTML urls  (Read 726 times)
Philippe
Newbie
*
Offline Offline

Posts: 34


View Profile WWW
Parsing text urls to HTML urls
« on: November 16, 2006, 09:29:07 AM »

Hi,

This is my problem :
In a string, some authors write URLs directly like : http://www.my-domainename.com.
Others use simply : www.my-domainename.com .
To render the text to HTML, a parser add http:// if it not exist and then build anchor.

See code below.

I need to add another case :
here, authors write all the HTML anchor like :
 
Code:
<a href="http://www.my-domainename.com">www.my-domainename.com<a>
and so the parser has nothing to do. How can I add these rules to existing one ?

Thx,
===Philippe
------------------------------------------------------------
Code:
ctx-parse: context [
    msg-block: copy ""

    process-url: func [txt /image /local t] [
        t: copy txt
        parse txt [["ftp." (insert t "ftp://") | "www." (insert t "http://")] to end]
        either find [".jpg" ".gif" ".png" ".bmp"] find/last txt "."
        [rejoin [{<img src="} t {" border="0">}]]
        [rejoin [{<a href="} t {">} txt {</a>}]]
    ]
   
    ;all-chars: exclude charset [#" " - #"^(FF)"] charset "[<"
    all-chars: charset [#" " - #"^(FF)"]
    non-white-space: complement charset " ^/^-^M<>"
    to-space: [some non-white-space | end]
    is-url: complement charset " ^/^-^M<>()"
    end-url: [some is-url | end]

    deb: fin: url: none
    rules: [
        deb: ["http" opt "s" "://" | "www." | "ftp://" | "ftp." ] end-url fin: (append msg-block process-url copy/part deb fin)
        | [crlf | cr | lf] (append msg-block "<br>")
        | tab (append msg-block "&nbsp;&nbsp;&nbsp;")
        | "<" (append msg-block "&lt;")
        | ">" (append msg-block "&gt;")
        | deb: all-chars fin: (append msg-block copy/part deb fin)
    ]

    parse-target: func [data /local msg] [
        clear msg-block
        parse/all trim/head/tail data [some rules]
        copy msg-block
    ]
]
; usage:
; ctx-parse/parse target {A string with http:// link , or anchor link or www link}
Logged

===Philippe
Pages: [1] Print 
Rebol Talk Forum  |  REBOL Discussions  |  REBOL Core  |  Topic: Parsing text urls to HTML urls
Jump to:  

  
Quick Search...

Advanced search
  
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 02, 2008, 06:24:49 AM
Username: Password: Session Length:
  

News: 01-09-08

Alpha version of REBOL 3 has been released!


  
2311 Posts in 595 Topics by 4138 Members
Latest Member: Irrederwasy

  Rebol Talk Forum | Powered by SMF 1.0.9.
© 2001-2005, Lewis Media. All Rights Reserved.

RT design by Defiant Pc