r
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.6KB

  1. #' @importFrom stringi stri_join
  2. oneline <- function(..., collapse = "\n") {
  3. stri_join(c(...), collapse = collapse)
  4. }
  5. #' @importFrom stringi stri_split_lines1
  6. reline <- function(...) {
  7. stri_split_lines1(oneline(...))
  8. }
  9. #' @importFrom stringi stri_join
  10. brkt <- function(x) {
  11. stri_join("[[", x, "]]")
  12. }
  13. #' @importFrom stringi stri_join
  14. brktn <- function(x) {
  15. stri_join("\n[[", x, "]]\n")
  16. }
  17. #' @importFrom stringi stri_join
  18. divwrap <- function(text, id, class = "redoc") {
  19. stri_join("<div class=\"", class, "\" id=\"", id, "\">\n", text, "\n\n</div>")
  20. }
  21. #' @importFrom stringi stri_join
  22. spanwrap <- function(text, id, class = "redoc") {
  23. # stri_join("[\n", text, "\n]{class=\"", class, "\" id=\"", id, "\"}")
  24. stri_join("<span class=\"", class, "\" id=\"", id, "\">", text, "</span>")
  25. }
  26. #' Get the line number of the first fixed match
  27. #' @noRd
  28. #' @importFrom stringi stri_locate_first_fixed stri_sub stri_replace_all_fixed
  29. #' stri_count_fixed
  30. stri_lineno_first_fixed <- function(text, pattern) {
  31. loc <- stri_locate_first_fixed(text, pattern)
  32. pre <- stri_sub(text, from = 1L, to = loc[, 1] - 1)
  33. as.integer(stri_count_lines(pre))
  34. }
  35. stri_count_lines <- function(text) {
  36. text <- normalize_newlines(text)
  37. stri_count_fixed(text, "\n") + 1
  38. }
  39. #' @importFrom stringi stri_locate_all_fixed stri_replace_all_fixed
  40. insert_at_line <- function(text, insertion, line, newline = TRUE) {
  41. text2 <- normalize_newlines(text)
  42. if (newline) insertion <- stri_join(insertion, "\n")
  43. line_locs <- stri_locate_all_fixed(text2, "\n")[[1]][, 1]
  44. stri_sub(text2, line_locs[line], to = line_locs[line] - 1) <- insertion
  45. return(text2)
  46. }
  47. get_prior_empty_line_loc <- function(text, line) {
  48. text <- normalize_newlines(text)
  49. line_locs <- c(0, stri_locate_all_fixed(text, "\n")[[1]][, 1])
  50. if (line > length(line_locs)) {
  51. return(stri_length(text) + 1)
  52. }
  53. empty_locs <- rbind(c(0, 0), stri_locate_all_regex(text, "(?s)\n\\h?\n")[[1]])
  54. empty_loc <- max(empty_locs[empty_locs <= line_locs[line]])
  55. empty_loc
  56. }
  57. #' @importFrom stringi stri_locate_all_fixed stri_replace_all_fixed
  58. #' stri_locate_all_regex stri_length stri_sub<-
  59. insert_at_prior_empty_line <- function(text, insertion, line) {
  60. text2 <- normalize_newlines(text)
  61. ll <- get_prior_empty_line_loc(text2, line)
  62. if (ll == stri_length(text2) + 1) insertion <- stri_join("\n", insertion)
  63. stri_sub(text2, ll, ll) <- stri_join(insertion, "\n")
  64. return(text2)
  65. }
  66. normalize_newlines <- function(text) {
  67. stri_replace_all_fixed(text, c("\r\n"), "\n",
  68. vectorize_all = FALSE
  69. )
  70. }
  71. remove_extra_newlines <- function(text) {
  72. stri_replace_all_regex(text, "[\n\r]{2,}", "\n\n")
  73. }
  74. last <- function(x) {
  75. x[length(x)]
  76. }