zoukankan      html  css  js  c++  java
  • 微信小程序 原生代码 转wepy 字符串处理

    import glob
    import os

    cwd = os.getcwd()
    sep = os.sep
    target = cwd + sep + 'pages' + sep + '*' + sep + '*'
    filelist = glob.glob(target)
    pageNameSet = set([i.split('\')[1] for i in glob.glob('./pages/*/*')])
    pageStrDicList = {}
    for i in pageNameSet:
    pageStrDicList[i] = {'wxml': '', 'wxss': '', 'json': '', 'js': ''}

    '''
    Page页面字符串结构
    <style lang="less">.wxss全部复制写入</style>
    <template>.wxml全部复制写入</template>
    <script>
    import wepy from 'wepy'
    .js Page({之前部分全部复制写入
    export default class Index extends wepy.page {
    config = {
    .json全部复制写入
    }
    data = {.js data:的对象}
    methods = { .js Page({ })中除去data:{}外的全部 }
    }
    </script>


    '''

    for i in filelist:
    for pageName in pageNameSet:
    try:
    if pageName in i:
    if i.endswith('wxml'):
    with open(i, 'r', encoding='utf-8') as fr:
    pageStrDicList[pageName]['wxml'] = fr.read()
    elif i.endswith('wxss'):
    with open(i, 'r', encoding='utf-8') as fr:
    pageStrDicList[pageName]['wxss'] = fr.read()
    elif i.endswith('json'):
    with open(i, 'r', encoding='utf-8') as fr:
    pageStrDicList[pageName]['json'] = fr.read()
    elif i.endswith('js'):
    with open(i, 'r', encoding='utf-8') as fr:
    pageStrDicList[pageName]['js'] = fr.read()
    break
    except Exception as e:
    print(i)
    print(e)

    for pageName in pageStrDicList:
    try:
    r = cwd + sep + 'res' + sep + pageName + '.wpy'
    i = pageStrDicList[pageName]
    s = '<style lang="less"> ' + i['wxss'] + '</style> '
    s += '<template> ' + i['wxml'] + '</template> '
    s += '''
    <script>
    import wepy from 'wepy'
    '''
    l = i['js'].split('Page({')
    s += l[0]
    s += '''
    export default class Index extends wepy.page {
    config =
    '''
    s += i['json']
    s += ' '
    if 'data:' in l[1]:
    # js代码必须被微信ide格式化,这样保证data:存在且第一个满足要求
    index_0 = l[1].index('data:')

    index_1 = l[1][index_0:].index(' },')
    s += ' data = ' + l[1][index_0:][5:index_1] + '} '

    s += ' methods = {'
    s += l[1][index_0 + 5 + (index_1 - 5) + len(' },'):].replace(': function', '').rstrip(' ').rstrip(
    ' ').rstrip('})')
    s += '} }'
    else:
    s += ' methods = {'
    s += l[1][len('Page({') + 1:].replace(': function', '').rstrip(' ').rstrip(
    ' ').rstrip('})')
    s += '} }'
    with open(r, 'w', encoding='utf-8') as fw:
    fw.write(s)
    except Exception as e:
    print(e)
    print(pageName)




  • 相关阅读:
    bzoj3223: Tyvj 1729 文艺平衡树
    bzoj1014: [JSOI2008]火星人prefix
    bzoj3231: [Sdoi2008]递归数列
    bzoj2282: [Sdoi2011]消防
    bzoj3195: [Jxoi2012]奇怪的道路
    成员内部类 局部内部类 匿名内部类
    静态代码块 构造代码块
    父类子类转换
    clone()方法
    后缀表达式求值
  • 原文地址:https://www.cnblogs.com/rsapaper/p/9571565.html
Copyright © 2011-2022 走看看