This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from stencila/develop
v0.28.0
- Loading branch information
Showing
45 changed files
with
1,818 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#' Opens the file that is currently open in the source editor in Stencila | ||
addin_open <- function() { | ||
# Get the path of the file currently in the source editor | ||
editor <- rstudioapi::getSourceEditorContext() | ||
path <- editor$path | ||
# If path is empty (as it is for a new document) we could revert to loading the | ||
# editor$contents but currently error | ||
if (nchar(path)==0) stop('Error when attempting to open in Stencila: no file currently active', call.=FALSE) | ||
host$open(paste0('file://', path)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#' A file storer | ||
#' @export | ||
FileStorer <- R6::R6Class('FileStorer', | ||
|
||
public = list( | ||
|
||
#' @section new(): | ||
#' | ||
#' Create a new \code{FileStorer} | ||
#' | ||
#' \describe{ | ||
#' \item{path}{Local file system path. Default \code{''}} | ||
#' \item{version}{Version of storer. Currently ignored but required for compatability. Default \code{NULL}} | ||
#' } | ||
initialize = function (path = '/', version = NULL) { | ||
path <- suppressWarnings(normalizePath(path)) | ||
if (file.exists(path)) { | ||
isdir <- file.info(path)[1, 'isdir'] | ||
} else { | ||
isdir <- !str_detect(path, '\\.([[:alnum:]]+)$') | ||
} | ||
private$.dir <- if (isdir) path else dirname(path) | ||
private$.main <- if (isdir) NULL else basename(path) | ||
}, | ||
|
||
#' @section getDirectory(): | ||
#' | ||
#' Get the directory path of this storer | ||
getDirectory = function () { | ||
private$.dir | ||
}, | ||
|
||
#' @section getMain(): | ||
#' | ||
#' Get the main file, if specified | ||
getMain = function () { | ||
private$.main | ||
}, | ||
|
||
#' @section getFiles(): | ||
#' | ||
#' List files within this storer | ||
getFiles = function () { | ||
I(setdiff(list.files(private$.dir), list.dirs(private$.dir, recursive = FALSE, full.names = FALSE))) | ||
}, | ||
|
||
#' @section getInfo(): | ||
#' | ||
#' Get information about this storer: | ||
#' | ||
#' \describe{ | ||
#' \item{dir}{The absolute file sytem path of the storer's directory} | ||
#' \item{main}{The main file, if specified} | ||
#' \item{files}{A list of file in the storer} | ||
#' } | ||
getInfo = function () { | ||
list( | ||
dir=self$getDirectory(), | ||
main=self$getMain(), | ||
files=self$getFiles() | ||
) | ||
}, | ||
|
||
#' @section readFile(): | ||
#' | ||
#' Read content from the file at \code{path} | ||
#' | ||
#' \describe{ | ||
#' \item{path}{Path within the storer} | ||
#' } | ||
readFile = function (path) { | ||
path <- file.path(private$.dir, path) | ||
connection <- file(path, 'rt') | ||
content <- readChar(connection, file.info(path)$size) | ||
close(connection) | ||
content | ||
}, | ||
|
||
#' @section writeFile(): | ||
#' | ||
#' Write content to the file at \code{path} | ||
#' | ||
#' \describe{ | ||
#' \item{path}{Path within the storer} | ||
#' \item{content}{Content to write} | ||
#' } | ||
writeFile = function (path, content) { | ||
path <- file.path(private$.dir, path) | ||
connection <- file(path, 'wt') | ||
# It would seem to make sense to use `writeChar` here but that | ||
# appears to write a binary encoded file. So using `cat`. | ||
cat(content, file=connection) | ||
close(connection) | ||
}, | ||
|
||
#' @section deleteFile(): | ||
#' | ||
#' Delete the file at \code{path} | ||
#' | ||
#' \describe{ | ||
#' \item{path}{Path within the storer} | ||
#' } | ||
deleteFile = function (path) { | ||
path <- file.path(private$.dir, path) | ||
file.remove(path) | ||
} | ||
|
||
), | ||
|
||
private = list( | ||
.dir = NULL, | ||
.main = NULL | ||
) | ||
) | ||
|
||
FileStorer$spec <- list( | ||
name = 'FileStorer', | ||
base = 'Storer', | ||
aliases = c('file') | ||
) |
Oops, something went wrong.