darthapo-stratus 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. data/Changelog +3 -0
  2. data/Manifest +87 -0
  3. data/Rakefile +82 -0
  4. data/Readme.markdown +15 -0
  5. data/bin/stratus +8 -0
  6. data/lib/stratus/cli.rb +206 -0
  7. data/lib/stratus/filters.rb +73 -0
  8. data/lib/stratus/generator/builder.rb +147 -0
  9. data/lib/stratus/generator/context.rb +39 -0
  10. data/lib/stratus/generator/renderer.rb +100 -0
  11. data/lib/stratus/generator/scanner.rb +75 -0
  12. data/lib/stratus/generator.rb +12 -0
  13. data/lib/stratus/resources/attachment.rb +41 -0
  14. data/lib/stratus/resources/base.rb +257 -0
  15. data/lib/stratus/resources/content.rb +17 -0
  16. data/lib/stratus/resources/hash_db.rb +128 -0
  17. data/lib/stratus/resources/layout.rb +18 -0
  18. data/lib/stratus/resources/template.rb +22 -0
  19. data/lib/stratus/resources.rb +75 -0
  20. data/lib/stratus/settings.rb +19 -0
  21. data/lib/stratus/tags/markdown.rb +19 -0
  22. data/lib/stratus/tags/textile.rb +19 -0
  23. data/lib/stratus/tags.rb +3 -0
  24. data/lib/stratus/tasks/app/boilerplate/StratusSite +25 -0
  25. data/lib/stratus/tasks/app/boilerplate/config/defaults/page.default.html +10 -0
  26. data/lib/stratus/tasks/app/boilerplate/config/defaults/post.default.html +10 -0
  27. data/lib/stratus/tasks/app/boilerplate/config/site.yaml +16 -0
  28. data/lib/stratus/tasks/app/boilerplate/content/pages/home/index.html +35 -0
  29. data/lib/stratus/tasks/app/boilerplate/themes/default/styles/main.css +12 -0
  30. data/lib/stratus/tasks/app/boilerplate/themes/default/templates/layouts/main.html +31 -0
  31. data/lib/stratus/tasks/app/boilerplate/themes/default/templates/objects/page.html +7 -0
  32. data/lib/stratus/tasks/app/boilerplate/themes/default/templates/objects/page.index.html +10 -0
  33. data/lib/stratus/tasks/app/boilerplate/themes/default/templates/objects/post.html +22 -0
  34. data/lib/stratus/tasks/app/boilerplate/themes/default/templates/objects/post.index.html +10 -0
  35. data/lib/stratus/tasks/app/site.rake +68 -0
  36. data/lib/stratus/tasks/shared/system.rake +16 -0
  37. data/lib/stratus/tasks/site/new.rake +108 -0
  38. data/lib/stratus/tasks/site/site.rake +21 -0
  39. data/lib/stratus/tasks/site/theme.rake +13 -0
  40. data/lib/stratus.rb +62 -0
  41. data/stratus.gemspec +31 -0
  42. data/test/fixtures/site/StratusSite +26 -0
  43. data/test/fixtures/site/config/defaults/article.default.html +7 -0
  44. data/test/fixtures/site/config/defaults/content.default.html +7 -0
  45. data/test/fixtures/site/config/defaults/page.default.html +10 -0
  46. data/test/fixtures/site/config/defaults/post.default.html +10 -0
  47. data/test/fixtures/site/config/site.yaml +25 -0
  48. data/test/fixtures/site/content/articles/001_im-the-title/index.html +10 -0
  49. data/test/fixtures/site/content/pages/about/index.html +7 -0
  50. data/test/fixtures/site/content/pages/home/index.html +37 -0
  51. data/test/fixtures/site/content/pages/projects/index.html +6 -0
  52. data/test/fixtures/site/content/posts/001_new-blog/dom.js +123 -0
  53. data/test/fixtures/site/content/posts/001_new-blog/index.html +24 -0
  54. data/test/fixtures/site/content/posts/001_new-blog/old.html +18 -0
  55. data/test/fixtures/site/content/posts/002_fun-for-the-whole-family/index.html +12 -0
  56. data/test/fixtures/site/content/posts/002_fun-for-the-whole-family/snippet_one.rb +5 -0
  57. data/test/fixtures/site/content/posts/003_life-is-quite-fun-really/index.html +12 -0
  58. data/test/fixtures/site/themes/default/scripts/code_highlighter.js +188 -0
  59. data/test/fixtures/site/themes/default/scripts/code_syntax.js +92 -0
  60. data/test/fixtures/site/themes/default/styles/code_syntax.css +68 -0
  61. data/test/fixtures/site/themes/default/styles/main.css +66 -0
  62. data/test/fixtures/site/themes/default/templates/layouts/main.html +36 -0
  63. data/test/fixtures/site/themes/default/templates/objects/article.html +7 -0
  64. data/test/fixtures/site/themes/default/templates/objects/article.index.html +8 -0
  65. data/test/fixtures/site/themes/default/templates/objects/feed.xml +25 -0
  66. data/test/fixtures/site/themes/default/templates/objects/page.html +7 -0
  67. data/test/fixtures/site/themes/default/templates/objects/page.index.html +10 -0
  68. data/test/fixtures/site/themes/default/templates/objects/post.html +22 -0
  69. data/test/fixtures/site/themes/default/templates/objects/post.index.html +10 -0
  70. data/test/fixtures/site/www/index.html +82 -0
  71. data/test/fixtures/site/www/pages/about/index.html +43 -0
  72. data/test/fixtures/site/www/pages/home/index.html +82 -0
  73. data/test/fixtures/site/www/pages/index.html +52 -0
  74. data/test/fixtures/site/www/pages/projects/index.html +43 -0
  75. data/test/fixtures/site/www/posts/fun-for-the-whole-family/index.html +66 -0
  76. data/test/fixtures/site/www/posts/fun-for-the-whole-family/snippet_one.rb +5 -0
  77. data/test/fixtures/site/www/posts/index.html +63 -0
  78. data/test/fixtures/site/www/posts/life-is-quite-fun-really/index.html +58 -0
  79. data/test/fixtures/site/www/posts/new-blog/dom.js +123 -0
  80. data/test/fixtures/site/www/posts/new-blog/index.html +192 -0
  81. data/test/fixtures/site/www/posts/new-blog/old.html +18 -0
  82. data/test/fixtures/site/www/theme/scripts/code_highlighter.js +188 -0
  83. data/test/fixtures/site/www/theme/scripts/code_syntax.js +92 -0
  84. data/test/fixtures/site/www/theme/styles/code_syntax.css +68 -0
  85. data/test/fixtures/site/www/theme/styles/main.css +66 -0
  86. data/test/fixtures/site/www/themes/default/styles/main.css +62 -0
  87. data/test/test_helper.rb +0 -0
  88. metadata +143 -0
@@ -0,0 +1,100 @@
1
+ module Stratus::Generator
2
+
3
+ # ==========
4
+ # = Liquid =
5
+ # ==========
6
+ class LiquidRenderer
7
+
8
+ attr_reader :context
9
+
10
+ def initialize
11
+ @context = LiquidContext.new
12
+ end
13
+
14
+ def render_content(content, path_to_root='../../')
15
+ in_context do
16
+ LiquidContext.path_to_root = path_to_root
17
+ context['this'] = content
18
+ template = template_for(content.collection_type, nil)
19
+ context['content'] = parser_for(template).render(context, [Stratus::Filters])
20
+ layout = layout_for(template)
21
+ parser_for(layout).render(context, [Stratus::Filters])
22
+ end
23
+ end
24
+
25
+ # Currently unused...
26
+ def render_content_fragment(content, state=nil)
27
+ in_context do
28
+ #LiquidContext.path_to_root = '../../' ???
29
+ context['this'] = content
30
+ template = template_for(content.collection_type, state)
31
+ parser_for(template).render(context, [Stratus::Filters])
32
+ end
33
+ end
34
+
35
+ def render_index_for(collection_type, state='index')
36
+ in_context do
37
+ LiquidContext.path_to_root = state.nil? ? '' : '../'
38
+ template = template_for(collection_type, state)
39
+ context['content'] = parser_for(template).render(context, [Stratus::Filters])
40
+ layout = layout_for(template)
41
+ parser_for(layout).render(context, [Stratus::Filters])
42
+ end
43
+ end
44
+
45
+ protected
46
+
47
+ # Allows all context changes to be scoped
48
+ def in_context(&block)
49
+ old_context = @context.clone
50
+ results = yield
51
+ @context = old_context
52
+ results
53
+ end
54
+
55
+ # loops through all the metadata fields and processes them through Liquid...
56
+ # def render_attributes(content)
57
+ # rendered_atts = {}
58
+ # context['this'] = content
59
+ # content.to_liquid.each do |key, value|
60
+ # if value.is_a? String
61
+ # puts "Rendering #{key} (#{value})"
62
+ # rendered_atts[key] = Liquid::Template.parse(value).render(context, [Stratus::Filters])
63
+ # end
64
+ # end
65
+ # rendered_atts
66
+ # end
67
+
68
+
69
+ def parser_for(template)
70
+ @parsers ||= Hash.new {|h,k|
71
+ h[k] = ::Liquid::Template.parse(k.body)
72
+ }
73
+ @parsers[template]
74
+ end
75
+
76
+ def template_for(collection_type, state=nil)
77
+ @templates ||= Hash.new {|h,k|
78
+ slugname = k.last.nil? ? "#{ k.first.singularize }.html" : "#{ k.first.singularize }.#{ k.last }.html"
79
+ # puts "Looking for #{ slugname }"
80
+ template = ::Stratus::Resources.templates(:first, :slug => slugname )
81
+ if template.nil?
82
+ slugname = k.last.nil? ? "_default.html" : "_default.#{ k.last }.html"
83
+ # puts "Not found, resorting to #{ slugname }"
84
+ template = ::Stratus::Resources.templates(:first, :slug => slugname )
85
+ end
86
+ h[k] = template
87
+ }
88
+ @templates[ [collection_type, state] ]
89
+ end
90
+
91
+ def layout_for(template)
92
+ @layouts ||= Hash.new {|h,k|
93
+ h[k] = ::Stratus::Resources.layouts(:first, :slug => "#{ k }.html")
94
+ }
95
+ @layouts[ template.metadata.fetch('layout', 'main') ]
96
+ end
97
+
98
+ end
99
+
100
+ end
@@ -0,0 +1,75 @@
1
+ require 'fileutils'
2
+
3
+ module Stratus::Generator
4
+
5
+ class Scanner
6
+ attr_reader :base, :content_path
7
+
8
+ def initialize(base_path)
9
+ @base = File.expand_path(base_path)
10
+ raise StandardError.new("Base path must be a directory.") unless File.directory?(base)
11
+
12
+ end
13
+
14
+ def sweep(clear_db=true)
15
+ @content_path = File.join(base, 'content')
16
+ resources.clear if clear_db
17
+ sweep_content
18
+ sweep_templates
19
+ sweep_layouts
20
+ end
21
+
22
+ private
23
+
24
+ def sweep_content
25
+ Dir[File.join(content_path, '*', '*')].each do |fullpath|
26
+ object_path = fullpath.gsub( "#{ content_path }/", '')
27
+ collection = object_path.split('/').first
28
+ object = Stratus::Resources::Content.new(fullpath, object_path)
29
+ resources.register_content( object )
30
+ sweep_attachments fullpath, object
31
+ end
32
+ # Set indices for all content...
33
+ Stratus::Resources.collection_types.each do |col_type|
34
+ count = 1
35
+ resources.all.find(:collection_type=>col_type, :sort_by=>:publish_date).each do |r|
36
+ r.index = count if r.index.nil?
37
+ count += 1
38
+ end
39
+ end
40
+ end
41
+
42
+ def sweep_attachments(from_path, content_object)
43
+ Dir[File.join(from_path, '*')].each do |filename|
44
+ next if File.basename(filename) == 'index.html'
45
+ resources.all << Stratus::Resources::Attachment.new(filename, content_object)
46
+ end
47
+ end
48
+
49
+ def sweep_templates
50
+ templates_path = File.join(base, 'themes', Stratus.setting('theme', 'default'), 'templates', 'objects')
51
+ Dir[File.join(templates_path, '*')].each do |fullpath|
52
+ object_path = fullpath.gsub( "#{ templates_path }/", '')
53
+ template = Stratus::Resources::Template.new(fullpath, "templates/#{object_path}")
54
+ resources.all << template
55
+ end
56
+ end
57
+
58
+ def sweep_layouts
59
+ layouts_path = File.join(base, 'themes', Stratus.setting('theme', 'default'), 'templates', 'layouts')
60
+ Dir[File.join(layouts_path, '*')].each do |fullpath|
61
+ object_path = fullpath.gsub( "#{ layouts_path }/", '')
62
+ layout = Stratus::Resources::Layout.new(fullpath, "layouts/#{object_path}")
63
+ resources.all << layout
64
+ end
65
+ end
66
+
67
+ def resources
68
+ Stratus::Resources
69
+ end
70
+
71
+ end
72
+
73
+
74
+
75
+ end
@@ -0,0 +1,12 @@
1
+ module Stratus::Generator
2
+
3
+ def self.config
4
+ ::Stratus.settings[:generator]
5
+ end
6
+
7
+ end
8
+
9
+ require 'stratus/generator/builder'
10
+ require 'stratus/generator/context'
11
+ require 'stratus/generator/renderer'
12
+ require 'stratus/generator/scanner'
@@ -0,0 +1,41 @@
1
+ module Stratus::Resources
2
+
3
+ class Attachment < Base
4
+
5
+ attr_reader :parent
6
+
7
+ def initialize(fullpath, content_object)
8
+ super(fullpath, content_object.content_path, :file)
9
+ @parent = content_object
10
+ @content_type = :attachment
11
+ @content_path = parent.content_path
12
+ @collection_type = parent.collection_type
13
+ end
14
+
15
+ def validate!
16
+ #raise StandardError.new("Posts must have a published-on date! #{content_path}") unless metadata.has_key?(:publish_on)
17
+ true
18
+ end
19
+
20
+ def full_path
21
+ "#{@parent.collection_type}/#{@parent.slug}/#{@slug}"
22
+ end
23
+
24
+ def output_path
25
+ File.join(Stratus.output_dir, full_path)
26
+ end
27
+
28
+ def is_homepage?
29
+ false
30
+ end
31
+
32
+
33
+ protected
34
+
35
+ def parse_file(filename)
36
+ load_filedata(filename)
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,257 @@
1
+ module Stratus::Resources
2
+ =begin
3
+
4
+ content_path:
5
+ - pages/about
6
+ - posts/20081105-my-slugline
7
+
8
+ content_type:
9
+ - content
10
+ - attachment
11
+
12
+ collection_type:
13
+ - pages
14
+ - posts
15
+
16
+ slug:
17
+ - about
18
+ - 20081105-my-slugline
19
+
20
+ =end
21
+
22
+ class Base
23
+ attr_reader :index, :slug, :content_path, :collection_type, :content_type, :source_path
24
+
25
+ def initialize(fullpath, content_path='', parse=:index)
26
+ @source_path = fullpath
27
+ @slug = File.basename(fullpath)
28
+ @content_type = :content
29
+ @content_path = content_path
30
+ @collection_type = content_path.split('/').first
31
+ @parse_mode = parse
32
+ @is_template = false
33
+ @index = if @slug =~ /^([\d]{3})_(.*)$/
34
+ @slug = $2
35
+ # @content_path = "#{collection_type}/#{slug}"
36
+ $1.to_i
37
+ else
38
+ nil
39
+ end
40
+
41
+ case parse
42
+ when :index
43
+ parse_file(File.join(source_path, 'index.html'))
44
+ when :file
45
+ parse_file(source_path)
46
+ when :template
47
+ @is_template = true
48
+ parse_file(source_path)
49
+ else
50
+ puts "Not parsing #{fullpath}"
51
+ end
52
+ end
53
+
54
+ def metadata
55
+ @meta ||= {} # Hash.new {|h,k| h[k] = []}
56
+ end
57
+
58
+ def full_path
59
+ is_homepage ? "" : "#{ collection_type }/#{ @slug }"
60
+ end
61
+
62
+ def output_path
63
+ File.join(Stratus.output_dir, @collection_type, @slug, 'index.html')
64
+ end
65
+
66
+ def attachments
67
+ Stratus::Resources.attachments( :content_path=>content_path )
68
+ end
69
+
70
+ def attachments_hash
71
+ returning( {} ) do |hash|
72
+ attachments.each do |att|
73
+ slug = att.slug.gsub(att.metadata[:ext], '')
74
+ hash[slug] = att
75
+ end
76
+ end
77
+ end
78
+
79
+
80
+ def method_missing(name, *args)
81
+ if name.to_s.ends_with? '='
82
+ metadata[name.to_s[0..-2].to_sym] = args.first
83
+ else
84
+ metadata[name]
85
+ end
86
+ end
87
+
88
+ def <=>( other )
89
+ return unless other.kind_of? Stratus::Resources::Base
90
+ self.content_path <=> other.content_path
91
+ end
92
+
93
+ def [](key)
94
+ if self.respond_to? key.to_sym
95
+ self.send(key.to_sym)
96
+ else
97
+ metadata[key]
98
+ end
99
+ end
100
+ def []=(key, value)
101
+ if self.respond_to? key.to_sym
102
+ self.send(key.to_sym)
103
+ else
104
+ metadata[key] = value
105
+ end
106
+ end
107
+
108
+ def is_homepage
109
+ Stratus.setting('homepage') == content_path
110
+ end
111
+
112
+ def next_content
113
+ Stratus::Resources.content(:first, :collection_type=>@collection_type, :index=>(@index + 1)) unless index.nil?
114
+ end
115
+
116
+ def prev_content
117
+ Stratus::Resources.content(:first, :collection_type=>@collection_type, :index=>(@index - 1)) unless index.nil?
118
+ end
119
+
120
+ def to_liquid
121
+ data = rendered_attributes({
122
+ 'slug' => @slug,
123
+ 'content_path' => @content_path,
124
+ 'collection_type' => @collection_type,
125
+ 'content_type' => @content_type,
126
+ 'full_path' => full_path,
127
+ 'source_path' => source_path,
128
+ 'attachments' => attachments,
129
+ 'attachment' => attachments_hash,
130
+ 'next' => next_content,
131
+ 'prev' => prev_content,
132
+ 'is_homepage' => is_homepage
133
+ })
134
+ end
135
+
136
+ # Subclasses need to override this to ensure all the proper metadata exists
137
+ def validate!
138
+ true
139
+ end
140
+
141
+ def fixup_meta
142
+ if metadata.has_key? :tags
143
+ tags = [metadata[:tags]].flatten.join(',')
144
+ tags = tags.split(',')
145
+ tags.map {|t| t.strip! }
146
+ metadata[:tags] = tags
147
+ end
148
+ if metadata.has_key? :category
149
+ cats = [metadata[:category]].flatten.join(',')
150
+ cats = cats.split(',')
151
+ cats.map {|t| t.strip! }
152
+ metadata[:categories] = cats
153
+ end
154
+ if metadata.has_key? :publish_date
155
+ metadata[:publish_date] = Chronic.parse( metadata[:publish_date] )
156
+ else
157
+ metadata[:publish_date] = Time.now
158
+ end
159
+ end
160
+
161
+ protected
162
+
163
+ def rendered_attributes(rendered_atts={})
164
+ context = Stratus::Generator::LiquidContext.new
165
+ context['this'] = rendered_atts
166
+ metadata.each do |key, value|
167
+ if value.is_a? String
168
+ rendered_atts[key.to_s] = Liquid::Template.parse(value).render(context, [Stratus::Filters])
169
+ else
170
+ rendered_atts[key.to_s] = value
171
+ end
172
+ end
173
+ rendered_atts
174
+ end
175
+
176
+ def parse_file(filename)
177
+ load_filedata(filename)
178
+ if File.size(filename) == 0
179
+ puts "!! File cannot be empty: #{@slug}"
180
+ # throw an error?
181
+ return
182
+ end
183
+ doc = Hpricot( open(filename) )
184
+
185
+ # Only grab root level meta tags for layouts and templates...
186
+ meta_q = (@is_template)? "/meta" : "//meta"
187
+
188
+ doc.search(meta_q).each do |meta_tag|
189
+ name = meta_tag[:name].gsub(' ', '_').gsub('-', '_').downcase.to_sym
190
+ # Coerce the meta data value if it's defined...
191
+ content = if meta_tag[:type]
192
+ case meta_tag[:type].downcase.to_sym
193
+ when :int
194
+ meta_tag[:content].to_i
195
+ when :integer
196
+ meta_tag[:content].to_i
197
+ when :float
198
+ meta_tag[:content].to_f
199
+ when :double
200
+ meta_tag[:content].to_f
201
+ when :array
202
+ meta_tag[:content].split(',')
203
+ when :list
204
+ meta_tag[:content].split(',')
205
+ when :date
206
+ Chronic.parse( meta_tag[:content] )
207
+ else
208
+ meta_tag[:content]
209
+ end
210
+ else
211
+ meta_tag[:content]
212
+ end
213
+ # Create an array of values for a meta key if it's the second time it's used
214
+ if metadata.has_key? name
215
+ metadata[name] = [metadata[name]] unless metadata[name].is_a? Array
216
+ metadata[name] << content
217
+ else
218
+ metadata[name] = content
219
+ end
220
+ end
221
+ # Now, remove the meta tags...
222
+ doc.search(meta_q).remove
223
+
224
+ if @parse_mode == :index # Content objects
225
+ doc.search("//title").each do |title_tag|
226
+ metadata[:title] = title_tag.inner_html
227
+ end
228
+ doc.search("//title").remove
229
+
230
+ # Whatever's left in the header, we'll pull out here...
231
+ metadata[:head] = doc.at("head").inner_html.squish
232
+
233
+ doc.search("//head").remove
234
+
235
+ # Grab the rest of the root level nodes and add their inner_html
236
+ # as metadata as well...
237
+ doc.search("/*").each do |tag|
238
+ metadata[tag.name.to_sym] = tag.inner_html unless tag.blank? or tag.is_a?( Hpricot::Text )
239
+ end
240
+
241
+ else # The rest
242
+ metadata[:body] = doc.to_html
243
+ end
244
+ end
245
+
246
+ def load_filedata(filename)
247
+ metadata[:filename] = File.basename(filename)
248
+ metadata[:ext] = File.extname(filename)
249
+ metadata[:mtime] = File.mtime(filename)
250
+ metadata[:ctime] = File.ctime(filename)
251
+ metadata[:size] = File.size(filename)
252
+ end
253
+
254
+
255
+ end
256
+
257
+ end
@@ -0,0 +1,17 @@
1
+ module Stratus::Resources
2
+ require 'pp'
3
+ class Content < Base
4
+
5
+ def validate!
6
+ raise StandardError.new("Posts must have a publish-date! #{content_path}") if (!metadata.has_key?(:publish_date) and collection_type == 'posts')
7
+ true
8
+ end
9
+
10
+ def full_path
11
+ parent_path = super
12
+ super.empty? ? "index.html" : "#{parent_path}/index.html"
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,128 @@
1
+ module Stratus::Resources
2
+
3
+ # A rudimentary "database" for holding resource objects and finding them.
4
+ # The database is held in a Ruby hash keyed by the collection/slug
5
+ # Based on Webby's DB class...
6
+ class HashDB
7
+ attr_reader :db
8
+
9
+ def initialize
10
+ @db = Hash.new {|h,k| h[k] = []}
11
+ end
12
+
13
+ def add( content )
14
+ ary = @db[content.content_path]
15
+
16
+ # make sure we don't duplicate contents
17
+ ary.delete content if ary.include? content
18
+ ary << content
19
+
20
+ content
21
+ end
22
+ alias :<< :add
23
+
24
+ def clear
25
+ @db.clear
26
+ self
27
+ end
28
+
29
+ def each( &block )
30
+ keys = @db.keys.sort
31
+ keys.each do |k|
32
+ @db[k].sort.each(&block)
33
+ end
34
+ self
35
+ end
36
+
37
+ # FIXME: Need to add support for:
38
+ # collection/*
39
+ # */*
40
+ def find( *args, &block )
41
+ opts = Hash === args.last ? args.pop : {}
42
+
43
+ limit = args.shift
44
+ limit = opts.delete(:limit) if opts.has_key?(:limit)
45
+ sort_by = opts.delete(:sort_by)
46
+ reverse = opts.delete(:reverse)
47
+
48
+ # figure out which collections to search through
49
+ search = self
50
+
51
+ # construct a search block if one was not supplied by the user
52
+ block ||= lambda do |content|
53
+ found = true
54
+ opts.each do |key, value|
55
+ if key == :content_path
56
+ found &&= content.__send__(key.to_sym).match( Regexp.new( value.gsub('*', '.*'), 'g' ) )
57
+ else
58
+ found &&= content.__send__(key.to_sym) == value
59
+ end
60
+ break if not found
61
+ end
62
+ found
63
+ end
64
+
65
+ # search through the collections for the desired content objects
66
+ ary = []
67
+ search.each do |content|
68
+ ary << content if block.call(content)
69
+ end
70
+
71
+ # sort the search results if the user gave an attribute to sort by
72
+ if sort_by
73
+ m = sort_by.to_sym
74
+ ary.delete_if {|p| p.__send__(m).nil?}
75
+ reverse ?
76
+ ary.sort! {|a,b| b.__send__(m) <=> a.__send__(m)} :
77
+ ary.sort! {|a,b| a.__send__(m) <=> b.__send__(m)}
78
+ end
79
+
80
+ # limit the search results
81
+ case limit
82
+ when :all, 'all'
83
+ ary
84
+ when Integer
85
+ ary.slice(0,limit)
86
+ else
87
+ ary.first
88
+ end
89
+ end
90
+
91
+ def siblings( content, opts = {} )
92
+ ary = @db[content.content_path].dup
93
+ ary.delete content
94
+ return ary unless opts.has_key? :sort_by
95
+
96
+ m = opts[:sort_by]
97
+ ary.sort! {|a,b| a.__send__(m) <=> b.__send__(m)}
98
+ ary.reverse! if opts[:reverse]
99
+ ary
100
+ end
101
+
102
+ end # class
103
+ end # module
104
+
105
+ # EOF
106
+
107
+ # Webby's LICENCE:
108
+ # MIT License
109
+ # Copyright (c) 2007 - 2008
110
+ #
111
+ # Permission is hereby granted, free of charge, to any person obtaining
112
+ # a copy of this software and associated documentation files (the
113
+ # 'Software'), to deal in the Software without restriction, including
114
+ # without limitation the rights to use, copy, modify, merge, publish,
115
+ # distribute, sub-license, and/or sell copies of the Software, and to
116
+ # permit persons to whom the Software is furnished to do so, subject to
117
+ # the following conditions:
118
+ #
119
+ # The above copyright notice and this permission notice shall be
120
+ # included in all copies or substantial portions of the Software.
121
+ #
122
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
123
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
124
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
125
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
126
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
127
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
128
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ module Stratus::Resources
2
+
3
+ class Layout < Template
4
+
5
+ def initialize(fullpath, content_path='')
6
+ super(fullpath, content_path)
7
+ @content_type = :layout
8
+ end
9
+
10
+ def validate!
11
+ metadata[:layout] = nil unless metadata.has_key? :layout
12
+ #raise StandardError.new("Posts must have a published-on date! #{content_path}") unless metadata.has_key?(:publish_on)
13
+ true
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,22 @@
1
+ module Stratus::Resources
2
+
3
+ class Template < Base
4
+
5
+ def initialize(fullpath, content_path='')
6
+ super(fullpath, content_path, :template)
7
+ @content_type = :template
8
+ end
9
+
10
+ def validate!
11
+ metadata[:layout] = 'main' unless metadata.has_key? :layout
12
+ #raise StandardError.new("Posts must have a published-on date! #{content_path}") unless metadata.has_key?(:publish_on)
13
+ true
14
+ end
15
+
16
+ def full_path
17
+ content_path
18
+ end
19
+
20
+ end
21
+
22
+ end