jadof 0.1.3 → 0.1.4

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.
data/lib/jadof/page.rb CHANGED
@@ -113,18 +113,28 @@ module JADOF #:nodoc:
113
113
 
114
114
  # @return [String] Using the #filename of the page given and available
115
115
  # Page.formatters, we render and return the page #body.
116
- def to_html page
116
+ def render page
117
117
  html = page.body
118
118
 
119
- page.filename.scan(/\.([^\.]+)/).reverse.each do |match| # [ ["markdown"], ["erb"] ]
119
+ page.extensions.reverse.each do |match| # [ ["markdown"], ["erb"] ]
120
120
  if formatter = formatters[ match.first ]
121
- html = formatter.call(html)
121
+ begin
122
+ html = formatter.call(html)
123
+ rescue ArgumentError => ex
124
+ if ex.message == 'wrong number of arguments (1 for 2)'
125
+ html = formatter.call(html, page)
126
+ else
127
+ raise
128
+ end
129
+ end
122
130
  end
123
131
  end
124
132
 
125
133
  html
126
134
  end
127
135
 
136
+ alias to_html render
137
+
128
138
  # @private
129
139
  # Helper for caching. Will check to see if the {Page.cache}
130
140
  # contains the given key and, if not, it will set the cache by calling
@@ -204,6 +214,11 @@ module JADOF #:nodoc:
204
214
  # it is in subdirectories.
205
215
  attr_accessor :parent
206
216
 
217
+ # @return [Array(String)] This file's extension(s).
218
+ def extensions
219
+ filename.scan(/\.([^\.]+)/).flatten
220
+ end
221
+
207
222
  # A page is a dumb object and doesn't know how to load itself from
208
223
  # a file on the filesystem. See `Page.from_path` to load a {Page}
209
224
  # from a file.
@@ -226,10 +241,12 @@ module JADOF #:nodoc:
226
241
  # For `foo.markdown.erb`, `Page.formatters['erb']` and `Page.formatters['markdown']` will be used.
227
242
  #
228
243
  # With multiple extensions, the *last* extension is used first, and then the second-to-last, etc.
229
- def to_html
230
- self.class.to_html self
244
+ def render
245
+ self.class.render self
231
246
  end
232
247
 
248
+ alias to_html render
249
+
233
250
  # @return [true, false] If 2 pages have the same system path, they're the same.
234
251
  def == other_page
235
252
  return false unless other_page.is_a? Page
@@ -54,6 +54,18 @@ shared_examples_for "JADOF Page" do
54
54
  @jadof_page_class[:foo].filename.should == 'foo.markdown'
55
55
  end
56
56
 
57
+ it 'can easily get file extensions' do
58
+ create_page '123'
59
+ create_page 'foo.markdown'
60
+ create_page 'bar.markdown.haml'
61
+ create_page 'abc.markdown.haml.something'
62
+
63
+ @jadof_page_class['123'].extensions.should == %w( )
64
+ @jadof_page_class[:foo ].extensions.should == %w( markdown )
65
+ @jadof_page_class[:bar ].extensions.should == %w( markdown haml )
66
+ @jadof_page_class[:abc ].extensions.should == %w( markdown haml something )
67
+ end
68
+
57
69
  it 'has a path (the full system path to the file)' do
58
70
  create_page 'foo.markdown'
59
71
 
@@ -217,6 +229,7 @@ shared_examples_for "JADOF Page" do
217
229
  **Hello World!**
218
230
  }
219
231
 
232
+ @jadof_page_class[:foo].render.should == '**Hello World!**'
220
233
  @jadof_page_class[:foo].to_html.should == '**Hello World!**'
221
234
  end
222
235
 
@@ -227,9 +240,50 @@ shared_examples_for "JADOF Page" do
227
240
  **Hello World!**
228
241
  }
229
242
 
243
+ @jadof_page_class[:foo].render.should == '<p><strong>Hello World!</strong></p>'
230
244
  @jadof_page_class[:foo].to_html.should == '<p><strong>Hello World!</strong></p>'
231
245
  end
232
246
 
247
+ it 'formatters have access to the page' do
248
+ @jadof_page_class.formatters['something'] = lambda do |text, page|
249
+ text.gsub('FILENAME', page.filename)
250
+ end
251
+
252
+ create_page 'foo.something', %{
253
+ Hello from FILENAME
254
+ }
255
+
256
+ @jadof_page_class[:foo].body.should == 'Hello from FILENAME'
257
+ @jadof_page_class[:foo].render.should == 'Hello from foo.something'
258
+ end
259
+
260
+ it 'formatters can be a class' do
261
+ class UpcaseFormatter
262
+ def self.call text
263
+ text.upcase
264
+ end
265
+ end
266
+
267
+ class FilenameFormatter
268
+ def self.call text, page
269
+ text.sub "FILENAME", page.filename
270
+ end
271
+ end
272
+
273
+ @jadof_page_class.formatters['upcase'] = UpcaseFormatter
274
+ @jadof_page_class.formatters['filename'] = FilenameFormatter
275
+
276
+ create_page 'foo.upcase', %{
277
+ Hello from FILENAME
278
+ }
279
+ create_page 'bar.filename', %{
280
+ Hello from FILENAME
281
+ }
282
+
283
+ @jadof_page_class[:foo].render.should == 'HELLO FROM FILENAME'
284
+ @jadof_page_class[:bar].render.should == 'Hello from bar.filename'
285
+ end
286
+
233
287
  it 'should support multiple file extensions, eg. foo.markdown.erb (erb renders first, then markdown)' do
234
288
  @jadof_page_class.formatters['markdown'] = lambda {|text| Maruku.new(text).to_html }
235
289
 
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jadof
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 4
9
+ version: 0.1.4
5
10
  platform: ruby
6
11
  authors:
7
12
  - remi
@@ -9,19 +14,23 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-02-21 00:00:00 -07:00
17
+ date: 2010-02-28 00:00:00 -07:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: indifferent-variable-hash
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 1
30
+ - 0
23
31
  version: 0.1.0
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  description:
26
35
  email: remi@remitaylor.com
27
36
  executables: []
@@ -51,18 +60,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
51
60
  requirements:
52
61
  - - ">="
53
62
  - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
54
65
  version: "0"
55
- version:
56
66
  required_rubygems_version: !ruby/object:Gem::Requirement
57
67
  requirements:
58
68
  - - ">="
59
69
  - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
60
72
  version: "0"
61
- version:
62
73
  requirements: []
63
74
 
64
75
  rubyforge_project:
65
- rubygems_version: 1.3.5
76
+ rubygems_version: 1.3.6
66
77
  signing_key:
67
78
  specification_version: 3
68
79
  summary: Just A Directory Of Files (JADOF)