Skip to content

Commit

Permalink
use xfun::process_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Mar 15, 2021
1 parent f324246 commit b488452
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Imports:
yaml (>= 2.1.19),
httpuv (>= 1.4.0),
later,
xfun (>= 0.21),
xfun (>= 0.22),
servr (>= 0.21)
Suggests:
testit,
Expand Down
13 changes: 6 additions & 7 deletions R/clean.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@

# a wrapper function to read a file as UTF-8, process the text, and write back
process_file = function(f, FUN, x = read_utf8(f)) {
x = FUN(x)
if (missing(f)) x else write_utf8(x, f)
xfun::process_file(f, FUN, x)
}

# replace three or more \n with two, i.e. two or more empty lines with one
remove_extra_empty_lines = function(...) process_file(..., FUN = function(x) {
remove_extra_empty_lines = function(...) xfun::process_file(..., fun = function(x) {
x = paste(gsub('\\s+$', '', x), collapse = '\n')
trim_ws(gsub('\n{3,}', '\n\n', x))
})

# replace [url](url) with <url>
process_bare_urls = function(...) process_file(..., FUN = function(x) {
process_bare_urls = function(...) xfun::process_file(..., fun = function(x) {
gsub('\\[([^]]+)]\\(\\1/?\\)', '<\\1>', x)
})

normalize_chars = function(...) process_file(..., FUN = function(x) {
normalize_chars = function(...) xfun::process_file(..., fun = function(x) {
# curly single and double quotes to straight quotes
x = gsub(paste0('[', intToUtf8(8216:8217), ']'), "'", x)
x = gsub(paste0('[', intToUtf8(8220:8221), ']'), '"', x)
Expand All @@ -28,7 +27,7 @@ normalize_chars = function(...) process_file(..., FUN = function(x) {
})

# clean up code blocks that have been syntax highlighted by Pandoc
remove_highlight_tags = function(...) process_file(..., FUN = function(x) {
remove_highlight_tags = function(...) xfun::process_file(..., fun = function(x) {
clean = function(x) {
# remove the <code></code> tags
x = gsub('^(\\s+)<code( class="[^"]*")?>(.*)', '\\1\\3', x)
Expand All @@ -44,6 +43,6 @@ remove_highlight_tags = function(...) process_file(..., FUN = function(x) {
})

# <img></img> to <img />
fix_img_tags = function(...) process_file(..., FUN = function(x) {
fix_img_tags = function(...) xfun::process_file(..., fun = function(x) {
gsub('></img>', ' />', x)
})

0 comments on commit b488452

Please sign in to comment.