Skip to content

Instantly share code, notes, and snippets.

@kliph
Last active April 6, 2018 15:14
Show Gist options
  • Save kliph/c376c66d0a4aa1dd1eba75bc5dca2882 to your computer and use it in GitHub Desktop.
Save kliph/c376c66d0a4aa1dd1eba75bc5dca2882 to your computer and use it in GitHub Desktop.
Analyze tags in blogs from Jekyll-style front-matters
require 'front_matter_parser'
require 'csv'
# Usage: ParseFrontMatterData.new.save_csv
# Will create `data.csv` in the current working directory
class ParseFrontMatterData
attr_accessor :blog_files, :front_matters
def initialize
@blog_files = get_blog_files
@front_matters = parse
end
def get_blog_files
Dir.glob('source/blog/**/*.html.markdown.erb')
end
def keys_to_select
%w[date title headline author tags]
end
def parse
@blog_files.map do |post|
md = File.read post
parsed = FrontMatterParser::Parser.new(:md).call(md)
parsed.front_matter.select { |k, _| keys_to_select.include? k }
end
end
def save_csv
CSV.open('data.csv', 'wb') do |csv|
csv << @front_matters.first.keys
@front_matters.map do |h|
csv << h.values
end
end
end
end
ParseFrontMatterData.new.save_csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment