Class: JekyllImport::Importers::Pluxml

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

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.process(options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
72
73
74
75
76
77
78
79
# File 'lib/jekyll-import/importers/pluxml.rb', line 28

def self.process(options)
  source       = options.fetch("source")
  layout       = options.fetch("layout")
  avoid_liquid = options.fetch("avoid_liquid")

  FileUtils.mkdir_p("_posts")
  FileUtils.mkdir_p("_drafts")

  # for each XML file in source location
  Dir.glob("*.xml", :base => source).each do |df|
    df = File.join(source, df)
    filename = File.basename(df, ".*")

    # prepare post file name in Jekyll format
    a_filename = filename.split(".")
    post_name  = a_filename.pop
    file_date  = a_filename.pop
      = file_date[0..3] + "-" + file_date[4..5] + "-" + file_date[6..7]

    # if draft, only take post name
    if filename.split(".")[1].split(",")[0] == "draft"
      directory = "_drafts"
      name      = post_name.to_s
    # if post, post date precede post name
    else
      directory = "_posts"
      name      = "#{}-#{post_name}"
    end

    xml = File.open(df) { |f| Nokogiri::XML(f) }
    raise "There doesn't appear to be any XML items at the source (#{df}) provided." unless xml

    doc = xml.xpath("document")
    header = {
      "layout" => layout,
      "title"  => doc.xpath("title").text,
      "tags"   => doc.xpath("tags").text.split(", "),
    }
    header["render_with_liquid"] = false if avoid_liquid

    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("chapo").text
      f.puts doc.xpath("content").text
    end

    Jekyll.logger.info "Wrote file #{path} successfully!"
  end
  nil
end

.require_depsObject



6
7
8
9
10
11
12
# File 'lib/jekyll-import/importers/pluxml.rb', line 6

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

.specify_options(c) ⇒ Object



14
15
16
17
18
# File 'lib/jekyll-import/importers/pluxml.rb', line 14

def self.specify_options(c)
  c.option "source",       "--source NAME",  "The PluXml data directory to import."
  c.option "layout",       "--layout NAME",  "The layout to apply. (default: 'post')"
  c.option "avoid_liquid", "--avoid_liquid", "Will add `render_with_liquid: false` in front matter. (default: false)"
end

.validate(options) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/jekyll-import/importers/pluxml.rb', line 20

def self.validate(options)
  abort "Missing mandatory option --source." if options["source"].nil?
  # no layout option, layout by default is post
  options["layout"] = "post" if options["layout"].nil?
  # no avoid_liquid option, avoid_liquid by default is false
  options["avoid_liquid"] = false if options["avoid_liquid"].nil?
end