Skip to content

Instantly share code, notes, and snippets.

Last active October 29, 2021 09:32
Show Gist options
  • Save Metaxal/77391e388303af5513d09bcd640f116c to your computer and use it in GitHub Desktop.
Save Metaxal/77391e388303af5513d09bcd640f116c to your computer and use it in GitHub Desktop.
(OBSOLETE: now part of quickscript-extra) Fetches a quickscript at a given url and adds it to the library
#lang racket/base
;;; License: [Apache License, Version 2.0]( or
;;; [MIT license]( at your option.
(require quickscript
(script-help-string "Fetches a quickscript at a given url and adds it to the library.")
(define dir user-script-dir)
(define url2script-submod-name 'url2script-info)
;; Will be in quickscript/utils at some point
(define (smart-open-file drfr f)
[(not (file-exists? f))
(message-box "Error"
(format "File not found: ~a" f)
'(ok stop))
[(send drfr still-untouched?)
(send drfr change-to-file f)
[(send drfr find-matching-tab f)
(λ (tab)
(send drfr change-to-tab tab)
(send drfr open-in-new-tab f)
(define (parse-url str)
; Do not keep trailing anchors
(set! str (regexp-replace #px"[#?].*" str ""))
(match str
; We can extract the filename
; ""
[(regexp #px"^https://gist\\.github(?:usercontent|)\\.com/[^/]+/[0-9a-f]+/raw/[0-9a-f]+/([^/]+)$"
(list _ filename))
(values str filename)]
; ""
; ""
[(or (regexp #px"^https://gist\\.github(?:usercontent|).com/[^/]+/[0-9a-f]+/raw$")
(regexp #px"^https://gitlab\\.com/snippets/[0-9]+/raw$")
(regexp #px"^http://pasterack\\.org/pastes/[0-9]+/raw$")
(regexp #px"^[0-9a-zA-Z]+$"))
(values str #f)]
; ""
; ""
; ""
[(or (regexp #px"^https://gist\\.github(?:usercontent|)\\.com/[^/]+/[0-9a-f]+$")
(regexp #px"^https://gitlab\\.com/snippets/[0-9]+$")
(regexp #px"^http://pasterack\\.org/pastes/[0-9]+$"))
(values (string-append str "/raw") #f)]
; ""
[(regexp #px"^[0-9a-zA-Z]+)$" (list _ name))
(values (string-append "" name) #f)]
; Any other kind of url, we assume a link to a raw file
[else (values str #f)]))
;; TODO: check it is indeed a (valid?) quickscript
;; TODO: get-pure-port also handles files. This could be useful.
;; To prevent CDN caching, add "?cachebust=<some-random-number>" at the end of the url
;; (or "&cachebust=..."), just to make sure the url is different.
(define (get-text-at-url aurl)
(port->string (get-pure-port (string->url aurl)
#:redirections 10)
#:close? #t))
;; Notice: Does not ask to replace (should be done prior).
;; Doesn't add a submodule if one already exists.
;; Allows the designer to give the default file name to save the script.
(define (write-script fout text aurl #:filename [filename (file-name-from-path fout)])
;; First, write the script to a temp file so as to check if it has some good module properties.
(define ftmp (make-temporary-file))
(display-to-file text ftmp #:exists 'replace) ; replace because `make-temporary-file` creates the file
(define submod-first-line
(string-append "(module " (symbol->string url2script-submod-name) " racket/base"))
(define add-submod?
(not (has-submod? ftmp))
#;(not (string-contains? text submod-first-line)))
#:exists 'replace
(if add-submod?
(provide filename url)
(define filename " (format "~s" (and filename (path->string filename))) ")
(define url " (format "~s" aurl) "))
(delete-file ftmp))
;; Don't allow file or network access in the url2script submodule,
;; in particular because this module is `require`d right after downloading,
;; before the user has a chance to look at the file.
(define dynreq-security-guard
(make-security-guard (current-security-guard)
(λ (sym pth access)
(unless (or (equal? access '(exists))
(equal? access '(read)))
(error (format "File access disabled ~a" (list sym pth access)))))
(λ _ (error "Network access disabled"))))
;; Get information from the url2script submodule.
(define (get-submod f sym [fail-thunk (λ () #f)])
(parameterize ([current-security-guard dynreq-security-guard]
[current-namespace (make-base-empty-namespace)])
(dynamic-require `(submod (file ,(path->string f)) ,url2script-submod-name)
;; Does the file contain a url2script submodule?
(define (has-submod? f)
(with-handlers ([exn:fail? (λ (e) #f)])
(get-submod f #f)
(define-script url2script
#:label "Fetch script…"
#:help-string "Asks for a URL and fetches the script"
#:menu-path ("url2script")
(λ (selection #:frame frame)
(define str (get-text-from-user
"Enter a URL to gist, gitlab snippet or pasterack, or to a raw racket file:"))
(when str
; At a special commit, with the name at the end, which we could extract.
(define-values (aurl maybe-filename) (parse-url str))
(define text (get-text-at-url aurl))
(define ftmp (make-temporary-file))
; Write a first time to maybe-write and read the submod infos
(write-script ftmp text aurl #:filename maybe-filename)
(define filename (get-submod ftmp 'filename))
; Ask the user for a filename and directory.
; Notice: If the directory is not in the Library's paths, Quickscript may not find the script.
; TODO: Check that it's in the Library's path and display a warning if not?
(define fout (put-file "url2script: Save script as…"
(or filename ".rkt")
'(("Racket source" "*.rkt")
("Any" "*.*"))))
(when fout
(write-script fout text str)
(smart-open-file frame fout))
(define-script update-script
#:label "Update current script"
#:help-string "Updates a script that was downloaded with url2script"
#:menu-path ("url2script")
(λ (selection #:file f #:frame drfr)
(when f
(define submod-url (get-submod f 'url))
(define-values (aurl _name) (parse-url submod-url))
(define text (get-text-at-url aurl))
(define ok?
(message-box "Attention"
"This will rewrite the current file. Continue?"
'(ok-cancel caution)))
(when ok?
(write-script f text aurl)
(when drfr (send drfr revert)))]
"Unable to find original url. Script may not have been downloaded with url2script."
'(ok stop))]))
;=== Tests ===;
(module+ test
(require rackunit)
(let ()
(define f (make-temporary-file))
(define aurl "")
(write-script f "#lang racket/base\n" aurl)
(check-equal? (get-submod f 'url)
(define (test-parse-url url)
(call-with-values (λ () (parse-url url)) list))
(test-parse-url "")
(test-parse-url "")
(test-parse-url "")
(list ""
(test-parse-url "")
; Filename is a little annoying to parse
(list ""
(test-parse-url "")
(list ""
(test-parse-url "")
(list "" #f))
(test-parse-url "")
(list "" #f))
(test-parse-url "")
(list "" #f))
(test-parse-url "")
(list "" #f))
(test-parse-url "")
(list "" #f))
(test-parse-url "")
(list "" #f))
;; TODO: Check that updating a script where the source does not have a url2script-info
;; submodule produces a script that still has the submodule
;=== url2script-info submodule ===;
(module url2script-info racket/base
(provide url filename)
(define filename "url2script.rkt")
(define url ""))
Copy link

Metaxal commented Jan 29, 2021

First install this script: Scripts|Manage scripts|New…, type url2script, then paste the content of the script at the url above in place of the default template, save, and finally Scripts|Manage scripts|Reload menu

Then to install a new script from (say) gist, just click on Scripts|url2script, paste in the url of the target script, type a script name (save it in the default directory if you don't know what you're doing) and reload the menu. Sometimes it can figure out a good default name.

Copy link

Metaxal commented Feb 1, 2021

The new version can update a script that was downloaded with url2script. This is possible because the url is stored in and read from the file (in a submodule).

url2script can even update itself!

Copy link

Metaxal commented Mar 27, 2021

You can find quickscripts to download with url2script here.

Copy link

Metaxal commented Oct 28, 2021

NEW: url2script is now part of the quickscript-extra package, which makes its installation much easier: raco pkg install quickcsript-extra or use DrRacket's File|Package manager….

You may need to delete the old url2script script file, or deactivate it in Scripts|Manage|Library… (make sure to disable the correct one).

The script on this page is deprecated but will be kept for reference. It will not be updated anymore.

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