@@ -88,28 +88,93 @@ void fluid_render_data(fluid_t *ctx)
88
88
89
89
void fluid_explode_sub_ctx (fluid_t * ctx , lexer_block_t * blk , fluid_t * sub_ctx )
90
90
{
91
- list_insert_nodes (& ctx -> lex_blocks , & blk -> node , & sub_ctx -> lex_blocks );
92
- list_remove_node (& ctx -> lex_blocks , & blk -> node );
93
- lexer_free_block (blk );
91
+ list_insert_nodes (& ctx -> lex_blocks , & blk -> node ,
92
+ sub_ctx -> lex_blocks .head , sub_ctx -> lex_blocks .tail );
93
+
94
+ lexer_remove_block (ctx , blk );
94
95
95
96
/* empty sub_ctx blocks to prevent lexer_teardown() from free-ing them */
96
97
sub_ctx -> lex_blocks .head = NULL ;
97
98
sub_ctx -> lex_blocks .tail = NULL ;
98
99
}
99
100
100
- int fluid_handle_includes (fluid_t * ctx )
101
+ int fluid_remove_blocks (fluid_t * ctx , lexer_block_t * start , lexer_block_t * end )
102
+ {
103
+ if (list_remove_nodes (& ctx -> lex_blocks , & start -> node , & end -> node )) {
104
+ printf ("fluid: block remove failed\n" );
105
+ return -1 ;
106
+ }
107
+
108
+ while (start != end ) {
109
+ lexer_remove_block (ctx , start );
110
+ start = CONTAINER_OF (start -> node .next , lexer_block_t , node );
111
+ }
112
+ lexer_remove_block (ctx , end );
113
+ return 0 ;
114
+ }
115
+
116
+ void fluid_handle_raw_blocks (fluid_t * ctx , lexer_block_t * start , lexer_block_t * end )
117
+ {
118
+ lexer_block_t * p ;
119
+
120
+ p = CONTAINER_OF (start -> node .next , lexer_block_t , node );
121
+ lexer_remove_block (ctx , start );
122
+ while (p != end ) {
123
+ lexer_block_cast_to_data (p );
124
+ p = CONTAINER_OF (p -> node .next , lexer_block_t , node );
125
+ }
126
+ lexer_remove_block (ctx , end );
127
+ }
128
+
129
+ int fluid_preprocessor (fluid_t * ctx )
101
130
{
102
- fluid_t * sub_ctx = NULL ;
103
- lexer_block_t * blk ;
104
131
node_t * p ;
132
+ lexer_block_t * blk ;
133
+ fluid_t * sub_ctx ;
134
+ lexer_block_t * comment_start = NULL ;
135
+ lexer_block_t * raw_start = NULL ;
105
136
106
137
p = ctx -> lex_blocks .head ;
107
138
while (p ) {
108
139
blk = CONTAINER_OF (p , lexer_block_t , node );
109
140
p = p -> next ;
141
+
142
+ /* strip comments */
143
+ if (comment_start ) {
144
+ if (blk -> type == LEXER_BLOCK_TAG &&
145
+ blk -> tok .tag .keyword == LIQ_KW_ENDCOMMENT ) {
146
+ if (fluid_remove_blocks (ctx , comment_start , blk ))
147
+ return -1 ;
148
+ comment_start = NULL ;
149
+ }
150
+ continue ;
151
+ }
152
+ else if (blk -> type == LEXER_BLOCK_TAG &&
153
+ blk -> tok .tag .keyword == LIQ_KW_COMMENT ) {
154
+ comment_start = blk ;
155
+ continue ;
156
+ }
157
+
158
+ /* force mark raw blocks as data blocks */
159
+ if (raw_start ) {
160
+ if (blk -> type == LEXER_BLOCK_TAG &&
161
+ blk -> tok .tag .keyword == LIQ_KW_ENDRAW ) {
162
+ fluid_handle_raw_blocks (ctx , raw_start , blk );
163
+ raw_start = NULL ;
164
+ }
165
+ continue ;
166
+ }
167
+ else if (blk -> type == LEXER_BLOCK_TAG &&
168
+ blk -> tok .tag .keyword == LIQ_KW_RAW ) {
169
+ raw_start = blk ;
170
+ continue ;
171
+ }
172
+
173
+ /* include files */
110
174
if (blk -> type == LEXER_BLOCK_TAG &&
111
- liquid_get_kw (blk -> tok .tag .keyword ) == LIQ_KW_INCLUDE &&
112
- blk -> tok .tag .tokens && blk -> tok .tag .tokens [0 ]) {
175
+ blk -> tok .tag .keyword == LIQ_KW_INCLUDE &&
176
+ blk -> tok .tag .tokens && blk -> tok .tag .tokens [0 ])
177
+ {
113
178
sub_ctx = fluid_load (ctx -> dirname , blk -> tok .tag .tokens [0 ]);
114
179
if (sub_ctx == NULL ) {
115
180
return -1 ;
@@ -118,14 +183,21 @@ int fluid_handle_includes(fluid_t *ctx)
118
183
if (lexer_lex (sub_ctx ) != 0 ) {
119
184
return -1 ;
120
185
}
121
- if (fluid_handle_includes (sub_ctx )) {
186
+ if (fluid_preprocessor (sub_ctx )) {
122
187
return -1 ;
123
188
}
124
189
fluid_explode_sub_ctx (ctx , blk , sub_ctx );
125
190
lexer_teardown (sub_ctx );
126
191
fluid_destroy_context (sub_ctx );
127
192
}
128
193
}
194
+
195
+ /**
196
+ * After preprocessing there may be consecutive data blocks
197
+ * so call lexer_grabage_collect() to squash them.
198
+ */
199
+ lexer_grabage_collect (ctx );
200
+
129
201
return 0 ;
130
202
}
131
203
@@ -148,7 +220,7 @@ int main(int argc, char *argv[])
148
220
return -1 ;
149
221
}
150
222
151
- if (fluid_handle_includes (ctx )) {
223
+ if (fluid_preprocessor (ctx )) {
152
224
return -1 ;
153
225
}
154
226
@@ -160,7 +232,7 @@ int main(int argc, char *argv[])
160
232
fluid_render_data (ctx );
161
233
162
234
lexer_teardown (ctx );
163
- parseer_teardown (ctx );
235
+ parser_teardown (ctx );
164
236
fluid_destroy_context (ctx );
165
237
return 0 ;
166
238
}
0 commit comments