Skip to content

Instantly share code, notes, and snippets.

@cbeyls
Created April 21, 2015 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbeyls/1faa90030aabf2659de7 to your computer and use it in GitHub Desktop.
Save cbeyls/1faa90030aabf2659de7 to your computer and use it in GitHub Desktop.
TextPattern import plugin for Jekyll that uses a YAML dump file of the "Textpattern" table from PHPMyAdmin instead of a local database. It also imports categories in addition to tags.
module JekyllImport
module Importers
class TextPattern < Importer
def self.require_deps
JekyllImport.require_with_fallback(%w[
rubygems
fileutils
safe_yaml
])
end
def self.specify_options(c)
c.option 'filename', '--filename FILE', 'YAML database export file name'
end
def self.process(options)
filename = options.fetch('filename')
db = YAML.load_file(filename)
FileUtils.mkdir_p "_posts"
db.each do |post|
# The only posts selected are those with a status of 4 or 5, which means
# "live" and "sticky" respectively.
# Other statuses are 1 => draft, 2 => hidden and 3 => pending.
next if post['Status'] < 4
# Get required fields and construct Jekyll compatible name.
title = post['Title']
slug = post['url_title']
date = DateTime.parse(post['Posted']).to_time
content = post['Body']
name = [date.strftime("%Y-%m-%d"), slug].join('-') + ".textile"
# Get the relevant fields as a hash, delete empty fields and convert
# to YAML for the header.
data = {
'layout' => 'post',
'title' => title.to_s,
'categories' => post['Category1'],
'tags' => post['Keywords'].split(','),
'date' => date
}.delete_if { |k,v| v.nil? || v == '' || v == [] }.to_yaml
# Write out the data and content to file.
File.open("_posts/#{name}", "w") do |f|
f.puts data
f.puts "---"
f.puts content
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment