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)




  • 相关阅读:
    Android TextView中文字通过SpannableString来设置超链接、颜色、字体等属性
    Android-自定义dialog
    安卓签名
    安卓 textview 换行 不满就换了
    [android] setOnTouchEvent 设置返回值为true 和 false的区别
    图片自动切换, 滑动循环切换图片
    Android访问中央气象台的天气预报API得到天气数据
    android:windowSoftInputMode属性详解
    Android中资源文件夹res/raw和assets的使用
    WPF基础学习笔记整理 (二) XAML
  • 原文地址:https://www.cnblogs.com/rsapaper/p/9571565.html
Copyright © 2011-2022 走看看