-
-
Save peet86/3c579e4843b8c9b6fcf7 to your computer and use it in GitHub Desktop.
Migrate your PyroCMS blog posts to jekyll and convert html content to markup format
This file contains 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
#!/usr/bin/env ruby | |
# | |
# Convert PyroCMS blog posts to jekyll posts | |
# | |
# Basic Usage | |
# ----------- | |
# | |
# ruby ./import_pyro_to_jekyll.rb feed_url | |
# | |
# where `feed_url` can have the following format: | |
# | |
# http://{your.pyrocms.url}/blog/rss | |
# | |
# Requirements | |
# ------------ | |
# | |
# * feedzirra: https://github.com/pauldix/feedzirra | |
# * reverse_markdown: https://github.com/xijo/reverse_markdown | |
# Notes | |
# ----- | |
# | |
# * Make sure you made the changes in PyroCMS rss.php to show the full output of article in feeds. | |
# * Plese note: the script converts PyroCMS blog category to jekyll and doesn't import pyrocms tags or comments. | |
include RbConfig | |
require 'rubygems' if CONFIG['host_os'].start_with? "darwin" | |
require 'feedjira' | |
require 'date' | |
require 'optparse' | |
require 'reverse_markdown' | |
def parse_post_entries(feed, verbose) | |
posts = [] | |
feed.entries.each do |post| | |
obj = Hash.new | |
#p post | |
file_name = post.published.strftime('%Y') + "-" + post.published.strftime('%m') + "-" + post.published.strftime('%d') + "-" + post.url.split('/').last + ".md" | |
obj["file_name"] = file_name | |
obj["title"] = post.title | |
obj["creation_datetime"] = post.published | |
obj["updated_datetime"] = post.updated | |
obj["content"] = ReverseMarkdown.convert(post.summary, unknown_tags: :drop, github_flavored: true) | |
obj["categories"] = post.categories.join(" ") | |
posts.push(obj) | |
end | |
return posts | |
end | |
def write_posts(posts, verbose) | |
Dir.mkdir("_posts") unless File.directory?("_posts") | |
total = posts.length, i = 1 | |
posts.each do |post| | |
file_name = "_posts/".concat(post["file_name"]) | |
header = %{--- | |
layout: post | |
title: #{post["title"]} | |
date: #{post["creation_datetime"]} | |
published: true | |
tags: #{post["categories"]} | |
--- | |
} | |
File.open(file_name, "w+") {|f| | |
f.write(header) | |
f.write(post["content"]) | |
f.close | |
} | |
if verbose | |
puts " [#{i}/#{total[0]}] Written post #{file_name}" | |
i += 1 | |
end | |
end | |
end | |
def main | |
options = {} | |
opt_parser = OptionParser.new do |opt| | |
opt.banner = "Usage: ./import.rb FEED_URL [OPTIONS]" | |
opt.separator "" | |
opt.separator "Options" | |
opt.on("-v", "--verbose", "Print out all.") do | |
options[:verbose] = true | |
end | |
end | |
opt_parser.parse! | |
if ARGV[0] | |
feed_url = ARGV.first | |
else | |
puts opt_parser | |
exit() | |
end | |
puts "Fetching feed #{feed_url}..." | |
feed = Feedjira::Feed.fetch_and_parse(feed_url) | |
puts "Parsing feed..." | |
posts = parse_post_entries(feed, options[:verbose]) | |
puts "Writing posts to _posts/..." | |
write_posts(posts, options[:verbose]) | |
puts "Done!" | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment