Class: JekyllImport::Importers::Pebble

Inherits:
JekyllImport::Importer show all
Defined in:
lib/jekyll-import/importers/pebble.rb

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.kebabize(string) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jekyll-import/importers/pebble.rb', line 73

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

.process(opts) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jekyll-import/importers/pebble.rb', line 15

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

.process_file(file) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jekyll-import/importers/pebble.rb', line 43

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

.require_depsObject



4
5
6
7
8
9
# File 'lib/jekyll-import/importers/pebble.rb', line 4

def self.require_deps
  JekyllImport.require_with_fallback(%w(
    nokogiri
    safe_yaml
  ))
end

.specify_options(c) ⇒ Object



11
12
13
# File 'lib/jekyll-import/importers/pebble.rb', line 11

def self.specify_options(c) 
  c.option "directory", "--directory PATH", "Pebble source directory"
end

.traverse_posts_within(directory, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jekyll-import/importers/pebble.rb', line 29

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