-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
97 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
module JekyllImport | ||
module Importers | ||
class Pebble < Importer | ||
def self.require_deps | ||
JekyllImport.require_with_fallback(%w( | ||
nokogiri | ||
safe_yaml | ||
)) | ||
end | ||
|
||
def self.specify_options(c) | ||
c.option "directory", "--directory PATH", "Pebble source directory" | ||
end | ||
|
||
def self.process(opts) | ||
options = { | ||
directory: opts.fetch("directory", "") | ||
} | ||
|
||
FileUtils.mkdir_p("_posts") | ||
FileUtils.mkdir_p("_drafts") | ||
|
||
traverse_posts_within(options[:directory]) do |file| | ||
next if file.end_with?('categories.xml') | ||
process_file(file) | ||
end | ||
end | ||
|
||
def self.traverse_posts_within(directory, &block) | ||
Dir.foreach(directory) do |fd| | ||
path = File.join(directory, fd) | ||
if fd == '.' || fd == '..' | ||
next | ||
elsif File.directory?(path) | ||
traverse_posts_within(path, &block) | ||
elsif path.end_with?('xml') | ||
yield(path) if block_given? | ||
else | ||
end | ||
end | ||
end | ||
|
||
def self.process_file(file) | ||
xml = File.open(file) { |f| Nokogiri::XML(f) } | ||
raise "There doesn't appear to be any XML items at the source (#{file}) provided." unless xml | ||
|
||
doc = xml.xpath("blogEntry") | ||
|
||
title = kebabize(doc.xpath('title').text).gsub('_', '-') | ||
date = Date.parse(doc.xpath('date').text) | ||
|
||
directory = "_posts" | ||
name = "#{date.strftime('%Y-%m-%d')}-#{title}" | ||
|
||
header = { | ||
"layout" => 'post', | ||
"title" => doc.xpath("title").text, | ||
"tags" => doc.xpath("tags").text.split(", "), | ||
"categories" => doc.xpath('category').text.split(', ') | ||
} | ||
header["render_with_liquid"] = false | ||
|
||
path = File.join(directory, "#{name}.html") | ||
File.open(path, "w") do |f| | ||
f.puts header.to_yaml | ||
f.puts "---\n\n" | ||
f.puts doc.xpath("body").text | ||
end | ||
|
||
Jekyll.logger.info "Wrote file #{path} successfully!" | ||
end | ||
|
||
def self.kebabize(string) | ||
kebab = '-'.freeze | ||
string.gsub!(/[^\w\-_]+/, kebab) | ||
|
||
unless kebab.nil? || kebab.empty? | ||
if kebab == "-".freeze | ||
re_duplicate_kebab = /-{2,}/ | ||
re_leading_trailing_kebab = /^-|-$/ | ||
else | ||
re_sep = Regexp.escape(kebab) | ||
re_duplicate_kebab = /#{re_sep}{2,}/ | ||
re_leading_trailing_kebab = /^#{re_sep}|#{re_sep}$/ | ||
end | ||
# No more than one of the kebab in a row. | ||
string.gsub!(re_duplicate_kebab, kebab) | ||
# Remove leading/trailing kebab. | ||
string.gsub!(re_leading_trailing_kebab, "".freeze) | ||
end | ||
|
||
string.downcase! | ||
string | ||
end | ||
end | ||
end | ||
end |