|
1 | 1 | Meteor?.startup ->
|
2 | 2 | if Meteor.isClient
|
3 |
| - console.log('startup4') |
4 | 3 | Template?.registerHelper 't9n', (x, params) ->
|
5 | 4 | T9n.get(x, true, params.hash)
|
6 | 5 |
|
@@ -75,36 +74,36 @@ class T9n
|
75 | 74 | throw Error "language #{language} does not exist"
|
76 | 75 | @language = language
|
77 | 76 | @depLanguage?.changed()
|
78 |
| - |
79 |
| - |
| 77 | + |
80 | 78 | @replaceParams = (str, args) ->
|
81 | 79 | index1 = 0
|
82 | 80 | strCopy = str
|
83 | 81 | while index1 > -1
|
84 | 82 | index1 = strCopy.indexOf('@{')
|
85 | 83 | if index1 > -1
|
86 |
| - index2 = strCopy.substring(index1).indexOf('}') |
| 84 | + index2 = strCopy.substring(index1).indexOf('}') # no nested tokens |
87 | 85 | token = strCopy.substring(index1, index2 + 1)
|
88 |
| - if token.indexOf('->') > -1 |
89 |
| - lines = strCopy.split(/\n/) |
90 |
| - tokenName = token.substring(2, token.indexOf(' ')) |
91 |
| - arg = args[tokenName] |
92 |
| - line = lines.find((l) -> l.indexOf("[#{arg}]") > -1) |
93 |
| - if line |
94 |
| - value = line.substring(line.lastIndexOf(']') + 1).trim() |
95 |
| - else |
96 |
| - line = lines.find((l) -> l.indexOf('[*]') > -1) |
97 |
| - value = |
98 |
| - line.substring(line.lastIndexOf(']') + 1) |
99 |
| - .trim() |
100 |
| - .replace(new RegExp("\\$#{tokenName}"), args[tokenName]) |
| 86 | + if token.indexOf('->') > -1 |
| 87 | + value = @handleSelector(strCopy, args, token) |
101 | 88 | str = str.replace(token, value)
|
102 |
| - else |
| 89 | + else # no selector, simply replace the token |
103 | 90 | tokenName = token.substring(2, token.indexOf('}'))
|
104 |
| - str = str.replace(token, args[tokenName]) |
| 91 | + str = str.replace(new RegExp(token, 'g'), args[tokenName]) |
| 92 | + str = str.replace(new RegExp("\\$#{tokenName}", 'g'), args[tokenName]) |
105 | 93 | strCopy = strCopy.substring(index2+2).trim()
|
106 | 94 | return str
|
107 | 95 |
|
| 96 | + @handleSelector = (str, args, token) -> |
| 97 | + tokenName = token.substring(2, token.indexOf(' ')) |
| 98 | + lineMap = str.split(/\n/).slice(1).map (line) -> |
| 99 | + regexString = line.trim().split(/\s/)[0] |
| 100 | + {regexString, line: line.substring(line.indexOf(regexString) + regexString.length)} |
| 101 | + foundLine = lineMap.find (map) -> new RegExp(map.regexString).test(args[tokenName]) |
| 102 | + if foundLine |
| 103 | + foundLine.line.substring(foundLine.line.lastIndexOf(']') + 1) |
| 104 | + .trim() |
| 105 | + .replace(new RegExp("\\$#{tokenName}"), args[tokenName]) |
| 106 | + |
108 | 107 | @T9n = T9n
|
109 | 108 | @t9n = (x, includePrefix, params) -> T9n.get(x)
|
110 | 109 | module?.exports.T9n = T9n
|
0 commit comments