Module: PostModern::Read::ClassMethods

Defined in:
lib/post-modern/read.rb

Instance Method Summary collapse

Instance Method Details

#extract_format!(ext) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/post-modern/read.rb', line 57

def extract_format!(ext)
  case ext
  when "md", "markdown", "mdown", "mkdown"
    "markdown"
  else
    ext
  end
end

#glob(path, &block) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/post-modern/read.rb', line 14

def glob(path, &block)
  Dir[path].collect do |file|
    next if File.directory?(file) || file =~ /\.$/
    post = Post.extract_from_file!(file)
    yield post if block_given?
    post
  end.compact
end

#read(file, locals = {}) ⇒ Object



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
# File 'lib/post-modern/read.rb', line 23

def read(file, locals = {})
  match, categories, date, slug, ext = *file.gsub("#{root_dir}/", "").match(/^(.+\/)*(?:(\d+-\d+-\d+)-)?(.*)(\.[^.]+)$/)
  content                            = IO.read(file)
  
  unless locals[:filters].present?
    locals[:filters] = [:haml, :highlight, :pretty]
  else
    locals[:filters] = Array(locals[:filters])
  end
  
  if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
    data                             = YAML.load($1).symbolize_keys
    content                          = content[($1.size + $2.size)..-1]
  else
    data                             = {}
  end
  
  data[:published_at]              ||= date if date.present?
  data[:format]                      = extract_format! ext.split(".").last
  data[:title]                     ||= slug.titleize if slug.present?
  data[:tags]                        = data[:tags].present? ? data[:tags].split(/,\s*/) : []
  data[:keywords]                    = data[:keywords].present? ? data[:keywords].split(/,\s*/) : []
  data[:slug]                      ||= slug
  data[:file]                        = file.gsub("#{root_dir}/", "")
  [:published_at, :edited_at].each do |date|
    data[date]                       = Time.parse(data[date].to_s) if data[date].present?
  end
  data[:categories]                  = categories.split("/").select(&:present?) if categories
  data[:published]                   = false unless data[:published].present?
  data[:content]                     = render(content, data.merge(locals))

  data
end

#root_dirObject



6
7
8
# File 'lib/post-modern/read.rb', line 6

def root_dir
  @root_dir ||= Rails.root.join("app/documents")
end

#root_dir=(value) ⇒ Object



10
11
12
# File 'lib/post-modern/read.rb', line 10

def root_dir=(value)
  @root_dir = value
end