Sha256: f803e32334531b6d5f78356ca5876242cb9cda52c8e6e580725a004d38c1b090

Contents?: true

Size: 1.92 KB

Versions: 49

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true
module Hamlit
  # Compile [:multi, [:static, 'foo'], [:dynamic, 'bar']] to [:dynamic, '"foo#{bar}"']
  class DynamicMerger < Temple::Filter
    def on_multi(*exps)
      exps = exps.dup
      result = [:multi]
      buffer = []

      until exps.empty?
        type, arg = exps.first
        if type == :dynamic && arg.count("\n") == 0
          buffer << exps.shift
        elsif type == :static && exps.size > (count = arg.count("\n")) &&
              exps[1, count].all? { |e| e == [:newline] }
          (1 + count).times { buffer << exps.shift }
        elsif type == :newline && exps.size > (count = count_newline(exps)) &&
              exps[count].first == :static && count == exps[count].last.count("\n")
          (count + 1).times { buffer << exps.shift }
        else
          result.concat(merge_dynamic(buffer))
          buffer = []
          result << compile(exps.shift)
        end
      end
      result.concat(merge_dynamic(buffer))

      result.size == 2 ? result[1] : result
    end

    private

    def merge_dynamic(exps)
      # Merge exps only when they have both :static and :dynamic
      unless exps.any? { |type,| type == :static } && exps.any? { |type,| type == :dynamic }
        return exps
      end

      strlit_body = String.new
      exps.each do |type, arg|
        case type
        when :static
          strlit_body << arg.dump.sub!(/\A"/, '').sub!(/"\z/, '').gsub('\n', "\n")
        when :dynamic
          strlit_body << "\#{#{arg}}"
        when :newline
          # newline is added by `gsub('\n', "\n")`
        else
          raise "unexpected type #{type.inspect} is given to #merge_dynamic"
        end
      end
      [[:dynamic, "%Q\0#{strlit_body}\0"]]
    end

    def count_newline(exps)
      count = 0
      exps.each do |exp|
        if exp == [:newline]
          count += 1
        else
          return count
        end
      end
      return count
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
hamlit-3.0.3-java lib/hamlit/dynamic_merger.rb
hamlit-3.0.3 lib/hamlit/dynamic_merger.rb
hamlit-3.0.2-java lib/hamlit/dynamic_merger.rb
hamlit-3.0.2 lib/hamlit/dynamic_merger.rb
hamlit-3.0.1-java lib/hamlit/dynamic_merger.rb
hamlit-3.0.1 lib/hamlit/dynamic_merger.rb
hamlit-3.0.0-java lib/hamlit/dynamic_merger.rb
hamlit-3.0.0 lib/hamlit/dynamic_merger.rb
hamlit-2.16.2-java lib/hamlit/dynamic_merger.rb
hamlit-2.16.2 lib/hamlit/dynamic_merger.rb
hamlit-2.16.1-java lib/hamlit/dynamic_merger.rb
hamlit-2.16.1 lib/hamlit/dynamic_merger.rb
hamlit-2.16.0-java lib/hamlit/dynamic_merger.rb
hamlit-2.16.0 lib/hamlit/dynamic_merger.rb
hamlit-2.15.2-java lib/hamlit/dynamic_merger.rb
hamlit-2.15.2 lib/hamlit/dynamic_merger.rb
hamlit-2.15.1-java lib/hamlit/dynamic_merger.rb
hamlit-2.15.1 lib/hamlit/dynamic_merger.rb
hamlit-2.15.0-java lib/hamlit/dynamic_merger.rb
hamlit-2.15.0 lib/hamlit/dynamic_merger.rb