Pet*_*lis 5 html javascript syntax-highlighting shiny
我有一个闪亮的应用程序,它可以根据用户输入动态生成计算机代码并将其呈现给用户,以便他们可以准确地看到正在发送到数据库的查询。我有 prism 语法高亮显示,适用于直接在用户界面函数中的代码,所以我知道 prism 正在工作;但是对于在服务器中生成并通过发送给用户的代码renderText,htmlOutput突出显示不起作用。
这是一个简单示例的图像:
这是用这个 R 文件(以及wwwShiny 应用程序文件夹中的 prism.css 和 prism.js)制作的
ui <- shinyUI(fluidPage(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "prism.css")
),
tags$body(
tags$script(src="prism.js")
),
HTML("<pre><code class='language-sql'>SELECT * FROM mytable WHERE 1=2
-- this chunk should be syntax highlighted and it is
</code></pre>"),
HTML("<pre>SELECT * FROM mytable WHERE 1=2
-- this chunk should not be syntax highlighted
</code></pre>"),
htmlOutput("sql")
)
)
# Define the server code
server <- function(input, output) {
txt <- "<pre><code class='language-sql'>SELECT * FROM mytable WHERE 1=2
-- this chunk should be syntax highlighted but isn't for some reason,
-- presumably connected to it getting to the UI via renderText and htmlOutput
</code></pre>"
output$sql <- renderText(txt)
}
Run Code Online (Sandbox Code Playgroud)
在 DOM Explorer 中,我可以看到在 Shiny 生成的网页中,第三个(不工作)块位于<div class="shiny-html-output shiny-bound-output">, then 中正确包装在<pre>和<code class="language-sql">标签中,如第一个块。因此,被包裹在该 div 中是阻止 prism.js 执行其突出显示操作的原因。
我应该怎么做才能像第一个块一样突出显示第三个块?
Prism.js 在加载后立即运行,因此之后动态添加的任何代码块都不会突出显示。一种选择是prism.js也在服务器功能中动态加载。
output$sql <- renderUI({
tagList(
tags$script(src = "prism.js"),
HTML(txt)
)
})
Run Code Online (Sandbox Code Playgroud)
但这不是很稳健。您可以轻松加载脚本的多个副本。如果您将其设置为shiny::singleton或用于htmltools::htmlDependency仅加载脚本一次,您很容易发现自己回到了原始情况。
更好的解决方案 - Prism 提供了一个 API,可让您以编程方式突出显示代码块:http://prismjs.com/extending.html#api
Prism.highlightAll()渲染任何代码块后运行怎么样?
library(shiny)
prismCodeBlock <- function(code) {
tagList(
HTML(code),
tags$script("Prism.highlightAll()")
)
}
prismDependencies <- tags$head(
tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/prism/1.8.4/prism.min.js"),
tags$link(rel = "stylesheet", type = "text/css",
href = "https://cdnjs.cloudflare.com/ajax/libs/prism/1.8.4/themes/prism.min.css")
)
prismSqlDependency <- tags$head(
tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/prism/1.8.4/components/prism-sql.min.js")
)
ui <- fluidPage(
prismDependencies,
prismSqlDependency,
HTML("<pre><code class='language-sql'>SELECT * FROM mytable WHERE 1=2
-- this chunk should be syntax highlighted and it is
</code></pre>"),
HTML("<pre>SELECT * FROM mytable WHERE 1=2
-- this chunk should not be syntax highlighted
</code></pre>"),
htmlOutput("sql")
)
server <- function(input, output) {
txt <- "<pre><code class='language-sql'>SELECT * FROM mytable WHERE 1=2
-- this chunk should be syntax highlighted but isn't for some reason,
-- presumably connected to it getting to the UI via renderText and htmlOutput
</code></pre>"
output$sql <- renderUI({
prismCodeBlock(txt)
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
为了进一步改进,您可以Prism.highlightElement()提高效率。还可以从这些 Prism 代码块中创建一个 HTML 小部件,以抽象出杂乱的细节。
| 归档时间: |
|
| 查看次数: |
1187 次 |
| 最近记录: |