Skip to content

Commit

Permalink
Merge pull request #78 from AdFabConnect/bug_77
Browse files Browse the repository at this point in the history
Bug 77
  • Loading branch information
gregorybesson committed Dec 13, 2016
2 parents c2f34b0 + 4443843 commit 8075f4f
Show file tree
Hide file tree
Showing 13 changed files with 2,526 additions and 2,275 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,25 @@ List of self-descriptive Abe tag
{{/each}}
```

## Template designer references

- [abe tags template integration](docs/abe-tags.md)
- [abe attributes](docs/abe-attributes.md)
- [abe handlebars variables](docs/abe-handlebars-variables.md)
- [abe handlebars helpers](docs/abe-handlebars-helpers.md)

## Template cms admin

- [abe users management](docs/abe-users.md)
- [abe route list](docs/abe-url.md)
- [abe config json](docs/abe-config.md)

## Template plugins developer

- [abe plugin install](docs/abe-plugins.md)
- [abe plugin hook list](docs/abe-hooks.md)
- [abe attributes](docs/abe-attributes.md)

More detailled documentation comming soon

## Adding a template and assets (css / js / images ...)
Expand Down
File renamed without changes.
115 changes: 115 additions & 0 deletions docs/abe-tags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Abe tag

> You can add abe tag to you template following simple rules
## Abe DOM element

This abe tag will display `text` on the document

TEMPLATE

```html
<html>
<head>
</head>
<body>
{{abe type='text' key='text_dom'}}
</body>
</html>
```

RENDER

```html
<html>
<head>
</head>
<body>
Hello
</body>
</html>
```

## Abe DOM Attributes

This example will be added to `body > class` html attributes

TEMPLATE

```html
<html>
<head>
</head>
<body class="{{abe type='text' key='text_class'}}">

</body>
</html>
```

RENDER

```html
<html>
<head>
</head>
<body class="class-hello">

</body>
</html>
```

## More examples

#### multiple attributes
multiple attributes are allowed on the same html DOM object

```html
<html>
<head>
</head>
<body class="{{abe type='text' key='text_class'}}" id="{{abe type='text' key='text_id'}}">

</body>
</html>
```

#### concatenate with attribute

```html
<html>
<head>
</head>
<body class="my-body-class {{abe type='text' key='text_class'}}">

</body>
</html>
```

#### if handlebars

On the next example if variable `text_class` is not empty use `text_class` otherwise use `my-default-class`

```html
<html>
<head>
</head>
<body class="{{#if text_class}}{{abe type='text' key='text_class'}}{{else}}my-default-class{{/if}}">

</body>
</html>
```

# [ WARNING ] THE FOLLOWING EXAMPLE ARE NOT IMPLEMENTED YET

Multiple abe attribute into the same html tag

```html
<html>
<head>
</head>
<body class="{{abe type='text' key='text_1'}}{{abe type='text' key='text_2'}}">

</body>
</html>
```

6 changes: 3 additions & 3 deletions src/cli/cms/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ export default class Page {
this.template = cmsTemplates.prepare.removeHandlebarsRawFromHtml(this.template)
this.template = cmsTemplates.prepare.addAbeHtmlTagBetweenAbeTags(this.template)
}

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

if(!this._onlyHTML){

Expand All @@ -87,6 +84,9 @@ export default class Page {
this.template = cmsTemplates.prepare.addAbeSourceComment(this.template, json)
}

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

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

Expand Down
21 changes: 17 additions & 4 deletions src/cli/cms/data/regex.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
export let abePattern = /[^"']({{abe.*?type=[\'|\"][text|rich|textarea]+[\'|\"][\s\S].*?}})/g
export let abeTag = /({{abe.*?[\s\S].*?}})/g;
//
export let abePattern = /[^"']({{abe.*?type=[\'|\"][text|rich|textarea]+[\'|\"][\s\S].*?}})/g;
// This pattern finds all abe tags enclosed in a HTML tag attribute
// export let abeAsAttributePattern = /( [A-Za-z0-9\-\_]+=["|']{1}{{abe.*?}})/g;
export let abeAsAttributePattern = /( [A-Za-z0-9\-\_]+=["|']{1}).*?({{abe.*?}})/g
// export let abeAsAttributePattern = /( [A-Za-z0-9\-\_]+=["|']{1})(.*?)({{abe.*?}})/g
export let abeAsAttributePattern = /( [A-Za-z0-9\-\_]+=["|']{1})([^=]*?)({{abe.*?["|'| ]}})["|']/g;
// This pattern finds all {{#each ...}}...{{/each}} blocks
export let eachBlockPattern = />\s*(\{\{#each (\r|\t|\n|.)*?\/each\}\})/g
// export let eachBlockPattern = />\s*(\{\{#each (\r|\t|\n|.)*?\/each\}\})/g;
export let eachBlockPattern = /(\{\{#each (\r|\t|\n|.)*?\/each\}\})/g;
// This pattern finds all {{#each ...}}...{{/each}} blocks
export let blockPattern = /(\{\{#each.*\}\}[\s\S]*?\{\{\/each\}\})/g
export let blockPattern = /(\{\{#each.*\}\}[\s\S]*?\{\{\/each\}\})/g;

/**
* escape a regex
Expand Down Expand Up @@ -45,6 +49,15 @@ export function isSingleAbe(str, text){
str.indexOf('attrAbe') < 0
}

/**
* Test if a string don't contains string key from ABE block statement
* @param {String} str string to test
* @return {Boolean} true = this is not a block content
*/
export function getEachParentKey(str){
return getAttr(str, 'key').split('.')[0]
}

/**
* Test if a string contains string key from ABE block statement
* @param {String} str string to test
Expand Down
132 changes: 87 additions & 45 deletions src/cli/cms/templates/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,39 @@ export function addAbeDataAttrForHtmlTag(template) {
return template
}

export function addHasAbeAttr(text) {
return text.replace('}}', ' has-abe=1}}')
}

export function getAbeAttributeData(match, text, htmlAttribute, abeTag) {
var valueOfAttritube
var key = cmsData.regex.getAttr(match, 'key')
var getattr
var res

if (cmsData.regex.isSingleAbe(match, text)) {
valueOfAttritube = key.replace(/\./g, '-')
key = cmsData.regex.validDataAbe(valueOfAttritube)
getattr = key.replace(/\./g, '-')
res = ' data-abe-attr-' + valueOfAttritube + '="' + htmlAttribute + '"' + ' data-abe-' + valueOfAttritube + '="' + getattr + '"' + abeTag
}else {
var valueOfAttritube = key.split('.')
var parentKey = valueOfAttritube.shift()
valueOfAttritube = `${parentKey}[index].${valueOfAttritube[0]}`
var valueOfAttritubeIndexed = valueOfAttritube.replace(/\[index\]/, '{{@index}}')
key = cmsData.regex.validDataAbe(valueOfAttritube)
getattr = key

res = ` data-abe-attr-${valueOfAttritube}="${htmlAttribute}" data-abe-${valueOfAttritube}="${getattr}"`
+ ` data-abe-attr-${valueOfAttritubeIndexed}="${htmlAttribute}" data-abe-${valueOfAttritubeIndexed}="${getattr}"${abeTag}`
}

return res
}

/**
*
* IF ABE TAG SINGLE (NOT ABE EACH STATEMENT)
*
* THIS:
<img src="{{abe type='image' key='image_key' tab='default'}}" alt="">
Expand All @@ -37,34 +69,44 @@ export function addAbeDataAttrForHtmlTag(template) {
<img data-abe-attr-image_key="src" data-abe-image_key="image_key" data-abe-attr-image_key="src"
data-abe-image_key="image_key" src="{{abe type='image' key='image_key' tab='default' has-abe=1 has-abe=1}}" alt="">
*
* IF ABE EACH TAG
* THIS:
{{#each test}}
<img src="{{abe type='image' key='test.img' desc='test_img' tab='default'}}" alt="">
{{/each}}
* BECOME:
{{#each test}}
<img data-abe-attr-test[index].img="src" data-abe-test[index].img="test[index].img" src="{{abe type='image' key='test.img' desc='test_img' tab='default' has-abe=1}}" alt="">
{{/each}}
* @param {[type]} template [description]
*/
export function addAbeDataAttrForHtmlAttributes(template) {
template = template.replace(/<([A-Za-z]+)/g, '\nABE_SPLIT<$1')
var match
while (match = cmsData.regex.abeAsAttributePattern.exec(template)) { // While regexp match {{attribut}}, ex: link, image ...
if(cmsData.regex.isSingleAbe(match[2], template)){
var more_attr = ''
var getattr = cmsData.regex.getAttr(match, 'key').replace(/\./g, '-')
var toReplace = match[0].replace(
cmsData.regex.escapeTextToRegex(match[1]),
' data-abe-attr-' + cmsData.regex.validDataAbe(getattr) + '="' + (match[0].split('=')[0]).trim() + '"' +
' data-abe-' + cmsData.regex.validDataAbe(getattr) + '="' + getattr + '"' + match[1])
export function addAbeDataAttrForHtmlAttributes(template, key) {
var text = template.replace(/<([A-Za-z]+)/g, '\nABE_SPLIT<$1')
let abeTagIntoAttribute = text.match(cmsData.regex.abeAsAttributePattern)

Array.prototype.forEach.call(abeTagIntoAttribute, (abeIntoTag) => {
let matchAbeTag = /({{abe.*?[\s\S].*?}})/g.exec(abeIntoTag)

if(matchAbeTag != null && matchAbeTag[1] != null) {
var toReplace = cmsTemplates.prepare.getAbeAttributeData(matchAbeTag[1], text, (abeIntoTag.split('=')[0]).trim(), abeIntoTag)

toReplace = toReplace.replace(
cmsData.regex.escapeTextToRegex(match[2]),
match[2].replace('}}', ' has-abe=1}}')
cmsData.regex.escapeTextToRegex(matchAbeTag[1]),
cmsTemplates.prepare.addHasAbeAttr(matchAbeTag[1])
)

template = template.replace(
cmsData.regex.escapeTextToRegex(match[0]),
text = text.replace(
cmsData.regex.escapeTextToRegex(abeIntoTag),
toReplace
)
}
}
template = template.replace(/\nABE_SPLIT</g, '<')
})
text = text.replace(/\nABE_SPLIT</g, '<')

return template
return text
}

/**
Expand Down Expand Up @@ -202,8 +244,7 @@ export function indexEachBlocks(template, onlyHtml) {
var blocks = cmsTemplates.prepare.splitEachBlocks(template)

Array.prototype.forEach.call(blocks, (block) => {
var key = block.match(/#each (.*)\}\}/)
key = key[1]
var key = block.match(/#each (.*)\}\}/)[1]
var match

if(!onlyHtml) {
Expand All @@ -220,36 +261,37 @@ export function indexEachBlocks(template, onlyHtml) {
template = template.replace(block, textEachWithIndex + `<!-- [[${key}]] ${cmsTemplates.encodeAbeTagAsComment(blockHtml)} -->`)
}

// Pour chaque tag Abe, je mets en forme ce tag avec des data- supplémentaires
while (match = cmsData.regex.abePattern.exec(block)) {
template = cmsTemplates.prepare.insertAbeEach(template, match, key, cmsData.regex.eachBlockPattern.lastIndex - block.length, onlyHtml)
}

// Pour chaque tag Abe attribut de HTML, je mets en forme ce tag avec des data- supplémentaires sur le tag html parent
while (match = cmsData.regex.abeAsAttributePattern.exec(block)) {
template = cmsTemplates.prepare.insertAbeEach(template, match, key, cmsData.regex.eachBlockPattern.lastIndex - block.length, onlyHtml)
}
// Pour chaque tag Abe
while (match = cmsData.regex.abeTag.exec(block)) {
// template = cmsTemplates.prepare.insertAbeEach(template, match, key, cmsData.regex.eachBlockPattern.lastIndex - block.length, onlyHtml)
template = cmsTemplates.prepare.addAbeDictionnary(template, match[0], key)
}
})

return template
}

export function insertAbeEach(template, theMatch, key, lastIndex, onlyHtml) {
var matchBlock = theMatch[0]
if(cmsData.regex.isEachStatement(matchBlock)) return
if(cmsData.regex.isBlockAbe(matchBlock)){
var matchblockattr = (matchBlock.split('=')[0]).trim()
var getattr = cmsData.regex.getAttr(matchBlock, 'key').replace('.', '[index].')
var newMatchBlock = ((!onlyHtml) ?
(/=[\"\']\{\{(.*?)\}\}/g.test(matchBlock) ?
' data-abe-attr-' + cmsData.regex.validDataAbe(getattr) + '="' + matchblockattr + '"' :
'') +
' data-abe-' + cmsData.regex.validDataAbe(getattr) + '="' + getattr + '" ' + matchBlock :
matchBlock)
.replace(new RegExp('(key=[\'|"])' + key + '.', 'g'), '$1' + key + '[index].')
.replace(/\{\{abe/, '{{abe dictionnary=\'' + key + '\'')

template = template.replace(matchBlock, newMatchBlock)
/**
* split {{#each}}...{{/each}} into an array
*
* THIS:
{{abe type='text' key='test.title' desc='test title' tab='default'}}
* BECOME THIS:
{{abe dictionnary='test' type='text' key='test.title' desc='test title' tab='default'}}
*
* @param {[type]} template [description]
* @return {[type]} [description]
*/
export function addAbeDictionnary(template, match, key) {
if(cmsData.regex.isEachStatement(match)) return

if(cmsData.regex.isBlockAbe(match)){
var abeDictionnary = match.replace(new RegExp('(key=[\'|"])' + key + '.', 'g'), '$1' + key + '[index].')
.replace(/\{\{abe/, '{{abe dictionnary=\'' + key + '\'')

template = template.replace(match, abeDictionnary)
}

return template
Expand Down
2 changes: 1 addition & 1 deletion src/cli/users/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export function isAbeRestrictedUrl(currentRoute) {
if( currentRoute.indexOf('/abe/users/forgot') > -1
|| currentRoute.indexOf('/abe/users/login') > -1
|| currentRoute.indexOf('/abe/users/reset') > -1
|| !/^\/abe/.test(currentRoute)) {
|| !/^\/abe\//.test(currentRoute)) {
return false
}

Expand Down
Loading

0 comments on commit 8075f4f

Please sign in to comment.