Debian Codegraph @

R/showcase.R

raw
1 #' @include globals.R
2 NULL
3
4 # Given the name of a license, return the appropriate link HTML for the
5 # license, which may just be the name of the license if the name is
6 # unrecognized.
7 #
8 # Recognizes the 'standard' set of licenses used for R packages
9 # (see http://cran.r-project.org/doc/manuals/R-exts.html)
10 licenseLink <- function(licenseName) {
11 licenses <- list(
12 "GPL-2" = "https://gnu.org/licenses/gpl-2.0.txt",
13 "GPL-3" = "https://gnu.org/licenses/gpl-3.0.txt",
14 "LGPL-3" = "https://www.gnu.org/licenses/lgpl-3.0.txt",
15 "LGPL-2" = "http://www.gnu.org/licenses/old-licenses/lgpl-2.0.txt",
16 "LGPL-2.1" = "http://www.gnu.org/licenses/lgpl-2.1.txt",
17 "AGPL-3" = "http://www.gnu.org/licenses/agpl-3.0.txt",
18 "Artistic-2.0" = "http://www.r-project.org/Licenses/Artistic-2.0",
19 "BSD_2_clause" = "http://www.r-project.org/Licenses/BSD_2_clause",
20 "BSD_3_clause" = "http://www.r-project.org/Licenses/BSD_3_clause",
21 "MIT" = "http://www.r-project.org/Licenses/MIT",
22 "CC-BY-SA-4.0" = "https://www.r-project.org/Licenses/CC-BY-SA-4.0")
23 if (exists(licenseName, where = licenses)) {
24 tags$a(href=licenses[[licenseName]], licenseName)
25 } else {
26 licenseName
27 }
28 }
29
30 # Returns tags containing showcase directives intended for the <HEAD> of the
31 # document.
32 showcaseHead <- function() {
33
34 deps <- list(
35 jqueryuiDependency(),
36 htmlDependency(
37 "highlight.js",
38 "6.2",
39 src = "www/shared/highlight",
40 package="shiny",
41 script = "highlight.pack.js",
42 stylesheet = "rstudio.css"
43 ),
44 htmlDependency(
45 "showcase",
46 "0.1.0",
47 src = "www/shared",
48 package = "shiny",
49 script = "shiny-showcase.js",
50 stylesheet = "shiny-showcase.css",
51 all_files = FALSE
52 )
53 )
54
55 mdfile <- file.path.ci(getwd(), 'Readme.md')
56 html <- tagList(
57 if (file.exists(mdfile)) {
58 md_content <- paste(readUTF8(mdfile), collapse="\n")
59 md_html <- commonmark::markdown_html(md_content, extensions = TRUE)
60 tags$template(id="showcase-markdown-content", HTML(md_html))
61 } else ""
62 )
63
64 return(attachDependencies(html, deps))
65 }
66
67 # Returns tags containing the application metadata (title and author) in
68 # showcase mode.
69 appMetadata <- function(desc) {
70 cols <- colnames(desc)
71 if ("Title" %in% cols)
72 with(tags, h4(class="text-muted shiny-showcase-apptitle", desc[1,"Title"],
73 if ("Author" %in% cols) small(
74 br(), "by",
75 if ("AuthorUrl" %in% cols)
76 a(href=desc[1,"AuthorUrl"], class="shiny-showcase-appauthor",
77 desc[1,"Author"])
78 else
79 desc[1,"Author"],
80 if ("AuthorEmail" %in% cols)
81 a(href=paste("mailto:", desc[1,"AuthorEmail"], sep = ''),
82 class="shiny-showcase-appauthoreemail",
83 desc[1,"AuthorEmail"])
84 else "")
85 else ""))
86 else ""
87 }
88
89 navTabsHelper <- function(files, prefix = "") {
90 lapply(files, function(file) {
91 with(tags,
92 li(class=if (tolower(file) %in% c("app.r", "server.r")) "active" else "",
93 a(href=paste("#", gsub(".", "_", file, fixed=TRUE), "_code", sep=""),
94 "data-toggle"="tab", "data-bs-toggle"="tab", paste0(prefix, file)))
95 )
96 })
97 }
98
99 navTabsDropdown <- function(files) {
100 if (length(files) > 0) {
101 with(tags,
102 li(role="presentation", class="dropdown",
103 a(class="dropdown-toggle", `data-toggle`="dropdown", `data-bs-toggle`="dropdown", href="#",
104 role="button", `aria-haspopup`="true", `aria-expanded`="false",
105 "www", span(class="caret")
106 ),
107 ul(class="dropdown-menu", navTabsHelper(files))
108 )
109 )
110 }
111 }
112
113 tabContentHelper <- function(files, path, language) {
114 lapply(files, function(file) {
115 tags$div(class=paste("tab-pane",
116 if (tolower(file) %in% c("app.r", "server.r")) " active"
117 else "",
118 sep=""),
119 id=paste(gsub(".", "_", file, fixed=TRUE),
120 "_code", sep=""),
121 tags$pre(class="shiny-code",
122 # we need to prevent the indentation of <code> ... </code>
123 HTML(format(tags$code(
124 class=paste0("language-", language),
125 paste(readUTF8(file.path.ci(path, file)), collapse="\n")
126 ), indent = FALSE))))
127 })
128 }
129
130 # Returns tags containing the application's code in Bootstrap-style tabs in
131 # showcase mode.
132 showcaseCodeTabs <- function(codeLicense) {
133 rFiles <- list.files(pattern = "\\.[rR]$")
134 wwwFiles <- list()
135 if (isTRUE(.globals$IncludeWWW)) {
136 path <- file.path(getwd(), "www")
137 wwwFiles$jsFiles <- list.files(path, pattern = "\\.js$")
138 wwwFiles$cssFiles <- list.files(path, pattern = "\\.css$")
139 wwwFiles$htmlFiles <- list.files(path, pattern = "\\.html$")
140 }
141 with(tags, div(id="showcase-code-tabs",
142 a(id="showcase-code-position-toggle",
143 class="btn btn-default btn-sm",
144 onclick="toggleCodePosition()",
145 icon("level-up-alt"),
146 "show with app"),
147 ul(class="nav nav-tabs",
148 navTabsHelper(rFiles),
149 navTabsDropdown(unlist(wwwFiles))
150 ),
151 div(class="tab-content", id="showcase-code-content",
152 tabContentHelper(rFiles, path = getwd(), language = "r"),
153 tabContentHelper(wwwFiles$jsFiles,
154 path = paste0(getwd(), "/www"),
155 language = "javascript"),
156 tabContentHelper(wwwFiles$cssFiles,
157 path = paste0(getwd(), "/www"),
158 language = "css"),
159 tabContentHelper(wwwFiles$htmlFiles,
160 path = paste0(getwd(), "/www"),
161 language = "xml")
162 ),
163 codeLicense))
164 }
165
166 # Returns tags containing the showcase application information (readme and
167 # code).
168 showcaseAppInfo <- function() {
169 descfile <- file.path.ci(getwd(), "DESCRIPTION")
170 hasDesc <- file.exists(descfile)
171 readmemd <- file.path.ci(getwd(), "Readme.md")
172 hasReadme <- file.exists(readmemd)
173 if (hasDesc) {
174 con <- textConnection(readUTF8(descfile))
175 on.exit(close(con), add = TRUE)
176 desc <- read.dcf(con)
177 }
178 with(tags,
179 div(class="container-fluid shiny-code-container well",
180 id="showcase-well",
181 div(class="row",
182 if (hasDesc || hasReadme) {
183 div(id="showcase-app-metadata", class="col-sm-4",
184 if (hasDesc) appMetadata(desc) else "",
185 if (hasReadme) div(id="readme-md"))
186 } else "",
187 div(id="showcase-code-inline",
188 class=if (hasReadme || hasDesc) "col-sm-8" else "col-sm-10 col-sm-offset-1",
189 showcaseCodeTabs(
190 if (hasDesc && "License" %in% colnames(desc)) {
191 small(class="showcase-code-license text-muted",
192 "Code license: ",
193 licenseLink(desc[1,"License"]))
194 } else "")))))
195 }
196
197
198 # Returns the body of the showcase document, given the HTML it should wrap.
199 showcaseBody <- function(htmlBody) {
200 with(tags, tagList(
201 table(id="showcase-app-code",
202 tr(td(id="showcase-app-container",
203 class="showcase-app-container-expanded",
204 htmlBody,
205 td(id="showcase-sxs-code",
206 class="showcase-sxs-code-collapsed")))),
207 showcaseAppInfo()))
208 }
209
210 # Sets the defaults for showcase mode (for app boot).
211 setShowcaseDefault <- function(showcaseDefault) {
212 .globals$showcaseDefault <- showcaseDefault
213 .globals$showcaseOverride <- as.logical(showcaseDefault)
214 }
215
216
217 # Given a UI tag/tagList, wrap it in appropriate tags for showcase mode.
218 showcaseUI <- function(ui) {
219 # If top-level tag is a body, replace its children with children wrapped in
220 # showcase stuff.
221 if (inherits(ui, "shiny.tag") && ui$name == "body") {
222 ui$children <- showcaseUI(ui$children)
223 return(ui)
224 }
225
226 tagList(
227 tags$head(showcaseHead()),
228 showcaseBody(ui)
229 )
230 }