Skip to content

Instantly share code, notes, and snippets.

@philou
Last active August 29, 2015 13:56
Show Gist options
  • Save philou/8929876 to your computer and use it in GitHub Desktop.
Save philou/8929876 to your computer and use it in GitHub Desktop.
Add keywords to your existing octopress posts
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