Skip to content

Instantly share code, notes, and snippets.

@sskylar
Last active December 18, 2015 19:39
Show Gist options
  • Save sskylar/5834451 to your computer and use it in GitHub Desktop.
Save sskylar/5834451 to your computer and use it in GitHub Desktop.
Jekyll to Siteleaf import script.Imports posts from Jekyll into a Siteleaf page, retains markdown and converts frontmatter into metadata/taxonomy. Requires the Siteleaf Gem (https://github.com/siteleaf/siteleaf-gem).Save to your Jekyll site folder and run in the command line using "ruby jekyll-siteleaf.rb".
require "siteleaf"
require "yaml"
# API settings
Siteleaf.api_key = '...'
Siteleaf.api_secret = '...'
# site settings
site_id = '...'
page_id = '...' # blog page to import posts into
test_mode = false # change to true to test your import
# get posts from Jekyll
files = Dir.glob('_posts/*.markdown')
# loop through and add posts to Siteleaf
files.each do |file|
puts "Creating post..."
# set up post
post = Siteleaf::Post.new
post.site_id = site_id
post.parent_id = page_id
# read file
content = File.read(file)
# get front matter and body from file
contentparts = /---(.*)---(.*)/m.match(content)
frontmatter = YAML.load(contentparts[1])
post.body = contentparts[2].strip
# parse frontmatter
post.meta = []
post.taxonomy = []
frontmatter.each do |k,v|
case k
when 'title'
post.title = v.strip
when 'tags', 'category'
post.taxonomy << {"key" => k.strip, "values" => (v.is_a?(Array) ? v.strip : [v])} if k && v
else
post.meta << {"key" => k.strip, "value" => v.strip} if k && v
end
end
# get date and slug from filename
fileparts = /([0-9]{4}\-[0-9]{2}\-[0-9]{2})\-([A-Za-z0-9\-]+)\.markdown/.match(file)
post.published_at = fileparts[1]
post.custom_slug = fileparts[2]
# save
puts test_mode ? post.inspect : post.save.inspect
end
# done!
puts "Success!"
@asimpson
Copy link

I updated the gist to be a bit more clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment