]> git.donarmstrong.com Git - xtable.git/blob - pkg/R/sanitize.R
1313a96a4e2f89199907e8ae5f460f10c16b4c0a
[xtable.git] / pkg / R / sanitize.R
1 sanitize <- function(str, type) {
2   if(type == "latex"){
3     result <- str
4     result <- gsub("\\\\", "SANITIZE.BACKSLASH", result)
5     result <- gsub("$", "\\$", result, fixed = TRUE)
6     result <- gsub(">", "$>$", result, fixed = TRUE)
7     result <- gsub("<", "$<$", result, fixed = TRUE)
8     result <- gsub("|", "$|$", result, fixed = TRUE)
9     result <- gsub("{", "\\{", result, fixed = TRUE)
10     result <- gsub("}", "\\}", result, fixed = TRUE)
11     result <- gsub("%", "\\%", result, fixed = TRUE)
12     result <- gsub("&", "\\&", result, fixed = TRUE)
13     result <- gsub("_", "\\_", result, fixed = TRUE)
14     result <- gsub("#", "\\#", result, fixed = TRUE)
15     result <- gsub("^", "\\verb|^|", result, fixed = TRUE)
16     result <- gsub("~", "\\~{}", result, fixed = TRUE)
17     result <- gsub("SANITIZE.BACKSLASH", "$\\backslash$", result, fixed = TRUE)
18     return(result)
19   } else {
20     result <- str
21     result <- gsub("&", "&amp;", result, fixed = TRUE)
22     result <- gsub(">", "&gt;", result, fixed = TRUE)
23     result <- gsub("<", "&lt;", result, fixed = TRUE)
24     return(result)
25   }
26 }
27
28
29 sanitize.numbers <- function(str, type, math.style.negative){
30   if (type == "latex"){
31     result <- str
32     if ( math.style.negative ) {
33       for(i in 1:length(str)) {
34         result[i] <- gsub("-", "$-$", result[i], fixed = TRUE)
35       }
36     }
37     return(result)
38   } else {
39     return(str)
40   }
41 }
42
43
44 sanitize.final <- function(str, type){
45   if (type == "latex"){
46     return(str)
47   } else {
48     str$text <- gsub("  *", " ",  str$text, fixed = TRUE)
49     str$text <- gsub(' align="left"',  "", str$text,
50                         fixed = TRUE)
51     return(str)
52   }
53 }