Skip to content

Instantly share code, notes, and snippets.

@hadley
Created August 23, 2012 19:34
Show Gist options
  • Save hadley/3440666 to your computer and use it in GitHub Desktop.
Save hadley/3440666 to your computer and use it in GitHub Desktop.
roc_title <- roccer("@title",
parse_tag = parse_string(),
output_tag = output_rd("title"),
)
roc_description <- roccer("@description",
parse_tag = parse_string(),
output_tag = output_rd("description"),
)
roc_details <- roccer("@details",
parse_tag = parse_string(),
output_tag = output_rd("details"),
)
roc_intro <- roccer("@_intro",
parse_after = c("title", "description", "details")
parse_roc = function(roc, obj) {
if (!is.null(roc$`_intro`)) {
paragraphs <- str_trim(str_split(intro, fixed('\n\n'))[[1]])
} else {
paragraphs <- NULL
}
# 1st paragraph = title (unless has @title)
if (!is.null(roc$title)) {
title <- roc$title
} else if (length(paragraphs) > 0) {
title <- paragraphs[1]
paragraphs <- paragraphs[-1]
} else {
title <- NULL
}
# 2nd paragraph = description (unless has @description)
if (!is.null(roc$description)) {
description <- roc$description
} else if (length(paragraphs) > 0) {
description <- paragraphs[1]
paragraphs <- paragraphs[-1]
} else {
# Description is required, so if missing description, repeat title.
description <- title
}
# Every thing else = details, combined with @details.
details <- c(paragraphs, roc$details)
if (length(details) > 0) {
details <- paste(details, collapse = "\n\n")
} else {
details <- NULL
}
list(
`_intro` = NULL,
title = title,
description = description,
details = details
)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment