# NOT RUN {
sgr_to_html("hello\033[31;42;1mworld\033[m")
sgr_to_html("hello\033[31;42;1mworld\033[m", classes=TRUE)
## Input contains HTML special chars
x <- "<hello \033[42m'there' \033[34m &\033[m \"moon\"!"
writeLines(x)
# }
# NOT RUN {
in_html(
c(
sgr_to_html(html_esc(x)), # Good
sgr_to_html(x) # Bad!
) )
# }
# NOT RUN {
## Generate some class names for basic colors
classes <- expand.grid(
"myclass",
c("fg", "bg"),
c("black", "red", "green", "yellow", "blue", "magenta", "cyan", "white")
)
classes # order is important!
classes <- do.call(paste, c(classes, sep="-"))
## We only provide 16 classes, so Only basic colors are
## mapped to classes; others styled inline.
sgr_to_html(
"\033[94mhello\033[m \033[31;42;1mworld\033[m",
classes=classes
)
## Create a whole web page with a style sheet for 256 colors and
## the colors shown in a table.
class.256 <- do.call(paste, c(expand.grid(c("fg", "bg"), 0:255), sep="-"))
sgr.256 <- sgr_256() # A demo of all 256 colors
writeLines(sgr.256[1:8]) # SGR formatting
## Convert to HTML using classes instead of inline styles:
html.256 <- sgr_to_html(sgr.256, classes=class.256)
writeLines(html.256[1]) # No inline colors
## Generate different style sheets. See `?make_styles` for details.
default <- make_styles(class.256)
mix <- matrix(c(.6,.2,.2, .2,.6,.2, .2,.2,.6), 3)
desaturated <- make_styles(class.256, mix)
writeLines(default[1:4])
writeLines(desaturated[1:4])
## Embed in HTML page and diplay; only CSS changing
# }
# NOT RUN {
in_html(html.256) # no CSS
in_html(html.256, css=default) # default CSS
in_html(html.256, css=desaturated) # desaturated CSS
# }
Run the code above in your browser using DataLab