Created: 2008-12-26 00:30
Updated: 2019-01-31 08:58
License: mit


AutoHtml is a collection of filters that transforms plain text into HTML code.


Add this line to your application's Gemfile:

gem 'auto_html'

And then execute:

$ bundle

Or install it yourself as:

$ gem install auto_html


AutoHtml uses concepts found in "Pipes and Filters" processing design pattern:

  • Filter - transforms an input. In AutoHtml context, this is any object that does the transformation through #call(String) method. Filter options should be passed in initializer. AutoHtml provides some filters already, ie Link, Image, Markdown, etc.
  • Pipeline - a composition of filters that transforms input by passing the output of one filter as input for the next filter in line. In AutoHtml context, this is the AutoHtml::Pipeline class. Since the same interface (method #call) is used to pass input, we can say that Pipeline is just another Filter, which means it can be used as a building block for other Pipelines, in a mix with other filters.


link_filter = '_blank')'Checkout out my blog:')
# => 'Checkout out my blog: <a target="blank" href=""></a>'

emoji_filter =':point_left: yo!')
# => '<img src="" class="emoji" title=":point_left:" alt=":point_left:" height="20" witdh="20" align="absmiddle" /> yo!'

# Use Pipeline to combine filters
base_format =, emoji_filter)'Checkout out my blog: :point_left: yo!')
# => 'Checkout out my blog: <a href=""></a> <img src="" class="emoji" title=":point_left:" alt=":point_left:" height="20" witdh="20" align="absmiddle" /> yo!'

# A pipeline can be reused in another pipeline. Note that the order of filters is important - ie you want
# `Image` before `Link` filter so that URL of the image gets transformed to `img` tag and not `a` tag.
comment_format =,, base_format)"Hello!\n\n Checkout out my blog: :point_left: yo! \n\n")
# => "<p>Hello!</p>\n\n<p>Checkout out my blog: <a href="<img src="" target="_blank"></a> <img src="" />" class="emoji" title=":point_left:" alt=":point_left:" height="20" witdh="20" align="absmiddle" /> yo! </p>\n\n<p><a href="<img src="" />" target="_blank"><img src="" /></a></p>\n"

Bundled filters

Bellow is the list of bundled filters along with their optional arguments on initialization and their default values.

  • AutoHtml::Emoji, width: 20, height: 20, asset_root: '/images'
  • AutoHtml::HtmlEscape
  • AutoHtml::Image, proxy: nil, alt: nil
  • AutoHtml::Link, target: nil, rel: nil
  • AutoHtml::Markdown
  • AutoHtml::SimpleFormat

Using AutoHtml with ActiveRecord

For performance reasons it's a good idea to store the formated output in the database, in a separate column, to avoid generating the same content on each access. This can be acomplished simply by overriding the attribute writter:

class Comment < ActiveRecord::Base

  def text=(t)
    self[:text_html] =

Now, every time text attribute is set, text_html will be set as well:

comment = 'Hey!')
comment.text_html # => '<p>Hey!</p>'

Code status

Circle CI


AutoHtml is released under the MIT License.

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more