Add rake tasks for new_post and new_page

This commit is contained in:
Michael Rose 2014-02-12 09:17:56 -05:00
parent d6938cef66
commit 23b099057a
4 changed files with 118 additions and 20 deletions

View file

@ -5,4 +5,5 @@ gem 'kramdown'
gem 'coderay' gem 'coderay'
gem 'rake' gem 'rake'
gem 'thor' gem 'thor'
gem 'activesupport' gem 'activesupport'
gem 'stringex'

View file

@ -25,17 +25,15 @@ General notes and suggestions for customizing So Simple Theme.
## Basic Setup for new Jekyll site ## Basic Setup for new Jekyll site
1. [Install Jekyll](http://jekyllrb.com) and read through the documentation if you haven't already. 1. [Install Bundler](http://bundler.io) `gem install bundler` and then install [Jekyll](http://jekyllrb.com) and all dependencies `bundle install`.
2. [Install Jekyll Extra - Kramdown](http://jekyllrb.com/docs/extras/#kramdown) 2. Fork the [So Simple Theme repo](https://github.com/mmistakes/so-simple-theme/fork).
3. Fork the [So Simple Theme repo](https://github.com/mmistakes/so-simple-theme/fork) 3. Clone the repo you just forked and rename it.
4. Clone the repo you just forked. 4. Edit `_config.yml` to personalize your site.
5. Edit `_config.yml` to personalize your site. 5. Check out the sample posts in `_posts` to see examples for pulling in large feature images, assigning categories and tags, and other YAML data.
6. Check out the sample posts in `_posts` to see examples for pulling in large feature images, assigning categories and tags, and other YAML data. 6. Read the documentation below for further customization pointers and documentation.
7. Read the documentation below for further customization pointers and documentation. [Download the Theme](https://github.com/mmistakes/so-simple-theme/archive/master.zip)
[Download the Theme](http://mmistakes.github.io/so-simple-theme) **Pro-tip:** Remove the sample posts in `_posts` and the `gh-pages` branch after cloning. There is a bunch of garbage in the `gh-pages` branch used for the theme's demo site.
**Pro-tip:** Delete the `gh-pages` branch after cloning and start fresh by branching off `master`. There is a bunch of garbage in `gh-pages` used for the theme's demo site that I'm guessing you don't want on your site.
--- ---

88
Rakefile.rb Normal file
View file

@ -0,0 +1,88 @@
require "rubygems"
require "bundler/setup"
require "stringex"
## -- Config -- ##
posts_dir = "_posts" # directory for blog files
new_post_ext = "md" # default new post file extension when using the new_post task
new_page_ext = "md" # default new page file extension when using the new_page task
#############################
# Create a new Post or Page #
#############################
# usage rake new_post
desc "Create a new post in #{posts_dir}"
task :new_post, :title do |t, args|
if args.title
title = args.title
else
title = get_stdin("Enter a title for your post: ")
end
filename = "#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.#{new_post_ext}"
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
tags = get_stdin("Enter tags to classify your post (comma separated): ")
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/&/,'&')}\""
post.puts "modified: #{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}"
post.puts "tags: [#{tags}]"
post.puts "image:"
post.puts " feature: "
post.puts " credit: "
post.puts " creditlink: "
post.puts "comments: "
post.puts "share: "
post.puts "---"
end
end
# usage rake new_page
desc "Create a new page"
task :new_page, :title do |t, args|
if args.title
title = args.title
else
title = get_stdin("Enter a title for your page: ")
end
filename = "#{title.to_url}.#{new_page_ext}"
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
tags = get_stdin("Enter tags to classify your page (comma separated): ")
puts "Creating new page: #{filename}"
open(filename, 'w') do |page|
page.puts "---"
page.puts "layout: page"
page.puts "permalink: /#{title.to_url}/"
page.puts "title: \"#{title}\""
page.puts "modified: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
page.puts "tags: [#{tags}]"
page.puts "image:"
page.puts " feature: "
page.puts " credit: "
page.puts " creditlink: "
page.puts "share: "
page.puts "---"
end
end
def get_stdin(message)
print message
STDIN.gets.chomp
end
def ask(message, valid_options)
if valid_options
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
else
answer = get_stdin(message)
end
answer
end

View file

@ -14,15 +14,14 @@ General notes and suggestions for customizing **So Simple Theme**.
## Basic Setup for a new Jekyll site ## Basic Setup for a new Jekyll site
1. [Install Jekyll](http://jekyllrb.com) and read through the documentation if you haven't already. 1. [Install Bundler](http://bundler.io) `gem install bundler` and then install [Jekyll](http://jekyllrb.com) and all dependencies `bundle install`.
2. [Install Jekyll Extra - Kramdown](http://jekyllrb.com/docs/extras/#kramdown) 2. Fork the [So Simple Theme repo](https://github.com/mmistakes/so-simple-theme/fork).
3. Fork the [So Simple Theme repo](https://github.com/mmistakes/so-simple-theme/fork) 3. Clone the repo you just forked and rename it.
4. Clone the repo you just forked. 4. Edit `_config.yml` to personalize your site.
5. Edit `_config.yml` to personalize your site. 5. Check out the sample posts in `_posts` to see examples for pulling in large feature images, assigning categories and tags, and other YAML data.
6. Check out the sample posts in `_posts` to see examples for pulling in large feature images, assigning categories and tags, and other YAML data. 6. Read the documentation below for further customization pointers and documentation.
7. Read the documentation below for further customization pointers and documentation.
<div markdown="0"><a href="https://github.com/mmistakes/so-simple-theme" class="btn">Download the Theme</a></div> <div markdown="0"><a href="https://github.com/mmistakes/so-simple-theme/archive/master.zip" class="btn">Download the Theme</a></div>
**Pro-tip:** Delete the `gh-pages` branch after cloning and start fresh by branching off `master`. There is a bunch of garbage in `gh-pages` used for the theme's demo site that I'm guessing you don't want on your site. **Pro-tip:** Delete the `gh-pages` branch after cloning and start fresh by branching off `master`. There is a bunch of garbage in `gh-pages` used for the theme's demo site that I'm guessing you don't want on your site.
{: .notice} {: .notice}
@ -186,7 +185,19 @@ For the most part you can leave these as is since the author/owner details are p
### Adding Posts and Pages ### Adding Posts and Pages
There are two main content layouts: `post.html` (for posts) and `page.html` (for pages). Both have support for large **feature images** that span the full-width of the screen, and both are meant for text heavy blog posts (or articles). There are two main content layouts: `post.html` (for posts) and `page.html` (for pages). Both have support for large **feature images** that span the full-width of the screen, and both are meant for text heavy blog posts (or articles).
There are two rake tasks that can be used to create a new post or page with all YAML Front Matter. Using either `rake new_post` or `rake new_page` will prompt you for a title and tags to classify them. Example below:
{% highlight bash %}
rake new_post
Enter a title for your post: My Awesome Post
Enter tags to classify your post (comma separated): web development, code
Creating new post: _posts/2014-02-10-my-awesome-post.md
{% endhighlight %}
There are a few configuration variables that can be changed in `Rakefile.rb`. By default posts and pages will be created in MarkDown using the `.md` extension.
#### Feature Images #### Feature Images