Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

Revision a1aebffe36b8b718457165826816502ca9d4ce2f authored by Hadley Wickham on 13 January 2022, 18:32:42 UTC, committed by cran-robot on 13 January 2022, 18:32:42 UTC
version 2.0.2
1 parent df465aa
  • Files
  • Changes
  • 7d0b99b
  • /
  • tools
  • /
  • syntax-highlight.R
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • revision
  • directory
  • content
revision badge
swh:1:rev:a1aebffe36b8b718457165826816502ca9d4ce2f
directory badge
swh:1:dir:af3259a4f1d14c03db6132a61c8185be9542efc6
content badge
swh:1:cnt:38cd8cb2e938be5c9f715c3d6333a24405af5108

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • revision
  • directory
  • content
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
syntax-highlight.R
library(jsonlite)
library(purrr)
library(dplyr)
library(fs)

# Pandoc styles are based on KDE default styles:
# https://docs.kde.org/stable5/en/kate/katepart/highlight.html#kate-highlight-default-styles
# But in HTML use two letter abbreviations:
# https://github.com/jgm/skylighting/blob/a1d02a0db6260c73aaf04aae2e6e18b569caacdc/skylighting-core/src/Skylighting/Format/HTML.hs#L117-L147
# Summary at
# https://docs.google.com/spreadsheets/d/1JhBtQSCtQ2eu2RepLTJONFdLEnhM3asUyMMLYE3tdYk/edit#gid=0

class <- c(
  "Keyword"        = "span.kw",
  "DataType"       = "span.dt",
  "DecVal"         = "span.dv",
  "BaseN"          = "span.bn",
  "Float"          = "span.fl",
  "Char"           = "span.ch",
  "String"         = "span.st",
  "Comment"        = "span.co",
  "Other"          = "span.ot",
  "Others"         = "span.ot", # both spellings used in themes
  "Alert"          = "span.al",
  "Function"       = "span.fu",
  "RegionMarker"   = "span.re",
  "Error"          = "span.er",
  "Constant"       = "span.cn",
  "SpecialChar"    = "span.sc",
  "VerbatimString" = "span.vs",
  "SpecialString"  = "span.ss",
  "Import"         = "span.im",
  "Documentation"  = "span.do",
  "Annotation"     = "span.an",
  "CommentVar"     = "span.cv",
  "Variable"       = "span.va",
  "ControlFlow"    = "span.cf",
  "Operator"       = "span.op",
  "BuiltIn"        = "span.bu",
  "Extension"      = "span.ex",
  "Preprocessor"   = "span.pp",
  "Attribute"      = "span.at",
  "Information"    = "span.in",
  "Warning"        = "span.wa",
  "Normal"         = ""
)

read_theme <- function(name) {
  jsonlite::read_json(paste0(
    "https://raw.githubusercontent.com/quarto-dev/quarto-cli/",
    "main/src/resources/pandoc/highlight-styles/", name
  ))
}

theme_df <- function(theme) {
  bg <- pluck(theme, "background-color") %||% pluck(theme, "editor-colors", "BackgroundColor")
  fg <- pluck(theme, "text-color")

  df <- purrr::map_df(theme$`text-styles`, compact, .id = "name")
  df %>%
    rename(color = any_of("text-color"), background = any_of("background-color")) %>%
    mutate(class = class[name], name = name, `selected-text-color` = NULL) %>%
    arrange(class) %>%
    structure(bg = bg, fg = fg)
}

style_to_css <- function(name, class, color = NA, background = NA, bold = FALSE, italic = FALSE, underline = FALSE, ...) {
  attr <- c(
    if (!is.na(color)) paste0("color:", color),
    if (!is.na(background)) paste0("background-color:", background),
    if (!is.na(bold) && bold) "font-weight: bold",
    if (!is.na(italic) && italic) "font-style: italic",
    if (!is.na(underline) && underline) "text-decoration: underline"
  )

  paste0("pre code ", class, " /* ", name, " */ {", paste0(attr, collapse = "; "), "}")
}


safe_format <- function(x) {
  ifelse(is.na(x), NA, format(x))
}

theme_as_css <- function(df, path = stdout()) {
  css <- df %>%
    filter(!is.na(class)) %>%
    mutate(name = safe_format(name), class = safe_format(class)) %>%
    pmap_chr(style_to_css)

  attrs <- attributes(df)
  if (any(c("bg", "fg") %in% names(attrs))) {
    pre <- paste0("pre {",
      paste(c(
        paste0("background-color: ", attrs$bg, ";", recycle0 = TRUE),
        paste0("color: ", attrs$fg, ";", recycle0 = TRUE)
      ), collapse = " "),
      "}"
    )
    css <- c(pre, css)
  }

  base::writeLines(css, path)
}

save_theme <- function(theme, name) {
  cat(name, "\n", sep = "")
  df <- theme_df(theme)
  theme_as_css(df, path("inst", "highlight-styles", path_ext_set(name, "scss")))
}

json <- gh::gh("/repos/{owner}/{repo}/contents/{path}",
  owner = "quarto-dev",
  repo = "quarto-cli",
  path = "src/resources/pandoc/highlight-styles"
)

theme_names <- json %>% map_chr("name")
theme_json <- map(theme_names, read_theme)
names(theme_json) <- theme_names
iwalk(theme_json, save_theme)

# themes <- theme_names %>% set_names() %>% map_df(theme_df, .id = "theme")
# themes %>% count(name, sort = T)
# themes %>% count(theme, is.na(color)) %>% print(n = Inf)
# themes %>% filter(is.na(class)) %>% print(n = Inf)
The diff you're trying to view is too large. Only the first 1000 changed files have been loaded.
Showing with 0 additions and 0 deletions (0 / 0 diffs computed)
swh spinner

Computing file changes ...

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API