@@ -23,10 +23,10 @@ class LaravelBladeParser
23
23
yield : / \@ y i e l d \( [ \' \" ] ? ( [ ^ \' \" ] * ) [ \' \" ] ? \) / gi,
24
24
stack : / \@ s t a c k \( \s * [ \' \" ] ( .* ) [ \' \" ] \) / gi,
25
25
26
- push : / \@ p u s h \( \s * [ \' \" ] \s * ( .* ) [ \' \" ] \s * \) ( (? ! \@ e n d p u s h | \@ s t o p ) .* \s * ) * (?: \@ e n d p u s h | \@ s t o p ) | \@ p u s h \( [ \' \" ] ( .* ) [ \' \" ] \s * \, \s * [ \" | \' ] ( .* ) [ \" | \' ] \s * \) / gi,
26
+ push : / \@ p u s h \( \s * [ \' \" ] \s * ( .* ) [ \' \" ] \s * \) ( (?: (? ! \@ e n d p u s h | \@ s t o p ) .* \s * ) * ) (?: \@ e n d p u s h | \@ s t o p ) | \@ p u s h \( [ \' \" ] ( .* ) [ \' \" ] \s * \, \s * [ \" | \' ] ( .* ) [ \" | \' ] \s * \) / gi,
27
27
28
- oneLineSection : / \@ s e c t i o n \( [ \' \" ] ( [ ^ \' \" ] * ) [ \' \" ] ? \s * \, \s * [ \' \" ] ? ( [ ^ \" \' \) ] * ) [ \' \" ] ? \ )/ gi,
29
- multiLineSection : / \@ s e c t i o n \( \s * [ \' \" ] ? ( [ ^ \' \" ] * ) [ \' \" ] ? \s * \) ( (?: (? ! \@ s t o p | \@ e n d s e c t i o n ) .* \s * ) * ) * (?: \@ s t o p | \@ e n d s e c t i o n ) / gi
28
+ section : / \@ s e c t i o n \( [ \' \" ] ( . * ) [ \' \" ] \s * \, \s * [ \' \" ] ( . * ) [ \' \" ] \) | \@ s e c t i o n \( \s * [ \' \" ] ( . * ) [ \' \" ] \s * \) ( (?: (? ! \@ s h o w | \@ s t o p | \@ e n d s e c t i o n ) . * \s * ) * ) ( \@ s h o w | \@ s t o p | \@ e n d s e c t i o n ) / gi,
29
+ showSection : / \@ s e c t i o n \( \s * [ \' \" ] ( . * ) [ \' \" ] \s * \) ( (?: (? ! \@ s h o w ) .* \s * ) * ) (?: \@ s h o w ) / gi,
30
30
} ,
31
31
encoding : 'utf8'
32
32
} ;
@@ -48,15 +48,15 @@ class LaravelBladeParser
48
48
*/
49
49
_init ( )
50
50
{
51
- this . html = this . _parse ( this . _getFileContent ( this . options . path ) ) ;
51
+ this . html = this . _compile ( this . _getFileContent ( this . options . path ) ) ;
52
52
}
53
53
54
54
/**
55
55
* @param content
56
56
*
57
57
* @private
58
58
*/
59
- _parse ( content )
59
+ _compile ( content )
60
60
{
61
61
let sections = { } ,
62
62
stacks = { } ;
@@ -66,29 +66,36 @@ class LaravelBladeParser
66
66
67
67
// @extends directive
68
68
if ( this . options . extends ) {
69
- content = content . replace ( this . options . regex . extends , ( match , value ) => {
69
+ content = content . replace ( this . options . regex . extends , ( match , value ) => {
70
70
let filePath = path . join ( this . options . folder , value . replace ( / \. / gi, "/" ) + '.blade.php' ) ;
71
71
72
72
return this . _getFileContent ( filePath ) ;
73
- } ) . replace ( this . options . regex . oneLineSection , ( match , key , value ) => {
74
- sections [ key ] = value ;
75
-
76
- return "" ;
77
- } ) . replace ( this . options . regex . multiLineSection , ( match , key , value ) => {
78
- sections [ key ] = value ;
79
-
80
- return "" ;
81
- } ) . replace ( this . options . regex . yield , ( match , key ) => {
82
- return typeof sections [ key ] == "undefined" ? "" : sections [ key ] ;
83
73
} ) ;
84
74
}
85
75
86
76
// @include directive
87
- content = content . replace ( this . options . regex . include , ( match , value ) => {
88
- let filePath = path . join ( this . options . folder , value . replace ( / \. / gi, "/" ) + '.blade.php' ) ,
89
- html = this . _getFileContent ( filePath ) ;
77
+ content = this . _compileIncludes ( content ) ;
78
+
79
+ // @section directive
80
+ content = content . replace ( this . options . regex . section , ( match , firstKey , firstValue , secondKey , secondValue , type ) => {
81
+ let key = secondKey != undefined ? secondKey : firstKey ,
82
+ value = secondValue != undefined ? secondValue : firstValue ;
90
83
91
- return this . _parse ( html ) ;
84
+ if ( type == "@show" ) {
85
+ sections [ key ] = sections [ key ] != undefined ? sections [ key ] : "" ;
86
+ sections [ key ] = value . replace ( / \@ p a r e n t / gi, "" ) ;
87
+
88
+ return `@yield('${ key } ')` ;
89
+ }
90
+
91
+ if ( value . match ( / \@ p a r e n t / g) ) {
92
+ sections [ key ] = sections [ key ] != undefined ? sections [ key ] : "" ;
93
+ sections [ key ] += value . replace ( / \@ p a r e n t / gi, "" ) ;
94
+ } else {
95
+ sections [ key ] = value ;
96
+ }
97
+
98
+ return "" ;
92
99
} ) ;
93
100
94
101
// @push directive
@@ -118,9 +125,29 @@ class LaravelBladeParser
118
125
return "" ;
119
126
} ) ;
120
127
128
+ // @yield directive
129
+ content = content . replace ( this . options . regex . yield , ( match , key ) => {
130
+ return sections [ key ] == undefined ? "" : sections [ key ] ;
131
+ } ) ;
132
+
121
133
return content ;
122
134
}
123
135
136
+ /**
137
+ * @param html
138
+ * @returns {XML|void|string|* }
139
+ * @private
140
+ */
141
+ _compileIncludes ( html )
142
+ {
143
+ return html . replace ( this . options . regex . include , ( match , value ) => {
144
+ let filePath = path . join ( this . options . folder , value . replace ( / \. / gi, "/" ) + '.blade.php' ) ,
145
+ html = this . _getFileContent ( filePath ) ;
146
+
147
+ return this . _compileIncludes ( html ) ;
148
+ } ) ;
149
+ }
150
+
124
151
/**
125
152
* @param filePath
126
153
*
0 commit comments