blogdown创建post模板文件夹

blogdown好像并没有自动创建post模板文件夹的函数,要达到的目的是,自动在content/en/post中创建文件夹,然后文件夹中有一个index.Rmd文件.代码如下:

new_post <- 
  function(
  post.name = "python for genomics class day03",
  date = Sys.Date(),
  post.title = "Python for genomics class day03",
  author = "Xiaotao Shen",
  categories = c("Python"),
  tags = c("Blog", "Chinese"),
  path = "D:/my github/shen/content/en/post"){
  post.name <- stringr::str_replace_all(post.name, " ", "-")
  file.name <- paste(date, post.name, sep = "-")
 
  dir.create(file.path(path, file.name))
  
  file <- "---"
  write.table(file, 
              file.path(path, file.name, "index.txt"), 
              row.names = FALSE, col.names = FALSE, 
              quote = FALSE, append = FALSE)
  
  write.table(paste("title:", post.title),
    file.path(path, file.name, "index.txt"), 
  row.names = FALSE, col.names = FALSE, 
  quote = FALSE, append = TRUE
  )
  
  write.table(paste("author:", author),
              file.path(path, file.name, "index.txt"), 
              row.names = FALSE, col.names = FALSE, 
              quote = FALSE, append = TRUE
  )
  
  write.table(paste("date:", date),
              file.path(path, file.name, "index.txt"), 
              row.names = FALSE, col.names = FALSE, 
              quote = FALSE, append = TRUE
  )
  
  write.table("slug: ''",
              file.path(path, file.name, "index.txt"), 
              row.names = FALSE, col.names = FALSE, 
              quote = FALSE, append = TRUE
  )
  
  write.table("categories:",
              file.path(path, file.name, "index.txt"), 
              row.names = FALSE, col.names = FALSE, 
              quote = FALSE, append = TRUE
  )
  
  for(i in 1:length(categories)){
    write.table(paste(" - ", categories[i]),
                file.path(path, file.name, "index.txt"), 
                row.names = FALSE, col.names = FALSE, 
                quote = FALSE, append = TRUE
    )
  }
    
    write.table("tags:",
                file.path(path, file.name, "index.txt"), 
                row.names = FALSE, col.names = FALSE, 
                quote = FALSE, append = TRUE
    )
    
    for(i in 1:length(tags)){
      write.table(paste(" - ", tags[i]),
                  file.path(path, file.name, "index.txt"), 
                  row.names = FALSE, col.names = FALSE, 
                  quote = FALSE, append = TRUE
      )
    }
      
      end <- 
"image:
  caption: ''
  focal_point: ''
output:
  blogdown::html_page:
    toc: true
    number_sections: true
---"
      write.table(
        end,
        file.path(path, file.name, "index.txt"),
        row.names = FALSE,
        col.names = FALSE,
        quote = FALSE,
        append = TRUE
      )
      
 file.rename(from = file.path(path, file.name, "index.txt"),
             to = file.path(path, file.name, "index.Rmd"))
 
  }

相关

下一页
上一页
comments powered by Disqus