Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A better CLI for migrating #253

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion bin/jekyll
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Basic Command Line Usage:
jekyll # . -> ./_site
jekyll <path to write generated site> # . -> <path>
jekyll <path to source> <path to write generated site> # <path> -> <path>

jekyll import <importer name> <options> # imports posts using named import script

Configuration is read from '<source>/_config.yml' but can be overriden
using the following options:

Expand All @@ -18,11 +19,37 @@ HELP
require 'optparse'
require 'jekyll'


exec = {}
options = {}
opts = OptionParser.new do |opts|
opts.banner = help

opts.on("--file [PATH]", "File to import from") do |import_file|
options['file'] = import_file
end

opts.on("--dbname [TEXT]", "DB to import from") do |import_dbname|
options['dbname'] = import_dbname
end

opts.on("--user [TEXT]", "Username to use when importing") do |import_user|
options['user'] = import_user
end

opts.on("--pass [TEXT]", "Password to use when importing") do |import_pass|
options['pass'] = import_pass
end

opts.on("--host [HOST ADDRESS]", "Host to import from") do |import_host|
options['host'] = import_host
end

opts.on("--site [SITE NAME]", "Site to import from") do |import_site|
options['site'] = import_site
end


opts.on("--[no-]safe", "Safe mode (default unsafe)") do |safe|
options['safe'] = safe
end
Expand Down Expand Up @@ -101,6 +128,59 @@ end
# Read command line options into `options` hash
opts.parse!


# Check for import stuff
if ARGV.size > 0
if ARGV[0] == 'import'
migrator = ARGV[1]

if migrator.nil?
puts "Invalid options. Run `jekyll --help` for assistance."
exit(1)
else
migrator = migrator.downcase
end

cmd_options = []
['file', 'dbname', 'user', 'pass', 'host', 'site'].each do |p|
cmd_options << "\"#{options[p]}\"" unless options[p].nil?
end

# It's import time
puts "Importing..."

# Ideally, this shouldn't be necessary. Maybe parse the actual
# src files for the migrator name?
migrators = {
:posterous => 'Posterous',
:wordpressdotcom => 'WordpressDotCom',
:wordpress => 'Wordpress',
:csv => 'CSV',
:drupal => 'Drupal',
:mephisto => 'Mephisto',
:mt => 'MT',
:textpattern => 'TextPattern',
:typo => 'Typo'
}

app_root = File.join(File.dirname(__FILE__), '..')

require "#{app_root}/lib/jekyll/migrators/#{migrator}"

if Jekyll.const_defined?(migrators[migrator.to_sym])
migrator_class = Jekyll.const_get(migrators[migrator.to_sym])
migrator_class.process(*cmd_options)
else
puts "Invalid migrator. Run `jekyll --help` for assistance."
exit(1)
end

exit(0)
end
end



# Get source and destintation from command line
case ARGV.size
when 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# coding: utf-8

require 'rubygems'
require 'hpricot'
require 'fileutils'

require 'date'

# This importer takes a wordpress.xml file,
# which can be exported from your
# wordpress.com blog (/wp-admin/export.php)
Expand All @@ -15,8 +19,13 @@ def self.process(filename = "wordpress.xml")
doc = Hpricot::XML(File.read(filename))

(doc/:channel/:item).each do |item|
title = item.at(:title).inner_text
name = "#{Date.parse((doc/:channel/:item).first.at(:pubDate).inner_text).to_s("%Y-%m-%d")}-#{title.downcase.gsub('[^a-z0-9]', '-')}.html"
title = item.at(:title).inner_text.strip
date = item.at(:pubDate).inner_text

ftitle = title.downcase.tr('áéíóúàèìòùâêîôûãẽĩõũñäëïöüç','aeiouaeiouaeiouaeiounaeiouc').gsub(/[^a-z0-9]/, '-')
fdate = DateTime.strptime(date, '%a, %d %b %G %T')

name = "#{fdate.strftime('%Y-%m-%d')}-#{ftitle}.html"

File.open("_posts/#{name}", "w") do |f|
f.puts <<-HEADER
Expand All @@ -32,7 +41,7 @@ def self.process(filename = "wordpress.xml")
posts += 1
end

"Imported #{posts} posts"
puts "Imported #{posts} posts"
end
end
end
end