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)




  • 相关阅读:
    如何用代码来修改目录的权限
    php广告显示设置存放记录的目录代码
    本函数用来改变目前 php 执行的目录到新的 directory 目录中
    for循环的时候是按照数字递增会造成一些元素被遗漏
    php常用的对字符串进行加密的算法
    关于如何用php 获取当前脚本的url
    将正确的 HTTP 头转发给后端服务器的一些问题
    应用服务器上部署自己的 blog 和 wiki 组件。
    PHP统计字符串里单词查询关键字
    (在线工具)JSON字符串转换成Java实体类(POJO)
  • 原文地址:https://www.cnblogs.com/rsapaper/p/9571565.html
Copyright © 2011-2022 走看看