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

Commit 93f63bb

Browse files
committed
Initial commit
1 parent 626599b commit 93f63bb

File tree

3 files changed

+116
-37
lines changed

3 files changed

+116
-37
lines changed

.gitignore

+1-37
Original file line numberDiff line numberDiff line change
@@ -1,37 +1 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
6-
# Runtime data
7-
pids
8-
*.pid
9-
*.seed
10-
11-
# Directory for instrumented libs generated by jscoverage/JSCover
12-
lib-cov
13-
14-
# Coverage directory used by tools like istanbul
15-
coverage
16-
17-
# nyc test coverage
18-
.nyc_output
19-
20-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21-
.grunt
22-
23-
# node-waf configuration
24-
.lock-wscript
25-
26-
# Compiled binary addons (http://nodejs.org/api/addons.html)
27-
build/Release
28-
29-
# Dependency directories
30-
node_modules
31-
jspm_packages
32-
33-
# Optional npm cache directory
34-
.npm
35-
36-
# Optional REPL history
37-
.node_repl_history
1+
/node_modules

index.js

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"use strict";
2+
3+
const htmlparser = require('htmlparser2'),
4+
fs = require('fs'),
5+
path = require('path');
6+
7+
class LaravelBladeParser
8+
{
9+
/**
10+
* @param options
11+
*/
12+
constructor(options)
13+
{
14+
this.html = "";
15+
this.defaultOptions = {
16+
folder: './resources/views',
17+
path: './resources/views/welcome.blade.php',
18+
regex: {
19+
include: /@include\(\s*[\'\"]([^\[\]]*)[\'\"]\s*(?:(?:.*[^\s\)])\s*)*\s*\)/gi,
20+
extends: /@extends\((?:[\'\"])(.*)(?:[\'\"])\)/gi,
21+
yield: /@yield\([\'\"]?([^\'\"]*)[\'\"]?\)/gi,
22+
multiLineSection: /@section\(\s*[\'\"]?([^\'\"]*)[\'\"]?\s*\)((?!\@stop).*\s*)*\@stop/gi,
23+
oneLineSection: /@section\([\'\"]([^\'\"]*)[\'\"]?\s*\,\s*[\'\"]?([^\"\']*)[\'\"]?\)/gi
24+
},
25+
encoding: 'utf8'
26+
};
27+
this.options = Object.assign(this.defaultOptions, options ? options : {});
28+
29+
this._init();
30+
}
31+
32+
/**
33+
* @returns {string|XML|string|void|*|*}
34+
*/
35+
getHTML()
36+
{
37+
return this.html;
38+
}
39+
40+
/**
41+
* @private
42+
*/
43+
_init()
44+
{
45+
this.html = this._parse(this._getFileContent(this.options.path));
46+
}
47+
48+
/**
49+
* @param content
50+
*
51+
* @private
52+
*/
53+
_parse(content)
54+
{
55+
var template = content,
56+
items = {};
57+
58+
template.replace(this.options.regex.extends, (match, value) => {
59+
let filePath = path.join(this.options.folder, value.replace(/\./gi, "/") + '.blade.php');
60+
61+
return this._getFileContent(filePath);
62+
}).replace(this.options.regex.oneLineSection, (match, key, value) => {
63+
items[key] = value;
64+
65+
return "";
66+
}).replace(this.options.regex.multiLineSection, (match, key, value) => {
67+
items[key] = value;
68+
69+
return "";
70+
}).replace(this.options.regex.yield, (match, key) => {
71+
return typeof items[key] == "undefined" ? "" : items[key];
72+
}).replace(this.options.regex.include, (match, value) => {
73+
let filePath = path.join(this.options.folder, value.replace(/\./gi, "/") + '.blade.php'),
74+
html = this._getFileContent(filePath);
75+
76+
return this._parse(html);
77+
});
78+
79+
return template;
80+
}
81+
82+
/**
83+
* @param filePath
84+
*
85+
* @returns {*}
86+
* @private
87+
*/
88+
_getFileContent(filePath)
89+
{
90+
return fs.readFileSync(filePath, this.options.encoding);
91+
}
92+
}
93+
94+
module.exports = options => new LaravelBladeParser(options).getHTML();

package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "laravel-blade-parser",
3+
"version": "1.0.0",
4+
"description": "NPM Package that parse blade templates into HTML.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"blade",
11+
"parser",
12+
"laravel"
13+
],
14+
"author": "Pavel Belyaev",
15+
"license": "MIT",
16+
"dependencies": {
17+
"fs": "0.0.2",
18+
"htmlparser2": "^3.9.0",
19+
"path": "^0.12.7"
20+
}
21+
}

0 commit comments

Comments
 (0)