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

Tidy up the importers for a beta4 release #80

Merged
merged 10 commits into from
Nov 9, 2013
44 changes: 29 additions & 15 deletions lib/jekyll-import/importers/csv.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,50 @@
require "csv"

module JekyllImport
module Importers
class CSV < Importer
def self.require_deps
JekyllImport.require_with_fallback(%w[
csv
fileutils
])
end

def self.specify_options(c)
c.option 'file', '--file NAME', 'The CSV file to import'
c.option 'file', '--file NAME', 'The CSV file to import (default: "posts.csv")'
end

# Reads a csv with title, permalink, body, published_at, and filter.
# It creates a post file for each row in the csv
def self.process(options)
file = options['file'] || "posts.csv"
file = options.fetch('file', "posts.csv")

FileUtils.mkdir_p "_posts"
posts = 0
abort "Cannot find the file '#{file}'. Aborting." unless File.file?(file)

::CSV.foreach(file) do |row|
next if row[0] == "title"
posts += 1
name = row[3].split(" ")[0]+"-"+row[1]+(row[4] =~ /markdown/ ? ".markdown" : ".textile")
File.open("_posts/#{name}", "w") do |f|
f.puts <<-HEADER
---
layout: post
title: #{row[0]}
---

HEADER
f.puts row[2]
end
name = build_name(row)
write_post(name, row[0], row[2])
end
"Created #{posts} posts!"
end

def self.write_post(name, title, content)
File.open("_posts/#{name}", "w") do |f|
f.puts <<-HEADER
---
layout: post
title: #{title}
---
HEADER
f.puts content
end
end

def self.build_name(row)
row[3].split(" ")[0]+"-"+row[1]+(row[4] =~ /markdown/ ? ".markdown" : ".textile")
end
end
end
end
14 changes: 7 additions & 7 deletions lib/jekyll-import/importers/drupal6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class Drupal6 < Importer
node AS n \
LEFT OUTER JOIN term_node AS tn ON tn.nid = n.nid \
LEFT OUTER JOIN term_data AS td ON tn.tid = td.tid \
WHERE (n.type = 'blog' OR n.type = 'story') \
WHERE (n.type = 'blog' OR n.type = 'story' OR n.type = 'article') \
AND n.vid = nr.vid \
GROUP BY n.nid"

def self.validate(options)
%w[dbname user pass].each do |option|
if options[option.to_sym].nil?
%w[dbname user].each do |option|
if options[option].nil?
abort "Missing mandatory option --#{option}."
end
end
Expand All @@ -34,8 +34,8 @@ def self.validate(options)
def self.specify_options(c)
c.option 'dbname', '--dbname DB', 'Database name'
c.option 'user', '--user USER', 'Database user name'
c.option 'password', '--password PW', "Database user's password"
c.option 'host', '--host HOST', 'Database host name'
c.option 'password', '--password PW', "Database user's password (default: '')"
c.option 'host', '--host HOST', 'Database host name (default: "localhost")'
c.option 'prefix', '--prefix PREFIX', 'Table prefix name'
end

Expand All @@ -51,7 +51,7 @@ def self.require_deps
def self.process(options)
dbname = options.fetch('dbname')
user = options.fetch('user')
pass = options.fetch('password')
pass = options.fetch('password', "")
host = options.fetch('host', "localhost")
prefix = options.fetch('prefix', "")

Expand Down Expand Up @@ -132,7 +132,7 @@ def self.process(options)
end

# TODO: Make dirs & files for nodes of type 'page'
# Make refresh pages for these as well
# Make refresh pages for these as well
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/jekyll-import/importers/drupal7.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class Drupal7 < Importer
n.status \
FROM node AS n, \
field_data_body AS fdb \
WHERE (n.type = 'blog' OR n.type = 'story') \
WHERE (n.type = 'blog' OR n.type = 'story' OR n.type = 'article') \
AND n.nid = fdb.entity_id \
AND n.vid = fdb.revision_id"

def self.validate(options)
%w[dbname user pass].each do |option|
if options[option.to_sym].nil?
%w[dbname user].each do |option|
if options[option].nil?
abort "Missing mandatory option --#{option}."
end
end
Expand All @@ -31,8 +31,8 @@ def self.validate(options)
def self.specify_options(c)
c.option 'dbname', '--dbname DB', 'Database name'
c.option 'user', '--user USER', 'Database user name'
c.option 'password', '--password PW', "Database user's password"
c.option 'host', '--host HOST', 'Database host name'
c.option 'password', '--password PW', 'Database user\'s password (default: "")'
c.option 'host', '--host HOST', 'Database host name (default: "localhost")'
c.option 'prefix', '--prefix PREFIX', 'Table prefix name'
end

Expand All @@ -48,7 +48,7 @@ def self.require_deps
def self.process(options)
dbname = options.fetch('dbname')
user = options.fetch('user')
pass = options.fetch('password')
pass = options.fetch('password', "")
host = options.fetch('host', "localhost")
prefix = options.fetch('prefix', "")

Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll-import/importers/enki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Enki < Importer
p.published_at as date,
p.cached_tag_list as tags
FROM posts p
EOS
EOS

def self.validate(options)
%w[dbname user].each do |option|
Expand Down Expand Up @@ -43,7 +43,7 @@ def self.require_deps
def self.process(options)
dbname = options.fetch('dbname')
user = options.fetch('user')
pass = options.fetch('pass', "")
pass = options.fetch('password', "")
host = options.fetch('host', "localhost")

FileUtils.mkdir_p('_posts')
Expand Down
6 changes: 3 additions & 3 deletions lib/jekyll-import/importers/joomla.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module JekyllImport
module Importers
class Joomla < Importer
def self.validate(options)
%w[dbname user pass].each do |option|
%w[dbname user].each do |option|
if options[option].nil?
abort "Missing mandatory option --#{option}."
end
Expand All @@ -19,7 +19,7 @@ def self.validate(options)
def self.specify_options(c)
c.option 'dbname', '--dbname', 'Database name'
c.option 'user', '--user', 'Database user name'
c.option 'password', '--password', "Database user's password"
c.option 'password', '--password', "Database user's password (default: '')"
c.option 'host', '--host', 'Database host name'
c.option 'section', '--section', 'Table prefix name'
c.option 'prefix', '--prefix', 'Table prefix name'
Expand All @@ -37,7 +37,7 @@ def self.require_deps
def self.process(options)
dbname = options.fetch('dbname')
user = options.fetch('user')
pass = options.fetch('pass')
pass = options.fetch('password', '')
host = options.fetch('host', "localhost")
section = options.fetch('section', '1')
table_prefix = options.fetch('prefix', "jos_")
Expand Down
11 changes: 10 additions & 1 deletion lib/jekyll-import/importers/marley.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
module JekyllImport
module Importers
class Marley < Importer
def self.validate(options)
if options['marley_data_dir'].nil?
Jekyll.logger.abort_with "Missing mandatory option --marley_data_dir."
else
unless File.directory?(options['marley_data_dir'])
raise ArgumentError, "marley dir '#{options['marley_data_dir']}' not found"
end
end
end

def self.regexp
{ :id => /^\d{0,4}-{0,1}(.*)$/,
:title => /^#\s*(.*)\s+$/,
Expand All @@ -24,7 +34,6 @@ def self.specify_options(c)

def self.process(options)
marley_data_dir = options.fetch('marley_data_dir')
raise ArgumentError, "marley dir #{marley_data_dir} not found" unless File.directory?(marley_data_dir)

FileUtils.mkdir_p "_posts"

Expand Down
18 changes: 13 additions & 5 deletions lib/jekyll-import/importers/mephisto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def self.postgres(c)
CSV.process
end

def self.validate(options)
%w[dbname user].each do |option|
if options[option].nil?
abort "Missing mandatory option --#{option}."
end
end
end

def self.require_deps
JekyllImport.require_with_fallback(%w[
rubygems
Expand All @@ -40,7 +48,7 @@ def self.require_deps
def self.specify_options(c)
c.option 'dbname', '--dbname DB', 'Database name'
c.option 'user', '--user USER', 'Database user name'
c.option 'password', '--password PW', "Database user's password"
c.option 'password', '--password PW', "Database user's password (default: '')"
c.option 'host', '--host HOST', 'Database host name (default: "localhost")'
end

Expand All @@ -59,10 +67,10 @@ def self.specify_options(c)
ORDER BY published_at"

def self.process(options)
dbname = options.fetch(:dbname)
user = options.fetch(:user)
pass = options.fetch(:pass)
host = options.fetch(:host, "localhost")
dbname = options.fetch('dbname')
user = options.fetch('user')
pass = options.fetch('password', '')
host = options.fetch('host', "localhost")

db = Sequel.mysql(dbname, :user => user,
:password => pass,
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll-import/importers/mt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def self.require_deps
def self.specify_options(c)
c.option 'dbname', '--dbname DB', 'Database name'
c.option 'user', '--user USER', 'Database user name'
c.option 'password', '--password PW', "Database user's password"
c.option 'password', '--password PW', "Database user's password, (default: '')"
c.option 'host', '--host HOST', 'Database host name (default: "localhost")'
end

Expand Down Expand Up @@ -65,7 +65,7 @@ def self.specify_options(c)
def self.process(options)
dbname = options.fetch('dbname')
user = options.fetch('user')
pass = options.fetch('pass')
pass = options.fetch('password', "")
host = options.fetch('host', "localhost")

options = default_options.merge(options)
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll-import/importers/posterous.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def self.fetch_one(url, limit = 10)

def self.process(options)
email = options.fetch('email')
pass = options.fetch('pass')
pass = options.fetch('password')
api_token = options.fetch('api_token')

@email, @pass, @api_token = email, pass, api_token
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll-import/importers/typo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ def self.specify_options(c)
c.option 'server', '--server TYPE', 'Server type ("mysql" or "postgres")'
c.option 'dbname', '--dbname DB', 'Database name'
c.option 'user', '--user USER', 'Database user name'
c.option 'password', '--password PW', "Database user's password"
c.option 'password', '--password PW', "Database user's password (default: '')"
c.option 'host', '--host HOST', 'Database host name'
end

def self.process(options)
server = options.fetch('server')
dbname = options.fetch('dbname')
user = options.fetch('user')
pass = options.fetch('pass')
pass = options.fetch('password', '')
host = options.fetch('host', "localhost")

FileUtils.mkdir_p '_posts'
Expand Down
22 changes: 11 additions & 11 deletions lib/jekyll-import/importers/wordpress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ def self.specify_options(c)
def self.process(opts)
options = {
:user => opts.fetch('user', ''),
:pass => options.fetch('password', ''),
:host => options.fetch('host', 'localhost'),
:dbname => options.fetch('dbname', ''),
:table_prefix => options.fetch('prefix', 'wp_'),
:clean_entities => options.fetch('clean_entities', true),
:comments => options.fetch('comments', true),
:categories => options.fetch('categories', true),
:tags => options.fetch('tags', true),
:more_excerpt => options.fetch('more_excerpt', true),
:more_anchor => options.fetch('more_anchor', true),
:status => options.fetch('status', ["publish"]).map(&:to_sym) # :draft, :private, :revision
:pass => opts.fetch('password', ''),
:host => opts.fetch('host', 'localhost'),
:dbname => opts.fetch('dbname', ''),
:table_prefix => opts.fetch('prefix', 'wp_'),
:clean_entities => opts.fetch('clean_entities', true),
:comments => opts.fetch('comments', true),
:categories => opts.fetch('categories', true),
:tags => opts.fetch('tags', true),
:more_excerpt => opts.fetch('more_excerpt', true),
:more_anchor => opts.fetch('more_anchor', true),
:status => opts.fetch('status', ["publish"]).map(&:to_sym) # :draft, :private, :revision
}

if options[:clean_entities]
Expand Down