jarrett-quarto 1.4.1 → 1.5.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.4.1
1
+ 1.5.0
@@ -27,6 +27,9 @@ module Quarto
27
27
  Dir.glob(project_path + '/models/*.rb').each do |model_file|
28
28
  require model_file
29
29
  end
30
+ Dir.glob(project_path + '/transformers/*.rb').each do |transformer_file|
31
+ require transformer_file
32
+ end
30
33
  generator = Quarto::Generator.new(project_path)
31
34
  generator.generate(&block)
32
35
  generator
@@ -4,7 +4,8 @@ module Quarto
4
4
  'models',
5
5
  'output',
6
6
  'pages',
7
- 'xml'
7
+ 'xml',
8
+ 'transformers'
8
9
  ]
9
10
 
10
11
  STARTER_GENERATE_FILE = %q(
@@ -9,7 +9,7 @@ module Quarto
9
9
  #
10
10
  # class MyTranformer < Quarto::Transformer
11
11
  # # This method will handle all <book> elements
12
- # def transform_book(book_element)
12
+ # def transform_book(book_element, raise_on_unrecognized_element)
13
13
  # # Return whatever string you like
14
14
  # # raise_on_unrecognized_element is provided
15
15
  # # so that you can pass it to recursive_transform
@@ -147,6 +147,21 @@ module Quarto
147
147
  element.to_s
148
148
  end
149
149
  end
150
+
151
+ # Replaces +element+ with +replace_with+, adding any
152
+ # +attributes+. This is a convenience method for use
153
+ # inside a custom transform method. It's not infinitely
154
+ # flexible, but it simplifies a common task. Calls
155
+ # recursive_transform on +element+'s children.
156
+ def replace_element(element, replace_with, raise_on_unrecognized_element, attributes = {})
157
+ raise ArgumentError, "Expected REXML::Element but got #{element.inspect}" unless element.is_a?(REXML::Element)
158
+ raise ArgumentError, "Expected String but got #{replace_with.inspect}" unless replace_with.is_a?(String)
159
+ raise ArgumentError, "Expected Hash but got #{attributes.inspect}" unless attributes.is_a?(Hash)
160
+ contents = element.children.inject('') do |result, child|
161
+ result + recursive_transform(child, raise_on_unrecognized_element)
162
+ end
163
+ content_tag(replace_with, contents, attributes)
164
+ end
150
165
  end
151
166
 
152
167
  class UnrecognizedElementError < RuntimeError; end
data/quarto.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{quarto}
5
- s.version = "1.4.1"
5
+ s.version = "1.5.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"]
@@ -57,6 +57,7 @@ Gem::Specification.new do |s|
57
57
  "spec/sample_project/models/location.rb",
58
58
  "spec/sample_project/models/mascot.rb",
59
59
  "spec/sample_project/models/product.rb",
60
+ "spec/sample_project/transformers/descriptions_transformer.rb",
60
61
  "spec/sample_project/urls.rb",
61
62
  "spec/spec_helper.rb",
62
63
  "spec/transformation_helper_spec.rb",
@@ -8,21 +8,30 @@ module Quarto
8
8
 
9
9
  def matches?(target)
10
10
  @target = target
11
+ return false unless @target.is_a?(REXML::Element)
11
12
  !@target.find_first_recursive { |node| node_matches?(node) }.nil?
12
13
  end
13
14
 
14
15
  def failure_message
15
- "Expected element '#{element_desc}' to be in:\n\n#{@target.to_s}"
16
+ if @target.is_a?(REXML::Element)
17
+ "Expected element #{element_desc} to be in:\n\n#{@target.to_s}"
18
+ else
19
+ "Expected #{@target.inspect} to be a REXML::Element"
20
+ end
16
21
  end
17
22
 
18
23
  def negative_failure_message
19
- "Expected element '#{element_desc}' not to be in:\n\n#{@target.to_s}"
24
+ if @target.is_a?(REXML::Element)
25
+ "Expected element #{element_desc} not to be in:\n\n#{@target.to_s}"
26
+ else
27
+ "Expected #{@target.inspect} to be a REXML::Element"
28
+ end
20
29
  end
21
30
 
22
31
  protected
23
32
 
24
33
  def element_desc
25
- desc = @element_name
34
+ desc = '<' + @element_name + '>'
26
35
  if @options.has_key?(:attributes)
27
36
  desc << " with attributes: #{@options[:attributes].inspect}"
28
37
  if @options.has_key?(:text)
@@ -0,0 +1,5 @@
1
+ class DescriptionsTransformer < Quarto::HtmlTransformer
2
+ def transform_tv(element, raise_on_unrecognized_elements)
3
+ replace_element(element, 'cite', raise_on_unrecognized_elements, 'class' => 'tv_show')
4
+ end
5
+ end
@@ -145,4 +145,22 @@ describe Quarto::Transformer do
145
145
  end
146
146
  end
147
147
  end
148
+
149
+ context '#replace_element' do
150
+ before :each do
151
+ @html = REXML::Document.new('<p><tv>The Office</tv> is a popular show.</p>')
152
+ @tv_element = REXML::XPath.first(@html, '//tv')
153
+ @t = Quarto::HtmlTransformer.new
154
+ end
155
+
156
+ it 'should substitute the given elements and attributes' do
157
+ output = @t.send(:replace_element, @tv_element, 'cite', false, {'class' => 'tv_show'})
158
+ REXML::Document.new(output).should have_element('cite', :attributes => {'class' => 'tv_show'}, :text => 'The Office')
159
+ end
160
+
161
+ it 'should pass the contents of the replaced tag to recursive_transform' do
162
+ @t.should_receive(:recursive_transform).with(@tv_element.children[0], false).and_return('The Office')
163
+ @t.send(:replace_element, @tv_element, 'cite', false, {'class' => 'tv_show'})
164
+ end
165
+ end
148
166
  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.4.1
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jarrett Colby
@@ -92,6 +92,7 @@ test_files:
92
92
  - spec/sample_project/models/location.rb
93
93
  - spec/sample_project/models/mascot.rb
94
94
  - spec/sample_project/models/product.rb
95
+ - spec/sample_project/transformers/descriptions_transformer.rb
95
96
  - spec/sample_project/urls.rb
96
97
  - spec/spec_helper.rb
97
98
  - spec/transformation_helper_spec.rb