Skip to content

Commit

Permalink
feature: abe import with variable inside filename
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Dec 1, 2016
1 parent 94f7308 commit 2dc7b1f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 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
27 changes: 20 additions & 7 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,7 +55,7 @@ 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) => {
Expand All @@ -64,8 +65,20 @@ export function includePartials(text) {
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 +128,7 @@ export function translate(text) {
return text
}

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

// HOOKS beforeGetTemplate
Expand All @@ -129,7 +142,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 +219,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

0 comments on commit 2dc7b1f

Please sign in to comment.