README.md in shortcode-1.1.1 vs README.md in shortcode-1.2.0
- old
+ new
@@ -25,21 +25,24 @@
```
$ gem install shortcode
```
-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
-Rails versions 3.2, 4.0, 4.1 and 4.2.
+Shortcode is tested against ruby version 2.2, 2.3, and 2.4 as well as jruby (2.x compatible), it will not work with ruby 1.8 and is no longer tested against ruby 1.9. Shortcode rails integration is tested against
+Rails versions 4.1, 4.2, 5.0 and 5.1.
## Usage
### Example
Shortcode is very simple to use, simply call the `process` method and pass it a string containing shortcode markup.
+You can create multiple instances of `Shortcode` with separate configurations for each.
+
```ruby
-Shortcode.process("[quote]Hello World[/quote]")
+shortcode = Shortcode.new
+shortcode.process("[quote]Hello World[/quote]")
```
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.
```ruby
@@ -57,16 +60,27 @@
### Tags
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:
```ruby
-Shortcode.setup do |config|
+shortcode = Shortcode.new
+shortcode.setup do |config|
config.block_tags = [:quote, :list]
config.self_closing_tags = [:gallery, :widget]
end
```
+Note that you can call the setup block multiple times if need be and add to it.
+
+For example:
+
+```ruby
+shortcode.setup do |config|
+ config.block_tags << :other_tag
+end
+```
+
### Templates
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
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:
@@ -116,26 +130,27 @@
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
values containing a template string. For instance:
```ruby
-Shortcode.setup do |config|
+shortcode = Shortcode.new
+
+shortcode.setup do |config|
config.templates = { gallery: 'template code' }
end
```
-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
-an exception will be raised.
+Note: Templates can be loaded from either the file system or the configuration templates. If `check_config_templates_first` is set to true (the default value) on the configuration then it will check configuration templates first, and file system templates if it doesn't find one. If `check_config_templates_first` is set to false on the configuration it will check for a file system template first, and then configuration templates if it doesn't find one. If it doesn't find a template in either spot then it will raise an error.
-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.
-
### Custom Helpers
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.
```ruby
-Shortcode.setup do |config|
+shortcode = Shortcode.new
+
+shortcode.setup do |config|
config.helpers = [CustomHelper, AnotherCustomHelper]
end
```
### Presenters
@@ -165,19 +180,19 @@
{ images: images }
end
private
- def images
- Image.where("id IN (?)", @attributes[:ids])
- end
+ def images
+ Image.where("id IN (?)", @attributes[:ids])
+ end
end
```
#### Using additional attributes
-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.
+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 achieved using additional attributes with a presenter.
```ruby
class GalleryPresenter
def self.for
@@ -197,49 +212,56 @@
{ images: images }
end
private
- def images
- @additional_attributes[:images].map &:url
- end
+ def images
+ @additional_attributes[:images].map &:url
+ end
end
+```
-# The hash containing the images attribute is passed through to the presenter
-# as the additional_attributes argument
-Shortcode.process('[gallery]', { images: @post.images })
+The hash containing the images attribute is passed through to the presenter as the additional_attributes argument to the `process` method.
+```ruby
+shortcode = Shortcode.new
+shortcode.process('[gallery]', { images: @post.images })
```
#### Registering presenters
-To register a presenter simply call `Shortcode.register_presenter` passing the presenter class e.g.
+To register a presenter simply call `register_presenter` passing the presenter class e.g.
```ruby
+shortcode = Shortcode.new
+
# A single presenter
-Shortcode.register_presenter(CustomPresenter)
+shortcode.register_presenter(CustomPresenter)
# Or multiple presenters in one call
-Shortcode.register_presenter(CustomPresenter, AnotherPresenter)
-
+shortcode.register_presenter(CustomPresenter, AnotherPresenter)
```
### Configuration
```ruby
-Shortcode.setup do |config|
+shortcode = Shortcode.new
+shortcode.setup do |config|
+
# the template parser to use
config.template_parser = :erb # :erb, :haml, :slim supported, :erb is default
# location of the template files, default is "app/views/shortcode_templates"
config.template_path = "support/templates/erb"
- # a hash of templates passed as strings, if this is set it overrides the
- # above template_path option. The default is nil
+ # a hash of templates passed as strings.
config.templates = { gallery: 'template code' }
+ # a boolean option to set whether configuration templates are checked first or file system templates
+ config.check_config_templates_first = true
+
# an array of helper modules to make available within templates
config.helpers = [CustomerHelper]
# a list of block tags to support e.g. [quote]Hello World[/quote]
config.block_tags = [:quote]
@@ -254,9 +276,26 @@
# Defaults to true, quotes must be present around attribute values
config.use_attribute_quotes = true
end
```
+### Singleton
+
+You can optionally use Shortcode as a singleton instance with the same configuration throughout.
+
+To do this, you call methods directly on the `Shortcode` class.
+
+For example:
+
+```ruby
+Shortcode.setup do |config|
+ config.block_tags = [:quote]
+end
+
+Shortcode.register_presenter(QuotePresenterClass)
+
+Shortcode.process('[quote]Some quote[/quote]')
+```
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)