|
| 1 | +#' Create a new blog post |
| 2 | +#' |
| 3 | +#' @param title A character string for the title of the post. It is converted |
| 4 | +#' to title case via [tools::toTitleCase()]. |
| 5 | +#' @param dest A character string (or NULL) for the path within `posts`. By |
| 6 | +#' default, the title is adapted as the directory name. |
| 7 | +#' @param open A logical: have the default editor open a window to edit the |
| 8 | +#' `index.qmd` file? |
| 9 | +#' @param call A call object for reporting errors. |
| 10 | +#' @param ... A named list of values to be added to the yaml header, such as |
| 11 | +#' `description`, `author`, `categories`, etc. |
| 12 | +#' @return The path to the index file. |
| 13 | +#' @export |
| 14 | +#' @examples |
| 15 | +#' \dontrun{ |
| 16 | +#' \donttest{ |
| 17 | +#' new_blog_post("making quarto blog posts", categories = c("R")) |
| 18 | +#' |
| 19 | +#' } |
| 20 | +#' } |
| 21 | +#' |
| 22 | +new_blog_post <- function(title, dest = NULL, open = rlang::is_interactive(), |
| 23 | + call = rlang::current_env(), ...) { |
| 24 | + |
| 25 | + if (is.null(dest)) { |
| 26 | + # Scrub title to make directory name |
| 27 | + dest <- gsub("[[:space:]]", "-", tolower(title)) |
| 28 | + } |
| 29 | + dest_path <- make_post_dir(dest, call) |
| 30 | + post_yaml <- make_post_yaml(title, ...) |
| 31 | + qmd_path <- write_post_yaml(post_yaml, dest_path, call) |
| 32 | + if (open) { |
| 33 | + file.edit(qmd_path) |
| 34 | + } |
| 35 | + invisible(qmd_path) |
| 36 | +} |
| 37 | + |
| 38 | +make_post_dir <- function(dest, call) { |
| 39 | + working <- fs::path_wd() |
| 40 | + |
| 41 | + post_path <- fs::path(working, "posts", dest) |
| 42 | + |
| 43 | + if (fs::dir_exists(post_path)) { |
| 44 | + cli::cli_abort("There is already a {.code {path}} directory in 'posts/'", |
| 45 | + call = call) |
| 46 | + } else { |
| 47 | + ret <- fs::dir_create(post_path) |
| 48 | + } |
| 49 | + ret |
| 50 | +} |
| 51 | + |
| 52 | +make_post_yaml <- function(title, ...) { |
| 53 | + default_values <- list( |
| 54 | + title = tools::toTitleCase(title), |
| 55 | + author = tools::toTitleCase(whoami::fullname("Your name")), |
| 56 | + date = format(Sys.Date(), "%Y-%m-%d"), |
| 57 | + categories = character(0) |
| 58 | + ) |
| 59 | + |
| 60 | + user_values <- list(...) |
| 61 | + |
| 62 | + yml_values <- utils::modifyList(default_values, user_values) |
| 63 | + if (length(yml_values$categories) == 0) { |
| 64 | + yml_values <- yml_values[names(yml_values) != "categories"] |
| 65 | + } |
| 66 | + yml_values <- yaml::as.yaml(yml_values) |
| 67 | + yml_values <- paste0("---\n", yml_values, "---\n") |
| 68 | + yml_values |
| 69 | +} |
| 70 | + |
| 71 | +write_post_yaml <- function(x, dest, call) { |
| 72 | + dest_file <- fs::path(dest, "index.qmd") |
| 73 | + if (fs::file_exists(dest_file)) { |
| 74 | + cli::cli_abort("There is already am index.qmd file at {.code {path}}", |
| 75 | + call = call) |
| 76 | + } else { |
| 77 | + ret <- cat(x, file = dest_file) |
| 78 | + } |
| 79 | + dest_file |
| 80 | +} |
0 commit comments