virgo 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generators/virgo/install/install_generator.rb +20 -0
- data/lib/generators/virgo/migrations/migrations_generator.rb +19 -0
- data/lib/generators/virgo/schedule/schedule_generator.rb +15 -0
- data/lib/generators/virgo/views/views_generator.rb +22 -0
- data/lib/tasks/virgo_tasks.rake +7 -0
- data/lib/virgo.rb +4 -0
- data/lib/virgo/development_mail_interceptor.rb +6 -0
- data/lib/virgo/email_validator.rb +6 -0
- data/lib/virgo/engine.rb +64 -0
- data/lib/virgo/reduce_validator.rb +6 -0
- data/lib/virgo/upload_helpers.rb +89 -0
- data/lib/virgo/version.rb +3 -0
- data/lib/virgo/view_help.rb +3 -0
- data/vendor/gems/shortcode/Appraisals +15 -0
- data/vendor/gems/shortcode/CHANGELOG.md +162 -0
- data/vendor/gems/shortcode/Gemfile +6 -0
- data/vendor/gems/shortcode/Gemfile.lock +168 -0
- data/vendor/gems/shortcode/LICENSE.txt +22 -0
- data/vendor/gems/shortcode/README.md +268 -0
- data/vendor/gems/shortcode/Rakefile +3 -0
- data/vendor/gems/shortcode/gemfiles/rails_3.2.gemfile +8 -0
- data/vendor/gems/shortcode/gemfiles/rails_4.0.gemfile +8 -0
- data/vendor/gems/shortcode/gemfiles/rails_4.1.gemfile +8 -0
- data/vendor/gems/shortcode/gemfiles/rails_4.2.gemfile +8 -0
- data/vendor/gems/shortcode/lib/shortcode.rb +53 -0
- data/vendor/gems/shortcode/lib/shortcode/configuration.rb +36 -0
- data/vendor/gems/shortcode/lib/shortcode/exceptions.rb +9 -0
- data/vendor/gems/shortcode/lib/shortcode/parser.rb +43 -0
- data/vendor/gems/shortcode/lib/shortcode/presenter.rb +49 -0
- data/vendor/gems/shortcode/lib/shortcode/processor.rb +17 -0
- data/vendor/gems/shortcode/lib/shortcode/railtie.rb +3 -0
- data/vendor/gems/shortcode/lib/shortcode/tag.rb +54 -0
- data/vendor/gems/shortcode/lib/shortcode/template_binding.rb +33 -0
- data/vendor/gems/shortcode/lib/shortcode/transformer.rb +17 -0
- data/vendor/gems/shortcode/lib/shortcode/version.rb +3 -0
- data/vendor/gems/shortcode/shortcode.gemspec +30 -0
- data/vendor/gems/shortcode/spec/parser_spec.rb +267 -0
- data/vendor/gems/shortcode/spec/performance_spec.rb +41 -0
- data/vendor/gems/shortcode/spec/presenter_spec.rb +126 -0
- data/vendor/gems/shortcode/spec/rails_helpers_spec.rb +72 -0
- data/vendor/gems/shortcode/spec/shortcode_spec.rb +57 -0
- data/vendor/gems/shortcode/spec/spec_helper.rb +32 -0
- data/vendor/gems/shortcode/spec/support/fixtures.rb +9 -0
- data/vendor/gems/shortcode/spec/support/fixtures/block_with_whitespace.txt +2 -0
- data/vendor/gems/shortcode/spec/support/fixtures/block_with_whitespace_output.html +2 -0
- data/vendor/gems/shortcode/spec/support/fixtures/complex_snippet.txt +13 -0
- data/vendor/gems/shortcode/spec/support/fixtures/complex_snippet_output.html +33 -0
- data/vendor/gems/shortcode/spec/support/fixtures/custom_helper.txt +1 -0
- data/vendor/gems/shortcode/spec/support/fixtures/custom_helper_output.html +1 -0
- data/vendor/gems/shortcode/spec/support/fixtures/full_quote.txt +1 -0
- data/vendor/gems/shortcode/spec/support/fixtures/full_quote_output.html +7 -0
- data/vendor/gems/shortcode/spec/support/fixtures/item.txt +1 -0
- data/vendor/gems/shortcode/spec/support/fixtures/item_presenter_attributes_output.html +4 -0
- data/vendor/gems/shortcode/spec/support/fixtures/item_presenter_output.html +4 -0
- data/vendor/gems/shortcode/spec/support/fixtures/long_text.txt +39 -0
- data/vendor/gems/shortcode/spec/support/fixtures/quote_with_extras.txt +1 -0
- data/vendor/gems/shortcode/spec/support/fixtures/quote_with_extras_output.html +7 -0
- data/vendor/gems/shortcode/spec/support/fixtures/rails_helper.txt +1 -0
- data/vendor/gems/shortcode/spec/support/fixtures/rails_helper_output_erb.html +1 -0
- data/vendor/gems/shortcode/spec/support/fixtures/rails_helper_output_haml.html +1 -0
- data/vendor/gems/shortcode/spec/support/fixtures/rails_helper_output_slim.html +1 -0
- data/vendor/gems/shortcode/spec/support/fixtures/simple_list.txt +5 -0
- data/vendor/gems/shortcode/spec/support/fixtures/simple_list_output.html +15 -0
- data/vendor/gems/shortcode/spec/support/fixtures/simple_quote.txt +1 -0
- data/vendor/gems/shortcode/spec/support/fixtures/simple_quote_output.html +3 -0
- data/vendor/gems/shortcode/spec/support/fixtures/simple_quote_presenter_attributes_output.html +6 -0
- data/vendor/gems/shortcode/spec/support/fixtures/simple_quote_presenter_output.html +6 -0
- data/vendor/gems/shortcode/spec/support/fixtures/timeline_event.txt +1 -0
- data/vendor/gems/shortcode/spec/support/fixtures/timeline_event_output.html +15 -0
- data/vendor/gems/shortcode/spec/support/fixtures/timeline_info.txt +1 -0
- data/vendor/gems/shortcode/spec/support/fixtures/timeline_info_output.html +14 -0
- data/vendor/gems/shortcode/spec/support/fixtures/timeline_person.txt +4 -0
- data/vendor/gems/shortcode/spec/support/fixtures/timeline_person_output.html +23 -0
- data/vendor/gems/shortcode/spec/support/fixtures/without_quotes.txt +1 -0
- data/vendor/gems/shortcode/spec/support/presenters/missing_attributes_presenter.rb +12 -0
- data/vendor/gems/shortcode/spec/support/presenters/missing_content_presenter.rb +12 -0
- data/vendor/gems/shortcode/spec/support/presenters/missing_for_presenter.rb +12 -0
- data/vendor/gems/shortcode/spec/support/presenters/missing_initialize_presenter.rb +12 -0
- data/vendor/gems/shortcode/spec/support/presenters/multiple_presenter.rb +19 -0
- data/vendor/gems/shortcode/spec/support/presenters/my_presenter.rb +19 -0
- data/vendor/gems/shortcode/spec/support/presenters/other_presenter.rb +19 -0
- data/vendor/gems/shortcode/spec/support/templates/erb/collapsible_list.html.erb +3 -0
- data/vendor/gems/shortcode/spec/support/templates/erb/custom_helper.html.erb +1 -0
- data/vendor/gems/shortcode/spec/support/templates/erb/item.html.erb +4 -0
- data/vendor/gems/shortcode/spec/support/templates/erb/quote.html.erb +13 -0
- data/vendor/gems/shortcode/spec/support/templates/erb/rails_helper.html.erb +1 -0
- data/vendor/gems/shortcode/spec/support/templates/erb/timeline_event.html.erb +17 -0
- data/vendor/gems/shortcode/spec/support/templates/erb/timeline_info.html.erb +15 -0
- data/vendor/gems/shortcode/spec/support/templates/erb/timeline_person.html.erb +23 -0
- data/vendor/gems/shortcode/spec/support/templates/haml/quote.html.haml +8 -0
- data/vendor/gems/shortcode/spec/support/templates/haml/rails_helper.html.haml +1 -0
- data/vendor/gems/shortcode/spec/support/templates/slim/quote.html.slim +8 -0
- data/vendor/gems/shortcode/spec/support/templates/slim/rails_helper.html.slim +1 -0
- data/vendor/gems/shortcode/spec/tag_spec.rb +67 -0
- data/vendor/gems/shortcode/spec/template_parsers_spec.rb +46 -0
- data/vendor/gems/shortcode/spec/transformer_spec.rb +122 -0
- metadata +97 -1
@@ -0,0 +1,168 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
shortcode (1.1.0)
|
5
|
+
parslet (= 1.7.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionmailer (4.2.1)
|
11
|
+
actionpack (= 4.2.1)
|
12
|
+
actionview (= 4.2.1)
|
13
|
+
activejob (= 4.2.1)
|
14
|
+
mail (~> 2.5, >= 2.5.4)
|
15
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
16
|
+
actionpack (4.2.1)
|
17
|
+
actionview (= 4.2.1)
|
18
|
+
activesupport (= 4.2.1)
|
19
|
+
rack (~> 1.6)
|
20
|
+
rack-test (~> 0.6.2)
|
21
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
22
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.1)
|
23
|
+
actionview (4.2.1)
|
24
|
+
activesupport (= 4.2.1)
|
25
|
+
builder (~> 3.1)
|
26
|
+
erubis (~> 2.7.0)
|
27
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
28
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.1)
|
29
|
+
activejob (4.2.1)
|
30
|
+
activesupport (= 4.2.1)
|
31
|
+
globalid (>= 0.3.0)
|
32
|
+
activemodel (4.2.1)
|
33
|
+
activesupport (= 4.2.1)
|
34
|
+
builder (~> 3.1)
|
35
|
+
activerecord (4.2.1)
|
36
|
+
activemodel (= 4.2.1)
|
37
|
+
activesupport (= 4.2.1)
|
38
|
+
arel (~> 6.0)
|
39
|
+
activesupport (4.2.1)
|
40
|
+
i18n (~> 0.7)
|
41
|
+
json (~> 1.7, >= 1.7.7)
|
42
|
+
minitest (~> 5.1)
|
43
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
44
|
+
tzinfo (~> 1.1)
|
45
|
+
appraisal (2.0.1)
|
46
|
+
activesupport (>= 3.2.21)
|
47
|
+
bundler
|
48
|
+
rake
|
49
|
+
thor (>= 0.14.0)
|
50
|
+
arel (6.0.2)
|
51
|
+
blankslate (3.1.3)
|
52
|
+
builder (3.2.2)
|
53
|
+
coveralls (0.8.1)
|
54
|
+
json (~> 1.8)
|
55
|
+
rest-client (>= 1.6.8, < 2)
|
56
|
+
simplecov (~> 0.10.0)
|
57
|
+
term-ansicolor (~> 1.3)
|
58
|
+
thor (~> 0.19.1)
|
59
|
+
diff-lcs (1.2.5)
|
60
|
+
docile (1.1.5)
|
61
|
+
domain_name (0.5.24)
|
62
|
+
unf (>= 0.0.5, < 1.0.0)
|
63
|
+
erubis (2.7.0)
|
64
|
+
globalid (0.3.5)
|
65
|
+
activesupport (>= 4.1.0)
|
66
|
+
haml (4.0.6)
|
67
|
+
tilt
|
68
|
+
http-cookie (1.0.2)
|
69
|
+
domain_name (~> 0.5)
|
70
|
+
i18n (0.7.0)
|
71
|
+
json (1.8.2)
|
72
|
+
loofah (2.0.2)
|
73
|
+
nokogiri (>= 1.5.9)
|
74
|
+
mail (2.6.3)
|
75
|
+
mime-types (>= 1.16, < 3)
|
76
|
+
mime-types (2.5)
|
77
|
+
mini_portile (0.6.2)
|
78
|
+
minitest (5.6.1)
|
79
|
+
netrc (0.10.3)
|
80
|
+
nokogiri (1.6.6.2)
|
81
|
+
mini_portile (~> 0.6.0)
|
82
|
+
parslet (1.7.0)
|
83
|
+
blankslate (>= 2.0, <= 4.0)
|
84
|
+
rack (1.6.4)
|
85
|
+
rack-test (0.6.3)
|
86
|
+
rack (>= 1.0)
|
87
|
+
rails (4.2.1)
|
88
|
+
actionmailer (= 4.2.1)
|
89
|
+
actionpack (= 4.2.1)
|
90
|
+
actionview (= 4.2.1)
|
91
|
+
activejob (= 4.2.1)
|
92
|
+
activemodel (= 4.2.1)
|
93
|
+
activerecord (= 4.2.1)
|
94
|
+
activesupport (= 4.2.1)
|
95
|
+
bundler (>= 1.3.0, < 2.0)
|
96
|
+
railties (= 4.2.1)
|
97
|
+
sprockets-rails
|
98
|
+
rails-deprecated_sanitizer (1.0.3)
|
99
|
+
activesupport (>= 4.2.0.alpha)
|
100
|
+
rails-dom-testing (1.0.6)
|
101
|
+
activesupport (>= 4.2.0.beta, < 5.0)
|
102
|
+
nokogiri (~> 1.6.0)
|
103
|
+
rails-deprecated_sanitizer (>= 1.0.1)
|
104
|
+
rails-html-sanitizer (1.0.2)
|
105
|
+
loofah (~> 2.0)
|
106
|
+
railties (4.2.1)
|
107
|
+
actionpack (= 4.2.1)
|
108
|
+
activesupport (= 4.2.1)
|
109
|
+
rake (>= 0.8.7)
|
110
|
+
thor (>= 0.18.1, < 2.0)
|
111
|
+
rake (10.4.2)
|
112
|
+
rest-client (1.8.0)
|
113
|
+
http-cookie (>= 1.0.2, < 2.0)
|
114
|
+
mime-types (>= 1.16, < 3.0)
|
115
|
+
netrc (~> 0.7)
|
116
|
+
rspec (3.2.0)
|
117
|
+
rspec-core (~> 3.2.0)
|
118
|
+
rspec-expectations (~> 3.2.0)
|
119
|
+
rspec-mocks (~> 3.2.0)
|
120
|
+
rspec-core (3.2.3)
|
121
|
+
rspec-support (~> 3.2.0)
|
122
|
+
rspec-expectations (3.2.1)
|
123
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
124
|
+
rspec-support (~> 3.2.0)
|
125
|
+
rspec-mocks (3.2.1)
|
126
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
127
|
+
rspec-support (~> 3.2.0)
|
128
|
+
rspec-support (3.2.2)
|
129
|
+
simplecov (0.10.0)
|
130
|
+
docile (~> 1.1.0)
|
131
|
+
json (~> 1.8)
|
132
|
+
simplecov-html (~> 0.10.0)
|
133
|
+
simplecov-html (0.10.0)
|
134
|
+
slim (3.0.3)
|
135
|
+
temple (~> 0.7.3)
|
136
|
+
tilt (>= 1.3.3, < 2.1)
|
137
|
+
sprockets (3.2.0)
|
138
|
+
rack (~> 1.0)
|
139
|
+
sprockets-rails (2.3.2)
|
140
|
+
actionpack (>= 3.0)
|
141
|
+
activesupport (>= 3.0)
|
142
|
+
sprockets (>= 2.8, < 4.0)
|
143
|
+
temple (0.7.5)
|
144
|
+
term-ansicolor (1.3.0)
|
145
|
+
tins (~> 1.0)
|
146
|
+
thor (0.19.1)
|
147
|
+
thread_safe (0.3.5)
|
148
|
+
tilt (2.0.1)
|
149
|
+
tins (1.5.1)
|
150
|
+
tzinfo (1.2.2)
|
151
|
+
thread_safe (~> 0.1)
|
152
|
+
unf (0.1.4)
|
153
|
+
unf_ext
|
154
|
+
unf_ext (0.0.7.1)
|
155
|
+
|
156
|
+
PLATFORMS
|
157
|
+
ruby
|
158
|
+
|
159
|
+
DEPENDENCIES
|
160
|
+
appraisal (= 2.0.1)
|
161
|
+
bundler (~> 1.3)
|
162
|
+
coveralls (~> 0)
|
163
|
+
haml (~> 4.0)
|
164
|
+
rails
|
165
|
+
rake (~> 10.4)
|
166
|
+
rspec (~> 3.1)
|
167
|
+
shortcode!
|
168
|
+
slim (~> 3.0)
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jamie Dyer
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,268 @@
|
|
1
|
+
# Shortcode
|
2
|
+
|
3
|
+
*Note: this branch extends the existing shortcode gem with support for multiple template paths. This is useful if you want to bundle the shortcode gem with a Rails Engine and still allow users to provide their own templates or overrides*
|
4
|
+
|
5
|
+
Shortcode is a ruby gem for parsing Wordpress style shortcodes, I created it while building a CMS for a client through [my ruby consultancy, Kernow Soul](http://kernowsoul.com). 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.
|
6
|
+
|
7
|
+
[![Gem Version](https://badge.fury.io/rb/shortcode.svg)](http://badge.fury.io/rb/shortcode)
|
8
|
+
[![Build Status](https://travis-ci.org/kernow/shortcode.png?branch=master)](https://travis-ci.org/kernow/shortcode)
|
9
|
+
[![Code Climate](https://codeclimate.com/github/kernow/shortcode.png)](https://codeclimate.com/github/kernow/shortcode)
|
10
|
+
[![Coverage Status](https://coveralls.io/repos/kernow/shortcode/badge.png)](https://coveralls.io/r/kernow/shortcode)
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'shortcode'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
```
|
23
|
+
$ bundle
|
24
|
+
```
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
```
|
29
|
+
$ gem install shortcode
|
30
|
+
```
|
31
|
+
|
32
|
+
Shortcode is tested against ruby version 2.0, 2.1, and 2.2 as well as jruby, it will not work with ruby 1.8 and is no longer tested against ruby 1.9. Shortcode rails integration is tested against
|
33
|
+
Rails versions 3.2, 4.0, 4.1 and 4.2.
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
### Example
|
38
|
+
|
39
|
+
Shortcode is very simple to use, simply call the `process` method and pass it a string containing shortcode markup.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
Shortcode.process("[quote]Hello World[/quote]")
|
43
|
+
```
|
44
|
+
|
45
|
+
In a Rails app, you can create helper methods to handle your shortcoded content and use them in your views with something similar to `<%= content_html @page.content %>`. Those two helper method can be used if your content contains html to be escaped or not.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
module PagesHelper
|
49
|
+
def content(c)
|
50
|
+
Shortcode.process(c)
|
51
|
+
end
|
52
|
+
|
53
|
+
def content_html(c)
|
54
|
+
raw content(c)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
### Tags
|
60
|
+
|
61
|
+
Any tags you wish to use with Shortcode need to be configured in the setup block, there are 2 types of tag, `block_tags` and `self_closing_tags`. Block tags have a matching open and close tag such as `[quote]A quote[/quote]`, self closing tags have no close tag, for example `[gallery]`. To define the tags Shortcode should parse do so in the configuration (in a Rails initializer for example) as follows:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
Shortcode.setup do |config|
|
65
|
+
config.block_tags = [:quote, :list]
|
66
|
+
config.self_closing_tags = [:gallery, :widget]
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
### Templates
|
71
|
+
|
72
|
+
Each shortcode tag needs a template in order to translate the shortcode into html (or other output). Templates can be written in erb, haml or slim and work in
|
73
|
+
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:
|
74
|
+
|
75
|
+
[quote author="Homer Simpson"]Doh![/quote]
|
76
|
+
|
77
|
+
And the erb template to render the shortcode
|
78
|
+
|
79
|
+
```erb
|
80
|
+
<blockquote>
|
81
|
+
<p class='quotation'>
|
82
|
+
<%= @content %>
|
83
|
+
</p>
|
84
|
+
<% if @attributes[:author] %>
|
85
|
+
<p class='citation'>
|
86
|
+
<span class='author'>
|
87
|
+
<%= @attributes[:author] %>
|
88
|
+
</span>
|
89
|
+
</p>
|
90
|
+
<% end %>
|
91
|
+
</blockquote>
|
92
|
+
```
|
93
|
+
|
94
|
+
If using the gem within a Rails project you can use the Rails helper methods within templates.
|
95
|
+
|
96
|
+
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
|
97
|
+
|
98
|
+
[collapsible_list]
|
99
|
+
[item]
|
100
|
+
[youtube id="12345"]
|
101
|
+
[/item]
|
102
|
+
[item]Hellow World[/item]
|
103
|
+
[/collapsible_list]
|
104
|
+
|
105
|
+
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.
|
106
|
+
|
107
|
+
There are 2 ways templates can be used with Shortcode, the default it to load templates from the file system, an alternative approach is to pass templates to the setup
|
108
|
+
block as strings.
|
109
|
+
|
110
|
+
#### Templates loaded from the file system
|
111
|
+
|
112
|
+
Simply create files with the extension or .erb, .haml, or .slim with a filename the same as the shortcode tag, e.g. gallery.html.erb would render a [gallery] shortcode tag. The default
|
113
|
+
location for template files is `app/views/shortcode_templates`, if you want to load templates from a different location use the `template_path` config option.
|
114
|
+
|
115
|
+
Note: only 1 template parser is supported at a time, if using haml for instance all templates must be haml.
|
116
|
+
|
117
|
+
#### Templates set as configuration options
|
118
|
+
|
119
|
+
The alternative way to define templates is to set them using the `templates` config option, this option can take a hash with keys of the same name as the shortcode tags and
|
120
|
+
values containing a template string. For instance:
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
Shortcode.setup do |config|
|
124
|
+
config.templates = { gallery: 'template code' }
|
125
|
+
end
|
126
|
+
```
|
127
|
+
|
128
|
+
If the `templates` config option is set all templates will be loaded from this hash, if a shortcode is encountered without a matching key in the `templates` config option
|
129
|
+
an exception will be raised.
|
130
|
+
|
131
|
+
Note: it's NOT possible to load templates from a config option AND from the file system, you must either load all templates from the file system or define all templates in a config option.
|
132
|
+
|
133
|
+
### Custom Helpers
|
134
|
+
|
135
|
+
If you wish to use custom helper modules in templates you can do so by specifying the helpers in a setup block which should be an array. Methods in the helper modules will then become available within all templates.
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
Shortcode.setup do |config|
|
139
|
+
config.helpers = [CustomHelper, AnotherCustomHelper]
|
140
|
+
end
|
141
|
+
```
|
142
|
+
|
143
|
+
### Presenters
|
144
|
+
|
145
|
+
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 `for` method can also return an array of symbols if the presenter is to be used for multiple shortcodes. The classes `initialize` method received the `attributes`, `content` and `additional_attributes` variables. Finally the class should define `content` and `attributes` methods.
|
146
|
+
|
147
|
+
In a rails app you could return image records to the template using something like this:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
class GalleryPresenter
|
151
|
+
|
152
|
+
def self.for
|
153
|
+
# An array can also be returned if the presenter should be applied
|
154
|
+
# to multiple shortcodes, e.g. [:gallery, :enhanced_gallery]
|
155
|
+
:gallery
|
156
|
+
end
|
157
|
+
|
158
|
+
def initialize(attributes, content, additional_attributes)
|
159
|
+
@content = content
|
160
|
+
end
|
161
|
+
|
162
|
+
def content
|
163
|
+
@content
|
164
|
+
end
|
165
|
+
|
166
|
+
def attributes
|
167
|
+
{ images: images }
|
168
|
+
end
|
169
|
+
|
170
|
+
private
|
171
|
+
|
172
|
+
def images
|
173
|
+
Image.where("id IN (?)", @attributes[:ids])
|
174
|
+
end
|
175
|
+
end
|
176
|
+
```
|
177
|
+
|
178
|
+
#### Using additional attributes
|
179
|
+
|
180
|
+
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.
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
class GalleryPresenter
|
184
|
+
|
185
|
+
def self.for
|
186
|
+
:gallery
|
187
|
+
end
|
188
|
+
|
189
|
+
def initialize(attributes, content, additional_attributes)
|
190
|
+
@content = content
|
191
|
+
@additional_attributes = additional_attributes
|
192
|
+
end
|
193
|
+
|
194
|
+
def content
|
195
|
+
@content
|
196
|
+
end
|
197
|
+
|
198
|
+
def attributes
|
199
|
+
{ images: images }
|
200
|
+
end
|
201
|
+
|
202
|
+
private
|
203
|
+
|
204
|
+
def images
|
205
|
+
@additional_attributes[:images].map &:url
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
# The hash containing the images attribute is passed through to the presenter
|
210
|
+
# as the additional_attributes argument
|
211
|
+
Shortcode.process('[gallery]', { images: @post.images })
|
212
|
+
|
213
|
+
```
|
214
|
+
|
215
|
+
#### Registering presenters
|
216
|
+
|
217
|
+
To register a presenter simply call `Shortcode.register_presenter` passing the presenter class e.g.
|
218
|
+
|
219
|
+
```ruby
|
220
|
+
# A single presenter
|
221
|
+
Shortcode.register_presenter(CustomPresenter)
|
222
|
+
|
223
|
+
# Or multiple presenters in one call
|
224
|
+
Shortcode.register_presenter(CustomPresenter, AnotherPresenter)
|
225
|
+
|
226
|
+
```
|
227
|
+
|
228
|
+
### Configuration
|
229
|
+
|
230
|
+
```ruby
|
231
|
+
Shortcode.setup do |config|
|
232
|
+
|
233
|
+
# the template parser to use
|
234
|
+
config.template_parser = :erb # :erb, :haml, :slim supported, :erb is default
|
235
|
+
|
236
|
+
# location of the template files, default is "app/views/shortcode_templates"
|
237
|
+
config.template_paths = ["support/templates/erb"]
|
238
|
+
|
239
|
+
# a hash of templates passed as strings, if this is set it overrides the
|
240
|
+
# above template_paths option. The default is nil
|
241
|
+
config.templates = { gallery: 'template code' }
|
242
|
+
|
243
|
+
# an array of helper modules to make available within templates
|
244
|
+
config.helpers = [CustomerHelper]
|
245
|
+
|
246
|
+
# a list of block tags to support e.g. [quote]Hello World[/quote]
|
247
|
+
config.block_tags = [:quote]
|
248
|
+
|
249
|
+
# a list of self closing tags to support e.g. [youtube id="12345"]
|
250
|
+
config.self_closing_tags = [:youtube]
|
251
|
+
|
252
|
+
# the type of quotes to use for attribute values, default is double quotes (")
|
253
|
+
config.attribute_quote_type = '"'
|
254
|
+
|
255
|
+
# Allows quotes around attributes to be omitted
|
256
|
+
# Defaults to true, quotes must be present around attribute values
|
257
|
+
config.use_attribute_quotes = true
|
258
|
+
end
|
259
|
+
```
|
260
|
+
|
261
|
+
|
262
|
+
## Contributing
|
263
|
+
|
264
|
+
1. Fork it
|
265
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
266
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
267
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
268
|
+
5. Create new Pull Request
|