123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- const tagsAndAttrs = require('./lib/tagsAndAttrs');
- class outwxml{
- constructor(option){
- const _ts = this;
- _ts.config = {};
- option = option || {};
- for(let i in option){
- _ts.config[i] = option[i];
- };
- _ts.m = {
- fs:require('fs'),
- path:require('path')
- };
- }
- init(){
- const _ts = this;
- _ts.outtag();
- let s = _ts.outwxml();
- _ts.m.fs.writeFileSync('./renderTemplate.wxml',s);
- }
-
- outtag(id){
- const _ts = this;
- let s = '',
- wxmlTag = tagsAndAttrs.wxml;
-
- wxmlTag.forEach((item,index)=>{
- let imgMode = '',
- attr = _ts.outattr(item);
- if(item === 'image'){
- imgMode = `mode="{{item.type === 'audio' ? '' : 'widthFix'}}"`;
- };
-
- if(item === 'checkbox-group'){
- attr += `bindchange="{{item.attr['bindchange']}}"`;
- };
- if(item === 'checkbox'){
- attr += `value="{{item.attr['value']}}"`;
- };
- s+= `<${item} wx:if="{{item.node === 'element' && item.tag === '${item}'}}" ${attr} ${imgMode}><block wx:for="{{item.child}}" wx:key="{{item}}"><template is="m${id}" data="{{item}}"/></block></${item}>`;
- });
- return s;
- }
-
- outattr(tagName){
- tagName = tagName || '';
- const _ts = this;
-
- let s = '',
- attr = [];
- attr.push(...tagsAndAttrs.attrs);
- switch (tagName) {
- case 'navigator':
- attr.push('href');
- break;
- case 'checkbox':
- case 'radio':
- case 'switch':
- attr.push('checked');
- break;
- case 'audio':
- attr.push('poster');
- attr.push('src');
- attr.push('name');
- attr.push('author');
- attr.push('loop');
-
- s += 'controls="true" ';
- break;
- case 'video':
- attr.push('poster');
- attr.push('src');
- break;
- case 'image':
- attr.push('src');
- break;
- };
- s += `data-_el="{{item}}"`;
- attr.forEach((item,index)=>{
- switch (item) {
- case 'class':
- s += `${item}="{{item.attr.class}}"`;
- break;
- case 'href':
- s += `url="{{item.attr.${item}}}"`;
- break;
- default:
- let aItem = item.split(':');
- if(aItem.length > 1){
- s += `${item}='__${aItem[0]}_${aItem[1]}'`;
- }else{
- s += `${item}="{{item.attr['${item}']}}"`;
- };
- break;
- };
- });
- return s;
- }
-
- outwxml(){
- const _ts = this;
- let s = '';
- for (let i = 0, len = _ts.config.depth; i<len; i++){
- let c = i < len - 1 ? i+1 : i;
- let temp = `<template name="m${i}"><block wx:if="{{item.node === 'text'}}">{{item.text}}</block>${_ts.outtag(c)}</template>`;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- s+=temp;
- };
- return s;
- }
- };
- new outwxml({depth:10}).init();
|