Skip to content

Commit

Permalink
Merge pull request #57 from AdFabConnect/abe_import_variable
Browse files Browse the repository at this point in the history
Abe import variable
  • Loading branch information
gregorybesson committed Dec 2, 2016
2 parents b89808d + 5381d0a commit cd30368
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/cli/cms/editor/handlebars/sourceAttr.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function sourceAttr(obj, params) {
export function get(obj, path) {
return path.split('.').reduce(function(prev, curr) {
return prev ? prev[curr] : undefined
}, obj || self)
}, obj || this)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/cli/cms/operations/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function draft(filePath, json, workflow = 'draft') {
var date = coreUtils.file.getDate(revisionPath)
cmsData.metas.add(json, workflow, date)

var template = cmsTemplates.template.getTemplate(json.abe_meta.template)
var template = cmsTemplates.template.getTemplate(json.abe_meta.template, json)

cmsData.source.getDataList(path.dirname(json.abe_meta.link), template, json)
.then(() => {
Expand Down Expand Up @@ -57,7 +57,7 @@ export function publish(filePath, json) {
// revisionPath = coreUtils.file.addDateIsoToRevisionPath(revisionPath, workflow)
cmsData.metas.add(json, 'publish')

var template = cmsTemplates.template.getTemplate(json.abe_meta.template)
var template = cmsTemplates.template.getTemplate(json.abe_meta.template, json)

cmsData.source.getDataList(path.dirname(json.abe_meta.link), template, json)
.then(() => {
Expand Down
28 changes: 20 additions & 8 deletions src/cli/cms/templates/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
abeExtend,
cmsTemplates
} from '../../'
import * as sourceAttr from '../../cms/editor/handlebars/sourceAttr'

export function getTemplatesAndPartials(templatesPath) {
var p = new Promise((resolve) => {
Expand Down Expand Up @@ -45,7 +46,7 @@ export function addOrder(text) {

export function getAbeImport(text) {
var partials = []
let listReg = /({{abe.*?type=[\'|\"]import.*?}})/g
let listReg = /({{abe.*type=[\'|\"]import.*}})/g
var match
while (match = listReg.exec(text)) {
partials.push(match[0])
Expand All @@ -54,18 +55,29 @@ export function getAbeImport(text) {
return partials
}

export function includePartials(text) {
export function includePartials(text, json) {
var abeImports = cmsTemplates.template.getAbeImport(text)

Array.prototype.forEach.call(abeImports, (abeImport) => {
var obj = cmsData.attributes.getAll(abeImport, {})


var file = obj.file
var partial = ''
file = path.join(config.root, config.partials, file)

if (file.indexOf('{{') > -1) {
var keys = sourceAttr.getKeys(file)
Array.prototype.forEach.call(keys, (key) => {
try {
var toEval = `${key.replace(/(\[|\.|\])/g, '\\$1')}`
file = file.replace(new RegExp(`\{\{${toEval}\}\}`, 'g'), eval(`json.${key}`))
}catch(e) {
}
})
}

if(coreUtils.file.exist(file)) {
partial = cmsTemplates.template.includePartials(fse.readFileSync(file, 'utf8'))
partial = cmsTemplates.template.includePartials(fse.readFileSync(file, 'utf8'), json)
}
text = text.replace(cmsData.regex.escapeTextToRegex(abeImport, 'g'), partial)
})
Expand Down Expand Up @@ -115,7 +127,7 @@ export function translate(text) {
return text
}

export function getTemplate (file) {
export function getTemplate (file, json = {}) {
var text = ''

// HOOKS beforeGetTemplate
Expand All @@ -129,7 +141,7 @@ export function getTemplate (file) {
file = path.join(config.root, config.templates.url, file + '.' + config.files.templates.extension)
if(coreUtils.file.exist(file)) {
text = fse.readFileSync(file, 'utf8')
text = cmsTemplates.template.includePartials(text)
text = cmsTemplates.template.includePartials(text, json)
text = cmsTemplates.template.translate(text)
text = cmsTemplates.template.addOrder(text)
}else {
Expand Down Expand Up @@ -206,12 +218,12 @@ export function recurseWhereVariables (where) {
return ar
}

export function getTemplatesTexts(templatesList) {
export function getTemplatesTexts(templatesList, json) {
var templates = []
var p = new Promise((resolve) => {
Array.prototype.forEach.call(templatesList, (file) => {
var template = fse.readFileSync(file, 'utf8')
template = cmsTemplates.template.includePartials(template)
template = cmsTemplates.template.includePartials(template, json)
var name = file.replace(path.join(config.root, config.templates.url, path.sep), '').replace(`.${config.files.templates.extension}`, '')
templates.push({
name: name,
Expand Down
2 changes: 1 addition & 1 deletion src/server/helpers/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var page = function (req, res) {
}else {
templateId = req.params[0]
}
var text = cmsTemplates.template.getTemplate(templateId)
var text = cmsTemplates.template.getTemplate(templateId, json)

if (!editor) {

Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/get-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function renderAbeAdmin(EditorVariables, obj, filePath) {
if(typeof _json !== 'undefined' && _json !== null
&& typeof _json.abe_meta !== 'undefined' && _json.abe_meta !== null) {

var text = cmsTemplates.template.getTemplate(_json.abe_meta.template)
var text = cmsTemplates.template.getTemplate(_json.abe_meta.template, _json)
var page = new Page(_json.abe_meta.template, text, _json, false)
pageHtml = page.html.replace(/"/g, '"').replace(/'/g, '\'').replace(/<!--/g, '<ABE!--').replace(/-->/g, '--ABE>')
}
Expand Down Expand Up @@ -143,7 +143,7 @@ var route = function(req, res, next) {
if(coreUtils.file.exist(jsonPath)) {
json = cmsData.file.get(jsonPath, 'utf8')
}
var text = cmsTemplates.template.getTemplate(template)
var text = cmsTemplates.template.getTemplate(template, json)

editor(text, json, linkPath)
.then((result) => {
Expand Down
65 changes: 65 additions & 0 deletions test/cms/data/attr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var chai = require('chai');
var sinonChai = require('sinon-chai')
var expect = chai.expect
chai.use(sinonChai)
var sinon = require('sinon');
var path = require('path');
var fse = require('fs-extra');

var config = require('../../../src/cli').config
config.set({root: path.join(process.cwd(), 'test', 'fixtures')})

var cmsData = require('../../../src/cli').cmsData;
var Manager = require('../../../src/cli').Manager;

describe('Attr', function() {
before( function(done) {
Manager.instance.init()
.then(function () {
this.fixture = {}
done()

}.bind(this))
});

/**
*
*
*/
it('new attr', function() {
var attr = new cmsData.attr("article-abe-d20160920T125255138Z.shtml")
chai.expect(attr.str).to.not.be.undefined;
chai.expect(attr.val.s).to.not.be.undefined;
chai.expect(attr.val.s).to.be.equal('d');
});

/**
*
*
*/
it('attr.remove', function() {
var attr = new cmsData.attr("article-abe-d20160920T125255138Z.html")
var filename = attr.remove()
chai.expect(filename).to.be.equal('article.html');
});

/**
*
*
*/
it('attr.getExtension', function() {
var attr = new cmsData.attr("article-abe-d20160920T125255138Z.html")
var extension = attr.getExtension()
chai.expect(extension).to.be.equal('html');
});

/**
*
*
*/
it('attr.insert', function() {
var attr = new cmsData.attr("article-abe-d20160920T125255138Z.html")
var extension = attr.insert('test')
chai.expect(extension).to.be.equal('article-abe-test.html');
});
});
40 changes: 40 additions & 0 deletions test/cms/editor/form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var chai = require('chai');
var sinonChai = require('sinon-chai')
var expect = chai.expect
chai.use(sinonChai)
var sinon = require('sinon');
var path = require('path');
var fse = require('fs-extra');

var config = require('../../../src/cli').config
config.set({root: path.join(process.cwd(), 'test', 'fixtures')})

var cmsEditor = require('../../../src/cli').cmsEditor;
var Manager = require('../../../src/cli').Manager;

describe('Form', function() {
before( function(done) {
Manager.instance.init()
.then(function () {
this.fixture = {}
done()

}.bind(this))
});

/**
* getTemplatesTexts
*
*/
it('new Form', function() {
// stub
var sinonInstance = sinon.sandbox.create();
// sinonInstance.stub(fse, 'readFileSync');

var form = new cmsEditor.form()
form.add({key: 'test'})
chai.expect(form._form.default.item[0].key).to.be.equal('test');
chai.expect(form.dontHaveKey('test')).to.be.equal(false);
chai.expect(form.form.default.item.length).to.be.above(0);
});
});

0 comments on commit cd30368

Please sign in to comment.