Skip to content

Commit bec750e

Browse files
committed
initial version of new_blog_post
1 parent ba8485a commit bec750e

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

DESCRIPTION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Depends:
1818
R (>= 3.6)
1919
Imports:
2020
cli,
21+
fs,
2122
jsonlite,
2223
later,
2324
processx,
@@ -26,6 +27,7 @@ Imports:
2627
rstudioapi,
2728
tools,
2829
utils,
30+
whoami,
2931
yaml
3032
Suggests:
3133
curl,

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(is_using_quarto)
4+
export(new_blog_post)
45
export(quarto_add_extension)
56
export(quarto_binary_sitrep)
67
export(quarto_create_project)

R/new-blog-post.R

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

man/new_blog_post.Rd

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)