Class: JekyllImport::Importers::Marley

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

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.process(options) ⇒ Object



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
# File 'lib/jekyll-import/importers/marley.rb', line 35

def self.process(options)
  marley_data_dir = options.fetch('marley_data_dir')

  FileUtils.mkdir_p "_posts"

  posts = 0
  Dir["#{marley_data_dir}/**/*.txt"].each do |f|
    next unless File.exists?(f)

    #copied over from marley's app/lib/post.rb
    file_content  = File.read(f)
    meta_content  = file_content.slice!( self.regexp[:meta] )
    body          = file_content.sub( self.regexp[:title], '').sub( self.regexp[:perex], '').strip

    title = file_content.scan( self.regexp[:title] ).first.to_s.strip
    prerex = file_content.scan( self.regexp[:perex] ).first.to_s.strip
    published_on = DateTime.parse( post[:published_on] ) rescue File.mtime( File.dirname(f) )
    meta          = ( meta_content ) ? YAML::load( meta_content.scan( self.regexp[:meta]).to_s ) : {}
    meta['title'] = title
    meta['layout'] = 'post'

    formatted_date = published_on.strftime('%Y-%m-%d')
    post_name =  File.dirname(f).split(%r{/}).last.gsub(/\A\d+-/, '')

    name = "#{formatted_date}-#{post_name}"
    File.open("_posts/#{name}.markdown", "w") do |f|
      f.puts meta.to_yaml
      f.puts "---\n"
      f.puts "\n#{prerex}\n\n" if prerex
      f.puts body
    end
    posts += 1
  end
  "Created #{posts} posts!"
end

.regexpObject



14
15
16
17
18
19
20
21
22
# File 'lib/jekyll-import/importers/marley.rb', line 14

def self.regexp
  { :id    => /^\d{0,4}-{0,1}(.*)$/,
    :title => /^#\s*(.*)\s+$/,
    :title_with_date => /^#\s*(.*)\s+\(([0-9\/]+)\)$/,
    :published_on => /.*\s+\(([0-9\/]+)\)$/,
    :perex => /^([^\#\n]+\n)$/,
    :meta  => /^\{\{\n(.*)\}\}\n$/mi # Multiline Regexp
  }
end

.require_depsObject



24
25
26
27
28
29
# File 'lib/jekyll-import/importers/marley.rb', line 24

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

.specify_options(c) ⇒ Object



31
32
33
# File 'lib/jekyll-import/importers/marley.rb', line 31

def self.specify_options(c)
  c.option 'marley_data_dir', '--marley_data_dir DIR', 'The dir containing your marley data'
end

.validate(options) ⇒ Object



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

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