Class: RubyUIConverter::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_ui_converter/lexer.rb

Overview

Splits an ERB document into HTML (with placeholders) + a registry of ERB tokens. Each ERB tag is replaced in the source by a unique placeholder so the HTML tokenizer can run over a well-formed string even when ERB appears inside tags/attributes. The placeholder uses only [A-Za-z0-9_] characters, which the HTML tokenizer treats as inert text/identifiers.

Defined Under Namespace

Classes: Token

Constant Summary collapse

PATTERN =

Matches <% %>, <%= %>, <%== %>, <%# %> with optional trim markers (<%- -%>).

/<%(={1,2}|#|-)?(.*?)(-)?%>/m
PLACEHOLDER_PREFIX =
"RUCxERBx"
PLACEHOLDER_SUFFIX =
"xERBxRUC"
PLACEHOLDER_PATTERN =
/RUCxERBx(\d+)xERBxRUC/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Lexer

Returns a new instance of Lexer.



19
20
21
# File 'lib/ruby_ui_converter/lexer.rb', line 19

def initialize(source)
  @source = source.to_s
end

Class Method Details

.placeholder(index) ⇒ Object



54
55
56
# File 'lib/ruby_ui_converter/lexer.rb', line 54

def self.placeholder(index)
  "#{PLACEHOLDER_PREFIX}#{index}#{PLACEHOLDER_SUFFIX}"
end

Instance Method Details

#build_token(marker, code) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby_ui_converter/lexer.rb', line 40

def build_token(marker, code)
  code = code.to_s
  case marker
  when "#"
    Token.new(type: :comment, value: code.strip, raw: false)
  when "="
    Token.new(type: :output, value: code.strip, raw: false)
  when "=="
    Token.new(type: :output, value: code.strip, raw: true)
  else
    Token.new(type: :eval, value: code.strip, raw: false)
  end
end

#tokenize_with_placeholdersArray(String, Hash{String => Token})

Returns:

  • (Array(String, Hash{String => Token}))


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_ui_converter/lexer.rb', line 24

def tokenize_with_placeholders
  registry = {}
  index = 0

  html = @source.gsub(PATTERN) do
    marker = Regexp.last_match(1)
    code = Regexp.last_match(2)
    key = self.class.placeholder(index)
    registry[key] = build_token(marker, code)
    index += 1
    key
  end

  [html, registry]
end