Skip to content
This repository was archived by the owner on Jun 8, 2018. It is now read-only.

Commit 5bb2353

Browse files
committed
Concat regexps, rename a method and etc.
1 parent 9709f45 commit 5bb2353

File tree

1 file changed

+47
-20
lines changed

1 file changed

+47
-20
lines changed

index.js

+47-20
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class LaravelBladeParser
2323
yield: /\@yield\([\'\"]?([^\'\"]*)[\'\"]?\)/gi,
2424
stack: /\@stack\(\s*[\'\"](.*)[\'\"]\)/gi,
2525

26-
push: /\@push\(\s*[\'\"]\s*(.*)[\'\"]\s*\)((?!\@endpush|\@stop).*\s*)*(?:\@endpush|\@stop)|\@push\([\'\"](.*)[\'\"]\s*\,\s*[\"|\'](.*)[\"|\']\s*\)/gi,
26+
push: /\@push\(\s*[\'\"]\s*(.*)[\'\"]\s*\)((?:(?!\@endpush|\@stop).*\s*)*)(?:\@endpush|\@stop)|\@push\([\'\"](.*)[\'\"]\s*\,\s*[\"|\'](.*)[\"|\']\s*\)/gi,
2727

28-
oneLineSection: /\@section\([\'\"]([^\'\"]*)[\'\"]?\s*\,\s*[\'\"]?([^\"\'\)]*)[\'\"]?\)/gi,
29-
multiLineSection: /\@section\(\s*[\'\"]?([^\'\"]*)[\'\"]?\s*\)((?:(?!\@stop|\@endsection).*\s*)*)*(?:\@stop|\@endsection)/gi
28+
section: /\@section\([\'\"](.*)[\'\"]\s*\,\s*[\'\"](.*)[\'\"]\)|\@section\(\s*[\'\"](.*)[\'\"]\s*\)((?:(?!\@show|\@stop|\@endsection).*\s*)*)(\@show|\@stop|\@endsection)/gi,
29+
showSection: /\@section\(\s*[\'\"](.*)[\'\"]\s*\)((?:(?!\@show).*\s*)*)(?:\@show)/gi,
3030
},
3131
encoding: 'utf8'
3232
};
@@ -48,15 +48,15 @@ class LaravelBladeParser
4848
*/
4949
_init()
5050
{
51-
this.html = this._parse(this._getFileContent(this.options.path));
51+
this.html = this._compile(this._getFileContent(this.options.path));
5252
}
5353

5454
/**
5555
* @param content
5656
*
5757
* @private
5858
*/
59-
_parse(content)
59+
_compile(content)
6060
{
6161
let sections = {},
6262
stacks = {};
@@ -66,29 +66,36 @@ class LaravelBladeParser
6666

6767
// @extends directive
6868
if (this.options.extends) {
69-
content = content.replace(this.options.regex.extends, (match, value) => {
69+
content = content.replace(this.options.regex.extends, (match, value) => {
7070
let filePath = path.join(this.options.folder, value.replace(/\./gi, "/") + '.blade.php');
7171

7272
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];
8373
});
8474
}
8575

8676
// @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;
9083

91-
return this._parse(html);
84+
if (type == "@show") {
85+
sections[key] = sections[key] != undefined ? sections[key] : "";
86+
sections[key] = value.replace(/\@parent/gi, "");
87+
88+
return `@yield('${key}')`;
89+
}
90+
91+
if (value.match(/\@parent/g)) {
92+
sections[key] = sections[key] != undefined ? sections[key] : "";
93+
sections[key] += value.replace(/\@parent/gi, "");
94+
} else {
95+
sections[key] = value;
96+
}
97+
98+
return "";
9299
});
93100

94101
// @push directive
@@ -118,9 +125,29 @@ class LaravelBladeParser
118125
return "";
119126
});
120127

128+
// @yield directive
129+
content = content.replace(this.options.regex.yield, (match, key) => {
130+
return sections[key] == undefined ? "" : sections[key];
131+
});
132+
121133
return content;
122134
}
123135

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+
124151
/**
125152
* @param filePath
126153
*

0 commit comments

Comments
 (0)