Type: Package
Title: Server-Side Syntax Highlighting
Version: 2.1.0
Description: Prism https://prismjs.com/ is a lightweight, extensible syntax highlighter, built with modern web standards in mind. This package provides server-side rendering in R using 'V8' such that no JavaScript library is required in the resulting HTML documents. Over 400 languages are supported.
License: MIT + file LICENSE
URL: https://ropensci.r-universe.dev/prismjs https://docs.ropensci.org/prismjs/
BugReports: https://github.com/ropensci/prismjs/issues
Encoding: UTF-8
Imports: V8, xml2
RoxygenNote: 7.2.1
Language: en-US
NeedsCompilation: no
Packaged: 2025-01-07 14:37:44 UTC; jeroen
Author: Jeroen Ooms ORCID iD [aut, cre], Lea Verou [cph] (Prism JavaScript library)
Maintainer: Jeroen Ooms <jeroenooms@gmail.com>
Repository: CRAN
Date/Publication: 2025-01-07 18:50:01 UTC

Prism Syntax Highlighter

Description

The prism_highlight_text function takes a string with a single code snippet and returns an html fragment with syntax classes. This html gets colorized by the prism stylesheet when both are inserted in an HTML document.

Usage

prism_highlight_text(txt, language = "r")

prism_highlight_document(
  input,
  output = NULL,
  include_css = FALSE,
  preview = interactive()
)

prism_process_xmldoc(doc)

prism_languages()

Arguments

txt

string with code that you want to highlight

language

the language that txt is in, one of prism_languages().

input

literal html string, connection, or file path, passed to xml2::read_html

output

path to file or connection to write to, passed to xml2::write_html. Set NULL to return the entire output document as a character string.

include_css

insert the Prism css style (with the default theme) into the html header.

preview

opens the generated output html in a browser

doc

an xml2 document that will be modified in place such that all ⁠<code class="language-xyz">⁠ elements are replaced with highlighted html.

Details

The function prism_highlight_document processes an entire HTML document, similar to how PrismJS works in a browser. It automatically finds all ⁠<code class="language-xyz">⁠ elements in the document and substitutes these with highlighted html elements. Again, CSS is needed to actually colorize the html, you can use include_css to automatically inject the CSS in the html header if your input document does not have this yet.

Value

html with classes that can be colorized using a prims stylesheet

Examples

html <- prism_highlight_text('p { color: red }', language = 'css')
cat(html)
prism_languages()