# Raconteur With Raconteur, you can define custom text tags and have them parsed according to your specifications, allowing you to do some neat pre- and post-processing of your texts. You could for instance insert dynamic content fetched from your database whenever {{ customer-quote: id=43 }} appears in your text. ## Installation Raconteur is a Ruby gem so install like any other gem. For example, if you're using Bundler, then add this to your application's Gemfile: ```ruby gem 'raconteur' ``` ## Usage Raconteur is based around the notion of processors. Each processor has a tag-name and a set of instructions for how it should perform the content replacement and what it should insert. ```ruby @raconteur = Raconteur.new @raconteur.processors.register!('customer-quote', { template: '
template
and a handler
. The template
is a simple string with {{ variables }} that are replaced with dynamic content. The handler
loads the customer quotes from our database based on the id that was passed in as a setting in the customer-quote tags (i.e. {{ customer-quote: id=43 }} invokes the handler with a settings hash of { id: "43" }).
- Finally, we call the parse
function on our Raconteur instance, which runs over the text and replaces all instances that match its registered processors according to their specifications.
Processors must always have a tag-name name along with either a template defined, a handler defined, or both. If a handler returns a hash (like in the above example), it should have a template defined as well – Raconteur will take the returned hash and use its keys as replacement variables for the template (again, like seen in the example above). Handlers may also return a string, in which Raconteur will use this as the replacement, allowing you full control over the replaced text. If there's only a template defined, Raconteur will simply pass in any inputted tag settings as variables and then replace the tag with that template.
Processors may also be used for wrapping tags, which is a great way to encapsulate sections in your text. Let's look at an example:
```ruby
@raconteur = Raconteur.new
@raconteur.processors.register!('gallery', {
template: 'Aside:
{{ _yield_ }}Aside:
# # Additional tangentially-related text. # #handler
method, allowing you to customize the behavior of a tag based on its surrounding context. For instance, you could register an {{ image }} tag which renders differently depending on whether it is placed within a {{% gallery %}} wrapper or not.
Let's take a look at tags:
- Tags are wrapper by two curly braces and their name should be a string without any white-space {{ like-this }}
.
- Tags don't need any settings. Something simple like {{ page-count }}
will work perfectly fine.
- If you do want to pass in settings to a tag, the tag-name should be followed by a colon and a set of key-value pairs. The key must not include any white-space characters and will be converted to an underscored symbol for the settings hash. The separator should be an equals '=' symbol. The value may either be a word without any white-space characters, or it can be "a text wrapped by quotes". {{ definition: term=UXD + description="User Experience Design (aka UXD + UED + XD) refers to the ..." }}
- To escape quotes within a quoted settings value, put a backslash in front of the quote. You may alternatively configure Raconteur to use a different symbol for wrapping the text. Both non-white-space values and quoted values may be used in the same tag (as seen in the above "User Experience Design" example where the term
isn't quoted but the description
is). The key-value pairs in settings don't need to be separated by anything other than a white-space character but it can greatly help readability to include some character (such as a '+' symbol, as seen in the above "User Experience Design" example).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/JamieAppleseed/raconteur.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).