2021-11-11 08:33:25 +00:00
|
|
|
# An example of using Janet's extensible module system to import files from
|
|
|
|
# URL. To try this, run `janet -l ./examples/urlloader.janet` from the command
|
|
|
|
# line, and then at the REPL type:
|
2019-05-25 21:10:25 +00:00
|
|
|
#
|
|
|
|
# (import https://raw.githubusercontent.com/janet-lang/janet/master/examples/colors.janet :as c)
|
|
|
|
#
|
2021-11-11 08:33:25 +00:00
|
|
|
# This will import a file using curl. You can then try:
|
2019-05-25 21:10:25 +00:00
|
|
|
#
|
|
|
|
# (print (c/color :green "Hello!"))
|
|
|
|
#
|
|
|
|
# This is a bit of a toy example (it just shells out to curl), but it is very
|
|
|
|
# powerful and will work well in many cases.
|
|
|
|
|
|
|
|
(defn- load-url
|
|
|
|
[url args]
|
2021-11-11 08:33:25 +00:00
|
|
|
(def p (os/spawn ["curl" url "-s"] :p {:out :pipe}))
|
|
|
|
(def res (dofile (p :out) :source url ;args))
|
|
|
|
(:wait p)
|
2019-05-25 21:10:25 +00:00
|
|
|
res)
|
|
|
|
|
|
|
|
(defn- check-http-url
|
|
|
|
[path]
|
2019-05-25 21:27:56 +00:00
|
|
|
(if (or (string/has-prefix? "http://" path)
|
|
|
|
(string/has-prefix? "https://" path))
|
|
|
|
path))
|
2019-05-25 21:10:25 +00:00
|
|
|
|
|
|
|
# Add the module loader and path tuple to right places
|
2019-05-25 21:27:56 +00:00
|
|
|
(array/push module/paths [check-http-url :janet-http])
|
2019-05-25 21:10:25 +00:00
|
|
|
(put module/loaders :janet-http load-url)
|