-
Notifications
You must be signed in to change notification settings - Fork 317
/
marley.rb
71 lines (60 loc) · 2.42 KB
/
marley.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# frozen_string_literal: true
module JekyllImport
module Importers
class Marley < Importer
def self.validate(options)
if options["marley_data_dir"].nil?
Jekyll.logger.abort_with "Missing mandatory option --marley_data_dir."
else
raise ArgumentError, "marley dir '#{options["marley_data_dir"]}' not found" unless File.directory?(options["marley_data_dir"])
end
end
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, } # Multiline Regexp
end
def self.require_deps
JekyllImport.require_with_fallback(%w(
fileutils
safe_yaml
))
end
def self.specify_options(c)
c.option "marley_data_dir", "--marley_data_dir DIR", "The dir containing your marley data."
end
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)
# copied over from marley's app/lib/post.rb
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
end
end
end