File: markdown.R

package info (click to toggle)
r-cran-commonmark 1.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,192 kB
  • sloc: ansic: 10,131; sh: 14; makefile: 6
file content (81 lines) | stat: -rw-r--r-- 4,138 bytes parent folder | download
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#' Parse and render markdown text
#'
#' Converts markdown text to several formats using John MacFarlane's [cmark](https://github.com/commonmark/cmark)
#' reference implementation. Supported output formats include `html`, `latex`, groff `man`, and normalized
#' "commonmark" markdown. In addition the markdown parse tree can be returned in xml format.
#'
#' Support for extensions (including tables and autolink) is provided via the Github
#' [fork](https://github.com/github/cmark-gfm) of cmark. For now these are opt-in and have to be
#' enabled with the `extensions` parameter. See also the manual page on [extensions].
#'
#' When smart punctuation is enabled, straight double and single quotes will be rendered as curly quotes,
#' depending on their position. Moreover `--` will be rendered as -- (en-dash), `---` will be
#' rendered as --- (em-dash), and `...` will be rendered as ... (ellipses).
#'
#' @useDynLib commonmark R_render_markdown
#' @aliases commonmark markdown
#' @export
#' @rdname commonmark
#' @name commonmark
#' @param text Markdown text
#' @param sourcepos Include source position attribute in output.
#' @param hardbreaks Treat newlines as hard line breaks. If this option is specified, hard wrapping is disabled
#' regardless of the value given with `width`.
#' @param smart Use smart punctuation. See details.
#' @param normalize Consolidate adjacent text nodes.
#' @param footnotes parse footnotes
#' @param extensions Enables Github extensions. Can be `TRUE` (all) `FALSE` (none) or a character
#' vector with a subset of available [extensions].
#' @param width Specify wrap width (default 0 = nowrap).
#' @examples md <- readLines("https://raw.githubusercontent.com/yihui/knitr/master/NEWS.md")
#' html <- markdown_html(md)
#' xml <- markdown_xml(md)
#' man <- markdown_man(md)
#' tex <- markdown_latex(md)
#' cm <- markdown_commonmark(md)
#' text <- markdown_text(md)
markdown_html <- function(text, hardbreaks = FALSE, smart = FALSE, normalize = FALSE, sourcepos = FALSE, footnotes = FALSE, extensions = FALSE){
  text <- enc2utf8(paste(text, collapse="\n"))
  extensions <- get_extensions(extensions)
  .Call(R_render_markdown, text, 1L, sourcepos, hardbreaks, smart, normalize, footnotes, 0L, extensions)
}

#' @export
#' @rdname commonmark
markdown_xml <- function(text, hardbreaks = FALSE, smart = FALSE, normalize = FALSE, sourcepos = FALSE, footnotes = FALSE, extensions = FALSE){
  text <- enc2utf8(paste(text, collapse="\n"))
  extensions <- get_extensions(extensions)
  .Call(R_render_markdown, text, 2L, sourcepos, hardbreaks, smart, normalize, footnotes, 0L, extensions)
}

#' @export
#' @rdname commonmark
markdown_man <- function(text, hardbreaks = FALSE, smart = FALSE, normalize = FALSE, footnotes = FALSE, width = 0, extensions = FALSE){
  text <- enc2utf8(paste(text, collapse="\n"))
  extensions <- get_extensions(extensions)
  .Call(R_render_markdown, text, 3L, FALSE, hardbreaks, smart, normalize, footnotes, as.integer(width), extensions)
}

#' @export
#' @rdname commonmark
markdown_commonmark <- function(text, hardbreaks = FALSE, smart = FALSE, normalize = FALSE, footnotes = FALSE, width = 0, extensions = FALSE){
  text <- enc2utf8(paste(text, collapse="\n"))
  extensions <- get_extensions(extensions)
  .Call(R_render_markdown, text, 4L, FALSE, hardbreaks, smart, normalize, footnotes, as.integer(width), extensions)
}

#' @export
#' @rdname commonmark
markdown_text <- function(text, hardbreaks = FALSE, smart = FALSE, normalize = FALSE, footnotes = FALSE, width = 0, extensions = FALSE){
  text <- enc2utf8(paste(text, collapse="\n"))
  extensions <- get_extensions(extensions)
  .Call(R_render_markdown, text, 5L, FALSE, hardbreaks, smart, normalize, footnotes, as.integer(width), extensions)
}

#' @export
#' @rdname commonmark
markdown_latex <- function(text, hardbreaks = FALSE, smart = FALSE, normalize = FALSE, footnotes = FALSE, width = 0, extensions = FALSE){
  text <- enc2utf8(paste(text, collapse="\n"))
  extensions <- get_extensions(extensions)
  .Call(R_render_markdown, text, 6L, FALSE, hardbreaks, smart, normalize, footnotes, as.integer(width), extensions)
}