File: sanitize.R

package info (click to toggle)
r-cran-xtable 1%3A1.8-4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 992 kB
  • sloc: sh: 19; makefile: 2
file content (97 lines) | stat: -rw-r--r-- 3,470 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
sanitize <- function(str, type = "latex") {
  if(type == "latex"){
    result <- str
    result <- gsub("\\\\", "SANITIZE.BACKSLASH", result)
    result <- gsub("$", "\\$", result, fixed = TRUE)
    result <- gsub(">", "$>$", result, fixed = TRUE)
    result <- gsub("<", "$<$", result, fixed = TRUE)
    result <- gsub("|", "$|$", result, fixed = TRUE)
    result <- gsub("{", "\\{", result, fixed = TRUE)
    result <- gsub("}", "\\}", result, fixed = TRUE)
    result <- gsub("%", "\\%", result, fixed = TRUE)
    result <- gsub("&", "\\&", result, fixed = TRUE)
    result <- gsub("_", "\\_", result, fixed = TRUE)
    result <- gsub("#", "\\#", result, fixed = TRUE)
    result <- gsub("^", "\\verb|^|", result, fixed = TRUE)
    result <- gsub("~", "\\~{}", result, fixed = TRUE)
    result <- gsub("SANITIZE.BACKSLASH", "$\\backslash$", result, fixed = TRUE)
    return(result)
  } else {
    result <- str
    result <- gsub("&", "&amp;", result, fixed = TRUE)
    result <- gsub(">", "&gt;", result, fixed = TRUE)
    result <- gsub("<", "&lt;", result, fixed = TRUE)
    return(result)
  }
}


sanitize.numbers <- function(str, type,
                             math.style.negative = FALSE,
                             math.style.exponents = FALSE){
  if (type == "latex"){
    result <- str
    if ( math.style.negative ) {
      for(i in 1:length(str)) {
        result[i] <- gsub("-", "$-$", result[i], fixed = TRUE)
      }
    }
    if ( math.style.exponents ) {
      if (is.logical(math.style.exponents) && ! math.style.exponents ) {
      } else if (is.logical(math.style.exponents) && math.style.exponents ||
                 math.style.exponents == "$$"
                 ) {
        for(i in 1:length(str)) {
          result[i] <-
            gsub("^\\$?(-?)\\$?([0-9.]+)[eE]\\$?(-?)\\+?\\$?0*(\\d+)$",
                 "$\\1\\2 \\\\times 10^{\\3\\4}$", result[i])
        }
      } else if (math.style.exponents == "ensuremath") {
        for(i in 1:length(str)) {
          result[i] <-
            gsub("^\\$?(-?)\\$?([0-9.]+)[eE]\\$?(-?)\\+?\\$?0*(\\d+)$",
                 "\\\\ensuremath{\\1\\2 \\\\times 10^{\\3\\4}}",
                 result[i])
        }
      } else if (math.style.exponents == "UTF8" ||
                 math.style.exponents == "UTF-8") {
        for(i in 1:length(str)) {
          ## this code turns 1e5 into a UTF-8 representation of 1\times10^5
          if (all(grepl("^\\$?(-?)\\$?([0-9.]+)[eE]\\$?(-?)\\+?\\$?0*(\\d+)$",
                        result[i]))) {
            temp <- strsplit(result[i],"eE",result[i])
            result[i] <-
              paste0(temp[1],
                     "\u00d710",
                     chartr("-1234567890",
                            "\u207b\u00b9\u00b2\u00b3\u2074\u2075\u20746\u20747\u20748\u20749\u2070",
                            temp[2]))
          }
        }
      }
    }
    return(result)
  } else {
    return(str)
  }
}


sanitize.final <- function(str, type){
  if (type == "latex"){
    return(str)
  } else {
    str$text <- gsub("  *", " ",  str$text, fixed = TRUE)
    str$text <- gsub(' align="left"',  "", str$text,
                     fixed = TRUE)
    return(str)
  }
}

### Some trivial helper functions
### Suggested by Stefan Edwards, sme@iysik.com
### Helper function for disabling sanitizing
as.is <- function(str) {str}

### Helper function for embedding names in a math environment
as.math <- function(str, ...) { paste0('$',str,'$', ...) }