Class: Awestruct::Extensions::Posts

Inherits:
Object
  • Object
show all
Defined in:
lib/awestruct/extensions/posts.rb

Defined Under Namespace

Classes: Archive

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_prefix = '', assign_to = :posts, archive_template = nil, archive_path = nil, opts = {}) ⇒ Posts

Returns a new instance of Posts.



7
8
9
10
11
12
13
14
# File 'lib/awestruct/extensions/posts.rb', line 7

def initialize(path_prefix='', assign_to=:posts, archive_template=nil, archive_path=nil, opts={})
  @archive_template = archive_template
  @archive_path     = archive_path
  @path_prefix      = path_prefix
  @assign_to        = assign_to
  @default_layout   = opts[:default_layout] || 'post'
  @wp_compat        = opts.has_key?(:wp_compat) ? opts[:wp_compat] : false
end

Instance Attribute Details

#archive_pathObject

Returns the value of attribute archive_path.



5
6
7
# File 'lib/awestruct/extensions/posts.rb', line 5

def archive_path
  @archive_path
end

#archive_templateObject

Returns the value of attribute archive_template.



5
6
7
# File 'lib/awestruct/extensions/posts.rb', line 5

def archive_template
  @archive_template
end

#assign_toObject

Returns the value of attribute assign_to.



5
6
7
# File 'lib/awestruct/extensions/posts.rb', line 5

def assign_to
  @assign_to
end

#default_layoutObject

Returns the value of attribute default_layout.



5
6
7
# File 'lib/awestruct/extensions/posts.rb', line 5

def default_layout
  @default_layout
end

#path_prefixObject

Returns the value of attribute path_prefix.



5
6
7
# File 'lib/awestruct/extensions/posts.rb', line 5

def path_prefix
  @path_prefix
end

#wp_compatObject

Returns the value of attribute wp_compat.



5
6
7
# File 'lib/awestruct/extensions/posts.rb', line 5

def wp_compat
  @wp_compat
end

Instance Method Details

#execute(site) ⇒ Object



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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/awestruct/extensions/posts.rb', line 16

def execute(site)
  posts   = []
  archive = Archive.new

  site.pages.each do |page|
    year, month, day, slug = nil

    if ( page.relative_source_path =~ /^#{@path_prefix}\// )
      # check for a date inside the page first
      if (page.date?)
        page.relative_source_path =~ /^#{@path_prefix}\/(.*)\..*$/
        date = page.date
        unless date.kind_of? DateTime
          if date.kind_of? String
            date = DateTime.parse date
          elsif date.kind_of? Date
            date = DateTime.new(date.year, date.month, date.day)
          elsif date.kind_of? Time
            date = date.to_datetime
          end
        end
        year = date.year
        month = sprintf( "%02d", date.month )
        day = sprintf( "%02d", date.day )
        page.date = date
        slug = $1
        if ( page.relative_source_path =~ /^#{@path_prefix}\/(2[0-9]{3})-([01][0-9])-([0123][0-9])-([^.]+)\..*$/ )
          slug = $4
        end
      elsif ( page.relative_source_path =~ /^#{@path_prefix}\/(2[0-9]{3})-([01][0-9])-([0123][0-9])-([^.]+)\..*$/ )
        year  = $1
        month = $2
        day   = $3
        slug  = $4
        page.date = DateTime.new( year.to_i, month.to_i, day.to_i )
      end

      # if a date was found create a post
      if( year and month and day)
        page.layout ||= @default_layout if @default_layout
        page.slug ||= slug
        context = page.create_context
        if (@wp_compat)
          page.output_path = "#{@path_prefix}/#{year}/#{month}/#{page.slug}.html"
        else
          page.output_path = "#{@path_prefix}/#{year}/#{month}/#{day}/#{page.slug}.html"
        end
        posts << page
      end
    end
  end

  posts = posts.sort_by{|each| [each.date, each.sequence || 0, File.mtime( each.source_path ), each.slug ] }.reverse

  last = nil
  singular = @assign_to.to_s.singularize
  posts.each do |e|
    if ( last != nil )
       e.send( "next_#{singular}=", last )
       last.send( "previous_#{singular}=", e )
    end
    last = e
    archive << e
  end
  site.pages.concat( archive.generate_pages( site.engine, archive_template, archive_path ) ) if (archive_template && archive_path)
  site.send( "#{@assign_to}=", posts )
  site.send( "#{@assign_to}_archive=", archive )

end