Rails: Using HTML typography automatically

There’s a way you can use special characters for HTML typography in Rails without much work: Just let your views about raw text and then overwrite the html_escape (h) method:

module ApplicationHelper

  def h(s)
    super(s). \
      gsub(‘(c)’  , ‘&copy’). \
      gsub(‘(r)’  , ‘®’). \
      gsub(‘(tm)’ , ‘™’). \
      gsub(’ 1/2 ‘, ’ ½ ‘). \
      gsub(’ 1/4 ‘, ’ ¼ ‘). \
      gsub(’ 3/4 ‘, ’ ¾ ‘). \
      gsub(‘… ‘ , ‘… ‘). \
      gsub(’ — ‘ , ’ — ‘). \
      gsub(’ - ‘  , ’ – ‘)
  end

In this case, three hyphens are replaced by ” — “, three dots by an ellipse etc.

Now just make sure that your views use it correctly and output “--” instead of “-” etc. when it is necessary.

Leave a Reply


blog.dev001.net is Digg proof thanks to caching by WP Super Cache