Skip to content

Commit

Permalink
close #67: add a function get_input() to get the input file path to f…
Browse files Browse the repository at this point in the history
…use()
  • Loading branch information
yihui committed Jan 22, 2025
1 parent 40873a9 commit 96ce82e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: litedown
Type: Package
Title: A Lightweight Version of R Markdown
Version: 0.5.6
Version: 0.5.7
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666", URL = "https://yihui.org")),
person("Tim", "Taylor", role = "ctb", comment = c(ORCID = "0000-0002-8587-7113")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(fuse)
export(fuse_book)
export(fuse_env)
export(fuse_site)
export(get_input)
export(html_format)
export(latex_format)
export(mark)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# CHANGES IN litedown VERSION 0.6

- Added a function `get_input()` to get the input file path to `fuse()` (thanks, @MichaelChirico, #67).

- Fixed a bug that `fuse()` fails to print the error location when the whole input document consists of a single chunk that throws an error (thanks, @kevinushey, yihui/knitr#2387).

# CHANGES IN litedown VERSION 0.5
Expand Down
13 changes: 11 additions & 2 deletions R/fuse.R
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,21 @@ convert_knitr = function(input) {
write_utf8(x, input)
}

#' Get the input file path of `fuse()`
#'
#' A helper function to return the input file path of [fuse()] when called
#' inside a code chunk.
#' @return A character string, or `NULL` if the input is not a file (e.g., when
#' using the `text` argument of `fuse()`).
#' @export
get_input = function() .env$input

# return a string to indicate the error location
get_loc = function(label) {
l = .env$source_pos; n = length(l)
if (n == 4) l = sprintf('#%d:%d-%d:%d', l[1], l[2], l[3], l[4]) # row1:col1-row2:col2
if (n == 2) l = sprintf('#%d-%d', l[1], l[2]) # row1-row2
paste0(.env$input, l, if (label != '') paste0(' [', label, ']'))
paste0(.env$input2 %|% .env$input, l, if (label != '') paste0(' [', label, ']'))
}

# save line numbers in .env to be used in error messages
Expand Down Expand Up @@ -564,7 +573,7 @@ fiss = function(input, output = '.R', text = NULL) {
# add ANSI link on input path if supported
ansi_link = function(x) {
if (length(x) && isTRUE(as.logical(Sys.getenv('RSTUDIO_CLI_HYPERLINKS'))))
.env$input = sprintf(
.env$input2 = sprintf(
'\033]8;%s;file://%s\a%s\033]8;;\a', link_pos(), normalize_path(x), x
)
}
Expand Down
16 changes: 16 additions & 0 deletions man/get_input.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 96ce82e

Please sign in to comment.