Sha256: b9c0ac7d9671cb12b28dc1b86be984cac1c1aefc86069eb72e0cb7db02c9e695

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

module ProperProperties
  module Parsing
    # Module to normalize the content of a properties file
    # 
    # @example Normalizes:
    #   # Comment 1
    #   ! Comment 2
    #   item0
    #   item1 = item1 
    #   item2 : item2 
    #   item3=line 1 \
    #         line 2
    #
    # @example Into:
    #
    #   item0
    #   item1=item1 
    #   item2=item2 
    #   item3=line 1 line 2
    #
    module Normalizer

      # Describes a single normalization rule by replacing content
      class Rule
        # Initializes a new rules base on a matching regexp
        # and a replacement as substitution
        # @param matcher [Regexp]
        # @param replacement [String]
        def initialize(matcher, replacement = '')
          @matcher     = matcher
          @replacement = replacement
        end

        # Apply the substitution to the text in place
        # @param text [string]
        # @return [String]
        def apply!(text)
          text.gsub!(@matcher, @replacement)
        end
      end

      # Collection of ordered rules
      RULES = []
       
      # Removes leading whitespace
      RULES << Rule.new(/^\s+/)

      # Strings ending with \ are concatenated (only if the number is odd)
      RULES << Rule.new(/^(.*[^\\]((\\){2})*)\\$\s+/, '\1')

      # Removes comments (after the application of the logic line concatenation)
      RULES << Rule.new(/^\s*[!\#].*$/)

      # Removes empty lines
      RULES << Rule.new(/^$(\r\n?|\n)/)

      # Unescape double backslashes
      RULES << Rule.new(/\\\\/, '\\')

      # Remove whitespace around delimiters and replace with =
      RULES << Rule.new(/^((?:(?:\\[=: \t])|[^=: \t])+)[ \t]*[=: \t][ \t]*/, '\1=')

      RULES.freeze

      # Normalizes the content of a properties file content by applying the RULES
      # @param text [String]
      # @return [String] 
      def self.normalize!(text)
        RULES.each do |rule|
          rule.apply!(text)
        end
        text
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
proper_properties-0.0.2 lib/proper_properties/parsing/normalizer.rb
proper_properties-0.0.1 lib/proper_properties/parsing/normalizer.rb