Skip to content

Commit

Permalink
enhancement: We detect abe tags including variables in each statements
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed Dec 30, 2016
1 parent ccafcc8 commit c202768
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
13 changes: 5 additions & 8 deletions src/cli/cms/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ export default class Page {
){
template = Handlebars.templates[templateId]
this.html = template(json, {data: {intl: config.intlData}})

//console.log('precompile')

} else {

this._onlyHTML = onlyHTML
Expand All @@ -62,13 +59,13 @@ export default class Page {
} else {
this.template = cmsTemplates.prepare.removeHandlebarsRawFromHtml(this.template)
}

console.log("cmsTemplates.prepare.indexEachBlocks")
// je rajoute les index pour chaque bloc lié à un each
this.template = cmsTemplates.prepare.indexEachBlocks(this.template, this._onlyHTML)

console.log("cmsData.source.removeDataList")
// We remove the {{abe type=data ...}} from the text
this.template = cmsData.source.removeDataList(this.template)

console.log("cmsTemplates.prepare.replaceAbeEachIndex")
// It's time to replace the [index] by {{@index}} (concerning each blocks)
this.template = cmsTemplates.prepare.replaceAbeEachIndex(this.template)

Expand All @@ -77,10 +74,10 @@ export default class Page {
fse.writeFileSync(this.HbsTemplatePath, Handlebars.precompile(this.template), 'utf8')
Manager.instance.addHbsTemplate(templateId)
}

console.log("Handlebars.compile")
// I compile the text
var compiledTemplate = Handlebars.compile(cmsTemplates.insertDebugtoolUtilities(this.template, this._onlyHTML))

console.log("compiledTemplate")
// I create the html page ! yeah !!!
this.html = compiledTemplate(json, {data: {intl: config.intlData}})
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/cms/templates/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export function indexEachBlocks(template, onlyHtml) {

var voidData = {}
voidData[key] = [{}]
var blockCompiled = Handlebars.compile(block.replace(/{{abe (.*?)}}/g, '[[abe $1]]').replace(new RegExp(`\\.\\.\/${config.meta.name}`, 'g'), config.meta.name))
var blockCompiled = Handlebars.compile(block.replace(/{{abe (.*?)["'] ?}}/g, '[[abe $1]]').replace(new RegExp(`\\.\\.\/${config.meta.name}`, 'g'), config.meta.name))
var blockHtml = blockCompiled(voidData, {data: {intl: config.intlData}}).replace(/\[\[abe (.*?)\]\]/g, '{{abe $1}}')

// je rajoute un data-abe-block avec index sur tous les tags html du bloc each
Expand Down
4 changes: 2 additions & 2 deletions src/cli/cms/templates/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ export function getTemplate (file, json = {}) {
text = cmsTemplates.template.includePartials(text, json)
text = cmsTemplates.template.translate(text)
text = cmsTemplates.template.addOrder(text)
}else {
text = `[ ERROR ] template ${file + '.' + config.files.templates.extension} doesn't exist anymore`
} else {
text = `[ ERROR ] template ${file} doesn't exist anymore`
}

// HOOKS afterGetTemplate
Expand Down
13 changes: 9 additions & 4 deletions test/cms/templates/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('cmsTemplates.prepare', function() {
source: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-abe-source.html'), 'utf-8'),
each: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-abe-each.html'), 'utf-8'),
eachMultiple: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-abe-each-multiple.html'), 'utf-8'),
eachVariable: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-abe-each-variable.html'), 'utf-8'),
rawHandlebar: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-raw-handlebars.html'), 'utf-8'),
noHtml: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'prepare-tag-nohtml.html'), 'utf-8')
}
Expand Down Expand Up @@ -220,12 +221,16 @@ describe('cmsTemplates.prepare', function() {

// test
var template = cmsTemplates.prepare.indexEachBlocks(this.fixture.each, false)
chai.expect(template.indexOf('data-abe-block="test{{@index}}"')).to.be.above(-1);
chai.expect(template.indexOf('<!-- [[test]]')).to.be.above(-1);
chai.expect(template.indexOf('data-abe-block="test{{@index}}"')).to.be.above(-1)
chai.expect(template.indexOf('<!-- [[test]]')).to.be.above(-1)

var template = cmsTemplates.prepare.indexEachBlocks(this.fixture.eachMultiple, false)
chai.expect(template.indexOf('data-abe-block')).to.be.above(-1);
chai.expect(template.indexOf('abe dictionnary=')).to.be.above(-1);
chai.expect(template.indexOf('data-abe-block')).to.be.above(-1)
chai.expect(template.indexOf('abe dictionnary=')).to.be.above(-1)

var template = cmsTemplates.prepare.indexEachBlocks(this.fixture.eachVariable, false)
chai.expect(template.indexOf('{{formatted_address}} - (lat:{{geometry.location.lat}}-lng:{{geometry.location.lng}})')).to.be.above(-1)
chai.expect(template.indexOf('<!-- [[test]]')).to.be.above(-1)
});

/**
Expand Down
4 changes: 2 additions & 2 deletions test/core/manager/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ describe('Manager', function() {
it('getStructureAndTemplates()', function() {
const data = Manager.instance.getStructureAndTemplates()
chai.assert.equal(data['templates'][0].name, 'article-data-arrayinline', 'failed !')
chai.assert.equal(data['templates'].length, 25, 'failed !')
chai.assert.equal(data['templates'].length, 26, 'failed !')
});

it('updateStructureAndTemplates()', function() {
Manager.instance.updateStructureAndTemplates()
const data = Manager.instance.getStructureAndTemplates()
chai.assert.equal(data['templates'][0].name, 'article-data-arrayinline', 'failed !')
chai.assert.equal(data['templates'].length, 25, 'failed !')
chai.assert.equal(data['templates'].length, 26, 'failed !')
});

it('getList()', function() {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/templates/prepare-tag-abe-each-variable.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#each test}}
{{abe type="data" key="test.gmaps" source="https://maps.googleapis.com/maps/api/geocode/json?key=gmapskey&address=" autocomplete="true" display="{{formatted_address}} - (lat:{{geometry.location.lat}}-lng:{{geometry.location.lng}})" desc='gmaps'}}
{{/each}}

0 comments on commit c202768

Please sign in to comment.