Class: Broadway::Processor::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/broadway/processors/post.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(post, site, options = {}) ⇒ Post

Returns a new instance of Post.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/broadway/processors/post.rb', line 89

def initialize(post, site, options = {})
  post.site = site
  post.file = Broadway::File.new(site, options)
  post.kind = "page" if post.file.slug == "index"
  post.slug = Broadway::Slug.new(post.file.slug, post)
  post.data = post.file.header
  post.title = post.data["title"].blank? ? post.slug.titleize : post.data["title"]
  if post.title.downcase == "index"
    post.title = ::File.basename(post.file.directory).titleize
  end
  unless options[:recursive] == false
    post.children = children(post.file.directory, site) if post.kind == "page"
  end
end

Class Method Details

.build(parent, site) ⇒ Object

RULES: Pages are only index.textile files, or static .html files (TODO) Posts are leaf nodes (name.textile files) Categories are the directoryectory name split plus anything extra defined Pages and Posts can also be obtained via index.xml files



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/broadway/processors/post.rb', line 11

def build(parent, site)
  result = Dir.entries(parent).collect do |path|
    # removes junk, leaves us with .xml, .textile, .html
    next if skip?(path)
    name  = File.file_name(path)
    next if name == "index"
    ext   = File.file_ext(path)
    file  = ::File.join(parent, path)
    if %w(textile markdown html).include?(ext)
      ::Broadway::Post.new(site, :file => file)
    elsif page = Post.page_exists?(file)
      ::Broadway::Post.new(site, :file => page)
    else
      nil
    end
  end.compact
  
  result.sort!
  
  # finally, we have set all the initial variables on the
  # pages and posts we need, now we can process them to find
  # the content and generate the urls
  result.each do |post|
    site.posts << post
    post.categories.each { |c| site.categories[c] << post }
    post.tags.each { |c| site.tags[c] << post }
  end
  
  result
end

.page_exists?(file) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/broadway/processors/post.rb', line 42

def page_exists?(file)
  %w(markdown textile txt).each do |extension|
    page = ::File.join(file, "index.#{extension}")
    return page if ::File.exists?(page)
  end
  nil
end

.post_attr_hash(post_attr) ⇒ Object

Constructs a hash map of Posts indexed by the specified Post attribute

Returns => [<Post>]



79
80
81
82
83
84
85
86
# File 'lib/broadway/processors/post.rb', line 79

def post_attr_hash(post_attr)
  # Build a hash map based on the specified post attribute ( post attr => array of posts )
  # then sort each array in reverse order
  hash = Hash.new { |hash, key| hash[key] = Array.new }
  self.posts.each { |p| p.send(post_attr.to_sym).each { |t| hash[t] << p } }
  hash.values.map { |sortme| sortme.sort! { |a, b| b <=> a} }
  return hash
end

.skip?(path) ⇒ Boolean

Filter out any files/directoryectories that are hidden or backup files (start with “.” or “#” or end with “~”), or contain site content (start with “_”), or are excluded in the site settingsuration, unless they are web server files such as ‘.htaccess’

Returns:

  • (Boolean)


54
55
56
57
58
59
# File 'lib/broadway/processors/post.rb', line 54

def skip?(path)
  #return true if ::File.directory?(path)
  file = ::File.basename(path)
  return true if ['.htaccess'].include?(file)
  ['.', '_', '#'].include?(file[0..0]) || file[-1..-1] == '~'# || self.exclude.include?(file)
end

.sort!Object

sort all pages and posts determined by index.xml file (and later, for dates)



63
64
65
# File 'lib/broadway/processors/post.rb', line 63

def sort!
  sort_content(:post, site.posts)
end

.sort_content(type, contents) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/broadway/processors/post.rb', line 67

def sort_content(type, contents)
  method = type.to_s.pluralize
  unnumbered = site.send(method).select { |content| content.position.nil? }
  start_index = site.send(method).length - unnumbered.length
  unnumbered.each_with_index { |content, index| content.position = start_index + index }
  site.send(method).sort!
  site.send(method)
end

Instance Method Details

#children(path, site) ⇒ Object



104
105
106
# File 'lib/broadway/processors/post.rb', line 104

def children(path, site)
  Post.build(path, site) if path
end

#read(attribute = nil) ⇒ Object



108
109
110
# File 'lib/broadway/processors/post.rb', line 108

def read(attribute = nil)
  post.file.read(attribute)
end

#write(to) ⇒ Object



112
113
114
# File 'lib/broadway/processors/post.rb', line 112

def write(to)
  post.file.write(to)
end