@@ -2,19 +2,26 @@ defmodule EEx.Engine do
2
2
@ moduledoc ~S"""
3
3
Basic EEx engine that ships with Elixir.
4
4
5
- An engine needs to implement four functions:
5
+ An engine needs to implement six functions:
6
6
7
- * `init(opts)` - returns the initial buffer
7
+ * `init(opts)` - called at the beginning of every text
8
+ and it must return the initial state.
8
9
9
- * `handle_body(quoted)` - receives the final built quoted
10
- expression, should do final post-processing and return a
11
- quoted expression.
10
+ * `handle_body(state)` - receives the state of the document
11
+ and it must return a quoted expression.
12
12
13
- * `handle_text(buffer , text)` - it receives the buffer ,
13
+ * `handle_text(state , text)` - it receives the state ,
14
14
the text and must return a new quoted expression.
15
15
16
- * `handle_expr(buffer, marker, expr)` - it receives the buffer,
17
- the marker, the expr and must return a new quoted expression.
16
+ * `handle_expr(state, marker, expr)` - it receives the state,
17
+ the marker, the expr and must return a new state.
18
+
19
+ * `handle_begin(state)` - called every time there a new state
20
+ is needed with an empty buffer. Typically called for do/end
21
+ blocks, case expressions, anonymous functions, etc
22
+
23
+ * `handle_end(state)` - opposite of `handle_begin(state)` and
24
+ it must return quoted expression
18
25
19
26
The marker is what follows exactly after `<%`. For example,
20
27
`<% foo %>` has an empty marker, but `<%= foo %>` has `"="`
@@ -27,10 +34,14 @@ defmodule EEx.Engine do
27
34
default implementations for the functions above.
28
35
"""
29
36
30
- @ callback init ( opts :: keyword ) :: Macro . t
31
- @ callback handle_body ( quoted :: Macro . t ) :: Macro . t
32
- @ callback handle_text ( buffer :: Macro . t , text :: String . t ) :: Macro . t
33
- @ callback handle_expr ( buffer :: Macro . t , marker :: String . t , expr :: Macro . t ) :: Macro . t
37
+ @ type state :: term
38
+
39
+ @ callback init ( opts :: keyword ) :: state
40
+ @ callback handle_body ( state ) :: Macro . t
41
+ @ callback handle_text ( state , text :: String . t ) :: state
42
+ @ callback handle_expr ( state , marker :: String . t , expr :: Macro . t ) :: state
43
+ @ callback handle_begin ( state ) :: state
44
+ @ callback handle_end ( state ) :: Macro . t
34
45
35
46
@ doc false
36
47
defmacro __using__ ( _ ) do
@@ -45,6 +56,14 @@ defmodule EEx.Engine do
45
56
EEx.Engine . handle_body ( quoted )
46
57
end
47
58
59
+ def handle_begin ( quoted ) do
60
+ EEx.Engine . handle_begin ( quoted )
61
+ end
62
+
63
+ def handle_end ( quoted ) do
64
+ EEx.Engine . handle_end ( quoted )
65
+ end
66
+
48
67
def handle_text ( buffer , text ) do
49
68
EEx.Engine . handle_text ( buffer , text )
50
69
end
@@ -104,6 +123,20 @@ defmodule EEx.Engine do
104
123
""
105
124
end
106
125
126
+ @ doc """
127
+ Returns an empty string as the new buffer.
128
+ """
129
+ def handle_begin ( _previous ) do
130
+ ""
131
+ end
132
+
133
+ @ doc """
134
+ End of the new buffer.
135
+ """
136
+ def handle_end ( quoted ) do
137
+ quoted
138
+ end
139
+
107
140
@ doc """
108
141
The default implementation simply returns the given expression.
109
142
"""
0 commit comments