Sha256: 0418edd91bf49d087a7732c0e04da095553d065f4a4b3af8a88f4d48a740a22d

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 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)
      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

4 entries across 4 versions & 2 rubygems

Version Path
sterile-1.0.19 lib/sterile/smart_format.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/sterile-1.0.17/lib/sterile/smart_format.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/sterile-1.0.17/lib/sterile/smart_format.rb
sterile-1.0.17 lib/sterile/smart_format.rb