Completed
Push — master ( bf7a8a...82793c )
by
unknown
02:04
created

abe-template.js ➔ getAbeImport   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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