1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16const reg = '{{{((.|\n)+?)}}}|{{((.|\n)+?)}}' 17const dataReg = new RegExp(reg, 'g') 18const expressionReg = new RegExp(reg) 19 20/** 21 * Parse expression in hml text 22 */ 23function parseData(v) { 24 v.match(dataReg).forEach(element => { 25 const repElement = element.toString().trim().replace(/\n/g, '') 26 v = v.replace(element, repElement) 27 }) 28 29 if (!expressionReg.test(v)) { 30 return null 31 } 32 let start 33 let next = 0 34 const res = [] 35 dataReg.lastIndex = 0 36 for (let i = next; i < v.length; i++) { 37 let match = dataReg.exec(v) 38 if (!match) { 39 break 40 } 41 start = match.index 42 getValue(next, start, v, res) 43 parseValue(match, res) 44 next = start + match[0].length 45 } 46 getValue(next, v.length, v, res) 47 return res 48} 49 50function getValue(begin, end, v, res) { 51 if (begin < end) { 52 res.push({ 53 value: v.slice(begin, end) 54 }) 55 } 56} 57 58function parseValue(match, res) { 59 let three= /^{{{.*}}}$/.test(match[0]) 60 let v = three ? match[1] : match[3] 61 res.push({ 62 tag: true, 63 value: v.trim() 64 }) 65} 66 67exports.parseText = parseData 68