Skip to content

Instantly share code, notes, and snippets.

@wush978
Last active December 12, 2015 12:09
Show Gist options
  • Save wush978/4770351 to your computer and use it in GitHub Desktop.
Save wush978/4770351 to your computer and use it in GitHub Desktop.
add " " before ```
#! /usr/bin/Rscript
# convert from:
#
# ```
# xxxxx....
# ```
#
# to:
#
# ```txt
# xxxxx....
# ```
argv <- commandArgs(TRUE)
src <- readLines(argv[1])
is_code_block <- FALSE
greg.result <- gregexpr(pattern="^(\\s{4})*```(?<decorator>.*)$", text=src, perl=TRUE)
match.index <- which(unlist(greg.result) == 1)
for(i in match.index) {
if (!is_code_block) {
if (attr(greg.result[[i]], "capture.length")[1,"decorator"] == 0) src[i] <- sub(pattern="```", replacement="```txt", x=src[i])
}
is_code_block <- !is_code_block
}
write(src, file=argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment