Class: Papercraft::TagTranslator

Inherits:
Prism::MutationCompiler
  • Object
show all
Includes:
Prism::DSL
Defined in:
lib/papercraft/compiler/tag_translator.rb

Overview

Translates a normal proc AST into an AST containing custom nodes used for generating HTML. This translation is the first step in compiling templates into procs that generate HTML.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ TagTranslator

Returns a new instance of TagTranslator.



13
14
15
16
# File 'lib/papercraft/compiler/tag_translator.rb', line 13

def initialize(root)
  @root = root
  super()
end

Class Method Details

.transform(ast, root) ⇒ Object



18
19
20
21
22
# File 'lib/papercraft/compiler/tag_translator.rb', line 18

def self.transform(ast, root)
  return nil if !ast

  ast.accept(new(root))
end

Instance Method Details

#match_block_call(node) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/papercraft/compiler/tag_translator.rb', line 79

def match_block_call(node)
  return if !node.receiver
  return if node.name != :call

  receiver = node.receiver
  return if !receiver.is_a?(Prism::LocalVariableReadNode)
  return if @root.parameters&.parameters.block&.name != receiver.name

  if node.block
    raise Papercraft::Error, 'No support for proc invocation with block'
  end

  BlockInvocationNode.new(node, self)
end

#match_builtin(node) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/papercraft/compiler/tag_translator.rb', line 35

def match_builtin(node)
  return if node.receiver

  case node.name
  when :render_yield
    RenderYieldNode.new(node, self)
  when :render_children
    RenderChildrenNode.new(node, self)
  when :raise
    visit_call_node(node, dont_translate: true)
  when :render
    RenderNode.new(node, self)
  when :raw
    RawNode.new(node, self)
  when :text
    TextNode.new(node, self)
  when :defer
    DeferNode.new(node, self)
  when :html, :html5, :markdown
    BuiltinNode.new(node, self)
  else
    nil
  end
end

#match_const_tag(node) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/papercraft/compiler/tag_translator.rb', line 67

def match_const_tag(node)
  return if node.name !~ /^[A-Z]/
  case node.receiver
  when nil, Prism::ConstantReadNode, Prism::ConstantPathNode
    # ok
  else
    return
  end

  ConstTagNode.new(node, self)
end

#match_extension(node) ⇒ Object



60
61
62
63
64
65
# File 'lib/papercraft/compiler/tag_translator.rb', line 60

def match_extension(node)
  return if node.receiver
  return if !Papercraft::Extensions[node.name]

  ExtensionTagNode.new(node, self)
end

#match_tag(node) ⇒ Object



94
95
96
97
98
# File 'lib/papercraft/compiler/tag_translator.rb', line 94

def match_tag(node)
  return if node.receiver

  TagNode.new(node, self)
end

#visit_call_node(node, dont_translate: false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/papercraft/compiler/tag_translator.rb', line 24

def visit_call_node(node, dont_translate: false)
  return super(node) if dont_translate

  match_builtin(node) ||
  match_extension(node) ||
  match_const_tag(node) ||
  match_block_call(node) ||
  match_tag(node) ||
  super(node)
end