First, I will layout the entire Rakefile for those whom prefer to copy and paste the code into their solution. Then, I’ll explain each section to those, like myself, whom have no idea what half of this code means.
How the Rakefile works
Each section is broken down into smaller code blocks. If at any point you get lost, refer to the completed code block at the top.
Require the gems needed in the Rakefile.
Side Note: You don’t need the
colorize gem for this to work. Colorize is a personal preference in order to customize my terminal. The colors help for better readability. If you choose to ignore it you may remove
.colorize(:color) every where in the document.
Namespace if you’d like
Using the namespace keeps our writing tasks better organization.
The two tasks included are
:publish. I’ve added descriptions to help explain our tasks in the task list. To view the list type the following in your terminal:
Below is the code for the namespace and tasks:
First, we need a title of the article when calling the task
$ rake draft:new in the terminal. The snippet will name the file and add the title to the front matter.
STDIN.gets.chomp will capture what you type in the terminal preceding the question.
The typed title needs to be converted into a readable string. Then replaces all special characters. Following the special characters is lowercasing everything, then replacing all spaces with a dash. This will now name the document properly for Jekyll.
fileutils gem plays a part in creating a file in the
_drafts folder. Create this folder before executing the Rakefile.
Write in Document
The following script will open the file that was just created and right the front matter for you with all of the following information. Feel free to edit this to fit your needs.
Open in Application
Finally, open the new document in your favorite writing application. I’ve chosen iA Writer.
Publishing an article from the drafts folder has its own task for specific reasons. For obvious reasons the publish date should be the date the article is published.
The terminal wil write out “List of drafts” followed by the obvious loop through all the draft documents in your
Following listing of the drafts will be a prompt asking what draft you wish to publish. The
@post_name variable will capture your text.
Move & Rename Draft
Added a new variable to display the publish date in the post name for Jekyll. Move the
@post_name chosen to the
_posts folder. Once again move within the same folder but prepend the
@post_date to the document description.
Add Publish Date to Front Matter
draft:new task you may notice we simply added
publish_date to the front matter. The next part will open the published file, look for the
publish_date text and replace it with the actual publish date.
Finally the terminal responds with the post copied and ready to deploy your new article.