Module: MarkdownDatafier
- Defined in:
- lib/markdown_datafier.rb,
lib/markdown_datafier/version.rb
Defined Under Namespace
Classes: MetadataParseError
Constant Summary
collapse
- VERSION =
"0.0.3"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
6
7
8
|
# File 'lib/markdown_datafier.rb', line 6
def config
@config
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
78
79
80
|
# File 'lib/markdown_datafier.rb', line 78
def append_file_extention(path)
path + ".mdown"
end
|
#content_directory ⇒ Object
21
22
23
|
# File 'lib/markdown_datafier.rb', line 21
def content_directory
@config["content_directory"]
end
|
#convert_body_to_html(content_hash) ⇒ Object
134
135
136
137
138
|
# File 'lib/markdown_datafier.rb', line 134
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
130
131
132
|
# File 'lib/markdown_datafier.rb', line 130
def default_nav_name(body)
body.split(/\r?\n\r?\n/, 2)[0].gsub(/# /, "")
end
|
#determine_file_path(path) ⇒ Object
66
67
68
|
# File 'lib/markdown_datafier.rb', line 66
def determine_file_path(path)
is_directory?(path) ? serve_index(path) : append_file_extention(path)
end
|
#determine_publish_datetime(content_hash) ⇒ Object
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/markdown_datafier.rb', line 108
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
52
53
54
55
56
57
58
59
60
|
# File 'lib/markdown_datafier.rb', line 52
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
25
26
27
|
# File 'lib/markdown_datafier.rb', line 25
def home_page
find_by_path("index")
end
|
#indexes_for_sections(directory = nil) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/markdown_datafier.rb', line 33
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
|
#initialize(attributes) ⇒ Object
13
14
15
|
# File 'lib/markdown_datafier.rb', line 13
def initialize(attributes)
set_config_directory(attributes[:config_directory])
end
|
#is_directory?(path) ⇒ Boolean
70
71
72
|
# File 'lib/markdown_datafier.rb', line 70
def is_directory?(path)
File.directory?(path)
end
|
#parse_file_content(content) ⇒ Object
86
87
88
|
# File 'lib/markdown_datafier.rb', line 86
def parse_file_content(content)
convert_body_to_html(set_meta_defaults(determine_publish_datetime(parse_meta(split_meta_and_body(content)))))
end
|
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/markdown_datafier.rb', line 95
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
74
75
76
|
# File 'lib/markdown_datafier.rb', line 74
def serve_index(shortname)
shortname.match(/\/$/) ? (shortname + "index.mdown") : (shortname + "/index.mdown")
end
|
#set_config_directory(path) ⇒ Object
17
18
19
|
# File 'lib/markdown_datafier.rb', line 17
def set_config_directory(path)
@config = YAML.load_file( path + "markdown_datafier.yml")
end
|
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/markdown_datafier.rb', line 119
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
29
30
31
|
# File 'lib/markdown_datafier.rb', line 29
def splash_page
find_by_path("splash")
end
|
#split_meta_and_body(content) ⇒ Object
90
91
92
93
|
# File 'lib/markdown_datafier.rb', line 90
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
62
63
64
|
# File 'lib/markdown_datafier.rb', line 62
def strip_leading_slashes(shortname)
shortname.match(/^\//) ? shortname.gsub(/^\//, "") : shortname
end
|
#sub_directories ⇒ Object
48
49
50
|
# File 'lib/markdown_datafier.rb', line 48
def sub_directories
sub_directories = Dir["*"].reject{|file| not File.directory?(file)}.map! {|sub_directory| "/" + sub_directory }
end
|
#underscores_to_dashes(shortname) ⇒ Object
82
83
84
|
# File 'lib/markdown_datafier.rb', line 82
def underscores_to_dashes(shortname)
shortname.gsub(/_/, "-")
end
|