jarrett-quarto 1.5.1 → 1.6.0

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/VERSION CHANGED
@@ -1 +1 @@
1
- 1.5.1
1
+ 1.6.0
@@ -1,6 +1,6 @@
1
1
  module Quarto
2
2
  class Rendering # :nodoc: all
3
- def initialize(__erb_template, __locals, __mixins)
3
+ def initialize(__erb_template, __locals, __mixins, __output_file_path)
4
4
  unless __erb_template.is_a?(ERB)
5
5
  raise ArgumentError, "Expected ERB but got #{__erb_template.inspect}"
6
6
  end
@@ -29,12 +29,19 @@ module Quarto
29
29
  end
30
30
 
31
31
  @result = __erb_template.result(__b)
32
+ @output_file_path = __output_file_path
32
33
  end
33
34
 
34
- def self.render(erb_template, locals, mixins = [], &block)
35
- new(erb_template, locals, mixins, &block).result
35
+ def self.render(erb_template, locals, mixins = [], output_file_path = '', &block)
36
+ new(erb_template, locals, mixins, output_file_path, &block).result
36
37
  end
37
38
 
38
39
  attr_reader :result
40
+
41
+ protected
42
+
43
+ def output_file_path
44
+ @output_file_path
45
+ end
39
46
  end
40
47
  end
@@ -17,30 +17,6 @@ module Quarto
17
17
  # end
18
18
  # end
19
19
  class Transformer
20
- # Macro to define the <tt>literal?</tt> method. Accepts
21
- # one or more element names.
22
- #
23
- # Example:
24
- #
25
- # class MyTransformer < Quarto::Transformer
26
- # literals 'div', 'p', 'a'
27
- # end
28
- #
29
- # The above is equivalent to:
30
- #
31
- # class MyTransformer < Quarto::Transformer
32
- # def literal?(element)
33
- # ['div', 'p', 'a'].include?(element.name)
34
- # end
35
- # end
36
- def self.literals(*args)
37
- class_eval(%Q(
38
- def literal?(element)
39
- [#{args.collect { |e| "'#{e}'" }.join(',')}].include?(element.name)
40
- end
41
- ))
42
- end
43
-
44
20
  # Recursively applies the transformation rules
45
21
  # you've defined to +element+ and its children,
46
22
  # returning the results as a string. Depending
@@ -76,7 +52,18 @@ module Quarto
76
52
 
77
53
  protected
78
54
 
79
- def content_tag(tag_name, *args) # :nodoc:
55
+ # Creates an XML tag with the specified name, returning a string.
56
+ # This method is meant to be called by subclasses of
57
+ # <tt>Transformer</tt>.
58
+ #
59
+ # Example:
60
+ #
61
+ # content_tag('img', 'src' => 'http://example.com/image.jpg')
62
+ #
63
+ # If you need to use an absolute path for something, e.g. an image,
64
+ # you should include <tt>Quarto::UrlHelper</tt> in your
65
+ # <tt>Transformer</tt> subclass and call <tt>abs_path</tt>.
66
+ def content_tag(tag_name, *args)
80
67
  if args.last.is_a?(Hash)
81
68
  attributes = args.pop
82
69
  else
@@ -126,6 +113,30 @@ module Quarto
126
113
  false
127
114
  end
128
115
 
116
+ # Macro to define the <tt>literal?</tt> method. Accepts
117
+ # one or more element names.
118
+ #
119
+ # Example:
120
+ #
121
+ # class MyTransformer < Quarto::Transformer
122
+ # literals 'div', 'p', 'a'
123
+ # end
124
+ #
125
+ # The above is equivalent to:
126
+ #
127
+ # class MyTransformer < Quarto::Transformer
128
+ # def literal?(element)
129
+ # ['div', 'p', 'a'].include?(element.name)
130
+ # end
131
+ # end
132
+ def self.literals(*args)
133
+ class_eval(%Q(
134
+ def literal?(element)
135
+ [#{args.collect { |e| "'#{e}'" }.join(',')}].include?(element.name)
136
+ end
137
+ ))
138
+ end
139
+
129
140
  # Recursively transform the +element+ and all its children,
130
141
  # returning a string. Custom transform methods often call
131
142
  # this method.
@@ -14,13 +14,36 @@ module Quarto
14
14
  end
15
15
  end
16
16
 
17
- # Generates an absolute URL, using the <tt>:site_root</tt> config value. (To change <tt>:site_root</tt>,
17
+ # Generates an absolute path, using the <tt>:site_root</tt> config value. (To change <tt>:site_root</tt>,
18
18
  # put something like this in <tt>generate.rb</tt>:
19
19
  # config(:site_root, 'http://your_domain.com/whatever')
20
- def abs_url(str)
20
+ def abs_path(str)
21
21
  "#{Quarto.config[:site_root]}#{str}"
22
22
  end
23
23
 
24
+ def abs_url(str)
25
+ warn 'Quarto::UrlHelper#abs_url is deprecated. Use abs_path instead.'
26
+ abs_path(str)
27
+ end
28
+
29
+ # Generates a relative path based on the location of the current output file and +path+.
30
+ # +path+ must be relative to the project's output directory. For example, if
31
+ # the current output file is in <tt>output/employees</tt>, and you call
32
+ # <tt>relative_path('images/foo.jpg')</tt>, the result will be <tt>../images/foo.jpg</tt>.
33
+ def relative_path(path)
34
+ current_hierarchy = output_file_path.split('/')
35
+ target_hierarchy = path.split('/')
36
+ while current_hierarchy[0] == target_hierarchy[0]
37
+ current_hierarchy.shift
38
+ target_hierarchy.shift
39
+ end
40
+ rel_path = current_hierarchy.inject('') do |result, dir|
41
+ result + '../'
42
+ end
43
+ #puts target_hierarchy.inspect
44
+ rel_path << target_hierarchy.join('/')
45
+ end
46
+
24
47
  def url_for_with_element_wrapper(options = {})
25
48
  if options.is_a?(Quarto::ElementWrapper::Base)
26
49
  if options.respond_to?(:to_path)
data/quarto.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{quarto}
5
- s.version = "1.5.1"
5
+ s.version = "1.6.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Jarrett Colby"]
9
- s.date = %q{2009-06-18}
9
+ s.date = %q{2009-06-19}
10
10
  s.default_executable = %q{quarto}
11
11
  s.description = %q{Quarto is a Ruby framework for generating collections of documents from XML. It steps in where XSLT just won't cut it. Potential applications include web sites and e-books. It's built on top of ERB and REXML.}
12
12
  s.email = %q{jarrett@uchicago.edu}
@@ -42,4 +42,11 @@ describe Quarto::Rendering do
42
42
  end
43
43
  end
44
44
  end
45
+
46
+ context '#output_file_path' do
47
+ it 'should return the path' do
48
+ rendering = Quarto::Rendering.new(ERB.new('foo'), {}, [], 'bar/baz')
49
+ rendering.send(:output_file_path).should == 'bar/baz'
50
+ end
51
+ end
45
52
  end
@@ -1,5 +1,5 @@
1
1
  Quarto.generate do
2
- config(:site_root, '') # This determines how abs_url works. If you need your absolute URLs to be prepended with something, you can put it here.
2
+ config(:site_root, '') # This determines how abs_path works. If you need your absolute URLs to be prepended with something, you can put it here.
3
3
 
4
4
  use_xml('companies.xml')
5
5
 
@@ -3,7 +3,7 @@ module Quarto
3
3
  include Quarto::UrlHelper
4
4
 
5
5
  def companies_url
6
- abs_url('companies.html')
6
+ abs_path('companies.html')
7
7
  end
8
8
  end
9
9
  end
@@ -145,10 +145,11 @@ describe Quarto::UrlHelper do
145
145
  end
146
146
  end
147
147
 
148
- it 'should not call abs_url' do
148
+ it 'should not call abs_url or abs_path' do
149
149
  ['foo', '../foo', '/foo'].each do |rel_url|
150
150
  template = TemplateOutsideRails.new
151
151
  template.should_not_receive(:abs_url)
152
+ template.should_not_receive(:abs_path)
152
153
  template.url_for(rel_url)
153
154
  end
154
155
  end
@@ -204,4 +205,46 @@ describe Quarto::UrlHelper do
204
205
  end
205
206
  end
206
207
  end
208
+
209
+ context '#relative_path' do
210
+ before :all do
211
+ class MockRendering
212
+ include Quarto::UrlHelper
213
+
214
+ def initialize(output_file_path)
215
+ @output_file_path = output_file_path
216
+ end
217
+
218
+ attr_accessor :output_file_path
219
+ end
220
+ end
221
+
222
+ after :all do
223
+ Object.class_eval do
224
+ remove_const :MockRendering
225
+ end
226
+ end
227
+
228
+ it 'should call output_file_path' do
229
+ rendering = MockRendering.new('employees')
230
+ rendering.should_receive('output_file_path').and_return('employees')
231
+ rendering.relative_path('images/foo.jpg')
232
+ end
233
+
234
+ it 'should derive the correct relative path from output_file_path to the given file' do
235
+ rendering = MockRendering.new('employees')
236
+ rendering.relative_path('images/foo.jpg').should == '../images/foo.jpg'
237
+ end
238
+
239
+ it 'should work for complex directory structures' do
240
+ [
241
+ ['countries/companies/employees', 'assets/images/foo.jpg', '../../../assets/images/foo.jpg'],
242
+ ['a/b/c/d/e/f', 'a/b/z', '../../../../z'],
243
+ ['a/b/z', 'a/b/c/d/e/f', '../c/d/e/f']
244
+ ].each do |output_file_path, target, expected|
245
+ rendering = MockRendering.new(output_file_path)
246
+ rendering.relative_path(target).should == expected
247
+ end
248
+ end
249
+ end
207
250
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jarrett-quarto
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jarrett Colby
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-18 00:00:00 -07:00
12
+ date: 2009-06-19 00:00:00 -07:00
13
13
  default_executable: quarto
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency