Sha256: c3691610a41783d373030f53aed18167b4741a43be1a707a4dde8bd7f62e4976

Contents?: true

Size: 1.12 KB

Versions: 5

Compression:

Stored size: 1.12 KB

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)
      string = string.to_s
      string = string.dup if string.frozen?
      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 = string.gsub(/[\p{Z}\s]+(<\/[a-zA-Z]+>)(['"][a-zA-Z])/, "\\1 \\2") # Fixes quote after whitespace + tag "<em>Dan. </em>'And"
      string.gsub_tags do |text|
        text.smart_format
      end.encode_entities.gsub(/(\<\/\w+\>)&ldquo;/, "\\1&rdquo;").gsub(/(\<\/\w+\>)&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

5 entries across 5 versions & 1 rubygems

Version Path
sterile-1.0.26 lib/sterile/smart_format.rb
sterile-1.0.25 lib/sterile/smart_format.rb
sterile-1.0.24 lib/sterile/smart_format.rb
sterile-1.0.23 lib/sterile/smart_format.rb
sterile-1.0.22 lib/sterile/smart_format.rb