File: parseRVignetteMetadata.R

package info (click to toggle)
r-cran-r.rsp 0.46.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,472 kB
  • sloc: javascript: 612; tcl: 304; sh: 18; makefile: 16
file content (53 lines) | stat: -rw-r--r-- 1,505 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
.parseRVignetteMetadata <- function(text, ...) {
  # Parse "\Vignette" directives into RSP metadata
  bfr <- unlist(strsplit(text, split="\n", fixed=TRUE), use.names=FALSE)

  pattern <- "[[:space:]]*%*[[:space:]]*\\\\Vignette(.*)\\{([^}]*)\\}"
  keep <- (regexpr(pattern, bfr) != -1L)
  bfr <- bfr[keep]

  # Nothing todo?
  if (length(bfr) == 0L) return(list())

  # Mapping from R vignette metadata to RSP metadata
  map <- c(
    # Official R vignette markup
    "IndexEntry"="title",
    "Keyword"="keyword",
    "Keywords"="keywords", ## Deprecated in R
    "Engine"="engine",
    # Custom
    "Subject"="subject",
    "Author"="author",
    "Date"="date",
    "Tangle"="tangle",
    "Compression"="compression"
  )

  metadata <- grep(pattern, bfr, value=TRUE)
  names <- gsub(pattern, "\\1", metadata)
  metadata <- gsub(pattern, "\\2", metadata)
  metadata <- trim(metadata)

  # Keep only known markup
  keep <- is.element(names, names(map))
  metadata <- metadata[keep]
  names <- names[keep]

  # Nothing todo?
  if (length(names) == 0L) return(list())

  # Rename
  names <- map[names]
  names(metadata) <- names
  metadata <- as.list(metadata)

  # Special: Merge all keyword meta data into one comma-separated entry
  idxs <- which(is.element(names(metadata), c("keyword", "keywords")))
  keywords <- unlist(metadata[idxs], use.names=FALSE)
  keywords <- paste(keywords, collapse=", ")
  metadata <- metadata[-idxs]
  metadata$keywords <- keywords

  metadata
} # .parseRVignetteMetadata()