16a23e08bSopenharmony_ci/*
26a23e08bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
36a23e08bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
46a23e08bSopenharmony_ci * you may not use this file except in compliance with the License.
56a23e08bSopenharmony_ci * You may obtain a copy of the License at
66a23e08bSopenharmony_ci *
76a23e08bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
86a23e08bSopenharmony_ci *
96a23e08bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
106a23e08bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
116a23e08bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126a23e08bSopenharmony_ci * See the License for the specific language governing permissions and
136a23e08bSopenharmony_ci * limitations under the License.
146a23e08bSopenharmony_ci */
156a23e08bSopenharmony_ci
166a23e08bSopenharmony_ciconst OHOS_THEME_PROP_GROUPS = require('./theme/ohosStyles')
176a23e08bSopenharmony_cimodule.exports = function (source) {
186a23e08bSopenharmony_ci  let result
196a23e08bSopenharmony_ci  // target format: this.$r("ohos.id_color_background") or this.$r('ohos.id_color_background')  or
206a23e08bSopenharmony_ci  // "this.$r('ohos.id_color_background')"
216a23e08bSopenharmony_ci  let ResourceRefReg = /"?this\s*\.\$r\s*\(\s*((['"]ohos\.(?<resName>\w+)['"]))\s*\)"?/g
226a23e08bSopenharmony_ci  while (result = ResourceRefReg.exec(source)) {
236a23e08bSopenharmony_ci    const resourceName = result.groups['resName']
246a23e08bSopenharmony_ci    if (resourceName && OHOS_THEME_PROP_GROUPS[resourceName]) {
256a23e08bSopenharmony_ci      const resourceId = "\"@ohos_id_" + OHOS_THEME_PROP_GROUPS[resourceName] + "\""
266a23e08bSopenharmony_ci      source = source.replace(result[0], resourceId)
276a23e08bSopenharmony_ci      // update the start position of the next match
286a23e08bSopenharmony_ci      ResourceRefReg.lastIndex -= result[0].length - resourceId.length
296a23e08bSopenharmony_ci    }
306a23e08bSopenharmony_ci  }
316a23e08bSopenharmony_ci
326a23e08bSopenharmony_ci  // target format: this.$r("sys.type.id_color_background") or this.$r('sys.type.id_color_background') or
336a23e08bSopenharmony_ci  // "this.$r('sys.type.id_color_background')"
346a23e08bSopenharmony_ci  // The "type" field can be "float", "color", "string" and so on.
356a23e08bSopenharmony_ci  let SysResourceTypeRefReg = /"?this\s*\.\$r\s*\(\s*((['"]sys\.(?<resType>\w+)\.(?<resName>\w+)['"]))\s*\)"?/g
366a23e08bSopenharmony_ci  while (result = SysResourceTypeRefReg.exec(source)) {
376a23e08bSopenharmony_ci    const resourceName = result.groups['resName']
386a23e08bSopenharmony_ci    const resourceType = result.groups['resType']
396a23e08bSopenharmony_ci    if (resourceName && resourceType && OHOS_THEME_PROP_GROUPS[resourceName]) {
406a23e08bSopenharmony_ci      const resourceId = "\"@sys." + resourceType + "." + OHOS_THEME_PROP_GROUPS[resourceName] + "\""
416a23e08bSopenharmony_ci      source = source.replace(result[0], resourceId)
426a23e08bSopenharmony_ci      // update the start position of the next match
436a23e08bSopenharmony_ci      SysResourceTypeRefReg.lastIndex -= result[0].length - resourceId.length
446a23e08bSopenharmony_ci    }
456a23e08bSopenharmony_ci  }
466a23e08bSopenharmony_ci
476a23e08bSopenharmony_ci  // target format: this.$r("app.type.developer_defined_color") or this.$r('app.type.developer_defined_color') or
486a23e08bSopenharmony_ci  // "this.$r('app.type.developer_defined_color')"
496a23e08bSopenharmony_ci  // The "type" field can be "float", "color", "string" and so on.
506a23e08bSopenharmony_ci  let AppResourceTypeRefReg = /"?this\s*\.\$r\s*\(\s*((['"]app\.(?<resType>\w+)\.(?<resName>\w+)['"]))\s*\)"?/g
516a23e08bSopenharmony_ci  while (result = AppResourceTypeRefReg.exec(source)) {
526a23e08bSopenharmony_ci    const resourceName = result.groups['resName']
536a23e08bSopenharmony_ci    const resourceType = result.groups['resType']
546a23e08bSopenharmony_ci    if (resourceName && resourceType) {
556a23e08bSopenharmony_ci      const resourceId = "\"@app." + resourceType + "." + resourceName + "\""
566a23e08bSopenharmony_ci      source = source.replace(result[0], resourceId)
576a23e08bSopenharmony_ci      // update the start position of the next match
586a23e08bSopenharmony_ci      AppResourceTypeRefReg.lastIndex -= result[0].length - resourceId.length
596a23e08bSopenharmony_ci    }
606a23e08bSopenharmony_ci  }
616a23e08bSopenharmony_ci
626a23e08bSopenharmony_ci  return source
636a23e08bSopenharmony_ci}