Skip to content

Instantly share code, notes, and snippets.

@thexavismith
Forked from codykrieger/newpost.rb
Last active December 20, 2015 22:39
Show Gist options
  • Save thexavismith/6206907 to your computer and use it in GitHub Desktop.
Save thexavismith/6206907 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# *********************************************
# Jekyll Post Generator Awesomeness
# by Cody Krieger (http://codykrieger.com)
# *********************************************
# *********************************************
# Modified by Tim Smith (http://ttimsmith.com)
# Originally created as a command line tool but
# modified to work as a script with Alfred.
# *********************************************
class String
# from ruby on rails (https://github.com/rails/rails)
# activesupport/lib/active_support/inflector/transliterate.rb
def parameterize(sep = '-')
# replace accented chars with their ascii equivalents
parameterized_string = self.dup
# Turn unwanted chars into the separator
parameterized_string.gsub!(/[^a-z0-9\-_]+/i, sep)
unless sep.nil? || sep.empty?
re_sep = Regexp.escape(sep)
# No more than one of the separator in a row.
parameterized_string.gsub!(/#{re_sep}{2,}/, sep)
# Remove leading/trailing separator.
parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/i, '')
end
parameterized_string.downcase
end
end
TEMPLATE = "/post-template.md"
POSTS_DIR = "/_drafts"
# Get the title and use it to derive the new filename
title = "{query}"
filename = "#{title.parameterize}.md"
filepath = File.join(POSTS_DIR, filename)
# Load in the template and set the title
post_text = File.read(TEMPLATE)
post_text.gsub!('%%TITLE%%', title)
# Write out the post
post_file = File.open(filepath, 'w')
post_file.puts post_text
post_file.close
# Changes to your Drafts directory (put full path)
Dir.chdir "/_drafts"
system(%[open "#{filename}"])
Error in user YAML: (<unknown>): found character that cannot start any token while scanning for the next token at line 2 column 8
--- 
layout: post
title: %%TITLE%%
custom_type: post
---

Hello, Jekyll!

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