Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. #' Converts an R reStructuredText document (Rrst) to R Markdown (Rmd)
  2. #'
  3. #' @param input character string with the path of the file to convert
  4. #' @param output optional character string with the path for the output file
  5. #' @param out_dir optional character string with the output directory. Not used
  6. #' if \code{output} is supplied.
  7. #' @param output_file logical whether to return the output to a file or not.
  8. #' If \code{FALSE}, then \code{output} and \code{out_dir} are ignored.
  9. #'
  10. #' @importFrom readr read_file write_file
  11. #' @importFrom tools file_path_sans_ext
  12.  
  13. rrst_to_rmd <- function(input, output, out_dir, output_file = TRUE) {
  14. library(readr)
  15. library(tools)
  16.  
  17. if (!file.exists(input))
  18. stop(sprintf('Unable to find %s', input))
  19.  
  20. # Find basic file name and file path
  21. input_name <- file_path_sans_ext(basename(input))
  22.  
  23. # Read file into R
  24. txt <- read_file(input)
  25.  
  26. #### Conversions -----------------------------------------------------------
  27. # h1
  28. txt_edit <- gsub('\\n~~~~*\\n', '\n======\n', txt)
  29. # h2
  30. txt_edit <- gsub('\\n\\+\\+\\+\\+*\\n', '\n------\n', txt_edit)
  31. # h3
  32. txt_edit <- gsub('\\n\\!\\!\\!\\!*\\n', '\\!\\!\\!\\!\\!\\!\\!\\!\n',
  33. txt_edit)
  34. txt_edit <- gsub('(\\n\\n){1}(.+?)(\\!\\!\\!\\!\\!\\!\\!\\!)',
  35. '\n\n### \\2', txt_edit, perl = TRUE)
  36.  
  37. # inline math
  38. txt_edit <- gsub('(?<!`)(?<=(?!``).)`(?!`)', '$', txt_edit, perl = TRUE)
  39. txt_edit <- gsub(":math:", '', txt_edit)
  40.  
  41. # inline code chunks
  42. txt_edit <- gsub('``', '`', txt_edit)
  43.  
  44. # display code chunks
  45. txt_edit <- gsub('\\n\\.\\. \\{', '\n```{', txt_edit)
  46. txt_edit <- gsub('\\n\\.\\. \\.\\.', '\n```', txt_edit)
  47.  
  48. # display math
  49. txt_edit <- gsub('(math::)(\\n\\n)', '\\1', txt_edit, perl = TRUE)
  50. txt_edit <- gsub('(math::)(\\s+)', '\\1', txt_edit)
  51. txt_edit <- gsub('math::.+?\\K(\\n{1} +)', 'PLACEHOLDER', txt_edit,
  52. perl = TRUE)
  53. txt_edit <- gsub('math::.*\\K\\n\\n', '\n$$\n\n', txt_edit, perl = TRUE)
  54. txt_edit <- gsub('.. math::', '\n$$\n ', txt_edit, perl = TRUE)
  55. txt_edit <- gsub('PLACEHOLDER', '\n ', txt_edit, perl = TRUE)
  56.  
  57. # Cleanup
  58. txt_edit <- gsub('\\n\\n\\n', '\n\n', txt_edit)
  59.  
  60. # Return output ------------------------------------------------------------
  61. if (isTRUE(output_file)) {
  62. if (missing(output)) {
  63. if (missing(out_dir))
  64. input_path <- dirname(input)
  65. else input_path <- out_dir
  66. output <- paste0(input_path, '/', input_name, '.Rmd')
  67. }
  68. write_file(txt_edit, path = output)
  69. }
  70. else
  71. return(txt_edit)
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement