Skip to content

Instantly share code, notes, and snippets.

@gilesbowkett
Created March 23, 2015 22:27
Show Gist options
  • Select an option

  • Save gilesbowkett/ea886e8040b50aa4896a to your computer and use it in GitHub Desktop.

Select an option

Save gilesbowkett/ea886e8040b50aa4896a to your computer and use it in GitHub Desktop.
# Create a skeleton blog post, with metadata.
#
# Example: coffee scripts/generate-blog-post.coffee -t "Hello World" -s "hello-world" -d 20150415 -a "Giles Bowkett"'
#
# Result: updates public/posts/_data.json and creates public/posts/20150415-hello-world.md
lineReader = require "line-reader"
fs = require "fs"
cliArgs = require "command-line-args"
cli = cliArgs([
{
name: "help",
type: Boolean,
description: "Print these instructions",
alias: "h"
},
{
name: "title",
type: String,
description: "Title of your blog post",
alias: "t"
},
{
name: "author",
type: String,
description: "Your name",
alias: "a"
},
{
name: "slug",
type: String,
description: "URL slug for your blog post",
alias: "s"
},
{
name: "date",
type: String,
description: "Date of publication. YYYYMMDD format, e.g., 20150307",
alias: "d"
},
])
usage = cli.getUsage({
header: "Generate a skeleton blog post. Required: title, author, slug, and date."
})
makeFilename = (options) ->
options.filename = options.date + "-" + options.slug.replace(/ /, "-")
createFile = (options) ->
template = """
introduce your idea here
<!-- more -->
get into it here
"""
fs.writeFile "public/posts/#{options.filename}.md", template
humanizeDate = (options) ->
matched = options.date.match(/(\d{4})(\d{2})(\d{2})/)
year = matched[1]
month = matched[2]
day = matched[3]
monthNames = {
"01": "January",
"02": "February",
"03": "March",
"04": "April",
"05": "May",
"06": "June",
"07": "July",
"08": "August",
"09": "September",
"10": "October",
"11": "November",
"12": "December"
}
options.date = "#{monthNames[month]} #{day}, #{year}"
addMetaData = (options) ->
template = """
{
"#{options.filename}": {
"title": "#{options.title}",
"author": "#{options.author}",
"published": "#{options.date}"
},
"""
postMetaData = template
primitiveCavemanShit = 0
lineReader.eachLine("public/posts/_data.json", (line, last) ->
if primitiveCavemanShit > 0
postMetaData += (line + "\n")
if last
fs.writeFile "public/posts/_data.json", postMetaData
primitiveCavemanShit += 1)
options = cli.parse()
if options.help
console.log usage
else if options.title and options.author and options.slug and options.date
# FIXME: this is kind of begging for an OO rewrite at this point
makeFilename options
createFile options
humanizeDate options
addMetaData options
else
console.log usage
@gilesbowkett

Copy link
Copy Markdown
Author

This code is released under the MIT Public License. Use it all you want. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment