How to generate YAML front matter block automatically
Is there any way to generate YAML front matter block of each markdown files automatically?
The answer is yes
! And there is more than one way to do it.
I want to introduce two ways, one is using rake
tool, the other is using bash
.
Rake tool
To generate YAML front matter blockk in markdown file, we need a program that automatically write a markdown file with YAML front matter block.
Here is the program tool “Rake”.
Rake is a Make-like program implemented in Ruby. Tasks and dependencies are specified in standard Ruby syntax.
Maybe you have some questions:
What is Make-like program ?
In software development, Make is a utility that automatically builds executable programs and libraries from source code by reading files called makefiles which specify how to derive the target program.
Makefiles are special format files that together with the make utility will help you to automagically build and manage your projects.
Do we need to write a makefile for Rake?
Of course. We need write a “Rakefile” for Rake utility.
Rakefiles (rake’s version of Makefiles) are completely defined in standard Ruby syntax. No XML files to edit. No quirky Makefile syntax to worry about (is that a tab or a space?)
Using Rake to generate YAML front matter block, step by step:
1.install Rake
gem install rake
Here is the simple usage of rake.
2.write Rakefile
Here is the Rakefile Format.
Tl;dr: Just see below.
require 'time'
# Usage: rake post title="A Title" date="2014-04-14" ## date is alternatively part
desc "Create a new post"
task :post do
unless FileTest.directory?('./_posts')
abort("rake aborted: '_posts' directory not found.")
end
title = ENV["title"] || "new-post"
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
begin
date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now)
.strftime('%Y-%m-%d')
rescue Exception => e
puts "Error: date format must be YYYY-MM-DD!"
exit -1
end
filename = File.join('.', '_posts', "#{date}-#{slug}.md")
if File.exist?(filename)
abort("rake aborted: #{filename} already exists.")
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "published: true"
post.puts "layout: post"
post.puts "title: #{title.gsub(/-/,' ')}"
post.puts "author: Yu" # change to your name
post.puts "category:"
post.puts "tags:"
post.puts "---"
end
end
You can save those code in a Rakefile, and move the Rakefile to a jekyll website folder, for example yulijia.github.io/
. When build a new post, we only need type rake post title="Hello, World"
on terminal in the jekyll website folder.
BASH script
If you do not like rake tool, you can use bash to generate YAML front matter block.
Here is the script:
#!/bin/bash
read -p "Enter the title: " title
Title=`echo $title | tr -d '[:punct:]'`
for word in $Title
do
dashedTitle=${dashedTitle}-${word}
done
filename="`date +%Y-%m-%d`${dashedTitle}.md"
touch $filename
echo "---" >> $filename
echo "published: true" >> $filename
echo "layout: post" >> $filename
echo "title: \"${title}\"" >> $filename
echo "author: Yu" >> $filename
echo "categories:" >> $filename
echo "tags:" >> $filename
echo "-" >> $filename
echo "---" >> $filename
echo "" >> $filename
I made a Jekyll theme
This article is definitely a advertisement :p
Last year, I made two Jekyll themes, one is Freshman, the other is Freshman21. Freshman is a simple html/css theme that I made to learn Jekyll and css. Freshman21 looks like WordPress theme. A tribute to Twenty Twelve and Twenty eleven. I send a pull request to jekyllthemes.org, thanks to Matt Harzewski, my theme is already updated to the website in January.
If you are a fan of WordPress theme and try using Jekyll to build your own blog, may the Freshman21 be with you.
Freshman21 has two version: one-colunm and two-columns.
Welcome to submit new request to the theme at github.
Today's recommended article
Today, I googled “A paper a day” to find if there any slogan about reading papers. Then I found this article A paper a day keeps the doctor away – advice for surviving in academia.
The author give us some useful advice for surviving in academia.
- There is no single road to the Ivory Tower.
- Find your balance.
- Try not to be an ass.
- Don’t be bitter.
- Keep up with the literature, “A paper a day”.
Live long and prosper, folks.
I am back
Winter is coming, I am back!
Poor drawing skill, I almost forgot how to use PhotoShop.
How to get RefSeq gene annotations from UCSC database?
I googled the question, the best answer is on this page
You can do this by using our Table Browser. If you’re unfamiliar with the Table Browser, please see the User’s Guide at http://genome.ucsc.edu/goldenPath/help/hgTablesHelp.html.
- From http://genome.ucsc.edu, select “Tables” from the blue navigation bar at the top of the screen.
- Select the following options:
Clade: Mammal
Genome: Human
Assembly: Feb. 2009 (GRCh37/hg19)
Group: Genes and Gene Prediction Tracks
Track: RefSeq Genes
Table: refGene- If you have a list of RefSeq IDs that you want annotated, on the “identifiers” line, click the “paste list” or “upload list” button to use your list of identifiers. If you want the annotation for all genes in a specific region, on the “region” line, you can click “genome” for the entire genome, “position” to define a single specific region or “define regions” to define multiple regions.
- Set “output” to “all fields from selected table” to list all fields in your output or to “selected fields from primary or related tables” to select only certain fields in your output
- Click the “get output” button (if you selected “selected fields from primary or related tables” in step 4, the next screen will allow you to select the fields you want in your output)