shortcode 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +29 -3
- data/lib/shortcode/configuration.rb +4 -0
- data/lib/shortcode/tag.rb +9 -0
- data/lib/shortcode/version.rb +1 -1
- data/spec/spec_helper.rb +2 -0
- data/spec/tag_spec.rb +38 -0
- metadata +2 -2
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -42,9 +42,13 @@ Shortcode.setup do |config|
|
|
42
42
|
# the template parser to use
|
43
43
|
config.template_parser = :haml # :erb or :haml supported, :haml is default
|
44
44
|
|
45
|
-
# location of the template files
|
45
|
+
# location of the template files, default is "app/views/shortcode_templates"
|
46
46
|
config.template_path = "support/templates/haml"
|
47
47
|
|
48
|
+
# a hash of templates passed as strings, if this is set it overrides the
|
49
|
+
# above template_path option. The default is nil
|
50
|
+
config.templates = { gallery: 'template code' }
|
51
|
+
|
48
52
|
# a list of block tags to support e.g. [quote]Hello World[/quote]
|
49
53
|
config.block_tags = [:quote]
|
50
54
|
|
@@ -58,7 +62,7 @@ end
|
|
58
62
|
|
59
63
|
### Templates
|
60
64
|
|
61
|
-
Each shortcode tag needs a template
|
65
|
+
Each shortcode tag needs a template in order to translate the shortcode into html (or other output). Templates can be written in HAML or erb and work in
|
62
66
|
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:
|
63
67
|
|
64
68
|
[quote author="Homer Simpson"]Doh![/quote]
|
@@ -73,7 +77,7 @@ And the haml template to render the shortcode
|
|
73
77
|
%span.author= @attributes[:author]
|
74
78
|
```
|
75
79
|
|
76
|
-
If using the gem within a Rails project you can use the Rails helper methods within
|
80
|
+
If using the gem within a Rails project you can use the Rails helper methods within templates.
|
77
81
|
|
78
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
|
79
83
|
|
@@ -86,6 +90,28 @@ Shortcodes can be nested inside other shortcodes, there are no limits imposed on
|
|
86
90
|
|
87
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.
|
88
92
|
|
93
|
+
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
|
94
|
+
block as strings.
|
95
|
+
|
96
|
+
#### Templates loaded from the file system
|
97
|
+
|
98
|
+
Simply create files with the extension or .haml or .erb with a filename the same as the shortcode tag, e.g. gallery.haml would render a [gallery] shortcode tag. The default
|
99
|
+
location for template files is `app/views/shortcode_templates`, if you want to load tempaltes from a different location use the `template_path` config option.
|
100
|
+
|
101
|
+
#### Templates set as configuration options
|
102
|
+
|
103
|
+
The alternative way to define tempaltes 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
|
104
|
+
values containing a template string. For instance:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
Shortcode.setup do |config|
|
108
|
+
config.templates = { gallery: 'template code' }
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
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
|
113
|
+
an exception will be raised.
|
114
|
+
|
89
115
|
### Presenters
|
90
116
|
|
91
117
|
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.
|
@@ -5,6 +5,9 @@ class Shortcode::Configuration
|
|
5
5
|
# Sets the template parser to use, supports :erb and :haml, default is :haml
|
6
6
|
attr_accessor :template_path
|
7
7
|
|
8
|
+
# Allows templates to be set from strings rather than read from the filesystem
|
9
|
+
attr_accessor :templates
|
10
|
+
|
8
11
|
# Set the supported block_tags
|
9
12
|
attr_accessor :block_tags
|
10
13
|
|
@@ -17,6 +20,7 @@ class Shortcode::Configuration
|
|
17
20
|
def initialize
|
18
21
|
@template_parser = :haml
|
19
22
|
@template_path = "app/views/shortcode_templates"
|
23
|
+
@templates = nil
|
20
24
|
@block_tags = []
|
21
25
|
@self_closing_tags = []
|
22
26
|
@quotes = '"'
|
data/lib/shortcode/tag.rb
CHANGED
@@ -14,6 +14,7 @@ class Shortcode::Tag
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def markup
|
17
|
+
return markup_from_config unless Shortcode.configuration.templates.nil?
|
17
18
|
template_files.each do |path|
|
18
19
|
return File.read(path) if File.file? path
|
19
20
|
end
|
@@ -37,6 +38,14 @@ class Shortcode::Tag
|
|
37
38
|
end
|
38
39
|
end
|
39
40
|
|
41
|
+
def markup_from_config
|
42
|
+
if Shortcode.configuration.templates.has_key? @name.to_sym
|
43
|
+
Shortcode.configuration.templates[@name.to_sym]
|
44
|
+
else
|
45
|
+
raise Shortcode::TemplateNotFound, "Shortcode.configuration.templates does not contain the key #{@name.to_sym}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
40
49
|
def template_files
|
41
50
|
template_paths.map do |filename|
|
42
51
|
File.join Shortcode.configuration.template_path, filename
|
data/lib/shortcode/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -13,8 +13,10 @@ RSpec.configure do |config|
|
|
13
13
|
Shortcode.setup do |config|
|
14
14
|
config.template_parser = :haml
|
15
15
|
config.template_path = File.join File.dirname(__FILE__), "support/templates/haml"
|
16
|
+
config.templates = nil
|
16
17
|
config.block_tags = [:quote, :collapsible_list, :item, :timeline_person, :rails_helper]
|
17
18
|
config.self_closing_tags = [:timeline_event, :timeline_info]
|
19
|
+
config.quotes = '"'
|
18
20
|
end
|
19
21
|
end
|
20
22
|
end
|
data/spec/tag_spec.rb
CHANGED
@@ -28,4 +28,42 @@ describe Shortcode::Tag do
|
|
28
28
|
|
29
29
|
end
|
30
30
|
|
31
|
+
context "templates from strings" do
|
32
|
+
|
33
|
+
let(:tag) { Shortcode::Tag.new('from_string', [{ key: 'string', value: 'batman' }]) }
|
34
|
+
|
35
|
+
before(:each) do
|
36
|
+
Shortcode.setup do |config|
|
37
|
+
config.template_parser = :erb
|
38
|
+
config.templates = {
|
39
|
+
from_string: '<p><%= @attributes[:string] %></p>'
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it "renders a template from a string" do
|
45
|
+
tag.render.should == '<p>batman</p>'
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
context "when the template is missing from the config" do
|
51
|
+
|
52
|
+
let(:tag) { Shortcode::Tag.new('missing', [{ key: 'string', value: 'batman' }]) }
|
53
|
+
|
54
|
+
before(:each) do
|
55
|
+
Shortcode.setup do |config|
|
56
|
+
config.template_parser = :erb
|
57
|
+
config.templates = {
|
58
|
+
from_string: '<p><%= @attributes[:string] %></p>'
|
59
|
+
}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it "raises an error" do
|
64
|
+
expect { tag.render }.to raise_error(Shortcode::TemplateNotFound)
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
31
69
|
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.
|
4
|
+
version: 0.3.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-05-
|
12
|
+
date: 2014-05-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: parslet
|