Module: MarkdownDatafier

Defined in:
lib/markdown_datafier.rb,
lib/markdown_datafier/version.rb

Defined Under Namespace

Classes: MetadataParseError

Constant Summary collapse

VERSION =
"1.0.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#content_pathObject

Returns the value of attribute content_path.



8
9
10
# File 'lib/markdown_datafier.rb', line 8

def content_path
  @content_path
end

Class Method Details

.rootObject



10
11
12
# File 'lib/markdown_datafier.rb', line 10

def self.root
  File.expand_path '../..', __FILE__
end

Instance Method Details

#append_file_extention(path) ⇒ Object



81
82
83
# File 'lib/markdown_datafier.rb', line 81

def append_file_extention(path)
  path + ".mdown"
end

#collect(directory = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/markdown_datafier.rb', line 18

def collect(directory=nil)
  directory = directory.nil? ? @content_path : "#{@content_path}#{directory}"
  collection = []
  Dir.foreach(directory) do |f|
    next if f == '.' || f == '..' || File.extname(f) != ".mdown"
    collection << find_by_path(File.basename(f, ".mdown"))
  end
  collection
end

#convert_body_to_html(content_hash) ⇒ Object



137
138
139
140
141
# File 'lib/markdown_datafier.rb', line 137

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



133
134
135
# File 'lib/markdown_datafier.rb', line 133

def default_nav_name(body)
  body.split(/\r?\n\r?\n/, 2)[0].gsub(/# /, "")
end

#determine_file_path(path) ⇒ Object



69
70
71
# File 'lib/markdown_datafier.rb', line 69

def determine_file_path(path)
  is_directory?(path) ? serve_index(path) : append_file_extention(path)
end

#determine_publish_datetime(content_hash) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/markdown_datafier.rb', line 111

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



55
56
57
58
59
60
61
62
63
# File 'lib/markdown_datafier.rb', line 55

def find_by_path(shortname)
  begin
    path = determine_file_path(@content_path + 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_pageObject



28
29
30
# File 'lib/markdown_datafier.rb', line 28

def home_page
  find_by_path("index")
end

#indexes_for_sections(directory = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/markdown_datafier.rb', line 36

def indexes_for_sections(directory=nil)
  sections = []
  if directory.nil?
    Dir.chdir(@content_path)
    currrent_dir_name = ""
  else
    Dir.chdir(@content_path + 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



14
15
16
# File 'lib/markdown_datafier.rb', line 14

def initialize(attributes)
  @content_path = attributes[:content_path]
end

#is_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/markdown_datafier.rb', line 73

def is_directory?(path)
  File.directory?(path)
end

#parse_file_content(content) ⇒ Object



89
90
91
# File 'lib/markdown_datafier.rb', line 89

def parse_file_content(content)
  convert_body_to_html(set_meta_defaults(determine_publish_datetime(parse_meta(split_meta_and_body(content)))))
end

#parse_meta(content_hash) ⇒ Object

Raises:



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/markdown_datafier.rb', line 98

def parse_meta(content_hash)
   = content_hash[:meta].split("\n").first =~ /^[\w ]+:/
  raise MetadataParseError unless 
  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



77
78
79
# File 'lib/markdown_datafier.rb', line 77

def serve_index(shortname)
  shortname.match(/\/$/) ? (shortname + "index.mdown") : (shortname + "/index.mdown")
end

#set_meta_defaults(content_hash) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/markdown_datafier.rb', line 122

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_pageObject



32
33
34
# File 'lib/markdown_datafier.rb', line 32

def splash_page
  find_by_path("splash")
end

#split_meta_and_body(content) ⇒ Object



93
94
95
96
# File 'lib/markdown_datafier.rb', line 93

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



65
66
67
# File 'lib/markdown_datafier.rb', line 65

def strip_leading_slashes(shortname)
   shortname.match(/^\//) ? shortname.gsub(/^\//, "") : shortname
end

#sub_directoriesObject



51
52
53
# File 'lib/markdown_datafier.rb', line 51

def sub_directories
  sub_directories = Dir["*"].reject{|file| not File.directory?(file)}.map! {|sub_directory| "/" + sub_directory }
end

#underscores_to_dashes(shortname) ⇒ Object



85
86
87
# File 'lib/markdown_datafier.rb', line 85

def underscores_to_dashes(shortname)
  shortname.gsub(/_/, "-")
end