Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,56 @@ fileinclude({
});
```

```
```html
<ul>
@@for (var i = 0; i < arr.length; i++) {
<li>`+arr[i]+`</li>
}
</ul>
```

### `loop` statement

* index.html

```html
<body>
@@loop('loop-article.html', [
{ "title": "My post title", "text": "<p>lorem ipsum...</p>" },
{ "title": "Another post", "text": "<p>lorem ipsum...</p>" },
{ "title": "One more post", "text": "<p>lorem ipsum...</p>" }
])
</body>
```

* loop-article.html

```html
<article>
<h1>@@title</h1>
@@text
</article>
```

### `loop` statement + data.json

data.json

```js
[
{ "title": "My post title", "text": "<p>lorem ipsum...</p>" },
{ "title": "Another post", "text": "<p>lorem ipsum...</p>" },
{ "title": "One more post", "text": "<p>lorem ipsum...</p>" }
]
```

* loop-article.html
```html
<body>
@@loop("loop-article.html", "data.json")
</body>
```

### License
MIT

Expand Down
1 change: 1 addition & 0 deletions example/c/c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is loop @@number
2 changes: 1 addition & 1 deletion example/conditional.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ this is conditional

Flatten object "@@obj"
@@if(typeof context.obj === 'object') {
Flatten variable "@@obj.obj_param"
Flatten variable "@@obj.obj_param"
}
Defined in parent "@@param"
Context variable "@@name"
16 changes: 11 additions & 5 deletions example/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ index

@@if (context.name === 'example') {
@@include('conditional.txt', {
"param": "success",
"obj": {
"obj_param": "flatten param"
}
"param": "success",
"obj": {
"obj_param": "flatten param"
}
})
@@include('conditional.txt', {
"param": "success too"
"param": "success too"
})

Defined in include @@param
}

@@loop('c/c.txt', [
{ "number": 1 },
{ "number": 2 },
{ "number": 3 }
])
7 changes: 6 additions & 1 deletion example/result/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ example

Flatten object "@@obj"

Flatten variable "flatten param"
Flatten variable "flatten param"

Defined in parent "success"
Context variable "example"

this is conditional

Flatten object "@@obj"

Defined in parent "success too"
Context variable "example"


Defined in include @@param


this is loop 1this is loop 2this is loop 3
89 changes: 81 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ module.exports = function(opts) {
name: 'include',
handler: includeHandler
});
text = replaceFunction(text, {
prefix: opts.prefix,
suffix: opts.suffix,
name: 'loop',
handler: loopHandler
});

function conditionalHandler(inst) {
// jshint ignore: start
Expand All @@ -108,9 +114,67 @@ module.exports = function(opts) {
}

function includeHandler(inst) {
var args = /[^)"\']*["\']([^"\']*)["\'](,\s*({[\s\S]*})){0,1}\s*/.exec(inst.args);
var args = /[^)"\']*["\']([^"\']*)["\'](,\s*({[\s\S]*})){0,1}\s*/.exec(inst.args);

if (args) {
var includePath = path.resolve(filebase, args[1]);
// for checking if we are not including the current file again
if (currentFilename.toLowerCase() === includePath.toLowerCase()) {
throw new Error('recursion detected in file: ' + currentFilename);
}

var includeContent = fs.readFileSync(includePath, 'utf-8');

if (opts.indent) {
includeContent = setIndent(inst.before, inst.before.length, includeContent);
}

// need to double each `$` to escape it in the `replace` function
// includeContent = includeContent.replace(/\$/gi, '$$$$');

// apply filters on include content
if (typeof opts.filters === 'object') {
includeContent = applyFilters(includeContent, args.input);
}

var recFile = new gutil.File({
cwd: process.cwd(),
base: file.base,
path: includePath,
contents: new Buffer(includeContent)
});

if (args) {
recFile = include(recFile, includeContent, args[3] ? JSON.parse(args[3]) : {});

return String(recFile.contents);
}
}

function loopHandler(inst) {
var args = /[^)"\']*["\']([^"\']*)["\'](,\s*([\s\S]*())){0,1}\s*/.exec(inst.args);
var arr = [];

if (args) {
// loop array in the json file
if (args[3].match(/^('|")[^']|[^"]('|")$/)) {
// clean filename var and define path
var jsonfile = file.base + args[3].replace(/^('|")/, '').replace(/('|")$/, '');
// check if json file exists
if (fs.existsSync(jsonfile)) {
arr = require(jsonfile);
} else {
return console.error("JSON file not exists:", jsonfile);
}
} else {
// loop array in the function
try {
arr = JSON.parse(args[3]);
} catch (err) {
return console.error(err, args[3]);
}
}

if (arr) {
var includePath = path.resolve(filebase, args[1]);
// for checking if we are not including the current file again
if (currentFilename.toLowerCase() === includePath.toLowerCase()) {
Expand All @@ -123,9 +187,6 @@ module.exports = function(opts) {
includeContent = setIndent(inst.before, inst.before.length, includeContent);
}

// need to double each `$` to escape it in the `replace` function
// includeContent = includeContent.replace(/\$/gi, '$$$$');

// apply filters on include content
if (typeof opts.filters === 'object') {
includeContent = applyFilters(includeContent, args.input);
Expand All @@ -138,11 +199,23 @@ module.exports = function(opts) {
contents: new Buffer(includeContent)
});

recFile = include(recFile, includeContent, args[3] ? JSON.parse(args[3]) : {});

return String(recFile.contents);
var contents = '';

for (var i in arr) {
if (arr.hasOwnProperty(i)) {
var context = arr[i];
recFile = include(recFile, includeContent, args[3] ? context : {});
// why handler dont reconize underscore?
// if (typeof context == 'object' && typeof context['_key'] == 'undefined') {
// context['_key'] = i;
// }
contents += String(recFile.contents);
}
}
}
return contents;
}
}

file.contents = new Buffer(text);

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
],
"author": "haoxin",
"contributors": [
"Bogdan Chadkin <trysound@yandex.ru>"
"Bogdan Chadkin <trysound@yandex.ru>",
"Arthur Araújo <webarthur@gmail.com>"
],
"license": "MIT",
"devDependencies": {
Expand Down