From f8020f19a3d5591fa4f065c3638cd1962f2fb637 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Tue, 16 Nov 2021 22:19:52 -0600 Subject: [PATCH] add a wrapper function `system3()` based on `system2()` to mark the character output of `system2()` as UTF-8 if appropriate to avoid problems like https://github.com/rstudio/blogdown/commit/e4a3f38548f489f48a6706d4c7296b70cf024af6 --- DESCRIPTION | 2 +- NAMESPACE | 1 + NEWS.md | 2 ++ R/command.R | 25 +++++++++++++++++++++++++ man/system3.Rd | 30 ++++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 man/system3.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 6ea98da..dec280f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: xfun Type: Package Title: Supporting Functions for Packages Maintained by 'Yihui Xie' -Version: 0.28.5 +Version: 0.28.6 Authors@R: c( person("Yihui", "Xie", role = c("aut", "cre", "cph"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), person("Wush", "Wu", role = "ctb"), diff --git a/NAMESPACE b/NAMESPACE index 97e4233..dd8f1c2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -104,6 +104,7 @@ export(strict_list) export(stringsAsStrings) export(strings_please) export(submit_cran) +export(system3) export(tinify) export(tojson) export(tree) diff --git a/NEWS.md b/NEWS.md index d9415ba..bb4b3c7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,8 @@ - Added functions `rest_api_raw()` and `rest_api()` to get data from a REST API; also added the function `github_api()` to get data from the Github API based on `rest_api_raw()`. +- Added a wrapper function `system3()` based on `system2()` to mark the character output of `system2()` as UTF-8 if appropriate. + # CHANGES IN xfun VERSION 0.28 - Added a new function `url_accessible()` to test if a URL can be downloaded. diff --git a/R/command.R b/R/command.R index 02ae0c1..79ec5cd 100644 --- a/R/command.R +++ b/R/command.R @@ -1,3 +1,28 @@ +#' Run \code{system2()} and mark its character output as UTF-8 if appropriate +#' +#' This is a wrapper function based on \code{system2()}. If \code{system2()} +#' returns character output (e.g., with the argument \code{stdout = TRUE}), +#' check if the output is encoded in UTF-8. If it is, mark it with UTF-8 +#' explicitly. +#' @param ... Passed to \code{\link{system2}()}. +#' @return The value returned by \code{system2()}. +#' @export +#' @examplesIf interactive() +#' a = shQuote(c('-e', 'print(intToUtf8(c(20320, 22909)))')) +#' x2 = system2('Rscript', a, stdout = TRUE) +#' Encoding(x2) # unknown +#' +#' x3 = xfun::system3('Rscript', a, stdout = TRUE) +#' # encoding of x3 should be UTF-8 if the current locale is UTF-8 +#' !l10n_info()[['UTF-8']] || Encoding(x3) == 'UTF-8' # should be TRUE +system3 = function(...) { + res = system2(...) + if (is.character(res)) { + if (all(is_utf8(res))) Encoding(res) = 'UTF-8' + } + if (is.integer(res) && res == 0) invisible(res) else res +} + #' Run OptiPNG on all PNG files under a directory #' #' Call the command \command{optipng} via \code{system2()} to optimize all PNG diff --git a/man/system3.Rd b/man/system3.Rd new file mode 100644 index 0000000..121cff2 --- /dev/null +++ b/man/system3.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/command.R +\name{system3} +\alias{system3} +\title{Run \code{system2()} and mark its character output as UTF-8 if appropriate} +\usage{ +system3(...) +} +\arguments{ +\item{...}{Passed to \code{\link{system2}()}.} +} +\value{ +The value returned by \code{system2()}. +} +\description{ +This is a wrapper function based on \code{system2()}. If \code{system2()} +returns character output (e.g., with the argument \code{stdout = TRUE}), +check if the output is encoded in UTF-8. If it is, mark it with UTF-8 +explicitly. +} +\examples{\dontshow{if (interactive()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +a = shQuote(c("-e", "print(intToUtf8(c(20320, 22909)))")) +x2 = system2("Rscript", a, stdout = TRUE) +Encoding(x2) # unknown + +x3 = xfun::system3("Rscript", a, stdout = TRUE) +# encoding of x3 should be UTF-8 if the current locale is UTF-8 +!l10n_info()[["UTF-8"]] || Encoding(x3) == "UTF-8" # should be TRUE +\dontshow{\}) # examplesIf} +}