Sha256: 85d93f6480eee5e0133351701c7fee3c9b51b7ec8275a3a100bb86b348c2e424

Contents?: true

Size: 927 Bytes

Versions: 1

Compression:

Stored size: 927 Bytes

Contents

# encoding: UTF-8

module Sterile

  class << self

    # Format text with proper "curly" quotes, m-dashes, copyright, trademark, etc.
    #
    #   q{"He said, 'Away with you, Drake!'"}.smart_format # => “He said, ‘Away with you, Drake!’”
    #
    def smart_format(string)
      smart_format_rules.each do |rule|
        string.gsub! rule[0], rule[1]
      end
      string
    end


    # Like +smart_format+, but works with HTML/XML (somewhat).
    #
    def smart_format_tags(string)
      string.gsub_tags do |text|
        text.smart_format
      end.encode_entities.gsub(/(\S)&ldquo;/, "\\1&rdquo;").gsub(/(\S)&lsquo;/, "\\1&rsquo;")
    end


    private

    # Lazy load smart formatting rules
    #
    def smart_format_rules
      @smart_format_rules ||= begin
        require "sterile/data/smart_format_rules"
        Data.smart_format_rules
      end
    end

  end # class << self

end # module Sterile

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sterile-1.0.8 lib/sterile/smart_format.rb