55# ' Special-purpose function to download a folder of course materials. The only
66# ' demand on the user is to confirm or specify where the new folder should be
77# ' stored. Workflow:
8- # ' * User executes something like: `use_course("http:// bit.ly/xxx-yyy-zzz")`.
8+ # ' * User executes something like: `use_course("bit.ly/xxx-yyy-zzz")`.
99# ' * User is asked to notice and confirm the location of the new folder. Specify
1010# ' `destdir` to skip this.
1111# ' * User is asked if they'd like to delete the ZIP file.
1212# ' * New folder is opened in the file manager, e.g. Finder or File Explorer.
1313# '
14+ # ' If `url` has no "http" prefix, "https://" is prepended, allowing for even
15+ # ' less typing by the user. Most URL shorteners give HTTPS links and,
16+ # ' anecdotally, we note this appears to work with [bit.ly](https://bitly.com/)
17+ # ' links, even though they are nominally HTTP.
18+ # '
1419# ' @param url Link to a ZIP file containing the materials, possibly behind a
1520# ' shortlink. Function developed with DropBox and GitHub in mind, but should
16- # ' work for ZIP files generally. See [use_course_details] for more.
21+ # ' work for ZIP files generally. If no "http" prefix is found, "https://" is
22+ # ' prepended. See [use_course_details] for more.
1723# ' @param destdir The new folder is stored here. Defaults to user's Desktop.
1824# '
1925# ' @return Path to the new directory holding the course materials, invisibly.
3440# ' use_course("https://api.github.com/repos/r-lib/rematch2/zipball/master")
3541# ' }
3642use_course <- function (url , destdir = NULL ) {
43+ url <- normalize_url(url )
3744 zipfile <- download_zip(
3845 url ,
3946 destdir = destdir %|| % conspicuous_place(),
@@ -60,7 +67,7 @@ use_course <- function(url, destdir = NULL) {
6067# '
6168# ' ## as called inside use_course()
6269# ' download_zip(
63- # ' url,
70+ # ' url, ## after post-processing with normalize_url()
6471# ' ## conspicuous_place() = Desktop or home directory or working directory
6572# ' destdir = destdir \\%||\\% conspicuous_place(),
6673# ' pedantic = is.null(destdir) && interactive()
@@ -241,6 +248,12 @@ tidy_unzip <- function(zipfile) {
241248 invisible (target )
242249}
243250
251+ normalize_url <- function (url ) {
252+ stopifnot(is.character(url ))
253+ has_scheme <- grepl(" ^http[s]?://" , url )
254+ ifelse(has_scheme , url , paste0(" https://" , url ))
255+ }
256+
244257conspicuous_place <- function () {
245258 Filter(dir.exists , c(
246259 file.path(Sys.getenv(" HOME" ), " Desktop" ), # typical macOS = ~/Desktop
0 commit comments