|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Htsl\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use Htsl\Htsl; |
|
6
|
|
|
use Htsl\ReadingBuffer\Contracts\ABuffer as Buffer; |
|
7
|
|
|
use Htsl\ReadingBuffer\Line; |
|
8
|
|
|
use Htsl\Helper\TGetter; |
|
9
|
|
|
use Htsl\Helper\IConfigProvider; |
|
10
|
|
|
use Htsl\Parser\Node\TagNode; |
|
11
|
|
|
use Htsl\Parser\Node\StringNode; |
|
12
|
|
|
use Htsl\Parser\Node\CommentNode; |
|
13
|
|
|
use Htsl\Parser\Node\ControlNode; |
|
14
|
|
|
use Htsl\Parser\Node\SectionNode; |
|
15
|
|
|
use Htsl\Parser\Node\NamelessSectionNode; |
|
16
|
|
|
use Htsl\Parser\Node\Contracts\ANode as Node; |
|
17
|
|
|
|
|
18
|
|
|
//////////////////////////////////////////////////////////////// |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @property-read string $content Result of document executing. |
|
22
|
|
|
* @property-read string $doctype Type of this document. |
|
23
|
|
|
* @property-read string|bool $indentation Indentation of document. |
|
24
|
|
|
* @property-read \Htsl\Parser\Node\Contracts\ANode|bool $scope Current scope on top of stack. |
|
25
|
|
|
* @property-read \Htsl\Htsl $htsl Htsl main object of document. |
|
26
|
|
|
* @property-read int $indentLevel Current indent level. |
|
27
|
|
|
*/ |
|
28
|
|
|
class Document implements IConfigProvider |
|
29
|
|
|
{ |
|
30
|
|
|
use TGetter; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Htsl main object owns this document. |
|
34
|
|
|
* |
|
35
|
|
|
* @access private |
|
36
|
|
|
* |
|
37
|
|
|
* @var \Htsl\Htsl |
|
38
|
|
|
*/ |
|
39
|
|
|
private $htsl; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Parent document. |
|
43
|
|
|
* |
|
44
|
|
|
* @access private |
|
45
|
|
|
* |
|
46
|
|
|
* @var \Htsl\Parser\Document | null |
|
47
|
|
|
*/ |
|
48
|
|
|
private $parent; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Reading buffer of this document. |
|
52
|
|
|
* |
|
53
|
|
|
* @access private |
|
54
|
|
|
* |
|
55
|
|
|
* @var \Htsl\ReadingBuffer\Contracts\ABuffer |
|
56
|
|
|
*/ |
|
57
|
|
|
private $buffer; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The indentation and whether output with linefeed and indent. |
|
61
|
|
|
* |
|
62
|
|
|
* @access private |
|
63
|
|
|
* |
|
64
|
|
|
* @var string | bool |
|
65
|
|
|
*/ |
|
66
|
|
|
private $indentation; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Type of this document. |
|
70
|
|
|
* |
|
71
|
|
|
* @access private |
|
72
|
|
|
* |
|
73
|
|
|
* @var string |
|
74
|
|
|
*/ |
|
75
|
|
|
private $docType; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Current embead script. |
|
79
|
|
|
* |
|
80
|
|
|
* @access private |
|
81
|
|
|
* |
|
82
|
|
|
* @var \Htsl\Embedment\Contract\Embedment |
|
83
|
|
|
*/ |
|
84
|
|
|
private $embedment; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Current indent level. |
|
88
|
|
|
* |
|
89
|
|
|
* @access private |
|
90
|
|
|
* |
|
91
|
|
|
* @var int |
|
92
|
|
|
*/ |
|
93
|
|
|
private $level= 0; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Section indent level. |
|
97
|
|
|
* |
|
98
|
|
|
* @access private |
|
99
|
|
|
* |
|
100
|
|
|
* @var int |
|
101
|
|
|
*/ |
|
102
|
|
|
private $sectionLevel= 0; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Opened nodes. |
|
106
|
|
|
* |
|
107
|
|
|
* @access private |
|
108
|
|
|
* |
|
109
|
|
|
* @var [ Htsl\Parser\Node\Contracts\ANode, ] |
|
110
|
|
|
*/ |
|
111
|
|
|
private $openedNodes= []; |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Current scopes. |
|
115
|
|
|
* |
|
116
|
|
|
* @access private |
|
117
|
|
|
* |
|
118
|
|
|
* @var [ Htsl\Parser\Node\Contracts\ANode, ] |
|
119
|
|
|
*/ |
|
120
|
|
|
private $scopes= []; |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Current line number. |
|
124
|
|
|
* |
|
125
|
|
|
* @access private |
|
126
|
|
|
* |
|
127
|
|
|
* @var int |
|
128
|
|
|
*/ |
|
129
|
|
|
private $lineNumber= 0; |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Current line. |
|
133
|
|
|
* |
|
134
|
|
|
* @access private |
|
135
|
|
|
* |
|
136
|
|
|
* @var \Htsl\ReadingBuffer\Line |
|
137
|
|
|
*/ |
|
138
|
|
|
private $currentLine; |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Sections that can be show. |
|
142
|
|
|
* |
|
143
|
|
|
* @access private |
|
144
|
|
|
* |
|
145
|
|
|
* @var [ Htsl\Parser\Section, ] |
|
146
|
|
|
*/ |
|
147
|
|
|
private $sections=[]; |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Whether the document is executed. |
|
151
|
|
|
* |
|
152
|
|
|
* @access private |
|
153
|
|
|
* |
|
154
|
|
|
* @var bool |
|
155
|
|
|
*/ |
|
156
|
|
|
private $isExecuted; |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Current Section. |
|
160
|
|
|
* |
|
161
|
|
|
* @access private |
|
162
|
|
|
* |
|
163
|
|
|
* @var \Htsl\Parser\Section |
|
164
|
|
|
*/ |
|
165
|
|
|
private $currentSection; |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* The content of this document. |
|
169
|
|
|
* |
|
170
|
|
|
* @access private |
|
171
|
|
|
* |
|
172
|
|
|
* @var string |
|
173
|
|
|
*/ |
|
174
|
|
|
private $content; |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Constructor of the Document. |
|
178
|
|
|
* |
|
179
|
|
|
* @access public |
|
180
|
|
|
* |
|
181
|
|
|
* @param \Htsl\Htsl $htsl |
|
182
|
|
|
* @param \Htsl\ReadingBuffer\Contracts\ABuffer $buffer |
|
183
|
|
|
* @param \Htsl\Parser\Document | null $parent |
|
184
|
|
|
*/ |
|
185
|
|
|
public function __construct( Htsl$htsl, Buffer$buffer, self$parent=null ) |
|
186
|
|
|
{ |
|
187
|
|
|
$this->htsl= $htsl; |
|
188
|
|
|
$this->buffer= $buffer; |
|
189
|
|
|
|
|
190
|
|
|
if( $parent ){ |
|
191
|
|
|
$this->parent= $parent; |
|
192
|
|
|
$this->docType= $parent->docType; |
|
193
|
|
|
$this->indentation= $parent->indentation; |
|
194
|
|
|
}else{ |
|
195
|
|
|
$this->parseFirstLine(); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Executing the document. |
|
201
|
|
|
* |
|
202
|
|
|
* @access public |
|
203
|
|
|
* |
|
204
|
|
|
* @return \Htsl\Parser\Document |
|
205
|
|
|
*/ |
|
206
|
|
|
public function execute():self |
|
207
|
|
|
{ |
|
208
|
|
|
if( $this->isExecuted ){ |
|
209
|
|
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
return $this->lineByLine() |
|
212
|
|
|
->bubbleSections() |
|
213
|
|
|
; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Alias of getContent. |
|
218
|
|
|
* |
|
219
|
|
|
* @access public |
|
220
|
|
|
* |
|
221
|
|
|
* @return string |
|
222
|
|
|
*/ |
|
223
|
|
|
public function __toString():string |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->getContent(); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Getting the result of document executing. |
|
230
|
|
|
* |
|
231
|
|
|
* @access protected |
|
232
|
|
|
* |
|
233
|
|
|
* @return string |
|
234
|
|
|
*/ |
|
235
|
|
|
protected function getContent():string |
|
236
|
|
|
{ |
|
237
|
|
|
if( $this->parent ){ |
|
238
|
|
|
return $this->execute()->parent->getContent(); |
|
239
|
|
|
}else{ |
|
240
|
|
|
return $this->execute()->content; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Getting the next line. |
|
246
|
|
|
* |
|
247
|
|
|
* @access protected |
|
248
|
|
|
* |
|
249
|
|
|
* @return \Htsl\ReadingBuffer\Line |
|
250
|
|
|
*/ |
|
251
|
|
|
protected function getLine():Line |
|
252
|
|
|
{ |
|
253
|
|
|
do{ |
|
254
|
|
|
$line= $this->buffer->getLine(); |
|
255
|
|
|
}while( $line->isEmpty() && $line->hasMore() ); |
|
256
|
|
|
|
|
257
|
|
|
return $line; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Getting the config of type of this document. |
|
262
|
|
|
* |
|
263
|
|
|
* @access public |
|
264
|
|
|
* |
|
265
|
|
|
* @param [ string, ] ...$keys |
|
266
|
|
|
* |
|
267
|
|
|
* @return mixed |
|
268
|
|
|
*/ |
|
269
|
|
|
public function getConfig( string...$keys ) |
|
270
|
|
|
{ |
|
271
|
|
|
return $this->htsl->getConfig(array_shift($keys),$this->docType,...$keys); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Getting the type of this document. |
|
276
|
|
|
* |
|
277
|
|
|
* @access public |
|
278
|
|
|
* |
|
279
|
|
|
* @return string |
|
280
|
|
|
*/ |
|
281
|
|
|
public function getDoctype():string |
|
282
|
|
|
{ |
|
283
|
|
|
return $this->docType; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Getting the indentation. |
|
288
|
|
|
* |
|
289
|
|
|
* @access public |
|
290
|
|
|
* |
|
291
|
|
|
* @return string | bool |
|
292
|
|
|
*/ |
|
293
|
|
|
public function getIndentation() |
|
294
|
|
|
{ |
|
295
|
|
|
return $this->indentation; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Getting the indent level. |
|
300
|
|
|
* |
|
301
|
|
|
* @access public |
|
302
|
|
|
* |
|
303
|
|
|
* @return int |
|
304
|
|
|
*/ |
|
305
|
|
|
public function getIndentLevel():int |
|
306
|
|
|
{ |
|
307
|
|
|
return $this->level; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Parsing the first line. |
|
312
|
|
|
* |
|
313
|
|
|
* @access protected |
|
314
|
|
|
* |
|
315
|
|
|
* @return \Htsl\Parser\Document |
|
316
|
|
|
*/ |
|
317
|
|
|
protected function parseFirstLine():self |
|
318
|
|
|
{ |
|
319
|
|
|
$line= $this->getLine(); |
|
320
|
|
|
|
|
321
|
|
|
if( '@'===$line->getChar(0) ){ |
|
322
|
|
|
return $this->setExtending($line); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
$this->docType= $line->content; |
|
326
|
|
|
$docTypeContent= $this->getConfig('doc_types') or $this->throw("DocType $this->docType is not supported"); |
|
327
|
|
|
|
|
328
|
|
|
$this->indentation= $this->htsl->getConfig('indentation',$this->docType) ?? ( function( $scalarOrFalse ){ return is_scalar($scalarOrFalse)?$scalarOrFalse:false; } )($this->htsl->getConfig('indentation')); |
|
329
|
|
|
|
|
330
|
|
|
return $this->appendLine($docTypeContent); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Setting that this document extends another document. |
|
335
|
|
|
* |
|
336
|
|
|
* @access protected |
|
337
|
|
|
* |
|
338
|
|
|
* @param \Htsl\ReadingBuffer\Line $firstLine |
|
339
|
|
|
* |
|
340
|
|
|
* @return \Htsl\Parser\Document |
|
341
|
|
|
*/ |
|
342
|
|
|
protected function setExtending( Line$firstLine ):self |
|
343
|
|
|
{ |
|
344
|
|
|
switch( $name= $firstLine->pregGet('/(?<=^@)[\w-:]+/') ){ |
|
345
|
|
|
default:{ |
|
346
|
|
|
$this->throw("The @$name is not supported."); |
|
347
|
|
|
}break; |
|
348
|
|
|
case 'extend':{ |
|
349
|
|
|
$this->extend($firstLine->pregGet('/(?<=\( ).*(?= \))/')); |
|
350
|
|
|
}break; |
|
351
|
|
|
case 'show': |
|
352
|
|
|
case 'include': |
|
353
|
|
|
case 'section':{ |
|
354
|
|
|
$this->throw("The @$name can not be used on first line."); |
|
355
|
|
|
}break; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
return $this; |
|
|
|
|
|
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* Parsing this document line by line. |
|
363
|
|
|
* |
|
364
|
|
|
* @access protected |
|
365
|
|
|
* |
|
366
|
|
|
* @return \Htsl\Parser\Document |
|
367
|
|
|
*/ |
|
368
|
|
|
protected function lineByLine():self |
|
369
|
|
|
{ |
|
370
|
|
|
while( ($line= $this->getLine())->hasMore() ){ |
|
371
|
|
|
$this->lineNumber+= 1; |
|
372
|
|
|
|
|
373
|
|
|
if( $this->embedment ){ |
|
374
|
|
|
$this->embeddingParse($line); |
|
375
|
|
|
}else{ |
|
376
|
|
|
$this->parseLine($line); |
|
377
|
|
|
} |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
$this->embedment and $this->breakEmbedding(); |
|
381
|
|
|
|
|
382
|
|
|
$this->closeNodes($this->level); |
|
383
|
|
|
|
|
384
|
|
|
$this->isExecuted= true; |
|
385
|
|
|
|
|
386
|
|
|
return $this; |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* Parsing embedded line. |
|
391
|
|
|
* |
|
392
|
|
|
* @access protected |
|
393
|
|
|
* |
|
394
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
|
395
|
|
|
* |
|
396
|
|
|
* @return \Htsl\Parser\Document |
|
397
|
|
|
*/ |
|
398
|
|
|
protected function embeddingParse( Line$line ):self |
|
399
|
|
|
{ |
|
400
|
|
|
if( $line->content==='<}' ){ |
|
401
|
|
|
$this->breakEmbedding(); |
|
402
|
|
|
}else{ |
|
403
|
|
|
$this->embedment->parseLine($line->getSubIndentLine()); |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
return $this; |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
/** |
|
410
|
|
|
* Starting the embedding. |
|
411
|
|
|
* |
|
412
|
|
|
* @access protected |
|
413
|
|
|
* |
|
414
|
|
|
* @param string $embedType |
|
415
|
|
|
* |
|
416
|
|
|
* @return \Htsl\Parser\Document |
|
417
|
|
|
*/ |
|
418
|
|
|
protected function startEmbedding( string$embedType ):self |
|
419
|
|
|
{ |
|
420
|
|
|
$embedmentClass= '\\Htsl\\Embedment\\'.ucfirst($embedType).'Embedment'; |
|
421
|
|
|
class_exists($embedmentClass) or $this->throw("Embed type $embedType not exists."); |
|
422
|
|
|
|
|
423
|
|
|
$this->embedment= new $embedmentClass($this); |
|
424
|
|
|
|
|
425
|
|
|
return $this; |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
/** |
|
429
|
|
|
* Ending the embedding. |
|
430
|
|
|
* |
|
431
|
|
|
* @access public |
|
432
|
|
|
* |
|
433
|
|
|
* @return \Htsl\Parser\Document |
|
434
|
|
|
*/ |
|
435
|
|
|
public function breakEmbedding():self |
|
436
|
|
|
{ |
|
437
|
|
|
$this->append($this->embedment->getContent()); |
|
438
|
|
|
$this->embedment= null; |
|
439
|
|
|
|
|
440
|
|
|
return $this; |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
/** |
|
444
|
|
|
* Parsing line. |
|
445
|
|
|
* |
|
446
|
|
|
* @access protected |
|
447
|
|
|
* |
|
448
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
|
449
|
|
|
* |
|
450
|
|
|
* @return \Htsl\Parser\Document |
|
451
|
|
|
*/ |
|
452
|
|
|
protected function parseLine( Line$line ):self |
|
453
|
|
|
{ |
|
454
|
|
|
$this->currentLine= $line; |
|
455
|
|
|
$this->setLevel($line->getIndentLevel()); |
|
456
|
|
|
|
|
457
|
|
|
$this->{'parse'.ucfirst($this->getLineType($line))}($line); |
|
458
|
|
|
|
|
459
|
|
|
return $this; |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
/** |
|
463
|
|
|
* Getting line type by analyzing first or first two characters of line. |
|
464
|
|
|
* |
|
465
|
|
|
* @access protected |
|
466
|
|
|
* |
|
467
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
|
468
|
|
|
* |
|
469
|
|
|
* @return string |
|
470
|
|
|
*/ |
|
471
|
|
|
protected function getLineType( Line$line ):string |
|
472
|
|
|
{ |
|
473
|
|
|
$possibleType= self::POSSIBLE_LINE_TYPES; |
|
474
|
|
|
|
|
475
|
|
|
for( $i=0; is_array($possibleType); ++$i ){ |
|
476
|
|
|
$possibleType= $possibleType[$line->getChar($i)]??$possibleType[' ']; |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
|
|
return $possibleType; |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
/** |
|
483
|
|
|
* Possible line types. |
|
484
|
|
|
* |
|
485
|
|
|
* @access public |
|
486
|
|
|
* |
|
487
|
|
|
* @const array |
|
488
|
|
|
* |
|
489
|
|
|
* @todo Make this const private when php 7.1 |
|
490
|
|
|
*/ |
|
491
|
|
|
const POSSIBLE_LINE_TYPES= [ |
|
492
|
|
|
'`'=> [ |
|
493
|
|
|
'='=> 'expressionHtmlLine', |
|
494
|
|
|
' '=> 'htmlLine', |
|
495
|
|
|
], |
|
496
|
|
|
'='=> 'expressionLine', |
|
497
|
|
|
'!'=> 'commentLine', |
|
498
|
|
|
'-'=> 'tagLine', |
|
499
|
|
|
'~'=> 'controlLine', |
|
500
|
|
|
'@'=> 'docControlLine', |
|
501
|
|
|
' '=> 'stringLine', |
|
502
|
|
|
]; |
|
503
|
|
|
|
|
504
|
|
|
/** |
|
505
|
|
|
* Parsing line as HTML content. |
|
506
|
|
|
* |
|
507
|
|
|
* @access protected |
|
508
|
|
|
* |
|
509
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
|
510
|
|
|
* |
|
511
|
|
|
* @return \Htsl\Parser\Document |
|
512
|
|
|
*/ |
|
513
|
|
|
protected function parseHtmlLine( Line$line ):self |
|
514
|
|
|
{ |
|
515
|
|
|
return $this->openNode(new StringNode($this,$line))->appendLine($line->slice(1)); |
|
516
|
|
|
} |
|
517
|
|
|
|
|
518
|
|
|
/** |
|
519
|
|
|
* Parsing line as string content. |
|
520
|
|
|
* |
|
521
|
|
|
* @access protected |
|
522
|
|
|
* |
|
523
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
|
524
|
|
|
* |
|
525
|
|
|
* @return \Htsl\Parser\Document |
|
526
|
|
|
*/ |
|
527
|
|
|
protected function parseStringLine( Line$line ):self |
|
528
|
|
|
{ |
|
529
|
|
|
return $this->openNode(new StringNode($this,$line))->appendLine($this->htmlEntities(trim($line->getContent()))); |
|
530
|
|
|
} |
|
531
|
|
|
|
|
532
|
|
|
/** |
|
533
|
|
|
* Parsing line as PHP expression. |
|
534
|
|
|
* |
|
535
|
|
|
* @access protected |
|
536
|
|
|
* |
|
537
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
|
538
|
|
|
* |
|
539
|
|
|
* @return \Htsl\Parser\Document |
|
540
|
|
|
*/ |
|
541
|
|
|
protected function parseExpressionLine( Line$line ):self |
|
542
|
|
|
{ |
|
543
|
|
|
$content= $line->slice(1); |
|
544
|
|
|
$ent_flag= var_export($this->htsl->getConfig('ENT_flags',$this->docType),true); |
|
545
|
|
|
$charset= var_export($this->htsl->getConfig('charset'),true); |
|
546
|
|
|
|
|
547
|
|
|
return $this->openNode(new StringNode($this,$line))->appendLine("<?=htmlentities($content,$ent_flag,$charset,false)?>"); |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
/** |
|
551
|
|
|
* Parsing line as PHP expression with HTML result. |
|
552
|
|
|
* |
|
553
|
|
|
* @access protected |
|
554
|
|
|
* |
|
555
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
|
556
|
|
|
* |
|
557
|
|
|
* @return \Htsl\Parser\Document |
|
558
|
|
|
*/ |
|
559
|
|
|
protected function parseExpressionHtmlLine( Line$line ):self |
|
560
|
|
|
{ |
|
561
|
|
|
$content= $line->slice(1); |
|
562
|
|
|
|
|
563
|
|
|
return $this->openNode(new StringNode($this,$line))->appendLine("<?$content?>"); |
|
564
|
|
|
} |
|
565
|
|
|
|
|
566
|
|
|
/** |
|
567
|
|
|
* Parsing line as comment. |
|
568
|
|
|
* |
|
569
|
|
|
* @access protected |
|
570
|
|
|
* |
|
571
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
|
572
|
|
|
* |
|
573
|
|
|
* @return \Htsl\Parser\Document |
|
574
|
|
|
*/ |
|
575
|
|
|
protected function parseCommentLine( Line$line ):self |
|
576
|
|
|
{ |
|
577
|
|
|
$node= new CommentNode($this,$line); |
|
578
|
|
|
|
|
579
|
|
|
return $this->openNode($node)->appendLine($node->open()); |
|
580
|
|
|
} |
|
581
|
|
|
|
|
582
|
|
|
/** |
|
583
|
|
|
* Parsing line as HTSL tag. |
|
584
|
|
|
* |
|
585
|
|
|
* @access protected |
|
586
|
|
|
* |
|
587
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
|
588
|
|
|
* |
|
589
|
|
|
* @return \Htsl\Parser\Document |
|
590
|
|
|
*/ |
|
591
|
|
|
protected function parseTagLine( Line$line ):self |
|
592
|
|
|
{ |
|
593
|
|
|
$node= new TagNode($this,$line); |
|
594
|
|
|
|
|
595
|
|
|
$this->appendLine($node->open()); |
|
596
|
|
|
|
|
597
|
|
|
$node->embed and $this->startEmbedding($node->embed); |
|
598
|
|
|
|
|
599
|
|
|
return $this->openNode($node); |
|
600
|
|
|
} |
|
601
|
|
|
|
|
602
|
|
|
/** |
|
603
|
|
|
* Parsing line as control node of Htsl.php. |
|
604
|
|
|
* |
|
605
|
|
|
* @access protected |
|
606
|
|
|
* |
|
607
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
|
608
|
|
|
* |
|
609
|
|
|
* @return \Htsl\Parser\Document |
|
610
|
|
|
*/ |
|
611
|
|
|
protected function parseControlLine( Line$line ):self |
|
612
|
|
|
{ |
|
613
|
|
|
$node= new ControlNode($this,$line); |
|
614
|
|
|
|
|
615
|
|
|
return $this->appendLine($node->open())->openNode($node); |
|
616
|
|
|
} |
|
617
|
|
|
|
|
618
|
|
|
/** |
|
619
|
|
|
* Parsing line as document control node of Htsl.php. |
|
620
|
|
|
* |
|
621
|
|
|
* @access protected |
|
622
|
|
|
* |
|
623
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
|
624
|
|
|
* |
|
625
|
|
|
* @return \Htsl\Parser\Document |
|
626
|
|
|
*/ |
|
627
|
|
|
protected function parseDocControlLine( Line$line ):self |
|
628
|
|
|
{ |
|
629
|
|
|
switch( $name= $line->pregGet('/(?<=^@)[\w-:]+/') ){ |
|
630
|
|
|
default:{ |
|
631
|
|
|
$this->throw("The @$name is not supported."); |
|
632
|
|
|
}break; |
|
633
|
|
|
case 'extend':{ |
|
634
|
|
|
$this->throw('The @extend can only be used on first line.'); |
|
635
|
|
|
}break; |
|
636
|
|
|
case 'include':{ |
|
637
|
|
|
$this->include($line); |
|
638
|
|
|
}break; |
|
639
|
|
|
case 'section':{ |
|
640
|
|
|
$this->defineSection($line); |
|
641
|
|
|
}break; |
|
642
|
|
|
case 'show':{ |
|
643
|
|
|
$this->showSection($line); |
|
644
|
|
|
}break; |
|
645
|
|
|
} |
|
646
|
|
|
|
|
647
|
|
|
return $this; |
|
648
|
|
|
} |
|
649
|
|
|
|
|
650
|
|
|
/** |
|
651
|
|
|
* Parsing extending defination. |
|
652
|
|
|
* |
|
653
|
|
|
* @access protected |
|
654
|
|
|
* |
|
655
|
|
|
* @param string $fileName |
|
656
|
|
|
* |
|
657
|
|
|
* @return \Htsl\Parser\Document |
|
658
|
|
|
*/ |
|
659
|
|
|
protected function extend( string$fileName ):self |
|
660
|
|
|
{ |
|
661
|
|
|
$this->parent= new static($this->htsl,$this->buffer->goSide($fileName)); |
|
662
|
|
|
|
|
663
|
|
|
$this->docType= $this->parent->docType; |
|
664
|
|
|
$this->indentation= $this->parent->indentation; |
|
665
|
|
|
|
|
666
|
|
|
return $this; |
|
667
|
|
|
} |
|
668
|
|
|
|
|
669
|
|
|
/** |
|
670
|
|
|
* Insert indentations into given paragraph. |
|
671
|
|
|
* |
|
672
|
|
|
* @access protected |
|
673
|
|
|
* |
|
674
|
|
|
* @param string $input |
|
675
|
|
|
* |
|
676
|
|
|
* @return string |
|
677
|
|
|
*/ |
|
678
|
|
|
protected function insertIndentations( string$input ):string |
|
679
|
|
|
{ |
|
680
|
|
|
return preg_replace('/(?<=^|\\n)(?!$)/',str_repeat($this->indentation,$this->level-$this->sectionLevel),$input); |
|
681
|
|
|
} |
|
682
|
|
|
|
|
683
|
|
|
/** |
|
684
|
|
|
* Include another document. |
|
685
|
|
|
* |
|
686
|
|
|
* @access protected |
|
687
|
|
|
* |
|
688
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
|
689
|
|
|
* |
|
690
|
|
|
* @return \Htsl\Parser\Document |
|
691
|
|
|
*/ |
|
692
|
|
|
protected function include( Line$line ):self |
|
693
|
|
|
{ |
|
694
|
|
|
$inclued= (new static($this->htsl,$this->buffer->goSide($line->pregGet('/(?<=\( ).*(?= \))/')),$this))->execute()->content; |
|
695
|
|
|
|
|
696
|
|
|
false===$this->indentation or $inclued= $this->insertIndentations($inclued); |
|
|
|
|
|
|
697
|
|
|
|
|
698
|
|
|
return $this->openNode(new StringNode($this,$line))->append($inclued); |
|
699
|
|
|
} |
|
700
|
|
|
|
|
701
|
|
|
/** |
|
702
|
|
|
* Starting to define a section. |
|
703
|
|
|
* |
|
704
|
|
|
* @access protected |
|
705
|
|
|
* |
|
706
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
|
707
|
|
|
* |
|
708
|
|
|
* @return \Htsl\Parser\Document |
|
709
|
|
|
*/ |
|
710
|
|
|
protected function defineSection( Line$line ):self |
|
711
|
|
|
{ |
|
712
|
|
|
$node= new SectionNode($this,$line); |
|
713
|
|
|
|
|
714
|
|
|
$node->open(); |
|
715
|
|
|
|
|
716
|
|
|
return $this->openNode($node); |
|
717
|
|
|
} |
|
718
|
|
|
|
|
719
|
|
|
/** |
|
720
|
|
|
* Showing a section. |
|
721
|
|
|
* |
|
722
|
|
|
* @access protected |
|
723
|
|
|
* |
|
724
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
|
725
|
|
|
* |
|
726
|
|
|
* @return \Htsl\Parser\Document |
|
727
|
|
|
*/ |
|
728
|
|
|
protected function showSection( Line$line ):self |
|
729
|
|
|
{ |
|
730
|
|
|
$sectionName= $line->pregGet('/(?<=\( ).*(?= \))/'); |
|
731
|
|
|
|
|
732
|
|
|
if( !isset($this->sections[$sectionName]) ){ |
|
733
|
|
|
return $this->openNode(new StringNode($this,$line)); |
|
734
|
|
|
} |
|
735
|
|
|
$content= $this->sections[$sectionName]->content; |
|
736
|
|
|
|
|
737
|
|
|
false===$this->indentation or $content= $this->insertIndentations($content); |
|
|
|
|
|
|
738
|
|
|
|
|
739
|
|
|
$this->append($content); |
|
740
|
|
|
|
|
741
|
|
|
$node= new NamelessSectionNode($this,$line); |
|
742
|
|
|
|
|
743
|
|
|
$node->open(); |
|
744
|
|
|
|
|
745
|
|
|
return $this->openNode($node); |
|
746
|
|
|
} |
|
747
|
|
|
|
|
748
|
|
|
/** |
|
749
|
|
|
* Setting document as section definer. |
|
750
|
|
|
* |
|
751
|
|
|
* @access public |
|
752
|
|
|
* |
|
753
|
|
|
* @param Section | null $section |
|
754
|
|
|
*/ |
|
755
|
|
|
public function setSection( Section$section=null ):self |
|
756
|
|
|
{ |
|
757
|
|
|
if( !$section ){ |
|
758
|
|
|
$this->sectionLevel= 0; |
|
759
|
|
|
$this->currentSection= null; |
|
760
|
|
|
|
|
761
|
|
|
return $this; |
|
762
|
|
|
} |
|
763
|
|
|
|
|
764
|
|
|
if( $this->currentSection ){ |
|
765
|
|
|
$this->throw('Nesting definition of section is forbidden.'); |
|
766
|
|
|
} |
|
767
|
|
|
|
|
768
|
|
|
if( isset($this->parent->sections[$section->name]) ){ |
|
769
|
|
|
$this->throw("Section {$section->name} already defined."); |
|
770
|
|
|
} |
|
771
|
|
|
|
|
772
|
|
|
$this->currentSection= $section; |
|
773
|
|
|
|
|
774
|
|
|
if( $section->name ){ |
|
775
|
|
|
$this->parent->sections[$section->name]=$section; |
|
776
|
|
|
} |
|
777
|
|
|
|
|
778
|
|
|
$this->sectionLevel= $this->level+1; |
|
779
|
|
|
|
|
780
|
|
|
return $this; |
|
781
|
|
|
} |
|
782
|
|
|
|
|
783
|
|
|
/** |
|
784
|
|
|
* Bubble the sections to parent document. |
|
785
|
|
|
* |
|
786
|
|
|
* @access protected |
|
787
|
|
|
* |
|
788
|
|
|
* @return \Htsl\Parser\Document |
|
789
|
|
|
*/ |
|
790
|
|
|
protected function bubbleSections():self |
|
791
|
|
|
{ |
|
792
|
|
|
if( $this->parent ){ |
|
793
|
|
|
foreach( $this->sections as $name=>$section ){ |
|
794
|
|
|
if( !isset($this->parent->sections[$name]) ){ |
|
795
|
|
|
$this->parent->sections[$name]=$section; |
|
796
|
|
|
}; |
|
797
|
|
|
} |
|
798
|
|
|
} |
|
799
|
|
|
|
|
800
|
|
|
return $this; |
|
801
|
|
|
} |
|
802
|
|
|
|
|
803
|
|
|
/** |
|
804
|
|
|
* Escaping characters to HTML entities. |
|
805
|
|
|
* |
|
806
|
|
|
* @access public |
|
807
|
|
|
* |
|
808
|
|
|
* @param string $input |
|
809
|
|
|
* |
|
810
|
|
|
* @return string |
|
811
|
|
|
*/ |
|
812
|
|
|
public function htmlEntities( string$input ):string |
|
813
|
|
|
{ |
|
814
|
|
|
return htmlentities($input,$this->htsl->getConfig('ENT_flags',$this->docType),$this->htsl->getConfig('charset'),false); |
|
815
|
|
|
} |
|
816
|
|
|
|
|
817
|
|
|
/** |
|
818
|
|
|
* Setting indent level of this document. |
|
819
|
|
|
* |
|
820
|
|
|
* @access protected |
|
821
|
|
|
* |
|
822
|
|
|
* @param int $level |
|
823
|
|
|
*/ |
|
824
|
|
|
protected function setLevel( int$level ):self |
|
825
|
|
|
{ |
|
826
|
|
|
$level-= $this->level; |
|
827
|
|
|
|
|
828
|
|
|
if( $level<=0 ){ |
|
829
|
|
|
$this->closeNodes(-$level); |
|
830
|
|
|
}elseif( $level==1 ){ |
|
831
|
|
|
$this->level+= 1; |
|
832
|
|
|
}else{ |
|
833
|
|
|
$this->throw('Indent error.'); |
|
834
|
|
|
} |
|
835
|
|
|
|
|
836
|
|
|
return $this; |
|
837
|
|
|
} |
|
838
|
|
|
|
|
839
|
|
|
/** |
|
840
|
|
|
* Opening a node. |
|
841
|
|
|
* |
|
842
|
|
|
* @access protected |
|
843
|
|
|
* |
|
844
|
|
|
* @param \Htsl\Parser\Node\Contracts\ANode $node |
|
845
|
|
|
* |
|
846
|
|
|
* @return \Htsl\Parser\Document |
|
847
|
|
|
*/ |
|
848
|
|
|
protected function openNode( Node$node ):self |
|
849
|
|
|
{ |
|
850
|
|
|
array_push($this->openedNodes,$node); |
|
851
|
|
|
|
|
852
|
|
|
$node->scope and $this->setScope($node); |
|
853
|
|
|
|
|
854
|
|
|
return $this; |
|
855
|
|
|
} |
|
856
|
|
|
|
|
857
|
|
|
/** |
|
858
|
|
|
* Closing open node or nodes. |
|
859
|
|
|
* |
|
860
|
|
|
* @access protected |
|
861
|
|
|
* |
|
862
|
|
|
* @param int $level |
|
863
|
|
|
* |
|
864
|
|
|
* @return \Htsl\Parser\Document |
|
865
|
|
|
*/ |
|
866
|
|
|
protected function closeNodes( int$level=0 ):self |
|
867
|
|
|
{ |
|
868
|
|
|
if( empty($this->openedNodes) ) return $this; |
|
869
|
|
|
|
|
870
|
|
|
while( $level-->=0 ){ |
|
871
|
|
|
$node= array_pop($this->openedNodes); |
|
872
|
|
|
|
|
873
|
|
|
$node->scope and $this->removeScope($node); |
|
874
|
|
|
|
|
875
|
|
|
$closer=$node->close($this->currentLine) and $this->appendLine($closer); |
|
876
|
|
|
|
|
877
|
|
|
$this->level-= $level>=0 ?1:0; |
|
878
|
|
|
} |
|
879
|
|
|
|
|
880
|
|
|
return $this; |
|
881
|
|
|
} |
|
882
|
|
|
|
|
883
|
|
|
/** |
|
884
|
|
|
* Pushing a scope to stack. |
|
885
|
|
|
* |
|
886
|
|
|
* @access protected |
|
887
|
|
|
* |
|
888
|
|
|
* @param \Htsl\Parser\Node\Contracts\ANode $scope |
|
889
|
|
|
*/ |
|
890
|
|
|
protected function setScope( Node$scope ):int |
|
891
|
|
|
{ |
|
892
|
|
|
return array_unshift($this->scopes,$scope); |
|
893
|
|
|
} |
|
894
|
|
|
|
|
895
|
|
|
/** |
|
896
|
|
|
* Getting current scope on top of stack. |
|
897
|
|
|
* |
|
898
|
|
|
* @access public |
|
899
|
|
|
* |
|
900
|
|
|
* @return \Htsl\Parser\Node\Contracts\ANode | null |
|
901
|
|
|
*/ |
|
902
|
|
|
public function getScope() |
|
903
|
|
|
{ |
|
904
|
|
|
return $this->scopes[0]??null; |
|
905
|
|
|
} |
|
906
|
|
|
|
|
907
|
|
|
/** |
|
908
|
|
|
* Pop a scope from stack. |
|
909
|
|
|
* |
|
910
|
|
|
* @access protected |
|
911
|
|
|
* |
|
912
|
|
|
* @param \Htsl\Parser\Node\Contracts\ANode $scope |
|
913
|
|
|
* |
|
914
|
|
|
* @return \Htsl\Parser\Document |
|
915
|
|
|
*/ |
|
916
|
|
|
protected function removeScope( Node$scope ):self |
|
917
|
|
|
{ |
|
918
|
|
|
if( $scope!==array_shift($this->scopes) ){ |
|
919
|
|
|
$this->throw('Scope nesting error'); |
|
920
|
|
|
}; |
|
921
|
|
|
|
|
922
|
|
|
return $this; |
|
923
|
|
|
} |
|
924
|
|
|
|
|
925
|
|
|
/** |
|
926
|
|
|
* Appending a line of content to parsing result. |
|
927
|
|
|
* |
|
928
|
|
|
* @access protected |
|
929
|
|
|
* |
|
930
|
|
|
* @param string $content |
|
931
|
|
|
* |
|
932
|
|
|
* @return \Htsl\Parser\Document |
|
933
|
|
|
*/ |
|
934
|
|
|
protected function appendLine( string$content ):self |
|
935
|
|
|
{ |
|
936
|
|
|
false===$this->indentation or $content= str_repeat($this->indentation,$this->level-$this->sectionLevel).$content."\n"; |
|
|
|
|
|
|
937
|
|
|
|
|
938
|
|
|
return $this->append($content); |
|
939
|
|
|
} |
|
940
|
|
|
|
|
941
|
|
|
/** |
|
942
|
|
|
* Appending some content to parsing result. |
|
943
|
|
|
* |
|
944
|
|
|
* @access protected |
|
945
|
|
|
* |
|
946
|
|
|
* @param string $content |
|
947
|
|
|
* |
|
948
|
|
|
* @return \Htsl\Parser\Document |
|
949
|
|
|
*/ |
|
950
|
|
|
protected function append( string$content ):self |
|
951
|
|
|
{ |
|
952
|
|
|
if( $this->currentSection ){ |
|
953
|
|
|
$this->currentSection->append($content); |
|
954
|
|
|
}else{ |
|
955
|
|
|
$this->content.=$content; |
|
956
|
|
|
} |
|
957
|
|
|
|
|
958
|
|
|
return $this; |
|
959
|
|
|
} |
|
960
|
|
|
|
|
961
|
|
|
/** |
|
962
|
|
|
* Getting the Htsl main object. |
|
963
|
|
|
* |
|
964
|
|
|
* @access public |
|
965
|
|
|
* |
|
966
|
|
|
* @return \Htsl\Htsl |
|
967
|
|
|
*/ |
|
968
|
|
|
public function getHtsl() |
|
969
|
|
|
{ |
|
970
|
|
|
return $this->htsl; |
|
971
|
|
|
} |
|
972
|
|
|
|
|
973
|
|
|
/** |
|
974
|
|
|
* Throw exception with document name and line number. |
|
975
|
|
|
* |
|
976
|
|
|
* @access public |
|
977
|
|
|
* |
|
978
|
|
|
* @param string $message |
|
979
|
|
|
* |
|
980
|
|
|
* @throw \Htsl\Parser\HtslParsingException |
|
981
|
|
|
*/ |
|
982
|
|
|
public function throw( string$message ) |
|
983
|
|
|
{ |
|
984
|
|
|
throw new HtslParsingException("$message at file {$this->buffer->filePath} line $this->lineNumber"); |
|
985
|
|
|
} |
|
986
|
|
|
} |
|
987
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.