|
server <- function(input, output, session) {
|
|
|
|
originalFileContent <- reactiveVal()
|
|
|
|
|
|
observeEvent(input$file1, {
|
|
req(input$file1)
|
|
|
|
fileContent <- readLines(input$file1$datapath)
|
|
|
|
originalFileContent(fileContent)
|
|
|
|
updateAceEditor(session, "code", value = paste(fileContent, collapse = "\n"), theme = "monokai")
|
|
})
|
|
|
|
|
|
generate_yaml <- function() {
|
|
yaml_header <- c(
|
|
"---",
|
|
paste0("author: ", input$author),
|
|
paste0("date: ", if(input$date_type == 'custom') {input$date} else {'"`r Sys.Date()`"'}),
|
|
paste0("description: ", input$description),
|
|
"format:",
|
|
" html:",
|
|
paste0(" highlight-style: ", input$highlight_style),
|
|
paste0(" theme: ", input$theme),
|
|
paste0(" toc: ", ifelse(input$toc, "true", "false")),
|
|
" code-fold: true",
|
|
paste0(" file: ", tools::file_path_sans_ext(input$file1$name), ".", tools::file_ext(input$file1$name)),
|
|
paste0(" title: ", input$title),
|
|
paste0(' subtitle: "', input$subtitle, '"')
|
|
)
|
|
|
|
if (input$`toc-float`) {
|
|
yaml_header <- c(yaml_header,
|
|
" toc_float:",
|
|
" collapsed: true",
|
|
" smooth_scroll: true")
|
|
}
|
|
|
|
yaml_header <- c(yaml_header, "---")
|
|
|
|
return(yaml_header)
|
|
}
|
|
|
|
|
|
|
|
observeEvent(input$generate, {
|
|
|
|
yaml_header <- generate_yaml()
|
|
|
|
new_fileContent <- c(yaml_header, originalFileContent())
|
|
|
|
updateAceEditor(session, "code", value = paste(new_fileContent, collapse = "\n"))
|
|
})
|
|
|
|
|
|
output$Download <- downloadHandler(
|
|
|
|
filename = function() {
|
|
paste(tools::file_path_sans_ext(input$file1$name), ".", tools::file_ext(input$file1$name), sep = "")
|
|
},
|
|
content = function(file) {
|
|
|
|
yaml_header <- generate_yaml()
|
|
|
|
new_fileContent <- c(yaml_header, originalFileContent())
|
|
|
|
writeLines(new_fileContent, file)
|
|
}
|
|
)
|
|
} |