When using octopress, if I want to preview an unpublished item(indicated in the yaml front matter), I have to manually modify the line from “published: false” to “published: true” first, then preview it. What I want is to automatize it.
The idea is direct: when running the “generate” Rake task, passing an command line argument which tells if I want to preview the unpublished items:
If yes, it will search all the unpublished items, record their file names(for later recovering) in a text file(“.unpublished”), and replace the “published: false” “yaml front matter” line with “true” value in it to let them can be previewed.
If no, but the unpublished file names list file exists, that means the current status is the unpublished items should be recovered to the unpublished status, replace the “published: true” line to “false” whose file name was recorded in the “.unpublished” file, then remove the “.unpublished” file.
In order to achieve this, I have to:
Read the Rakefile format document and know how to add arguments to a rake task;
Search how to use “sed” to replace the “yaml front matter” line content.
What the final “Rakefile” task looks like:
desc "Generate jekyll site"
- About the “sed” command, it’s important to give a lines range in where the replacement happens because wrong replacement would happen out of the “yaml front matter” lines. I refered here to get the answer.