forked from pbelyaev/laravel-blade-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (104 loc) · 3.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"use strict";
const htmlparser = require('htmlparser2'),
fs = require('fs'),
path = require('path');
class LaravelBladeParser
{
/**
* @param options
*/
constructor(options)
{
this.html = "";
this.defaultOptions = {
folder: './resources/views',
path: './resources/views/welcome.blade.php',
extends: true,
regex: {
include: /\@include\(\s*[\'\"]([^\[\]\'\"]*)[\'\"]\s*(?:(?:.*[^\s\)])\s*)*\s*\)/gi,
extends: /\@extends\((?:[\'\"])(.*)(?:[\'\"])\)/gi,
yield: /\@yield\([\'\"]?([^\'\"]*)[\'\"]?\)/gi,
stack: /\@stack\(\s*[\'\"]\s*([^\'\"]*)[\'\"]\)/gmi,
oneLinePush: /\@push\([\'\"]([^\'\"]*)[\'\"]\s*\,\s*[\"\']?([^\'\"\)]*)[\'\"]?\s*\)/gi,
multiLinePush: /\@push\(\s*[\'\"]\s*([^\'\"]*)[\'\"]\s*\)((?:(?!\@endpush|\@stop).*\s*)*)*(?:\@endpush|\@stop)/gi,
oneLineSection: /\@section\([\'\"]([^\'\"]*)[\'\"]?\s*\,\s*[\'\"]?([^\"\'\)]*)[\'\"]?\)/gi,
multiLineSection: /\@section\(\s*[\'\"]?([^\'\"]*)[\'\"]?\s*\)((?:(?!\@stop|\@endsection).*\s*)*)*(?:\@stop|\@endsection)/gi
},
encoding: 'utf8'
};
this.options = Object.assign(this.defaultOptions, options ? options : {});
this._init();
}
/**
* @returns {string|XML|string|void|*|*}
*/
getHTML()
{
return this.html;
}
/**
* @private
*/
_init()
{
this.html = this._parse(this._getFileContent(this.options.path));
}
/**
* @param content
*
* @private
*/
_parse(content)
{
var sections = {},
stacks = {};
if (this.options.extends) {
content = content.replace(this.options.regex.extends, (match, value) => {
let filePath = path.join(this.options.folder, value.replace(/\./gi, "/") + '.blade.php');
return this._getFileContent(filePath);
}).replace(this.options.regex.oneLineSection, (match, key, value) => {
sections[key] = value;
return "";
}).replace(this.options.regex.multiLineSection, (match, key, value) => {
sections[key] = value;
return "";
}).replace(this.options.regex.yield, (match, key) => {
return typeof sections[key] == "undefined" ? "" : sections[key];
});
}
content = content. replace(this.options.regex.include, (match, value) => {
let filePath = path.join(this.options.folder, value.replace(/\./gi, "/") + '.blade.php'),
html = this._getFileContent(filePath);
return this._parse(html);
}).replace(this.options.regex.oneLinePush, (match, key, value) => {
if (stacks[key] == undefined) stacks[key] = [];
stacks[key].push(value);
return "";
}).replace(this.options.regex.multiLinePush, (match, key, value) => {
if (stacks[key] == undefined) stacks[key] = [];
stacks[key].push(value);
return "";
}).replace(this.options.regex.stack, (match, key) => {
if (stacks[key] != undefined) {
let html = "";
stacks[key].forEach((item) => {
html += item;
});
return html;
}
return "";
});
return content;
}
/**
* @param filePath
*
* @returns {*}
* @private
*/
_getFileContent(filePath)
{
return fs.readFileSync(filePath, this.options.encoding);
}
}
module.exports = options => new LaravelBladeParser(options).getHTML();