Class: Stack::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/stack/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, target, parent = nil) ⇒ Generator

Returns a new instance of Generator.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/stack/generator.rb', line 15

def initialize(source, target, parent = nil)
  self.source = source
  self.target = target
  self.parent = parent
  
  self.remove_first = false
  
  self.layouts = (parent) ? parent.layouts.dup : { }
  self.articles = (parent) ? parent.articles.dup : { }
  
  self.children = [ ]

  # read layouts
  # read pages
  # write pages
  
  process!
end

Instance Attribute Details

#articlesObject

Returns the value of attribute articles.



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

def articles
  @articles
end

#childrenObject

Returns the value of attribute children.



6
7
8
# File 'lib/stack/generator.rb', line 6

def children
  @children
end

#layoutsObject

Returns the value of attribute layouts.



7
8
9
# File 'lib/stack/generator.rb', line 7

def layouts
  @layouts
end

#pagesObject

Returns the value of attribute pages.



9
10
11
# File 'lib/stack/generator.rb', line 9

def pages
  @pages
end

#parentObject

Returns the value of attribute parent.



3
4
5
# File 'lib/stack/generator.rb', line 3

def parent
  @parent
end

#processed_atObject

Returns the value of attribute processed_at.



13
14
15
# File 'lib/stack/generator.rb', line 13

def processed_at
  @processed_at
end

#remove_firstObject

Returns the value of attribute remove_first.



11
12
13
# File 'lib/stack/generator.rb', line 11

def remove_first
  @remove_first
end

#sourceObject

Returns the value of attribute source.



4
5
6
# File 'lib/stack/generator.rb', line 4

def source
  @source
end

#targetObject

Returns the value of attribute target.



4
5
6
# File 'lib/stack/generator.rb', line 4

def target
  @target
end

#transformed_atObject

Returns the value of attribute transformed_at.



13
14
15
# File 'lib/stack/generator.rb', line 13

def transformed_at
  @transformed_at
end

Instance Method Details

#articles_payloadObject



129
130
131
132
133
134
135
136
137
138
# File 'lib/stack/generator.rb', line 129

def articles_payload
  a = Array.new
  
  self.articles.each { |name, article|
    article.inspect
    a.push(article.payload)
  }
  
  a
end

#process!Object



34
35
36
37
38
39
40
41
42
# File 'lib/stack/generator.rb', line 34

def process!
  self.processed_at = Time.now
  
  read_articles
  read_layouts
  read_pages
  
  read_children
end

#read_articlesObject



50
51
52
53
54
# File 'lib/stack/generator.rb', line 50

def read_articles
  if File.exists?(File.join(self.source, "_articles"))
    @articles = @articles.merge(read_pages_from_directory("_articles", Stack::Templates::Article))
  end
end

#read_childrenObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/stack/generator.rb', line 60

def read_children
  entries = Dir.entries(self.source)
  directories = entries.select { |e| File.directory?(File.join(self.source, e)) }
  directories = directories.reject { |d| d[0..0]=~/\.|_/ or d[-1..-1]=="~" }
  directories.each do |dir|
    gen = Stack::Generator.new(File.join(self.source, dir), File.join(self.target, dir), self)
    #gen.process!
    self.children.push(gen)
  end
end

#read_layoutsObject



44
45
46
47
48
# File 'lib/stack/generator.rb', line 44

def read_layouts
  if File.exists?(File.join(self.source, "_layouts"))
    @layouts = @layouts.merge(read_pages_from_directory("_layouts", Stack::Templates::Layout))
  end
end

#read_pagesObject



56
57
58
# File 'lib/stack/generator.rb', line 56

def read_pages
  @pages = read_pages_from_directory(self.source, Stack::Templates::Page)
end

#to_hashObject



120
121
122
123
124
125
126
127
# File 'lib/stack/generator.rb', line 120

def to_hash
  {
    :processed_at => self.processed_at,
    :transformed_at => self.transformed_at,
    :time => Time.now,
    :articles => self.articles_payload
  }
end

#transform!(match_only = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/stack/generator.rb', line 71

def transform!(match_only = nil)
  if self.remove_first
    FileUtils.rm_r self.target
  end
  
  self.transformed_at = Time.now
  
  self.articles.each do |name, article|
    if (match_only)
      match_only.each { |m|
        if File.dirname(m.path) == File.dirname(article.path) && File.basename(m.path) == article.basename
          article.write!
        end
      }
    else
      article.write!
    end
  end
  
  self.pages.each do |name, page|
    if (match_only)
      match_only.each { |m|
        if File.dirname(m.path) == File.dirname(page.path) && File.basename(m.path) == page.basename
          page.write!
        end
      }
    else
      page.write!
    end
  end
  
  transform_assets
  
  self.children.each { |c| c.transform!(match_only) }
end

#transform_assetsObject



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/stack/generator.rb', line 107

def transform_assets
  files = Dir.entries(self.source)
  files = files.reject { |e| File.directory?(File.join(self.source, e)) }
  files = files.reject { |e| e[0..0]=~/\.|_/ or e[-1..-1]=="~" }
  files = files.reject { |e| (%w(.yaml .yml)+Stack::EXTENSIONS).include?(File.extname(e)) } # Exception cases

  FileUtils.mkdir_p(self.target)
  
  files.each do |f|
    FileUtils.cp(File.join(self.source, File.basename(f)), File.join(self.target, File.basename(f)))
  end
end