1
+ # ' Create a post from the template
2
+ # '
3
+ # ' @details create a R Markdown file and open it.
4
+ # '
5
+ # ' @param title Title for the post
6
+ # ' @param slug Short informative slug with words separated by "-"
7
+ # ' @param category Either "tutorials", "practicals" or "case studies"
8
+ # ' @param author Author as a vector
9
+ # ' @param date Date for the post
10
+ # ' @param licence_name Name of the license
11
+ # ' @param licence_url URL to the license
12
+ # '
13
+ # ' @return Used only for side-effects.
14
+ # ' @export
15
+ # '
16
+ create_post <- function (title = " A study of Zika and Cholera" ,
17
+ slug = NULL ,
18
+ category = c(" tutorials" , " practicals" , " case studies" ),
19
+ author = c(" Celina Turchi" , " John Snow" ),
20
+ date = Sys.Date(),
21
+ licence_name = " CC-BY" ,
22
+ licence_url = " https://creativecommons.org/licenses/by/3.0/" ){
23
+ if (is.null(slug )){
24
+ slug <- snakecase :: to_any_case(title , case = " snake" ,
25
+ sep_out = " -" , sep_in = " -" )
26
+ }
27
+
28
+ singular_category <- switch (category [[1 ]],
29
+ " practicals" = " practical" ,
30
+ " tutorials" = " tutorial" ,
31
+ " case studies" = " study" )
32
+
33
+ post_path <- file.path(" content" , " post" ,
34
+ glue :: glue(" {singular_category}-{slug}.Rmd" ))
35
+
36
+ file.copy(system.file(" rmarkdown" , " templates" ,
37
+ " skeleton" ,
38
+ " skeleton.Rmd" , package = " learn" ),
39
+ post_path ,
40
+ overwrite = TRUE )
41
+
42
+ file.copy(system.file(" rmarkdown" , " templates" ,
43
+ " resources" ,
44
+ " practical-phylogenetics.bib" , package = " learn" ),
45
+ file.path(" content" , " post" , paste0(slug , " .bib" )),
46
+ overwrite = TRUE )
47
+
48
+ file.copy(system.file(" rmarkdown" , " templates" ,
49
+ " resources" ,
50
+ " placeholder.jpg" , package = " learn" ),
51
+ file.path(" static" ," img" , " highres" , paste0(slug , " .jpg" )),
52
+ overwrite = TRUE )
53
+
54
+ yaml <- rmarkdown :: yaml_front_matter(post_path , encoding = " UTF-8" )
55
+
56
+ yaml $ title <- title
57
+ yaml $ author <- toString(author )
58
+ yaml $ authors <- jsonlite :: toJSON(author )
59
+ yaml $ topics <- jsonlite :: toJSON(yaml $ topics )
60
+ yaml $ categories <- jsonlite :: toJSON(category )
61
+ yaml $ date <- as.character(date )
62
+ yaml $ bibliography <- paste0(slug , " .bib" )
63
+ yaml $ image <- glue :: glue(" img/highres/{slug}.jpg" )
64
+ yaml $ licenses <- licence_name
65
+
66
+ file_content <- readLines(post_path )
67
+ i = grep(' ^---\\ s*$' , file_content )
68
+ file_content <- file_content [(i [2 ]+ 1 ): length(file_content )]
69
+
70
+ newyaml <- yaml :: as.yaml(yaml )
71
+ newyaml <- stringr :: str_remove_all(newyaml , " '" )
72
+
73
+ file_content [grepl(" \\ - Zulma Cucunuba \\ & Pierre Nouvellet\\ : initial version" ,
74
+ file_content )] <-
75
+ glue :: glue(" - {toString(author)}: initial version" )
76
+
77
+ file_content [grepl(" \\ [CC-BY\\ ]\\ (htt" , file_content )] <-
78
+ glue :: glue(" **License**: [{licence_name}]({licence_url})" )
79
+
80
+ file_content [grepl(" Zulma Cucunuba \\ & Pierre Nouvellet, 2017" ,
81
+ file_content )] <-
82
+ glue :: glue(" **Copyright**: {toString(author)}, {format(date, '%Y')}" )
83
+
84
+
85
+ file_content <- c(c(" ---" ),
86
+ newyaml ,
87
+ c(" ---" ),
88
+ file_content )
89
+ print(post_path )
90
+ writeLines(file_content , post_path ,
91
+ useBytes = TRUE )
92
+ usethis :: edit_file(post_path )
93
+
94
+ }
0 commit comments