shortcode 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +68 -19
- data/lib/shortcode.rb +16 -2
- data/lib/shortcode/tag.rb +7 -7
- data/lib/shortcode/version.rb +1 -1
- data/shortcode.gemspec +1 -1
- data/spec/shortcode_spec.rb +31 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/support/templates/erb/quote.html.erb +5 -5
- data/spec/support/templates/haml/collapsible_list.html.haml +1 -1
- data/spec/support/templates/haml/item.html.haml +2 -2
- data/spec/support/templates/haml/quote.html.haml +6 -6
- data/spec/support/templates/haml/timeline_event.html.haml +4 -4
- data/spec/support/templates/haml/timeline_info.html.haml +2 -2
- data/spec/support/templates/haml/timeline_person.html.haml +8 -8
- metadata +5 -3
data/README.md
CHANGED
@@ -1,45 +1,94 @@
|
|
1
1
|
# Shortcode
|
2
2
|
|
3
|
-
A ruby gem for parsing Wordpress style shortcodes. The gem uses a PEG (Parsing Expression Grammar) parser rather than using regular expressions so its easier to understand, test and extend.
|
3
|
+
A ruby gem for parsing Wordpress style shortcodes. The gem uses a [PEG](http://en.wikipedia.org/wiki/Parsing_expression_grammar) (Parsing Expression Grammar) parser rather than using regular expressions so its easier to understand, test and extend.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
|
9
|
+
```ruby
|
10
|
+
gem 'shortcode'
|
11
|
+
```
|
10
12
|
|
11
13
|
And then execute:
|
12
14
|
|
13
|
-
|
15
|
+
```
|
16
|
+
$ bundle
|
17
|
+
```
|
14
18
|
|
15
19
|
Or install it yourself as:
|
16
20
|
|
17
|
-
|
21
|
+
```
|
22
|
+
$ gem install shortcode
|
23
|
+
```
|
18
24
|
|
19
25
|
## Usage
|
20
26
|
|
21
|
-
Example
|
27
|
+
### Example
|
22
28
|
|
23
|
-
|
24
|
-
transformer = Shortcode::Transformer.new
|
25
|
-
transformer.apply(parser.parse("[quote]Hello World[/quote]"))
|
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.
|
26
30
|
|
27
|
-
|
31
|
+
The former is good if you want to work with the intemediary hash returned by the parser
|
28
32
|
|
29
|
-
|
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)
|
38
|
+
```
|
30
39
|
|
31
|
-
|
32
|
-
config.template_parser = :haml # :erb or :haml supported, :haml is default
|
40
|
+
The shorthand wraps up the above code into a single method call for convenience
|
33
41
|
|
34
|
-
|
35
|
-
|
42
|
+
```ruby
|
43
|
+
Shortcode.process("[quote]Hello World[/quote]")
|
44
|
+
```
|
36
45
|
|
37
|
-
|
38
|
-
config.block_tags = [:quote, :collapsible_list, :item, :timeline_person]
|
46
|
+
### Configuration
|
39
47
|
|
40
|
-
|
41
|
-
|
42
|
-
|
48
|
+
```ruby
|
49
|
+
Shortcode.setup do |config|
|
50
|
+
|
51
|
+
# the template parser to use
|
52
|
+
config.template_parser = :haml # :erb or :haml supported, :haml is default
|
53
|
+
|
54
|
+
# location of the template files
|
55
|
+
config.template_path = "support/templates/haml"
|
56
|
+
|
57
|
+
# a list of block tags to support e.g. [quote]Hello World[/quote]
|
58
|
+
config.block_tags = [:quote]
|
59
|
+
|
60
|
+
# a list of self closing tags to support e.g. [youtube id="12345"]
|
61
|
+
config.self_closing_tags = [:youtube]
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
### Templates
|
66
|
+
|
67
|
+
Each shortcode tag needs a template file to translate the shortcode into html. Templates can be written in HAML or erb and work in
|
68
|
+
a similar way to views in Rails. The main content of a tag is passed via the instance variable `@content`. Any attributes defined on a tag are passed in via an `@attributes` hash, shortcodes can have any number of attributes. For instance a quote shortcode might look like this:
|
69
|
+
|
70
|
+
[quote author="Homer Simpson"]Doh![/quote]
|
71
|
+
|
72
|
+
And the haml template to render the shortcode
|
73
|
+
|
74
|
+
```haml
|
75
|
+
%blockquote
|
76
|
+
%p.quotation= @content
|
77
|
+
-if @attributes[:author]
|
78
|
+
%p.citation
|
79
|
+
%span.author= @attributes[:author]
|
80
|
+
```
|
81
|
+
|
82
|
+
Shortcodes can be nested inside other shortcodes, there are no limits imposed on the nesting depth. This can be useful when creating complex content such as a collapsible list that can have any content inside each element. We could have the following shortcodes
|
83
|
+
|
84
|
+
[collapsible_list]
|
85
|
+
[item]
|
86
|
+
[youtube id="12345"]
|
87
|
+
[/item]
|
88
|
+
[item]Hellow World[/item]
|
89
|
+
[/collapsible_list]
|
90
|
+
|
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.
|
43
92
|
|
44
93
|
## Contributing
|
45
94
|
|
data/lib/shortcode.rb
CHANGED
@@ -15,16 +15,30 @@ module Shortcode
|
|
15
15
|
|
16
16
|
# Set the supported block_tags
|
17
17
|
mattr_accessor :block_tags
|
18
|
-
@@block_tags = [:quote
|
18
|
+
@@block_tags = [:quote]
|
19
19
|
|
20
20
|
# Set the supported self_closing_tags
|
21
21
|
mattr_accessor :self_closing_tags
|
22
|
-
@@self_closing_tags = [:
|
22
|
+
@@self_closing_tags = [:youtube]
|
23
23
|
|
24
24
|
def self.setup
|
25
25
|
yield self
|
26
26
|
end
|
27
27
|
|
28
|
+
def self.process(code)
|
29
|
+
transformer.apply(parser.parse(code))
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def self.parser
|
35
|
+
@@parser ||= Shortcode::Parser.new
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.transformer
|
39
|
+
@@transformer ||= Shortcode::Transformer.new
|
40
|
+
end
|
41
|
+
|
28
42
|
end
|
29
43
|
|
30
44
|
require 'shortcode/version'
|
data/lib/shortcode/tag.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
class Shortcode::Tag
|
2
2
|
|
3
|
-
def initialize(name,
|
3
|
+
def initialize(name, attributes=[])
|
4
4
|
@name = name.downcase
|
5
|
-
|
5
|
+
set_attributes attributes
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
8
|
+
def set_attributes(attributes)
|
9
9
|
hash = {}
|
10
|
-
|
11
|
-
@
|
10
|
+
attributes.each { |o| hash[o[:key].to_sym] = o[:value] }
|
11
|
+
@attributes = hash
|
12
12
|
end
|
13
13
|
|
14
14
|
def markup
|
@@ -18,8 +18,8 @@ class Shortcode::Tag
|
|
18
18
|
raise Shortcode::TemplateNotFound, "Searched in:", template_files
|
19
19
|
end
|
20
20
|
|
21
|
-
def wrap(
|
22
|
-
@
|
21
|
+
def wrap(content='')
|
22
|
+
@content = content
|
23
23
|
render_template
|
24
24
|
end
|
25
25
|
|
data/lib/shortcode/version.rb
CHANGED
data/shortcode.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["jamie@kernowsoul.com"]
|
11
11
|
spec.description = "Gem for parsing wordpress style shortcodes"
|
12
12
|
spec.summary = "Gem for parsing wordpress style shortcodes"
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "https://github.com/kernow/shortcode"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'parslet/rig/rspec'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
describe Shortcode do
|
6
|
+
|
7
|
+
let(:simple_quote) { load_fixture :simple_quote }
|
8
|
+
let(:simple_quote_output) { load_fixture :simple_quote_output, :html }
|
9
|
+
|
10
|
+
context "simple_quote" do
|
11
|
+
|
12
|
+
it "converts into html" do
|
13
|
+
Shortcode.process(simple_quote).should == simple_quote_output
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
context "erb templates" do
|
19
|
+
|
20
|
+
before(:each) do
|
21
|
+
Shortcode.setup do |config|
|
22
|
+
config.template_parser = :erb
|
23
|
+
config.template_path = File.join File.dirname(__FILE__), "support/templates/erb"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it "converts into html" do
|
28
|
+
Shortcode.process(simple_quote).gsub("\n",'').should == simple_quote_output.gsub("\n",'')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -10,6 +10,8 @@ RSpec.configure do |config|
|
|
10
10
|
Shortcode.setup do |config|
|
11
11
|
config.template_parser = :haml
|
12
12
|
config.template_path = File.join File.dirname(__FILE__), "support/templates/haml"
|
13
|
+
config.block_tags = [:quote, :collapsible_list, :item, :timeline_person]
|
14
|
+
config.self_closing_tags = [:timeline_event, :timeline_info]
|
13
15
|
end
|
14
16
|
end
|
15
17
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
<blockquote>
|
2
|
-
<p class='quotation'><%= @
|
3
|
-
<% if @
|
2
|
+
<p class='quotation'><%= @content %></p>
|
3
|
+
<% if @attributes[:author] || @attributes[:title] %>
|
4
4
|
<p class='citation'>
|
5
|
-
<% if @
|
6
|
-
<span class='author'><%= @
|
5
|
+
<% if @attributes[:author] %>
|
6
|
+
<span class='author'><%= @attributes[:author] %></span>
|
7
7
|
<% end %>
|
8
|
-
<% if @
|
8
|
+
<% if @attributes[:title] <% end %><span class='position'><%= @attributes[:title] %></span>
|
9
9
|
<% end %>
|
10
10
|
</p>
|
11
11
|
<% end %>
|
@@ -1,2 +1,2 @@
|
|
1
1
|
.collapsible
|
2
|
-
= @
|
2
|
+
= @content
|
@@ -1,3 +1,3 @@
|
|
1
1
|
%section
|
2
|
-
%h5= @
|
3
|
-
%div= @
|
2
|
+
%h5= @attributes[:title]
|
3
|
+
%div= @content
|
@@ -1,8 +1,8 @@
|
|
1
1
|
%blockquote
|
2
|
-
%p.quotation= @
|
3
|
-
-if @
|
2
|
+
%p.quotation= @content
|
3
|
+
-if @attributes[:author] || @attributes[:title]
|
4
4
|
%p.citation
|
5
|
-
-if @
|
6
|
-
%span.author= @
|
7
|
-
-if @
|
8
|
-
%span.position= @
|
5
|
+
-if @attributes[:author]
|
6
|
+
%span.author= @attributes[:author]
|
7
|
+
-if @attributes[:title]
|
8
|
+
%span.position= @attributes[:title]
|
@@ -3,8 +3,8 @@
|
|
3
3
|
%tbody
|
4
4
|
%tr
|
5
5
|
%th{scope: :row}
|
6
|
-
= @
|
6
|
+
= @attributes[:date]
|
7
7
|
%td.event
|
8
|
-
%span.description= @
|
9
|
-
-if @
|
10
|
-
%a.read_more{href: @
|
8
|
+
%span.description= @attributes[:title]
|
9
|
+
-if @attributes[:link]
|
10
|
+
%a.read_more{href: @attributes[:link]} Read More
|
@@ -3,13 +3,13 @@
|
|
3
3
|
%tbody
|
4
4
|
%tr
|
5
5
|
%th{scope: :row}
|
6
|
-
= @
|
6
|
+
= @attributes[:date]
|
7
7
|
%td.team_news
|
8
|
-
%img{src: @
|
9
|
-
%h4= @
|
10
|
-
%span.name= @
|
11
|
-
%span.position= @
|
12
|
-
-if @
|
13
|
-
%a.read_more{href: @
|
8
|
+
%img{src: @attributes[:image]}
|
9
|
+
%h4= @attributes[:title]
|
10
|
+
%span.name= @attributes[:name]
|
11
|
+
%span.position= @attributes[:position]
|
12
|
+
-if @attributes[:link]
|
13
|
+
%a.read_more{href: @attributes[:link]} View Profile
|
14
14
|
.description
|
15
|
-
%p= @
|
15
|
+
%p= @content
|
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
|
+
version: 0.0.2
|
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: 2013-08-
|
12
|
+
date: 2013-08-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: parslet
|
@@ -128,6 +128,7 @@ files:
|
|
128
128
|
- lib/shortcode/version.rb
|
129
129
|
- shortcode.gemspec
|
130
130
|
- spec/parser_spec.rb
|
131
|
+
- spec/shortcode_spec.rb
|
131
132
|
- spec/spec_helper.rb
|
132
133
|
- spec/support/fixtures.rb
|
133
134
|
- spec/support/fixtures/complex_snippet.txt
|
@@ -155,7 +156,7 @@ files:
|
|
155
156
|
- spec/support/templates/haml/timeline_person.html.haml
|
156
157
|
- spec/tag_spec.rb
|
157
158
|
- spec/transformer_spec.rb
|
158
|
-
homepage:
|
159
|
+
homepage: https://github.com/kernow/shortcode
|
159
160
|
licenses:
|
160
161
|
- MIT
|
161
162
|
post_install_message:
|
@@ -182,6 +183,7 @@ specification_version: 3
|
|
182
183
|
summary: Gem for parsing wordpress style shortcodes
|
183
184
|
test_files:
|
184
185
|
- spec/parser_spec.rb
|
186
|
+
- spec/shortcode_spec.rb
|
185
187
|
- spec/spec_helper.rb
|
186
188
|
- spec/support/fixtures.rb
|
187
189
|
- spec/support/fixtures/complex_snippet.txt
|