Sha256: 9adfe793ecd091309f6ec29bd3f71f1b2513ffc1c0bc33e4eadcf495b66937c4

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

require 'erubi'
require_relative 'token'
require_relative 'location'

module BetterHtml
  class NodeIterator
    class JavascriptErb < ::Erubi::Engine
      attr_reader :tokens

      def initialize(source)
        @source = source
        @parsed_document = ""
        @tokens = []
        super(source, regexp: HtmlErb::REGEXP_WITHOUT_TRIM, trim: false)
      end

      def add_text(text)
        add_token(:text, text)
        append(text)
      end

      def add_code(code)
        text = "<%#{code}%>"
        add_token(:stmt, text, code)
        append(text)
      end

      def add_expression(indicator, code)
        text = "<%#{indicator}#{code}%>"
        add_token(indicator == '=' ? :expr_literal : :expr_escaped, text, code)
        append(text)
      end

      private

      def add_token(type, text, code = nil)
        start = @parsed_document.size
        stop = start + text.size
        lines = @parsed_document.split("\n", -1)
        line = lines.empty? ? 1 : lines.size
        column = lines.empty? ? 0 : lines.last.size
        @tokens << Token.new(
          type: type,
          text: text,
          code: code,
          location: Location.new(@source, start, stop, line, column)
        )
      end

      def append(text)
        @parsed_document << text
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
better_html-0.0.12 lib/better_html/node_iterator/javascript_erb.rb
better_html-0.0.11 lib/better_html/node_iterator/javascript_erb.rb
better_html-0.0.10 lib/better_html/node_iterator/javascript_erb.rb
better_html-0.0.9 lib/better_html/node_iterator/javascript_erb.rb