-
Notifications
You must be signed in to change notification settings - Fork 7
/
html.R
133 lines (119 loc) · 4.37 KB
/
html.R
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#' @rdname Analysis
#' @export
Html <- R6::R6Class("Html",
inherit=ResultsElement,
private=list(
.scripts=character(),
.stylesheets=character(),
.content=''),
active=list(
scripts=function(value) {
if (base::missing(value))
return(private$.scripts)
if ( ! is.character(value))
stop('scripts must be a string')
private$.scripts <- value
base::invisible(self)
},
stylesheets=function(value) {
if (base::missing(value))
return(private$.stylesheets)
if ( ! is.character(value))
stop('stylesheets must be a string')
private$.stylesheets <- value
base::invisible(self)
},
content=function(value) {
if (base::missing(value))
return(private$.content)
self$setContent(value)
base::invisible(self)
}
),
public=list(
initialize=function(
options,
name='',
title='',
visible=TRUE,
clearWith='*',
refs=character(),
content='') {
super$initialize(
options=options,
name=name,
title=title,
visible=visible,
clearWith=clearWith,
refs=refs)
private$.content <- content
},
setContent=function(value) {
knitted <- knitr::knit(text=value)
knitMeta <- attr(knitted, 'html_dependencies')
if ( ! is.null(knitMeta)) {
knitMeta <- knitMeta[[1]]
package <- self$analysis$package
srcPath <- normalizePath(knitMeta$src$file)
rootPath <- normalizePath(system.file(package=package))
relPath <- substring(srcPath, nchar(rootPath)+1)
joinPaths <- function(path) {
if (identical(relPath, ''))
return(path)
file.path(relPath, path)
}
scripts <- sapply(knitMeta$script, joinPaths, USE.NAMES=FALSE)
sss <- sapply(knitMeta$stylesheet, joinPaths, USE.NAMES=FALSE)
private$.scripts <- scripts
private$.stylesheets <- sss
}
attributes(knitMeta) <- NULL
private$.content <- knitted
private$.stale <- FALSE
},
isFilled=function() {
if (private$.stale)
return(FALSE)
if (identical(private$.content, ''))
return(FALSE)
return(TRUE)
},
asString=function() {
content <- htmlToText(private$.content)
paras <- strsplit(content, '\n')
paras <- sapply(paras, strwrap, indent=1, exdent=1)
content <- paste0(paras, collapse="\n")
content <- paste0("\n", content, "\n")
content
},
fromProtoBuf=function(element, oChanges, vChanges) {
if ( ! private$.stale)
return()
private$.stale <- element$stale
someChanges <- length(oChanges) > 0 || length(vChanges) > 0
if (someChanges && base::identical('*', private$.clearWith)) {
private$.stale <- TRUE
} else if (base::any(oChanges %in% private$.clearWith)) {
private$.stale <- TRUE
} else {
for (clearName in private$.clearWith) {
if (base::any(vChanges %in% private$.options$option(clearName)$vars)) {
private$.stale <- TRUE
break()
}
}
}
super$fromProtoBuf(element, oChanges, vChanges)
private$.content <- element$html$content
private$.scripts <- element$html$scripts
private$.stylesheets <- element$html$stylesheets
},
asProtoBuf=function(incAsText=FALSE, status=NULL, includeState=TRUE) {
element <- super$asProtoBuf(incAsText=TRUE, status=status, includeState=includeState)
element$html$content <- private$.content
element$html$scripts <- private$.scripts
element$html$stylesheets <- private$.stylesheets
element
}
)
)