|
1
|
|
|
import fse from 'fs-extra' |
|
2
|
|
|
import extend from 'extend' |
|
3
|
|
|
import {Promise} from 'es6-promise' |
|
4
|
|
|
import path from 'path' |
|
5
|
|
|
import { |
|
6
|
|
|
getAttr |
|
7
|
|
|
,Util |
|
8
|
|
|
,config |
|
9
|
|
|
,fileUtils |
|
10
|
|
|
,FileParser |
|
|
|
|
|
|
11
|
|
|
,escapeTextToRegex |
|
12
|
|
|
,Hooks |
|
13
|
|
|
,Plugins |
|
|
|
|
|
|
14
|
|
|
} from '../../' |
|
15
|
|
|
|
|
16
|
|
|
export function addOrder(text) { |
|
17
|
|
|
var regAbe = /{{abe[\S\s].*?key=['|"]([\S\s].*?['|"| ]}})/g |
|
18
|
|
|
var matches = text.match(regAbe) |
|
19
|
|
|
var order = 0 |
|
20
|
|
|
|
|
21
|
|
|
if(typeof matches !== 'undefined' && matches !== null){ |
|
22
|
|
|
Array.prototype.forEach.call(matches, (match) => { |
|
23
|
|
|
if(typeof match !== 'undefined' && match !== null) { |
|
24
|
|
|
|
|
25
|
|
|
var keyAttr = getAttr(match, 'key') |
|
|
|
|
|
|
26
|
|
|
var orderAttr = getAttr(match, 'order') |
|
27
|
|
|
|
|
28
|
|
|
if(typeof orderAttr === 'undefined' || orderAttr === null || orderAttr === '') { |
|
29
|
|
|
var matchOrder = match.replace(/\}\}$/, ` order='${order}'}}`) |
|
30
|
|
|
text = text.replace(match, matchOrder) |
|
31
|
|
|
} |
|
32
|
|
|
order++ |
|
33
|
|
|
} |
|
34
|
|
|
}) |
|
35
|
|
|
} |
|
36
|
|
|
return text |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
export function getAbeImport(text) { |
|
40
|
|
|
var partials = [] |
|
41
|
|
|
let listReg = /({{abe.*?type=[\'|\"]import.*?}})/g |
|
42
|
|
|
var match |
|
43
|
|
|
while (match = listReg.exec(text)) { |
|
44
|
|
|
partials.push(match[0]) |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return partials |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
export function includePartials(text) { |
|
51
|
|
|
var abeImports = getAbeImport(text) |
|
52
|
|
|
|
|
53
|
|
|
Array.prototype.forEach.call(abeImports, (abeImport) => { |
|
54
|
|
|
var obj = Util.getAllAttributes(abeImport, {}) |
|
55
|
|
|
|
|
56
|
|
|
var file = obj.file |
|
57
|
|
|
var partial = '' |
|
58
|
|
|
file = path.join(config.root, config.partials, file) |
|
59
|
|
|
if(fileUtils.isFile(file)) { |
|
60
|
|
|
partial = includePartials(fse.readFileSync(file, 'utf8')) |
|
61
|
|
|
} |
|
62
|
|
|
text = text.replace(escapeTextToRegex(abeImport, 'g'), partial) |
|
63
|
|
|
}) |
|
64
|
|
|
|
|
65
|
|
|
return text |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
function translate(text) { |
|
69
|
|
|
var importReg = /({{abe.*type=[\'|\"]translate.*}})/g |
|
70
|
|
|
var match |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
var matches = text.match(importReg) |
|
74
|
|
|
var order = 0 |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
if(typeof matches !== 'undefined' && matches !== null) { |
|
77
|
|
|
Array.prototype.forEach.call(matches, (match) => { |
|
78
|
|
|
var splitedMatches = match.split('{{abe ') |
|
79
|
|
|
|
|
80
|
|
|
Array.prototype.forEach.call(splitedMatches, (splitedMatch) => { |
|
81
|
|
|
var currentMatch = `{{abe ${splitedMatch}` |
|
82
|
|
|
if(/({{abe.*type=[\'|\"]translate.*}})/.test(currentMatch)) { |
|
83
|
|
|
var locale = getAttr(currentMatch, 'locale') |
|
84
|
|
|
var source = getAttr(currentMatch, 'source') |
|
85
|
|
|
|
|
86
|
|
|
if (locale.indexOf('{{') === -1) { |
|
87
|
|
|
locale = `'${locale}'` |
|
88
|
|
|
}else { |
|
89
|
|
|
locale = locale.replace(/\{\{(.*?)\}\}/, '$1') |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (source.indexOf('{{') === -1) { |
|
93
|
|
|
source = `'${source.replace(/'/g, '\\\'')}'` |
|
94
|
|
|
}else { |
|
95
|
|
|
source = source.replace(/\{\{(.*?)\}\}/, '$1') |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// var replace = `{{{i18nAbe ${locale} ${source}}}}` |
|
99
|
|
|
var replace = currentMatch.replace('{{abe', '{{i18nAbe') |
|
100
|
|
|
replace = replace.replace(/locale=['|"].*?['|"]/, locale) |
|
101
|
|
|
replace = replace.replace(/source=['|"].*?['|"]/, source) |
|
102
|
|
|
replace = replace.replace(/{{i18nAbe.*?}}/, `{{{i18nAbe ${locale} ${source}}}}`) |
|
103
|
|
|
|
|
104
|
|
|
text = text.replace(escapeTextToRegex(currentMatch, 'g'), replace) |
|
105
|
|
|
} |
|
106
|
|
|
}) |
|
107
|
|
|
}) |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return text |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
export function getTemplate (file) { |
|
114
|
|
|
var text = '' |
|
115
|
|
|
|
|
116
|
|
|
// HOOKS beforeGetTemplate |
|
117
|
|
|
file = Hooks.instance.trigger('beforeGetTemplate', file) |
|
118
|
|
|
|
|
119
|
|
|
file = file.replace(path.join(config.root, config.templates.url), '') |
|
120
|
|
|
file = file.replace(config.root, '') |
|
121
|
|
|
if (file.indexOf('.') > -1) { |
|
122
|
|
|
file = fileUtils.removeExtension(file) |
|
123
|
|
|
} |
|
124
|
|
|
file = path.join(config.root, config.templates.url, file + '.' + config.files.templates.extension) |
|
125
|
|
|
if(fileUtils.isFile(file)) { |
|
126
|
|
|
text = fse.readFileSync(file, 'utf8') |
|
127
|
|
|
text = includePartials(text) |
|
128
|
|
|
text = translate(text) |
|
129
|
|
|
text = addOrder(text) |
|
130
|
|
|
}else { |
|
131
|
|
|
text = `[ ERROR ] template ${config.templates.url} doesn't exist anymore` |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// HOOKS afterGetTemplate |
|
135
|
|
|
text = Hooks.instance.trigger('afterGetTemplate', text) |
|
136
|
|
|
|
|
137
|
|
|
return text |
|
138
|
|
|
} |