shortcode 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -28,19 +28,19 @@ $ gem install shortcode
28
28
 
29
29
  Shortcode can be used in one of two ways, the parser and transformer can be called seperatly or the `process` method can be used which performs both the parsing and transforming and returns the result.
30
30
 
31
- The former is good if you want to work with the intemediary hash returned by the parser
31
+ The shorthand wraps up the above code into a single method call for convenience
32
32
 
33
33
  ```ruby
34
- parser = Shortcode::Parser.new
35
- transformer = Shortcode::Transformer.new
36
- parsed_hash = parser.parse("[quote]Hello World[/quote]")
37
- transformer.apply(parsed_hash)
34
+ Shortcode.process("[quote]Hello World[/quote]")
38
35
  ```
39
36
 
40
- The shorthand wraps up the above code into a single method call for convenience
37
+ The longer form is good if you want to work with the intemediary hash returned by the parser
41
38
 
42
39
  ```ruby
43
- Shortcode.process("[quote]Hello World[/quote]")
40
+ parser = Shortcode::Parser.new
41
+ transformer = Shortcode::Transformer.new
42
+ parsed_hash = parser.parse("[quote]Hello World[/quote]")
43
+ transformer.apply(parsed_hash)
44
44
  ```
45
45
 
46
46
  ### Configuration
@@ -90,6 +90,48 @@ Shortcodes can be nested inside other shortcodes, there are no limits imposed on
90
90
 
91
91
  Three templates would be required to support the above content, `[:collapsible_list, :item, :youtube]`. Each template is rendered in isolation and has no knowledge of parent or child elements.
92
92
 
93
+ ### Presenters
94
+
95
+ Sometimes the data passed to the template from the shortcode it not enough. Lets say you want to render a gallery of images using id numbers of images stored in a database, e.g. `[gallery ids="1,2,3,4"]`. This is where presenters can help, they allow you to modify the `@content` and `@attributes` variables before they are sent to the template for rendering. Presenters are simple classes that define four methods. The class method `for` should return the name of the shortcode (as a symbol) it should be applied to. The classes `initialize` method received the `attributes` and `content` variables. Finally the class should define `content` and `attributes` methods.
96
+
97
+ In a rails app you could return image records to the template using something like this:
98
+
99
+ ```ruby
100
+ class CustomPrsenter
101
+
102
+ def self.for
103
+ :quote
104
+ end
105
+
106
+ def initialize(attributes, content)
107
+ @attributes = attributes
108
+ @content = content
109
+ end
110
+
111
+ def content
112
+ @content
113
+ end
114
+
115
+ def attributes
116
+ { images: images }
117
+ end
118
+
119
+ private
120
+
121
+ def images
122
+ @attributes.ids.split(',').collect { |n| Image.find(n) }
123
+ end
124
+ end
125
+ ```
126
+
127
+ #### Registering presenters
128
+
129
+ To register a presenter simply call `Shortcode.register_presenter` passing the presenter class e.g.
130
+
131
+ ```ruby
132
+ Shortcode.register_presenter(CustomPrsenter)
133
+ ```
134
+
93
135
  ## Contributing
94
136
 
95
137
  1. Fork it
data/lib/shortcode.rb CHANGED
@@ -5,7 +5,9 @@ require 'erb'
5
5
  module Shortcode
6
6
  extend self
7
7
 
8
- attr_accessor :configuration
8
+ attr_accessor :configuration, :presenters
9
+ @@presenters = {}
10
+
9
11
 
10
12
  def setup
11
13
  self.configuration ||= Configuration.new
@@ -16,6 +18,10 @@ module Shortcode
16
18
  transformer.apply(parser.parse(code))
17
19
  end
18
20
 
21
+ def register_presenter(presenter)
22
+ self.presenters[presenter.for.to_sym] = presenter
23
+ end
24
+
19
25
  private
20
26
 
21
27
  def parser
@@ -31,6 +37,7 @@ end
31
37
  require 'shortcode/version'
32
38
  require 'shortcode/configuration'
33
39
  require 'shortcode/parser'
40
+ require 'shortcode/presenter'
34
41
  require 'shortcode/transformer'
35
42
  require 'shortcode/tag'
36
43
  require 'shortcode/exceptions'
@@ -0,0 +1,27 @@
1
+ class Shortcode::Presenter
2
+
3
+ def initialize(name, attributes, content)
4
+ @attributes = attributes
5
+ @content = content
6
+ initialize_custom_presenter(name)
7
+ end
8
+
9
+ def content
10
+ @content
11
+ end
12
+
13
+ def attributes
14
+ @attributes
15
+ end
16
+
17
+ private
18
+
19
+ def initialize_custom_presenter(name)
20
+ if Shortcode.presenters.has_key? name.to_sym
21
+ presenter = Shortcode.presenters[name.to_sym].new(@attributes, @content)
22
+ @attributes = presenter.attributes
23
+ @content = presenter.content
24
+ end
25
+ end
26
+
27
+ end
data/lib/shortcode/tag.rb CHANGED
@@ -1,14 +1,16 @@
1
1
  class Shortcode::Tag
2
2
 
3
- def initialize(name, attributes=[])
4
- @name = name.downcase
5
- set_attributes attributes
3
+ def initialize(name, attributes=[], content='')
4
+ @name = name.downcase
5
+ presenter = Shortcode::Presenter.new name, set_attributes(attributes), content
6
+ @attributes = presenter.attributes
7
+ @content = presenter.content
6
8
  end
7
9
 
8
10
  def set_attributes(attributes)
9
11
  hash = {}
10
12
  attributes.each { |o| hash[o[:key].to_sym] = o[:value] }
11
- @attributes = hash
13
+ hash
12
14
  end
13
15
 
14
16
  def markup
@@ -18,8 +20,7 @@ class Shortcode::Tag
18
20
  raise Shortcode::TemplateNotFound, "Searched in:", template_files
19
21
  end
20
22
 
21
- def wrap(content='')
22
- @content = content
23
+ def render
23
24
  render_template
24
25
  end
25
26
 
@@ -6,11 +6,11 @@ class Shortcode::Transformer < Parslet::Transform
6
6
  options: subtree(:options),
7
7
  inner: sequence(:inner),
8
8
  close: simple(:name)
9
- ) { Shortcode::Tag.new(name.to_s, options).wrap(inner.join) }
9
+ ) { Shortcode::Tag.new(name.to_s, options, inner.join).render }
10
10
  rule(
11
11
  open_close: simple(:name),
12
12
  options: subtree(:options)
13
- ) { Shortcode::Tag.new(name.to_s, options).wrap }
13
+ ) { Shortcode::Tag.new(name.to_s, options).render }
14
14
 
15
15
  rule(body: sequence(:strings)) { strings.join }
16
16
  end
@@ -1,3 +1,3 @@
1
1
  module Shortcode
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'parslet/rig/rspec'
3
+ require 'pp'
4
+
5
+ class MyPrsenter
6
+
7
+ def self.for
8
+ :quote
9
+ end
10
+
11
+ def initialize(attributes, content)
12
+ @attributes = attributes
13
+ @content = content
14
+ end
15
+
16
+ def content
17
+ @content
18
+ end
19
+
20
+ def attributes
21
+ { title: "my custom title" }
22
+ end
23
+ end
24
+
25
+ describe Shortcode::Presenter do
26
+
27
+ let(:simple_quote) { load_fixture :simple_quote }
28
+ let(:simple_quote_output) { load_fixture :simple_quote_presenter_output, :html }
29
+
30
+ describe "using a custom presenter" do
31
+
32
+ before do
33
+ Shortcode.register_presenter MyPrsenter
34
+ end
35
+
36
+ it "uses the custome attributes" do
37
+ Shortcode.process(simple_quote).gsub("\n",'').should == simple_quote_output.gsub("\n",'')
38
+ end
39
+
40
+ end
41
+
42
+ end
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,7 @@ RSpec.configure do |config|
7
7
  config.order = "random"
8
8
 
9
9
  config.before(:each) do
10
+ Shortcode.presenters = {}
10
11
  Shortcode.setup do |config|
11
12
  config.template_parser = :haml
12
13
  config.template_path = File.join File.dirname(__FILE__), "support/templates/haml"
@@ -0,0 +1,6 @@
1
+ <blockquote>
2
+ <p class='quotation'>hello</p>
3
+ <p class='citation'>
4
+ <span class='position'>my custom title</span>
5
+ </p>
6
+ </blockquote>
data/spec/tag_spec.rb CHANGED
@@ -7,7 +7,7 @@ describe Shortcode::Tag do
7
7
  let(:tag) { Shortcode::Tag.new('doesnt_exist') }
8
8
 
9
9
  it "raises a TemplateNotFound error when the file doesn't exists" do
10
- expect { tag.wrap }.to raise_error(Shortcode::TemplateNotFound)
10
+ expect { tag.render }.to raise_error(Shortcode::TemplateNotFound)
11
11
  end
12
12
 
13
13
  end
@@ -23,7 +23,7 @@ describe Shortcode::Tag do
23
23
  end
24
24
 
25
25
  it "raises a TemplateNotFound error when the file doesn't exists" do
26
- expect { tag.wrap }.to raise_error(Shortcode::TemplateParserNotSupported)
26
+ expect { tag.render }.to raise_error(Shortcode::TemplateParserNotSupported)
27
27
  end
28
28
 
29
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shortcode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -108,12 +108,14 @@ files:
108
108
  - lib/shortcode/configuration.rb
109
109
  - lib/shortcode/exceptions.rb
110
110
  - lib/shortcode/parser.rb
111
+ - lib/shortcode/presenter.rb
111
112
  - lib/shortcode/tag.rb
112
113
  - lib/shortcode/transformer.rb
113
114
  - lib/shortcode/version.rb
114
115
  - shortcode.gemspec
115
116
  - spec/parser_spec.rb
116
117
  - spec/performance_spec.rb
118
+ - spec/presenter_spec.rb
117
119
  - spec/shortcode_spec.rb
118
120
  - spec/spec_helper.rb
119
121
  - spec/support/fixtures.rb
@@ -128,6 +130,7 @@ files:
128
130
  - spec/support/fixtures/simple_list_output.html
129
131
  - spec/support/fixtures/simple_quote.txt
130
132
  - spec/support/fixtures/simple_quote_output.html
133
+ - spec/support/fixtures/simple_quote_presenter_output.html
131
134
  - spec/support/fixtures/timeline_event.txt
132
135
  - spec/support/fixtures/timeline_event_output.html
133
136
  - spec/support/fixtures/timeline_info.txt
@@ -171,6 +174,7 @@ summary: Gem for parsing wordpress style shortcodes
171
174
  test_files:
172
175
  - spec/parser_spec.rb
173
176
  - spec/performance_spec.rb
177
+ - spec/presenter_spec.rb
174
178
  - spec/shortcode_spec.rb
175
179
  - spec/spec_helper.rb
176
180
  - spec/support/fixtures.rb
@@ -185,6 +189,7 @@ test_files:
185
189
  - spec/support/fixtures/simple_list_output.html
186
190
  - spec/support/fixtures/simple_quote.txt
187
191
  - spec/support/fixtures/simple_quote_output.html
192
+ - spec/support/fixtures/simple_quote_presenter_output.html
188
193
  - spec/support/fixtures/timeline_event.txt
189
194
  - spec/support/fixtures/timeline_event_output.html
190
195
  - spec/support/fixtures/timeline_info.txt