gumdrop 0.8.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (201) hide show
  1. data/.gitignore +3 -0
  2. data/ChangeLog.md +13 -0
  3. data/Gemfile +6 -2
  4. data/Gemfile.lock +43 -3
  5. data/Rakefile +37 -21
  6. data/Readme.md +60 -158
  7. data/bin/gumdrop +2 -1
  8. data/gumdrop.gemspec +11 -4
  9. data/lib/gumdrop/builder.rb +164 -0
  10. data/lib/gumdrop/cli/external.rb +14 -9
  11. data/lib/gumdrop/cli/internal.rb +54 -22
  12. data/lib/gumdrop/cli.rb +21 -0
  13. data/lib/gumdrop/content.rb +195 -132
  14. data/lib/gumdrop/data.rb +185 -0
  15. data/lib/gumdrop/data_providers/csv.rb +18 -0
  16. data/lib/gumdrop/data_providers/pstore.rb +25 -0
  17. data/lib/gumdrop/data_providers/sqlite.rb +43 -0
  18. data/lib/gumdrop/data_providers/xml.rb +15 -0
  19. data/lib/gumdrop/data_providers/yaml_and_json.rb +18 -0
  20. data/lib/gumdrop/data_providers/yamldb.rb +18 -0
  21. data/lib/gumdrop/data_providers/yamldoc.rb +12 -0
  22. data/lib/gumdrop/generator.rb +118 -94
  23. data/lib/gumdrop/renderer.rb +272 -0
  24. data/lib/gumdrop/server.rb +36 -27
  25. data/lib/gumdrop/site.rb +283 -338
  26. data/lib/gumdrop/support/compressor.rb +52 -0
  27. data/lib/gumdrop/support/sprockets.rb +14 -23
  28. data/lib/gumdrop/support/stitch.rb +25 -14
  29. data/lib/gumdrop/support/yaml_doc_template.rb +29 -0
  30. data/lib/gumdrop/util/configurable.rb +44 -0
  31. data/lib/gumdrop/util/core_ex.rb +84 -0
  32. data/lib/gumdrop/util/eventable.rb +35 -0
  33. data/lib/gumdrop/util/hash_object.rb +70 -0
  34. data/lib/gumdrop/util/loggable.rb +44 -0
  35. data/lib/gumdrop/util/pager.rb +51 -0
  36. data/lib/gumdrop/{support → util}/proxy_handler.rb +7 -6
  37. data/lib/gumdrop/util/scanner.rb +47 -0
  38. data/lib/gumdrop/util/site_access.rb +16 -0
  39. data/lib/gumdrop/util/view_helpers.rb +50 -0
  40. data/lib/gumdrop/util/yaml_doc.rb +51 -0
  41. data/lib/gumdrop/version.rb +2 -2
  42. data/lib/gumdrop.rb +40 -77
  43. data/specs/content_spec.rb +83 -73
  44. data/specs/fixtures/expected/data-access/from-csv.html +4 -0
  45. data/specs/fixtures/expected/data-access/from-json.html +6 -0
  46. data/specs/fixtures/expected/data-access/from-sqlite.html +5 -0
  47. data/specs/fixtures/expected/data-access/from-xml.html +4 -0
  48. data/specs/fixtures/expected/data-access/from-yaml.html +5 -0
  49. data/specs/fixtures/expected/data-access/from-yamldb.html +5 -0
  50. data/specs/fixtures/expected/data-access/from-yamldoc.html +2 -0
  51. data/specs/fixtures/expected/gen-with-block/as-file.html +1 -0
  52. data/specs/fixtures/expected/gen-with-block/as-plain.html +1 -0
  53. data/specs/fixtures/expected/gen-with-block/layout-nested.html +1 -0
  54. data/specs/fixtures/expected/gen-with-block/layout-nil.html +1 -0
  55. data/specs/fixtures/expected/gen-with-block/layout-wrap.html +1 -0
  56. data/specs/fixtures/expected/gen-with-inline-render/0-as-file.html +4 -0
  57. data/specs/fixtures/expected/gen-with-inline-render/0-as-plain.html +4 -0
  58. data/specs/fixtures/expected/gen-with-inline-render/0-layout-nil.html +4 -0
  59. data/specs/fixtures/expected/gen-with-inline-render/0-layout-sub.html +4 -0
  60. data/specs/fixtures/expected/gen-with-inline-render/0-layout-wrap.html +4 -0
  61. data/specs/fixtures/expected/gen-with-inline-render/1-as-file.html +4 -0
  62. data/specs/fixtures/expected/gen-with-inline-render/1-as-plain.html +4 -0
  63. data/specs/fixtures/expected/gen-with-inline-render/1-layout-nil.html +4 -0
  64. data/specs/fixtures/expected/gen-with-inline-render/1-layout-sub.html +4 -0
  65. data/specs/fixtures/expected/gen-with-inline-render/1-layout-wrap.html +4 -0
  66. data/specs/fixtures/expected/goodbye.html +1 -0
  67. data/specs/fixtures/expected/hello.html +1 -0
  68. data/specs/fixtures/expected/image.png +0 -0
  69. data/specs/fixtures/expected/index.html +47 -0
  70. data/specs/fixtures/expected/js/sprockets-app.js +7 -0
  71. data/specs/fixtures/expected/js/stitch-app.js +65 -0
  72. data/specs/fixtures/expected/js/stitch-app.min.js +5 -0
  73. data/specs/fixtures/expected/js/stitch-app.min.src.js +65 -0
  74. data/specs/fixtures/expected/js/straight.js +8 -0
  75. data/specs/fixtures/expected/js/test-coffee.js +5 -0
  76. data/specs/fixtures/expected/pages/docs/force-abs.html +2 -0
  77. data/specs/fixtures/expected/pages/docs/index.html +2 -0
  78. data/specs/fixtures/expected/partials/hoisted-data.html +3 -0
  79. data/specs/fixtures/expected/partials/nested.html +7 -0
  80. data/specs/fixtures/expected/partials/params.html +1 -0
  81. data/specs/fixtures/expected/partials/with-layouts.html +1 -0
  82. data/specs/fixtures/expected/robots.txt +1 -0
  83. data/specs/fixtures/expected/test-multi-procs.html +4 -0
  84. data/specs/fixtures/expected/test-nested-layout.html +1 -0
  85. data/specs/fixtures/expected/test-no-layout.html +2 -0
  86. data/specs/fixtures/expected/test-yamldoc.html +1 -0
  87. data/specs/fixtures/expected/test.html +2 -0
  88. data/{templates/backbone → specs/fixtures/source}/Gemfile +8 -4
  89. data/specs/fixtures/source/Gemfile.lock +84 -0
  90. data/specs/fixtures/source/Gumdrop +60 -9
  91. data/specs/fixtures/source/data/csv_data.csv +2 -0
  92. data/specs/fixtures/source/data/json_data.json +13 -0
  93. data/specs/fixtures/source/data/sql_data.sqlite +0 -0
  94. data/specs/fixtures/source/data/xml_data.xml +4 -0
  95. data/specs/fixtures/source/data/yaml_db.yamldb +9 -0
  96. data/specs/fixtures/source/data/yaml_items.yaml +2 -0
  97. data/specs/fixtures/source/data/yamldoc.yamldoc +4 -0
  98. data/specs/fixtures/source/data-access/from-csv.html.erb +4 -0
  99. data/specs/fixtures/source/data-access/from-json.html.erb +6 -0
  100. data/specs/fixtures/source/data-access/from-sqlite.html.erb +5 -0
  101. data/specs/fixtures/source/data-access/from-xml.html.erb +5 -0
  102. data/specs/fixtures/source/data-access/from-yaml.html.erb +5 -0
  103. data/specs/fixtures/source/data-access/from-yamldb.html.erb +5 -0
  104. data/specs/fixtures/source/data-access/from-yamldoc.html.erb +2 -0
  105. data/specs/fixtures/source/gen-with-inline-render.generator +7 -0
  106. data/specs/fixtures/source/goodbye.html.erb +1 -0
  107. data/specs/fixtures/source/hello.html +1 -0
  108. data/specs/fixtures/source/image.png +0 -0
  109. data/specs/fixtures/source/index.html.erb +6 -0
  110. data/specs/fixtures/source/js/sprockets/app.js +2 -0
  111. data/specs/fixtures/source/js/sprockets/view.js.coffee +1 -0
  112. data/specs/fixtures/source/js/stitch/app.js +1 -0
  113. data/specs/fixtures/source/js/stitch/view.js.coffee +1 -0
  114. data/specs/fixtures/source/js/straight.js +8 -0
  115. data/specs/fixtures/source/js/test-coffee.js.coffee +1 -0
  116. data/specs/fixtures/source/layouts/site.layout.erb +1 -0
  117. data/specs/fixtures/source/layouts/sub.layout.erb +1 -0
  118. data/specs/fixtures/source/layouts/wrap.layout.erb +1 -0
  119. data/specs/fixtures/source/pages/_users-nested.html.erb +7 -0
  120. data/specs/fixtures/source/pages/docs/force-abs.html.erb +2 -0
  121. data/specs/fixtures/source/pages/docs/index.html.erb +2 -0
  122. data/specs/fixtures/source/partials/_post_entry.html.erb +4 -0
  123. data/specs/fixtures/source/partials/_provides_name.html.erb +1 -0
  124. data/specs/fixtures/source/partials/_user-params.html.erb +1 -0
  125. data/specs/fixtures/source/partials/_user.html.erb +1 -0
  126. data/specs/fixtures/source/partials/_user_sets_layout.html.erb +1 -0
  127. data/specs/fixtures/source/partials/hoisted-data.html.erb +6 -0
  128. data/specs/fixtures/source/partials/nested.html.erb +1 -0
  129. data/specs/fixtures/source/partials/params.html.erb +1 -0
  130. data/specs/fixtures/source/partials/with-layouts.html.erb +1 -0
  131. data/specs/fixtures/source/test-multi-procs.html.markdown.erb +3 -0
  132. data/specs/fixtures/source/test-nested-layout.html.erb +1 -0
  133. data/specs/fixtures/source/test-no-layout.html.erb +2 -0
  134. data/specs/fixtures/source/test-yamldoc.html.erb.yamldoc +4 -0
  135. data/specs/fixtures/source/test.html.erb +2 -0
  136. data/specs/hash_object_spec.rb +53 -8
  137. data/specs/render_spec.rb +10 -0
  138. data/specs/site_spec.rb +79 -0
  139. data/specs/spec_helper.rb +59 -0
  140. data/templates/blank/Gemfile +0 -4
  141. data/templates/blank/Gumdrop +180 -113
  142. data/templates/default/Gemfile +3 -8
  143. data/templates/default/Gumdrop +166 -95
  144. data/templates/default/ReadMe.md +14 -0
  145. data/templates/default/config.ru +1 -0
  146. data/templates/default/data/news/20120703.yamldoc +14 -0
  147. data/templates/default/data/news/20120704.yamldoc +8 -0
  148. data/templates/{backbone/source/feed.xml.builder.txt → default/source/feed.xml.builder} +4 -4
  149. data/templates/default/source/index.html.erb +10 -1
  150. data/templates/default/source/theme/layout/_sidebar.html +1 -0
  151. data/templates/{backbone/source/theme/templates/site.template.slim → default/source/theme/layout/site.layout.slim} +3 -3
  152. metadata +212 -63
  153. data/lib/gumdrop/context.rb +0 -135
  154. data/lib/gumdrop/data_manager.rb +0 -214
  155. data/lib/gumdrop/support/base_packager.rb +0 -60
  156. data/lib/gumdrop/support/callbacks.rb +0 -30
  157. data/lib/gumdrop/support/hash_object.rb +0 -22
  158. data/lib/gumdrop/view_helpers.rb +0 -25
  159. data/notes.md +0 -347
  160. data/specs/deferred_loader_spec.rb +0 -31
  161. data/specs/fixtures/expected/posts/post1.html +0 -1
  162. data/specs/fixtures/expected/posts/post1.js +0 -14
  163. data/specs/fixtures/expected/posts/post2.html +0 -5
  164. data/specs/fixtures/expected/sub/sub/sub/test.html +0 -5
  165. data/specs/fixtures/expected/sub/sub/sub/test2.html +0 -5
  166. data/specs/fixtures/expected/test.js +0 -14
  167. data/specs/fixtures/source/posts/post1.html.slim +0 -6
  168. data/specs/fixtures/source/posts/post1.js.coffee +0 -3
  169. data/specs/fixtures/source/posts/post2.html.erb +0 -5
  170. data/specs/fixtures/source/sub/sub/sub/test.html +0 -5
  171. data/specs/fixtures/source/sub/sub/sub/test2.html.erb +0 -5
  172. data/specs/fixtures/source/test.js.coffee +0 -4
  173. data/specs/fixtures/source/test.js.erb.coffee +0 -3
  174. data/templates/backbone/Gumdrop +0 -123
  175. data/templates/backbone/Rakefile +0 -38
  176. data/templates/backbone/app/app.js.coffee +0 -49
  177. data/templates/backbone/app/init.js.coffee +0 -10
  178. data/templates/backbone/app/models/.gitkeep +0 -0
  179. data/templates/backbone/app/utils/index.js.coffee +0 -27
  180. data/templates/backbone/app/views/home.js.coffee +0 -17
  181. data/templates/backbone/app/views/styles/home.scss +0 -8
  182. data/templates/backbone/app/views/templates/home.mustache +0 -2
  183. data/templates/backbone/config.ru +0 -20
  184. data/templates/backbone/lib/all.js.coffee +0 -18
  185. data/templates/backbone/lib/backbone.js +0 -1158
  186. data/templates/backbone/lib/hogan.js +0 -509
  187. data/templates/backbone/lib/jquery.js +0 -9266
  188. data/templates/backbone/lib/underscore.js +0 -981
  189. data/templates/backbone/powrc +0 -2
  190. data/templates/backbone/source/default.htaccess.erb +0 -27
  191. data/templates/backbone/source/favicon.ico +0 -0
  192. data/templates/backbone/source/index.html.erb +0 -2
  193. data/templates/backbone/source/theme/screen.css.sass +0 -9
  194. data/templates/backbone/source/theme/scripts/app.js.coffee +0 -4
  195. data/templates/backbone/source/theme/styles/_tools.scss +0 -434
  196. data/templates/backbone/source/theme/templates/app.template.slim +0 -14
  197. data/templates/default/Rakefile +0 -38
  198. data/templates/default/powrc +0 -2
  199. data/templates/default/source/default.htaccess.erb +0 -27
  200. data/templates/default/source/feed.xml.builder.txt +0 -23
  201. data/templates/default/source/theme/templates/site.template.slim +0 -33
@@ -0,0 +1,185 @@
1
+ # require 'ostruct'
2
+
3
+ module Gumdrop
4
+
5
+ class DataManager
6
+ include Util::SiteAccess
7
+
8
+ attr_reader :cache
9
+
10
+ def initialize(data_dir="./data")
11
+ @dir= File.expand_path data_dir
12
+ # reset
13
+ @cache= Hash.new &method(:_cache_dataset)
14
+ end
15
+
16
+ def dir=(path)
17
+ @dir= File.expand_path path
18
+ end
19
+
20
+ def method_missing(key, value=nil)
21
+ @cache[key]
22
+ end
23
+
24
+ def set(key, value)
25
+ @cache[key]= value
26
+ end
27
+
28
+ def reset
29
+ @cache.clear
30
+ end
31
+
32
+ def contents(pattern=nil, opts={})
33
+ if pattern.nil?
34
+ site.contents.all
35
+ else
36
+ site.contents(pattern, opts)
37
+ end
38
+ end
39
+
40
+ def pager_for(key, opts={})
41
+ data= case key
42
+ when Symbol
43
+ @cache[key]
44
+ when Array
45
+ key
46
+ else
47
+ raise "pager_for requires a lookup symbol or array data."
48
+ end
49
+ base_path= opts.fetch(:base_path, 'page')
50
+ page_size= opts.fetch(:page_size, 5)
51
+ Util::Pager.new( data, base_path, page_size )
52
+ end
53
+
54
+ private
55
+
56
+ def _cache_dataset(hash, key)
57
+ hash[key]= load_data(key) #unless @cache.has_key? key
58
+ end
59
+
60
+ def load_data(key)
61
+ path=_get_filename key
62
+ return nil if path.nil?
63
+ if File.directory? path
64
+ _load_from_directory path
65
+ else
66
+ _load_from_file path
67
+ end
68
+ end
69
+
70
+ def _load_from_file( filename )
71
+ ext=File.extname(filename)[1..-1]
72
+ provider= Data::Provider.for ext
73
+ case
74
+ when provider.nil?
75
+ raise "Unknown data type (#{ext}) for #{filename}"
76
+ when provider.available?
77
+ data= provider.data_for filename
78
+ log.debug " loaded: #{filename}"
79
+ data
80
+ else
81
+ raise "Unavailable data type (#{ext}) for #{filename}"
82
+ end
83
+ end
84
+
85
+ def _load_from_directory( filepath )
86
+ all=[]
87
+ Dir[ filepath / _supported_type_glob ].each do |filename|
88
+ id= File.basename(filename).gsub(File.extname(filename), '')
89
+ obj_hash= _load_from_file filename
90
+ obj_hash._id = id
91
+ all << obj_hash
92
+ end
93
+ all
94
+ end
95
+
96
+ def _get_filename(path)
97
+ lpath= _local_path_to(path)
98
+ if File.directory? lpath
99
+ lpath
100
+ else
101
+ _registered_data_types.each do |ext|
102
+ lpath= _local_path_to("#{path}.#{ext}")
103
+ return lpath if File.exists? lpath
104
+ end
105
+ log.warn "No data found for #{path}"
106
+ nil
107
+ end
108
+ end
109
+
110
+ def _local_path_to(filename)
111
+ File.join(@dir.to_s, filename.to_s)
112
+ end
113
+
114
+ def _registered_data_types
115
+ Data::Provider.registered_exts
116
+ end
117
+
118
+ def _supported_type_glob
119
+ "{*.#{ _registered_data_types.join ',*.'}}"
120
+ end
121
+
122
+ end
123
+
124
+ module Data
125
+ # Base class for Data Proviers
126
+ class Provider
127
+
128
+ def available?
129
+ raise "available? must be implemented! (#{self.class})"
130
+ end
131
+
132
+ def data_for(filepath)
133
+ raise "data_for must be implemented! (#{self.class})"
134
+ end
135
+
136
+ def supply_data(object)
137
+ case object
138
+ when Hash
139
+ object = object.clone
140
+ object.each do |key, value|
141
+ object[key] = supply_data(value)
142
+ end
143
+ # OpenStruct.new(object)
144
+ Gumdrop::Util::HashObject.from object
145
+ when Array
146
+ object = object.clone
147
+ object.map! { |item| supply_data(item) }
148
+ else
149
+ object
150
+ end
151
+ end
152
+
153
+ class << self
154
+
155
+ def extensions(*extnames)
156
+ provider_class= self
157
+ @@providers ||= {}
158
+ extnames.each do |ext|
159
+ @@providers[ext.to_s]= provider_class
160
+ end
161
+ end
162
+ alias_method :extension, :extensions
163
+
164
+ def registered_exts
165
+ @@providers.keys
166
+ end
167
+
168
+ def for(ext)
169
+ case
170
+ when provider_class= @@providers[ext]
171
+ provider_class.new
172
+ when provider_class= @@providers[".#{ ext }"]
173
+ provider_class.new
174
+ else
175
+ nil
176
+ end
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end
182
+
183
+ Dir[File.dirname(__FILE__) / 'data_providers' / '*.rb'].each do |lib|
184
+ require lib
185
+ end
@@ -0,0 +1,18 @@
1
+ module Gumdrop::Data
2
+ class CSVDataProvider < Provider
3
+
4
+ extension :csv
5
+
6
+ def available?
7
+ require 'csv'
8
+ true
9
+ rescue LoadError
10
+ false
11
+ end
12
+
13
+ def data_for(filepath)
14
+ supply_data CSV.read(filepath)
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,25 @@
1
+ module Gumdrop::Data
2
+ class PStoreDataProvider < Provider
3
+
4
+ extension :pstore
5
+
6
+ def available?
7
+ require 'pstore'
8
+ true
9
+ rescue LoadError
10
+ false
11
+ end
12
+
13
+ def data_for(filepath)
14
+ data={}
15
+ store= PStore.new(filepath)
16
+ store.transaction true do
17
+ store.roots.each do |root|
18
+ data[root]= store[root]
19
+ end
20
+ end
21
+ supply_data data
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,43 @@
1
+ module Gumdrop::Data
2
+ class SqliteDataProvider < Provider
3
+
4
+ extensions :sqlite, :sqlite3, :db
5
+
6
+ def available?
7
+ require 'sqlite3'
8
+ true
9
+ rescue LoadError
10
+ false
11
+ end
12
+
13
+ def data_for(filepath)
14
+ @db= SQLite3::Database.new( filepath )
15
+ @live= SqliteLiveData.new @db, self
16
+ end
17
+
18
+ end
19
+
20
+ class SqliteLiveData
21
+ attr_reader :db, :provider
22
+
23
+ def initialize(db, provider)
24
+ @db= db
25
+ @provider= provider
26
+ @data_hash= Hash.new &method(:load_data_for)
27
+ end
28
+
29
+ def load_data_for(hash, table_name)
30
+ data=[]
31
+ db.results_as_hash = true
32
+ db.execute( "select * from #{ table_name.to_s };" ) do |row|
33
+ data << row.reject {|key,col| key.is_a? Fixnum }
34
+ end
35
+ hash[table_name]= provider.supply_data data
36
+ end
37
+
38
+ def method_missing(key, *args)
39
+ @data_hash[key]
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ module Gumdrop::Data
2
+ class XMLDataProvider < Provider
3
+
4
+ extension :xml
5
+
6
+ def available?
7
+ true
8
+ end
9
+
10
+ def data_for(filepath)
11
+ supply_data Hash.from_xml File.read(filepath)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module Gumdrop::Data
2
+ class YAMLandJSONDataProvider < Provider
3
+
4
+ extensions :yaml, :yml, :json
5
+
6
+ def available?
7
+ require 'yaml'
8
+ true
9
+ rescue LoadError
10
+ false
11
+ end
12
+
13
+ def data_for(filepath)
14
+ supply_data YAML.load_file(filepath)
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module Gumdrop::Data
2
+ class YAMLDBDataProvider < YAMLandJSONDataProvider
3
+
4
+ extension :yamldb
5
+
6
+
7
+ def data_for(filepath)
8
+ docs=[]
9
+ File.open(filepath, 'r') do |f|
10
+ YAML.load_documents(f) do |doc|
11
+ docs << supply_data( doc ) #unless doc.has_key?("__proto__")
12
+ end
13
+ end
14
+ docs
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ module Gumdrop::Data
2
+ class YAMLDocDataProvider < YAMLandJSONDataProvider
3
+
4
+ extension :yamldoc
5
+
6
+ def data_for(filepath)
7
+ yamldoc= Gumdrop::Util::YamlDoc.new File.read(filepath), true
8
+ supply_data yamldoc.data
9
+ end
10
+
11
+ end
12
+ end
@@ -1,124 +1,148 @@
1
1
  module Gumdrop
2
2
 
3
3
  class Generator
4
+ include Util::SiteAccess
4
5
 
5
- include Support::Stitch
6
- include Support::Sprockets
7
-
8
-
9
- attr_reader :filename, :base_path, :params, :pages
10
-
11
- def initialize(content, site, opts={})
12
- @site= site
13
- @content= content
14
- if @content.is_a? Proc
15
- @filename= ""
16
- @base_path= ""
17
- else
18
- @filename= content.filename || ""
19
- @base_path= content.slug || ""
20
- end
21
- @params= HashObject.new
6
+ def initialize(content=nil, opts={}, &block) # block?
7
+ @content= content || block
8
+ @dsl= DSL.new self
22
9
  @pages= []
10
+ @filename= content.nil? ? (opts[:filename] || '') : content.filename
11
+ @base_path= content.nil? ? (opts[:base_path] || '') : content.slug
23
12
  end
24
-
25
- # This should probably not be accessible to the generators
26
- def execute
27
- if @content.is_a? Proc
28
- instance_eval &@content
29
- else
30
- instance_eval File.read(@content.path)
13
+
14
+ def unload
15
+ pages.each do |content|
16
+ site.contents.remove content
31
17
  end
18
+ @dsl= DSL.new self
32
19
  end
33
20
 
34
- def site
35
- @site
36
- end
37
-
38
- def data
39
- @site.data
21
+ def reload
22
+ unload
23
+ execute
40
24
  end
41
25
 
42
- def config
43
- @site.config
44
- end
45
-
46
- def set(var_name, value)
47
- params[var_name]= value
26
+ def pages
27
+ @pages
48
28
  end
49
29
 
50
- def unload
51
- @pages.each do |content|
52
- @site.content_hash.delete content.uri
53
- end
54
- end
55
-
56
- def page(name, opts={}, &block)
57
- name= name[1..-1] if name.starts_with?('/')
58
- opts= params.reverse_merge(opts)
59
- filepath= if @base_path.empty?
60
- File.join @site.src_path, name
61
- else
62
- File.join @site.src_path, @base_path, @name
63
- end
64
- content= GeneratedContent.new(filepath, block, @site, self, opts)
65
- if opts.has_key? :template and !opts[:template].nil?
66
- content.template = if @site.layouts.has_key?( opts[:template] )
67
- @site.layouts[ opts[:template] ]
30
+ def gen_page(name, opts={}, params={}, &block)
31
+ event_block :generate_item do
32
+ name.relative!
33
+ opts= params.reverse_merge(opts)
34
+ filepath= if @base_path.empty?
35
+ site.source_path / name
36
+ else
37
+ site.source_path / @base_path / name
38
+ end
39
+ if block.nil?
40
+ handler= _render_inline_content opts
41
+ content= site.contents.create filepath, self, &handler
68
42
  else
69
- @site.layouts[ "#{opts[:template]}.template" ]
70
- end.template
43
+ content= site.contents.create filepath, self, &block
44
+ end
45
+ content.params.merge! opts
46
+ content.params.merge! opts[:params] if opts.has_key?(:params)
47
+ log.debug " generated: #{content.uri}"
48
+ @pages << content
71
49
  end
72
- content.ignore site.greylist.any? {|pattern| site.path_match name, pattern }
73
- unless content.ignored
74
- content.ignore site.blacklist.any? {|pattern| site.path_match name, pattern }
75
- end
76
- @site.report " generated: #{content.uri}", :info
77
- @site.content_hash[content.uri]= content
78
- @pages << content
79
- content
80
50
  end
81
51
 
82
- # FIXME: Does redirect require abs-paths?
83
- def redirect(from, opts={})
84
- if opts[:to]
85
- page from do
86
- <<-EOF
87
- <meta http-equiv="refresh" content="0;url=#{ opts[:to] }">
88
- <script> window.location.href='#{ opts[:to] }'; </script>
89
- EOF
52
+ def execute
53
+ log.debug "(Generator '#{ @filename }')"
54
+ if @content.is_a? Proc
55
+ if @content.arity == 1
56
+ @content.call @dsl
57
+ else
58
+ @dsl.instance_eval &@content
90
59
  end
91
- opts[:from]= from
92
- @site.redirects << opts
93
60
  else
94
- @site.report "You must specify :to in a redirect", :warning
61
+ @dsl.instance_eval @content.body
95
62
  end
63
+ log.debug " created: #{ @pages.size } pages"
96
64
  end
97
-
98
- end
99
-
100
- class GeneratedContent < Content
101
- # Nothing special, per se...
102
-
103
- def initialize(path, block, site, generator, params={})
104
- super(path, site, params)
105
- @content_block= block
106
- @generated= true
107
- @generator= generator
65
+
66
+ def _render_inline_content(opts)
67
+ Proc.new {
68
+ renderer= site.active_renderer || Renderer.new
69
+ content= site.resolve(opts[:render], opts)
70
+ opts[:inline_render]= true
71
+ renderer.draw content, opts
72
+ }
108
73
  end
109
74
 
110
- def render(context=nil, ignore_layout=false, reset_context=true, locals={})
111
- if @content_block.nil?
112
- super(context, ignore_layout, reset_context, locals)
113
- else
114
- @content_block.call
75
+ class DSL
76
+ include Util::SiteAccess
77
+ include Util::ViewHelpers
78
+
79
+ attr_reader :params
80
+
81
+ def initialize(generator)
82
+ @generator= generator
83
+ @params= Util::HashObject.new
84
+ end
85
+
86
+ def data
87
+ site.data
88
+ end
89
+
90
+ def config
91
+ site.config
92
+ end
93
+
94
+ def options
95
+ site.options
96
+ end
97
+
98
+ def env
99
+ site.env
100
+ end
101
+
102
+ def mode
103
+ site.mode
115
104
  end
105
+
106
+ def set(var_name, value)
107
+ @params[var_name]= value
108
+ end
109
+ def get(var_name)
110
+ @params[var_name]
111
+ end
112
+
113
+ def page(name, opts={}, &block)
114
+ @generator.gen_page name, opts, @params, &block
115
+ end
116
+ alias_method :content, :page
117
+
118
+ def file(name, opts={}, &block)
119
+ opts[:layout]= opts[:layout] || false
120
+ page name, opts, &block
121
+ end
122
+
116
123
  end
124
+ end
125
+
126
+
127
+ class << self
117
128
 
118
- def useLayout?
119
- !@content_block.nil? or !template.nil?
129
+ # Generate a page (based on data or whatever) from your
130
+ # Gumdrop file like this:
131
+ #
132
+ # Gumdrop.generate 'label' do |gen|
133
+ # gen.file 'my-page.html' do
134
+ # # whatever you return is set as the page contents.
135
+ # "hello!"
136
+ # end
137
+ # end
138
+ #
139
+ def generate(name=nil, opts={}, &block)
140
+ opts[:filename]= name unless opts[:filename]
141
+ site.generators << Generator.new(nil, opts, &block)
120
142
  end
121
143
 
122
144
  end
123
-
145
+
124
146
  end
147
+
148
+