Class: JekyllImport::Importers::Marley
Class Method Summary
collapse
inherited, run, stringify_keys, subclasses
Class Method Details
.process(options) ⇒ Object
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
|
# File 'lib/jekyll-import/importers/marley.rb', line 36
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.exist?(f)
file_content = File.read(f)
meta_content = file_content.slice!(regexp[:meta])
body = file_content.sub(regexp[:title], "").sub(regexp[:perex], "").strip
title = file_content.scan(regexp[:title]).first.to_s.strip
prerex = file_content.scan(regexp[:perex]).first.to_s.strip
published_on = DateTime.parse(post[:published_on]) rescue File.mtime(File.dirname(f))
meta = meta_content ? YAML.safe_load(meta_content.scan(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(%r!\A\d+-!, "")
name = "#{formatted_date}-#{post_name}"
File.open("_posts/#{name}.markdown", "w") do |post|
post.puts meta.to_yaml
post.puts "---\n"
post.puts "\n#{prerex}\n\n" if prerex
post.puts body
end
posts += 1
end
"Created #{posts} posts!"
end
|
.regexp ⇒ Object
16
17
18
19
20
21
22
23
|
# File 'lib/jekyll-import/importers/marley.rb', line 16
def self.regexp
{ :id => %r!^\d{0,4}-{0,1}(.*)$!,
:title => %r!^#\s*(.*)\s+$!,
:title_with_date => %r!^#\s*(.*)\s+\(([0-9\/]+)\)$!,
:published_on => %r!.*\s+\(([0-9\/]+)\)$!,
:perex => %r!^([^\#\n]+\n)$!,
:meta => %r!^\{\{\n(.*)\}\}\n$!mi, }
end
|
.require_deps ⇒ Object
25
26
27
28
29
30
|
# File 'lib/jekyll-import/importers/marley.rb', line 25
def self.require_deps
JekyllImport.require_with_fallback(%w(
fileutils
safe_yaml
))
end
|
.specify_options(c) ⇒ Object
32
33
34
|
# File 'lib/jekyll-import/importers/marley.rb', line 32
def self.specify_options(c)
c.option "marley_data_dir", "--marley_data_dir DIR", "The dir containing your marley data"
end
|
.validate(options) ⇒ Object
6
7
8
9
10
11
12
13
14
|
# File 'lib/jekyll-import/importers/marley.rb', line 6
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
|