Package 'klippy'

Title: Copy to Clipboard Buttons for R Markdown HTML Documents
Description: Insert copy to clipboard buttons in HTML documents generated with 'rmarkdown'. This package is suited for a call in a 'knitr' chunk. Buttons events are rendered using 'clipboard.js' library and 'Bootstrap.js' Tooltip plugin.
Authors: Romain Lesur [aut, cre] , Zeno Rocha [cph] (clipboard.js library), GitHub, Inc. [cph] (Primer Tooltips library & Octicons library)
Maintainer: Romain Lesur <[email protected]>
License: MIT + file LICENSE
Version: 0.0.0.9500
Built: 2024-12-06 05:11:06 UTC
Source: https://github.com/rlesur/klippy

Help Index


Provide HTML dependencies

Description

These functions provide HTML dependencies for clipboard.js, klippy and ⁠Primer Tooltips CSS⁠ for re-use.

Usage

html_dependency_clipboard()

html_dependency_primer_tooltips()

html_dependency_klippy()

Value

An object that can be included in a list of dependencies passed to htmltools::attachDependencies().

See Also

Other HTML dependencies functions: klippy_dependencies()


Insert copy to clipboard buttons in HTML documents

Description

klippy insert copy to clipboard buttons (or "klippies") in R Markdown HTML documents. In the rendered document, "klippies" are inserted in the upper left corner of the code chunks. klippy() function is suited for a call in a knitr code chunk.

Usage

klippy(
  lang = c("r", "markdown"),
  all_precode = FALSE,
  position = c("top", "left"),
  color = "auto",
  tooltip_message = "Copy code",
  tooltip_success = "Copied!"
)

Arguments

lang

A character string or a vector of character strings with language names. If a character string contains multiple languages names, these names have to be separated by boundaries (e.g., spaces). Void string can be passed to lang argument.

all_precode

A logical scalar. If TRUE, a "klippy" is added to all ⁠HTML <pre>⁠ elements having an ⁠HTML <code>⁠ element as a child.

position

A character vector with klippy position. Accepted values are "top", "bottom", "left" and "right". Abbreviated forms are allowed.

color

String of any of the three kinds of R color specifications, i.e., either a color name (as listed by grDevices::colors()), a hexadecimal string of the form "#rrggbb" or "#rrggbbaa" (see grDevices::rgb()), or a positive integer i meaning ⁠[palette][grDevices::palette]()[i]⁠. Default value is "auto": color is set to the anchor color of the document.

tooltip_message

String with the tooltip message.

tooltip_success

String with the tooltip message shown when code is successfully copied.

Details

klippy() function appends JavaScript functions and CSS in the rendered document that:

  1. Add klippy to the class attribute of selected ⁠<pre>⁠ elements.

  2. Add a ⁠<button>⁠ element (a "klippy") as a child for all ⁠<pre>⁠ elements with a klippy class attribute.

  3. Instantiate clipboard.js event listeners and attach them to klippies.

klippy class can also be appended to a ⁠<pre>⁠ element using ⁠knitr class.source⁠ chunk option. "Klippy" buttons are not rendered if the browser does not support clipboard.js library (see here for details).

Value

An HTML tag object that can be rendered as HTML using as.character().

Examples

tf <- tempfile(fileext = c(".Rmd", ".html"))
writeLines(
  c("```{r klippy, echo=FALSE, include=TRUE}",
    "klippy::klippy()",
    "```",
    "Insert this chunk in your `Rmd` file:",
    "````markdown",
    "`r ''````{r klippy, echo=FALSE, include=TRUE}",
    "klippy::klippy()",
    "```",
    "````"
  ),
  tf[1]
)

rmarkdown::render(tf[1], "html_document", tf[2])

List klippy dependencies

Description

This function is used to get the list of klippy dependencies.

Usage

klippy_dependencies()

Value

A list of dependencies that can be passed to htmltools::attachDependencies() or rmarkdown::html_document_base().

See Also

Other HTML dependencies functions: html_dependencies