Skip to content

Commit

Permalink
bug unknow file include + unit test get template
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Sep 28, 2016
1 parent bf7a8a0 commit b665c9b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 9 deletions.
28 changes: 20 additions & 8 deletions src/cli/helpers/abe-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
,Plugins
} from '../../cli'

function addOrder(text) {
export function addOrder(text) {
var regAbe = /{{abe[\S\s].*?key=['|"]([\S\s].*?['|"| ]}})/g
var matches = text.match(regAbe)
var order = 0
Expand All @@ -38,19 +38,31 @@ function addOrder(text) {
return text
}

function partials(text) {
var importReg = /({{abe.*type=[\'|\"]import.*}})/g
export function getAbeImport(text) {
var partials = []
let listReg = /({{abe.*?type=[\'|\"]import.*?}})/g
var match
while (match = listReg.exec(text)) {
partials.push(match[0])
}

return partials
}

while (match = importReg.exec(text)) {
var file = getAttr(match[0], 'file')
export function includePartials(text) {
var abeImports = getAbeImport(text)

Array.prototype.forEach.call(abeImports, (abeImport) => {
var obj = Util.getAllAttributes(abeImport, {})

var file = obj.file
var partial = ''
file = path.join(config.root, config.partials, file)
if(fileUtils.isFile(file)) {
partial = fse.readFileSync(file, 'utf8')
}
text = text.replace(escapeTextToRegex(match[0], 'g'), partial)
}
text = text.replace(escapeTextToRegex(abeImport, 'g'), partial)
})

return text
}
Expand Down Expand Up @@ -114,7 +126,7 @@ export function getTemplate (file) {
file = path.join(config.root, config.templates.url, file + '.' + config.files.templates.extension)
if(fileUtils.isFile(file)) {
text = fse.readFileSync(file, 'utf8')
text = partials(text)
text = includePartials(text)
text = translate(text)
text = addOrder(text)
}else {
Expand Down
1 change: 1 addition & 0 deletions src/cli/helpers/abe-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ export default class Utils {
tab: 'default',
type: 'text',
value: '',
file: '',
visible: true
}

Expand Down
7 changes: 6 additions & 1 deletion test/fixtures/templates/article.html
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
"{{abe type='data' key='top_things_slider_highlight' desc='Automatic slider' source='select abe_meta from ./' editable='false'}}"
{{abe type='data' key='top_things_slider_highlight' desc='Automatic slider' source='select abe_meta from ./' editable='false'}}

{{abe type='import' file='title.html'}}

<div>{{abe type='import' file='test.html'}}</div>{{abe type='import' file='test.html'}}
{{abe type='import' file='test.html'}}
1 change: 1 addition & 0 deletions test/fixtures/templates/partials/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
1 change: 1 addition & 0 deletions test/fixtures/templates/partials/title.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{abe type='text' key='title' desc='titre' tab='default'}}
50 changes: 50 additions & 0 deletions test/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var chai = require('chai');

var config = require('../src/cli').config
config.set({root: __dirname + '/fixtures'})

var getTemplate = require('../src/cli').getTemplate
var includePartials = require('../src/cli/helpers/abe-template').includePartials
var getAbeImport = require('../src/cli/helpers/abe-template').getAbeImport
var Manager = require('../src/cli').Manager;
var fse = require('fs-extra');

describe('Template', function() {
before( function(done) {
Manager.instance.init()
.then(function () {
this.fixture = {
template: fse.readFileSync(__dirname + '/fixtures/templates/article.html', 'utf-8')
}
done()

}.bind(this))
});

/**
* getAbeImport
*
*/
it('getAbeImport()', function() {
var res = getAbeImport(this.fixture.template)
chai.expect(res).to.have.length(4);
});

/**
* includePartials
*
*/
it('includePartials()', function() {
var template = includePartials(this.fixture.template)
chai.expect(template).to.contain("{{abe type='text' key='title' desc='titre' tab='default'}}");
});

/**
* getTemplate
*
*/
it('getTemplate()', function() {
var template = getTemplate('article')
chai.expect(template).to.contain("{{abe type='text' key='title' desc='titre' tab='default' order='1'}}");
});
});

0 comments on commit b665c9b

Please sign in to comment.