shortcode 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,7 +3,6 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
- Gemfile.lock
7
6
  InstalledFiles
8
7
  _yardoc
9
8
  coverage
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ shortcode (0.1.2)
5
+ haml (~> 4.0)
6
+ parslet (= 1.6.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ appraisal (1.0.0.beta3)
12
+ bundler
13
+ rake
14
+ thor (>= 0.14.0)
15
+ blankslate (2.1.2.4)
16
+ diff-lcs (1.2.4)
17
+ haml (4.0.5)
18
+ tilt
19
+ parslet (1.6.0)
20
+ blankslate (~> 2.0)
21
+ rake (10.1.0)
22
+ rspec (2.14.1)
23
+ rspec-core (~> 2.14.0)
24
+ rspec-expectations (~> 2.14.0)
25
+ rspec-mocks (~> 2.14.0)
26
+ rspec-core (2.14.4)
27
+ rspec-expectations (2.14.0)
28
+ diff-lcs (>= 1.1.3, < 2.0)
29
+ rspec-mocks (2.14.1)
30
+ thor (0.18.1)
31
+ tilt (2.0.1)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ appraisal (= 1.0.0.beta3)
38
+ bundler (~> 1.3)
39
+ rake
40
+ rspec
41
+ shortcode!
data/README.md CHANGED
@@ -28,23 +28,12 @@ $ gem install shortcode
28
28
 
29
29
  ### Example
30
30
 
31
- 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.
32
-
33
- The shorthand wraps up the above code into a single method call for convenience
31
+ Shortcode is very simple to use, simply call the `process` method and pass it a string containing shortcode markup.
34
32
 
35
33
  ```ruby
36
34
  Shortcode.process("[quote]Hello World[/quote]")
37
35
  ```
38
36
 
39
- The longer form is good if you want to work with the intemediary hash returned by the parser
40
-
41
- ```ruby
42
- parser = Shortcode::Parser.new
43
- transformer = Shortcode::Transformer.new
44
- parsed_hash = parser.parse("[quote]Hello World[/quote]")
45
- transformer.apply(parsed_hash)
46
- ```
47
-
48
37
  ### Configuration
49
38
 
50
39
  ```ruby
@@ -61,6 +50,9 @@ Shortcode.setup do |config|
61
50
 
62
51
  # a list of self closing tags to support e.g. [youtube id="12345"]
63
52
  config.self_closing_tags = [:youtube]
53
+
54
+ # the type of quotes to use for attribute values, default is double quotes (")
55
+ config.quotes = '"'
64
56
  end
65
57
  ```
66
58
 
@@ -96,20 +88,21 @@ Three templates would be required to support the above content, `[:collapsible_l
96
88
 
97
89
  ### Presenters
98
90
 
99
- 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.
91
+ 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`, `content` and `additional_attributes` variables. Finally the class should define `content` and `attributes` methods.
100
92
 
101
93
  In a rails app you could return image records to the template using something like this:
102
94
 
103
95
  ```ruby
104
- class CustomPresenter
96
+ class GalleryPresenter
105
97
 
106
98
  def self.for
107
- :quote
99
+ :gallery
108
100
  end
109
101
 
110
- def initialize(attributes, content)
102
+ def initialize(attributes, content, additional_attributes)
111
103
  @attributes = attributes
112
104
  @content = content
105
+ @additional_attributes = additional_attributes
113
106
  end
114
107
 
115
108
  def content
@@ -128,6 +121,43 @@ class CustomPresenter
128
121
  end
129
122
  ```
130
123
 
124
+ #### Using additional attributes
125
+
126
+ At times you may want to pass through additional attributes to a presenter, for instance if you have a [gallery] shortcode tag and you want to pull out all images for a post, this can be achived using additional attributes with a presenter.
127
+
128
+ ```ruby
129
+ class GalleryPresenter
130
+
131
+ def self.for
132
+ :gallery
133
+ end
134
+
135
+ def initialize(attributes, content, additional_attributes)
136
+ @attributes = attributes
137
+ @content = content
138
+ @additional_attributes = additional_attributes
139
+ end
140
+
141
+ def content
142
+ @content
143
+ end
144
+
145
+ def attributes
146
+ { images: images }
147
+ end
148
+
149
+ private
150
+
151
+ def images
152
+ @additional_attributes[:images].map &:url
153
+ end
154
+ end
155
+
156
+ # The hash containing the images attribute is passed through to the presenter as the additional_attributes argument
157
+ Shortcode.process('[gallery]', { images: @post.images })
158
+
159
+ ```
160
+
131
161
  #### Registering presenters
132
162
 
133
163
  To register a presenter simply call `Shortcode.register_presenter` passing the presenter class e.g.
@@ -11,10 +11,14 @@ class Shortcode::Configuration
11
11
  # Set the supported self_closing_tags
12
12
  attr_accessor :self_closing_tags
13
13
 
14
+ # Set the quotation sign used for attribute values. Defaults to double quote (")
15
+ attr_accessor :quotes
16
+
14
17
  def initialize
15
18
  @template_parser = :haml
16
19
  @template_path = "app/views/shortcode_templates"
17
20
  @block_tags = []
18
21
  @self_closing_tags = []
22
+ @quotes = '"'
19
23
  end
20
24
  end
@@ -3,7 +3,7 @@ class Shortcode::Parser < Parslet::Parser
3
3
  rule(:block_tag) { match_any_of Shortcode.configuration.block_tags }
4
4
  rule(:self_closing_tag) { match_any_of Shortcode.configuration.self_closing_tags }
5
5
 
6
- rule(:quotes) { str('"') }
6
+ rule(:quotes) { str(Shortcode.configuration.quotes) }
7
7
 
8
8
  rule(:space) { str(' ').repeat(1) }
9
9
  rule(:space?) { space.maybe }
@@ -1,8 +1,9 @@
1
1
  class Shortcode::Presenter
2
2
 
3
- def initialize(name, attributes, content)
3
+ def initialize(name, attributes, content, additional_attributes)
4
4
  @attributes = attributes
5
5
  @content = content
6
+ @additional_attributes = additional_attributes
6
7
  initialize_custom_presenter(name)
7
8
  end
8
9
 
@@ -18,7 +19,7 @@ class Shortcode::Presenter
18
19
 
19
20
  def initialize_custom_presenter(name)
20
21
  if Shortcode.presenters.has_key? name.to_sym
21
- presenter = Shortcode.presenters[name.to_sym].new(@attributes, @content)
22
+ presenter = Shortcode.presenters[name.to_sym].new(@attributes, @content, @additional_attributes)
22
23
  @attributes = presenter.attributes
23
24
  @content = presenter.content
24
25
  end
@@ -0,0 +1,17 @@
1
+ class Shortcode::Processor
2
+
3
+ def process(string, additional_attributes=nil)
4
+ transformer.apply parser.parse(string), additional_attributes: additional_attributes
5
+ end
6
+
7
+ private
8
+
9
+ def parser
10
+ @parser ||= Shortcode::Parser.new
11
+ end
12
+
13
+ def transformer
14
+ @transformer ||= Shortcode::Transformer.new
15
+ end
16
+
17
+ end
data/lib/shortcode/tag.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  class Shortcode::Tag
2
2
 
3
- def initialize(name, attributes=[], content='')
3
+ def initialize(name, attributes=[], content='', additional_attributes=nil)
4
4
  @name = name.downcase
5
- presenter = Shortcode::Presenter.new name, set_attributes(attributes), content
5
+ presenter = Shortcode::Presenter.new name, set_attributes(attributes), content, additional_attributes
6
6
  @attributes = presenter.attributes
7
7
  @content = presenter.content
8
8
  end
@@ -6,11 +6,12 @@ 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, inner.join).render }
9
+ ) { Shortcode::Tag.new(name.to_s, options, inner.join, additional_attributes).render }
10
10
  rule(
11
11
  open_close: simple(:name),
12
12
  options: subtree(:options)
13
- ) { Shortcode::Tag.new(name.to_s, options).render }
13
+ ) { Shortcode::Tag.new(name.to_s, options, '', additional_attributes).render }
14
14
 
15
15
  rule(body: sequence(:strings)) { strings.join }
16
+
16
17
  end
@@ -1,3 +1,3 @@
1
1
  module Shortcode
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/shortcode.rb CHANGED
@@ -3,32 +3,31 @@ require 'haml'
3
3
  require 'erb'
4
4
 
5
5
  module Shortcode
6
- extend self
7
6
 
8
- attr_accessor :configuration, :presenters
9
- self.presenters = {}
7
+ class << self
8
+ attr_writer :configuration, :presenters
9
+ end
10
10
 
11
- def setup
12
- self.configuration ||= Configuration.new
13
- yield configuration
11
+ def self.process(string, additional_attributes=nil)
12
+ Shortcode::Processor.new.process string, additional_attributes
14
13
  end
15
14
 
16
- def process(code)
17
- transformer.apply(parser.parse(code))
15
+ def self.setup
16
+ yield configuration
18
17
  end
19
18
 
20
- def register_presenter(presenter)
21
- self.presenters[presenter.for.to_sym] = presenter
19
+ def self.register_presenter(presenter)
20
+ presenters[presenter.for.to_sym] = presenter
22
21
  end
23
22
 
24
23
  private
25
24
 
26
- def parser
27
- @@parser = Shortcode::Parser.new
25
+ def self.presenters
26
+ @presenters ||= {}
28
27
  end
29
28
 
30
- def transformer
31
- @@transformer = Shortcode::Transformer.new
29
+ def self.configuration
30
+ @configuration ||= Configuration.new
32
31
  end
33
32
 
34
33
  end
@@ -37,6 +36,7 @@ require 'shortcode/version'
37
36
  require 'shortcode/configuration'
38
37
  require 'shortcode/parser'
39
38
  require 'shortcode/presenter'
39
+ require 'shortcode/processor'
40
40
  require 'shortcode/transformer'
41
41
  require 'shortcode/tag'
42
42
  require 'shortcode/exceptions'
data/shortcode.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "parslet", "1.5.0"
21
+ spec.add_dependency "parslet", "1.6.0"
22
22
  spec.add_dependency "haml", "~> 4.0"
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.3"
data/spec/parser_spec.rb CHANGED
@@ -25,6 +25,22 @@ describe Shortcode::Parser do
25
25
  end
26
26
  end
27
27
 
28
+ it "parses quotes using custom quotations" do
29
+ Shortcode.setup do |config|
30
+ config.quotes = "'"
31
+ end
32
+
33
+ quotes.each do |string|
34
+ # Change double quotes to single quotes in the fixture
35
+ parser.should parse(string.gsub '"', "'")
36
+ end
37
+
38
+ # Reset configuration to default
39
+ Shortcode.setup do |config|
40
+ config.quotes = '"'
41
+ end
42
+ end
43
+
28
44
  it "parses list shortcodes" do
29
45
  collapsible_lists.each do |string|
30
46
  parser.should parse(string)
@@ -32,7 +32,7 @@ require 'benchmark'
32
32
 
33
33
  # it "runs quickly" do
34
34
  # Benchmark.realtime {
35
- # transformer.apply(parsed_hash)
35
+ # transformer.apply(parsed_hash, additional_attributes: nil)
36
36
  # }.should < 0.1
37
37
  # end
38
38
 
@@ -2,15 +2,16 @@ require 'spec_helper'
2
2
  require 'parslet/rig/rspec'
3
3
  require 'pp'
4
4
 
5
- class MyPrsenter
5
+ class MyPresenter
6
6
 
7
7
  def self.for
8
8
  :quote
9
9
  end
10
10
 
11
- def initialize(attributes, content)
11
+ def initialize(attributes, content, additional_attributes)
12
12
  @attributes = attributes
13
13
  @content = content
14
+ @additional_attributes = additional_attributes
14
15
  end
15
16
 
16
17
  def content
@@ -18,23 +19,28 @@ class MyPrsenter
18
19
  end
19
20
 
20
21
  def attributes
21
- { title: "my custom title" }
22
+ @additional_attributes || { title: "my custom title" }
22
23
  end
23
24
  end
24
25
 
25
26
  describe Shortcode::Presenter do
26
27
 
27
- let(:simple_quote) { load_fixture :simple_quote }
28
- let(:simple_quote_output) { load_fixture :simple_quote_presenter_output, :html }
28
+ let(:simple_quote) { load_fixture :simple_quote }
29
+ let(:presenter_output) { load_fixture :simple_quote_presenter_output, :html }
30
+ let(:attributes_output) { load_fixture :simple_quote_presenter_attributes_output, :html }
29
31
 
30
32
  describe "using a custom presenter" do
31
33
 
32
34
  before do
33
- Shortcode.register_presenter MyPrsenter
35
+ Shortcode.register_presenter MyPresenter
34
36
  end
35
37
 
36
- it "uses the custome attributes" do
37
- Shortcode.process(simple_quote).gsub("\n",'').should == simple_quote_output.gsub("\n",'')
38
+ it "uses the custom attributes" do
39
+ Shortcode.process(simple_quote).gsub("\n",'').should == presenter_output.gsub("\n",'')
40
+ end
41
+
42
+ it "passes through additional attributes" do
43
+ Shortcode.process(simple_quote, { title: 'Additional attribute title' }).gsub("\n",'').should == attributes_output.gsub("\n",'')
38
44
  end
39
45
 
40
46
  end
@@ -0,0 +1,6 @@
1
+ <blockquote>
2
+ <p class='quotation'>hello</p>
3
+ <p class='citation'>
4
+ <span class='position'>Additional attribute title</span>
5
+ </p>
6
+ </blockquote>
@@ -33,7 +33,7 @@ describe Shortcode do
33
33
 
34
34
  it "converts into html" do
35
35
  obj = parser.parse(simple_quote)
36
- html = transformer.apply obj
36
+ html = transformer.apply obj, additional_attributes: nil
37
37
  html.should == simple_quote_output
38
38
  end
39
39
 
@@ -42,7 +42,7 @@ describe Shortcode do
42
42
  context "full_quote" do
43
43
 
44
44
  it "converts into html" do
45
- html = transformer.apply(parser.parse(full_quote))
45
+ html = transformer.apply(parser.parse(full_quote), additional_attributes: nil)
46
46
  html.should == full_quote_output
47
47
  end
48
48
 
@@ -51,7 +51,7 @@ describe Shortcode do
51
51
  context "quote_with_extras" do
52
52
 
53
53
  it "converts into html" do
54
- html = transformer.apply(parser.parse(quote_with_extras))
54
+ html = transformer.apply(parser.parse(quote_with_extras), additional_attributes: nil)
55
55
  html.should == quote_with_extras_output
56
56
  end
57
57
 
@@ -60,7 +60,7 @@ describe Shortcode do
60
60
  context "simple_list" do
61
61
 
62
62
  it "converts into html" do
63
- html = transformer.apply(parser.parse(simple_list))
63
+ html = transformer.apply(parser.parse(simple_list), additional_attributes: nil)
64
64
  html.should == simple_list_output
65
65
  end
66
66
 
@@ -69,7 +69,7 @@ describe Shortcode do
69
69
  context "timeline_event" do
70
70
 
71
71
  it "converts into html" do
72
- html = transformer.apply(parser.parse(timeline_event))
72
+ html = transformer.apply(parser.parse(timeline_event), additional_attributes: nil)
73
73
  html.should == timeline_event_output
74
74
  end
75
75
 
@@ -78,7 +78,7 @@ describe Shortcode do
78
78
  context "timeline_info" do
79
79
 
80
80
  it "converts into html" do
81
- html = transformer.apply(parser.parse(timeline_info))
81
+ html = transformer.apply(parser.parse(timeline_info), additional_attributes: nil)
82
82
  html.should == timeline_info_output
83
83
  end
84
84
 
@@ -87,7 +87,7 @@ describe Shortcode do
87
87
  context "timeline_person" do
88
88
 
89
89
  it "converts into html" do
90
- html = transformer.apply(parser.parse(timeline_person))
90
+ html = transformer.apply(parser.parse(timeline_person), additional_attributes: nil)
91
91
  html.should == timeline_person_output
92
92
  end
93
93
 
@@ -96,7 +96,7 @@ describe Shortcode do
96
96
  context "complex_snippet" do
97
97
 
98
98
  it "converts into html" do
99
- html = transformer.apply(parser.parse(complex_snippet))
99
+ html = transformer.apply(parser.parse(complex_snippet), additional_attributes: nil)
100
100
  html.should == complex_snippet_output
101
101
  end
102
102
  end
@@ -111,7 +111,7 @@ describe Shortcode do
111
111
  end
112
112
 
113
113
  it "converts into html" do
114
- html = transformer.apply(parser.parse(simple_quote))
114
+ html = transformer.apply(parser.parse(simple_quote), additional_attributes: nil)
115
115
  html.gsub("\n",'').should == simple_quote_output.gsub("\n",'')
116
116
  end
117
117
  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.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-18 00:00:00.000000000 Z
12
+ date: 2014-05-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parslet
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - '='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.5.0
21
+ version: 1.6.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - '='
28
28
  - !ruby/object:Gem::Version
29
- version: 1.5.0
29
+ version: 1.6.0
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: haml
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -103,6 +103,7 @@ files:
103
103
  - .travis.yml
104
104
  - Appraisals
105
105
  - Gemfile
106
+ - Gemfile.lock
106
107
  - LICENSE.txt
107
108
  - README.md
108
109
  - Rakefile
@@ -115,6 +116,7 @@ files:
115
116
  - lib/shortcode/exceptions.rb
116
117
  - lib/shortcode/parser.rb
117
118
  - lib/shortcode/presenter.rb
119
+ - lib/shortcode/processor.rb
118
120
  - lib/shortcode/railtie.rb
119
121
  - lib/shortcode/tag.rb
120
122
  - lib/shortcode/transformer.rb
@@ -141,6 +143,7 @@ files:
141
143
  - spec/support/fixtures/simple_list_output.html
142
144
  - spec/support/fixtures/simple_quote.txt
143
145
  - spec/support/fixtures/simple_quote_output.html
146
+ - spec/support/fixtures/simple_quote_presenter_attributes_output.html
144
147
  - spec/support/fixtures/simple_quote_presenter_output.html
145
148
  - spec/support/fixtures/timeline_event.txt
146
149
  - spec/support/fixtures/timeline_event_output.html
@@ -206,6 +209,7 @@ test_files:
206
209
  - spec/support/fixtures/simple_list_output.html
207
210
  - spec/support/fixtures/simple_quote.txt
208
211
  - spec/support/fixtures/simple_quote_output.html
212
+ - spec/support/fixtures/simple_quote_presenter_attributes_output.html
209
213
  - spec/support/fixtures/simple_quote_presenter_output.html
210
214
  - spec/support/fixtures/timeline_event.txt
211
215
  - spec/support/fixtures/timeline_event_output.html