Module: MarkdownDatafier
- Defined in:
- lib/markdown_datafier.rb,
lib/markdown_datafier/version.rb
Defined Under Namespace
Classes: MetadataParseError
Constant Summary
collapse
- VERSION =
"1.0.0"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#content_directory ⇒ Object
Returns the value of attribute content_directory.
6
7
8
|
# File 'lib/markdown_datafier.rb', line 6
def content_directory
@content_directory
end
|
Class Method Details
.root ⇒ Object
9
10
11
|
# File 'lib/markdown_datafier.rb', line 9
def self.root
File.expand_path '../..', __FILE__
end
|
Instance Method Details
#append_file_extention(path) ⇒ Object
66
67
68
|
# File 'lib/markdown_datafier.rb', line 66
def append_file_extention(path)
path + ".mdown"
end
|
#convert_body_to_html(content_hash) ⇒ Object
122
123
124
125
126
|
# File 'lib/markdown_datafier.rb', line 122
def convert_body_to_html(content_hash)
converter = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true)
content_hash[:body] = converter.render(content_hash[:body])
content_hash
end
|
#default_nav_name(body) ⇒ Object
118
119
120
|
# File 'lib/markdown_datafier.rb', line 118
def default_nav_name(body)
body.split(/\r?\n\r?\n/, 2)[0].gsub(/# /, "")
end
|
#determine_file_path(path) ⇒ Object
54
55
56
|
# File 'lib/markdown_datafier.rb', line 54
def determine_file_path(path)
is_directory?(path) ? serve_index(path) : append_file_extention(path)
end
|
#determine_publish_datetime(content_hash) ⇒ Object
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/markdown_datafier.rb', line 96
def determine_publish_datetime(content_hash)
if [:date, :publish_date, :publish_datetime].any? {|symbol| content_hash[:meta].key? symbol}
content_hash[:meta][:publish_datetime] = Time.parse(content_hash[:meta][:publish_datetime]).utc.iso8601 if content_hash[:meta][:publish_datetime]
content_hash[:meta][:publish_datetime] = Time.parse(content_hash[:meta].delete(:date)).utc.iso8601 if content_hash[:meta][:date]
content_hash[:meta][:publish_datetime] = Time.parse(content_hash[:meta].delete(:publish_date)).utc.iso8601 if content_hash[:meta][:publish_date]
else
content_hash[:meta][:publish_datetime] = Time.parse(content_hash[:meta][:create_datetime]).utc.iso8601
end
content_hash
end
|
#find_by_path(shortname) ⇒ Object
40
41
42
43
44
45
46
47
48
|
# File 'lib/markdown_datafier.rb', line 40
def find_by_path(shortname)
begin
path = determine_file_path(content_directory + strip_leading_slashes(shortname))
content = "Shortname: #{shortname}\nCreate Datetime: #{File.ctime(path)}\n" + File.open(path).read
parse_file_content(content)
rescue
nil
end
end
|
#home_page ⇒ Object
13
14
15
|
# File 'lib/markdown_datafier.rb', line 13
def home_page
find_by_path("index")
end
|
#indexes_for_sections(directory = nil) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/markdown_datafier.rb', line 21
def indexes_for_sections(directory=nil)
sections = []
if directory.nil?
Dir.chdir(content_directory)
currrent_dir_name = ""
else
Dir.chdir(content_directory + directory)
currrent_dir_name = File.basename(Dir.pwd)
end
sub_directories.each do |section|
sections << find_by_path(currrent_dir_name + section)
end
sections
end
|
#is_directory?(path) ⇒ Boolean
58
59
60
|
# File 'lib/markdown_datafier.rb', line 58
def is_directory?(path)
File.directory?(path)
end
|
#parse_file_content(content) ⇒ Object
74
75
76
|
# File 'lib/markdown_datafier.rb', line 74
def parse_file_content(content)
convert_body_to_html(set_meta_defaults(determine_publish_datetime(parse_meta(split_meta_and_body(content)))))
end
|
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/markdown_datafier.rb', line 83
def parse_meta(content_hash)
is_metadata = content_hash[:meta].split("\n").first =~ /^[\w ]+:/
raise MetadataParseError unless is_metadata
parsed_hash = Hash.new
content_hash[:meta].split("\n").each do |line|
key, value = line.split(/\s*:\s*/, 2)
next if value.nil?
parsed_hash[key.downcase.gsub(/\s+/, "_").to_sym] = value.chomp
end
content_hash[:meta] = parsed_hash
content_hash
end
|
#serve_index(shortname) ⇒ Object
62
63
64
|
# File 'lib/markdown_datafier.rb', line 62
def serve_index(shortname)
shortname.match(/\/$/) ? (shortname + "index.mdown") : (shortname + "/index.mdown")
end
|
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/markdown_datafier.rb', line 107
def set_meta_defaults(content_hash)
meta_hash = content_hash[:meta]
meta_hash[:nav_name] ||= default_nav_name(content_hash[:body])
meta_hash[:position] ||= nil
meta_hash[:large_image] ||= nil
meta_hash[:medium_image] ||= nil
meta_hash[:small_image] ||= nil
content_hash[:meta] = meta_hash
content_hash
end
|
#splash_page ⇒ Object
17
18
19
|
# File 'lib/markdown_datafier.rb', line 17
def splash_page
find_by_path("splash")
end
|
#split_meta_and_body(content) ⇒ Object
78
79
80
81
|
# File 'lib/markdown_datafier.rb', line 78
def split_meta_and_body(content)
meta, body = content.split(/\r?\n\r?\n/, 2)
{:meta => meta}.merge!({:body => body})
end
|
#strip_leading_slashes(shortname) ⇒ Object
50
51
52
|
# File 'lib/markdown_datafier.rb', line 50
def strip_leading_slashes(shortname)
shortname.match(/^\//) ? shortname.gsub(/^\//, "") : shortname
end
|
#sub_directories ⇒ Object
36
37
38
|
# File 'lib/markdown_datafier.rb', line 36
def sub_directories
sub_directories = Dir["*"].reject{|file| not File.directory?(file)}.map! {|sub_directory| "/" + sub_directory }
end
|
#underscores_to_dashes(shortname) ⇒ Object
70
71
72
|
# File 'lib/markdown_datafier.rb', line 70
def underscores_to_dashes(shortname)
shortname.gsub(/_/, "-")
end
|