module MarkdownDatafier

Constants

VERSION

Attributes

content_path[RW]

Public Class Methods

new(attributes) click to toggle source
# File lib/markdown_datafier.rb, line 14
def initialize(attributes)
  @content_path = attributes[:content_path]
end
root() click to toggle source
# File lib/markdown_datafier.rb, line 10
def self.root
  File.expand_path '../..', __FILE__
end

Public Instance Methods

append_file_extention(path) click to toggle source
# File lib/markdown_datafier.rb, line 81
def append_file_extention(path)
  path + ".mdown"
end
collect(directory=nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_page() click to toggle source
# File lib/markdown_datafier.rb, line 28
def home_page
  find_by_path("index")
end
indexes_for_sections(directory=nil) click to toggle source
# 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
is_directory?(path) click to toggle source
# File lib/markdown_datafier.rb, line 73
def is_directory?(path)
  File.directory?(path)
end
parse_file_content(content) click to toggle source
# 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) click to toggle source
# File lib/markdown_datafier.rb, line 98
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) click to toggle source
# 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) click to toggle source
# 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_page() click to toggle source
# File lib/markdown_datafier.rb, line 32
def splash_page
  find_by_path("splash")
end
split_meta_and_body(content) click to toggle source
# 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) click to toggle source
# File lib/markdown_datafier.rb, line 65
def strip_leading_slashes(shortname)
   shortname.match(/^\//) ? shortname.gsub(/^\//, "") : shortname
end
sub_directories() click to toggle source
# 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) click to toggle source
# File lib/markdown_datafier.rb, line 85
def underscores_to_dashes(shortname)
  shortname.gsub(/_/, "-")
end