Skip to content

Instantly share code, notes, and snippets.

@mjgpy3
Created June 13, 2020 14:28
Show Gist options
  • Save mjgpy3/6c23eafe0790bd6e4c0dba90b372385e to your computer and use it in GitHub Desktop.
Save mjgpy3/6c23eafe0790bd6e4c0dba90b372385e to your computer and use it in GitHub Desktop.
Red Links
@greggirwin
Copy link

Hi MIchael. Someone posted your video to our Gitter channel (where most chat occurs), and I wanted to thank you for posting it. In return, here's a parse-based version for you to check out.

Red [
	Title:   "Download links"
	Purpose: "Read a URL and extract links from it."
]

;url: to url! system/script/args
url: http://example.com
url: http://github.com
;url: http://red-lang.org

raw-html: read url

; Using `parse` is more involved than simple splitting,
; but gives you much more control and robustness. This
; shows a few of its features and syntax.
links: parse raw-html [			; build up a block of links
	collect [					; collect makes parse return a block
		some [					; some = 1 or more
			thru {href="}		; curly syntax lets us use quotes inside
			ahead [				; lookahead for specific link types
				"/"				; local link, no :// check needed
				| [				; or specific url schemes
					"http" opt #"s"	; http(s)
					| "ftp"			; or ftp
				]
				"://"			; scheme tail check
			]
			copy link to {"}	; copy to next quote
			keep (to url! link) ; keep works with collect
		]
	]
]
;!! There is also a `collect` func that works outside of `parse`.

new-line/all links on			; mark each item in the block as a new line
print mold links				; mold shows block contents when printed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment