Last active
August 29, 2015 13:56
-
-
Save philou/8929876 to your computer and use it in GitHub Desktop.
Add keywords to your existing octopress posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desc "add keywords to posts" | |
task :add_keywords do | |
Dir.glob(File.join(File.dirname(__FILE__),"source/_posts/*.markdown")) do |file| | |
yaml = YAML.load_file(file) | |
keywords = yaml['categories'] || [] | |
title = yaml['title'] | |
keywords += title.scan(/'.*'/).map {|kw| kw.gsub("'",'')} | |
keywords += title.scan(/\S+[\sv~>]+?\d+\.\d+\.?\d*/) | |
title = title.gsub(/[\sv~>]+?\d+\.\d+\.?\d*/,'') | |
title = title.gsub(/['\?:\d\.]/,'') | |
keywords += title.split() | |
keywords_hash = {} | |
keywords.each do |keyword| | |
keywords_hash[keyword.downcase] = keyword | |
end | |
%w(# a an i on of to in my from how for the just best own after full if ~> and with when do does not are et up way first set like be there only can one per).each do |word| | |
keywords_hash.delete(word) | |
end | |
system "sed -i '1 a\ | |
keywords: \"#{keywords_hash.values.join(', ')}\" | |
' #{file}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment