|
| 1 | +/** |
| 2 | + * Created by doubleluo |
| 3 | + */ |
| 4 | +var gulp = require('gulp'); |
| 5 | +var replace = require('gulp-replace'); |
| 6 | +var through = require('through2'); |
| 7 | +var fs = require('fs'); |
| 8 | +var path = require('path'); |
| 9 | +var File = require('vinyl'); |
| 10 | +var util = require(path.join(__dirname, '../lib/util')); |
| 11 | +var cheerio = require('cheerio'); |
| 12 | + |
| 13 | +module.exports = function (options) { |
| 14 | + |
| 15 | + var mkdirs = function(dirpath, callback) { |
| 16 | + fs.exists(dirpath, function(exists) { |
| 17 | + if(exists) { |
| 18 | + callback(dirpath); |
| 19 | + } else { |
| 20 | + mkdirs(path.dirname(dirpath), function(){ |
| 21 | + fs.mkdir(dirpath, callback); |
| 22 | + }); |
| 23 | + } |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + var mkdirone = false; |
| 28 | + var marchRegInline = /<img.*?src=['"].*?(\w+)\.svg\?i['"].*?>/gi; |
| 29 | + var marchRegSybmol = /<img.*?src=['"].*?(\w+)\.svg\?s['"].*?>/gi; |
| 30 | + var marchRegBase = /<img.*?src=['"].*?(\w+)\.svg['"].*?>/gi; |
| 31 | + var src = [],id,style,className,data,width,height,newcontent,$; |
| 32 | + |
| 33 | + function getBase(){ |
| 34 | + id = $.attr('id'); |
| 35 | + style = $.attr('style'); |
| 36 | + className = $.attr('class'); |
| 37 | + data = fs.readFileSync(path.join(options.devPath+'/img', src[0])).toString(); |
| 38 | + width = data.match(/<svg(.*?)width=["|'](.*?)(px)?["|']/)[2]; |
| 39 | + height = data.match(/<svg(.*?)height=["|'](.*?)(px)?["|']/)[2]; |
| 40 | + fileName = src[0].substr(src[0].lastIndexOf('/')+1,src[0].indexOf('.svg')-7); |
| 41 | + newcontent = []; |
| 42 | + } |
| 43 | + |
| 44 | + function getAttr(){ |
| 45 | + if(id){ |
| 46 | + newcontent.push(' id="'+ id +'"'); |
| 47 | + } |
| 48 | + if(style){ |
| 49 | + newcontent.push(' style="'+ style +'"'); |
| 50 | + } |
| 51 | + if(className){ |
| 52 | + newcontent.push(' class="'+ className +'"'); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + function inline(contents){ |
| 57 | + getAttr(); |
| 58 | + data = data.replace('<svg','<svg width="'+ width +'" height="'+ height +'"'+newcontent.join('')); |
| 59 | + if(options.SVGGracefulDegradation){ |
| 60 | + data = data.replace('>','><image width="'+ width +'" height="'+ height +'" src="'+src[0].replace('.svg','.png')+'" />'); |
| 61 | + } |
| 62 | + return data; |
| 63 | + } |
| 64 | + function sybmol(contents){ |
| 65 | + var newFile = path.join(options.devPath + '/symboltemp/', fileName); |
| 66 | + fs.writeFile(newFile, data, {encoding: 'utf8'}); |
| 67 | + newcontent.push('<svg width="'+ width +'" height="'+ height +'" xmlns="http://www.w3.org/2000/svg"'); |
| 68 | + getAttr(); |
| 69 | + newcontent.push('>'); |
| 70 | + if(options.SVGGracefulDegradation){ |
| 71 | + newcontent.push('<image src="'+src[0].replace('.svg','.png')+'"',' />'); |
| 72 | + } |
| 73 | + newcontent.push('<use xlink:href="'+ ((options && options.symbolBaseUrl)||'../symbolsvg/symbol.svg')+ '#' + fileName.replace('.svg','') +'"/>'); |
| 74 | + newcontent.push('</svg>'); |
| 75 | + return newcontent.join(''); |
| 76 | + } |
| 77 | + function base(contents){ |
| 78 | + newcontent.push('<svg width="'+ width +'" height="'+ height +'" xmlns="http://www.w3.org/2000/svg"'); |
| 79 | + getAttr(); |
| 80 | + newcontent.push('>'); |
| 81 | + if(options.SVGGracefulDegradation){ |
| 82 | + newcontent.push('<image width="'+ width +'" height="'+ height +'" src="'+src[0].replace('.svg','.png')+'" xlink:href="'+src[0]+'">'); |
| 83 | + }else{ |
| 84 | + newcontent.push('<image width="'+ width +'" height="'+ height +'" xlink:href="'+src[0]+'">'); |
| 85 | + } |
| 86 | + newcontent.push('</svg>'); |
| 87 | + return newcontent.join(''); |
| 88 | + } |
| 89 | + |
| 90 | + function run(regData,contents,marchReg){ |
| 91 | + if(regData){ |
| 92 | + for(var i = 0;i<regData.length;i++){ |
| 93 | + var replaceData = ''; |
| 94 | + $ = cheerio(regData[i]); |
| 95 | + src = $.attr('src').split('?'); |
| 96 | + if(src[0].indexOf('.svg')>-1){ |
| 97 | + getBase(); |
| 98 | + if(src[1] == 'i'){ |
| 99 | + replaceData = inline(contents); |
| 100 | + }else if(src[1] == 's'){ |
| 101 | + replaceData = sybmol(contents); |
| 102 | + }else{ |
| 103 | + replaceData = base(contents); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + contents = contents.replace(regData[i], replaceData); |
| 108 | + |
| 109 | + } |
| 110 | + } |
| 111 | + return contents; |
| 112 | + } |
| 113 | + |
| 114 | + return through.obj(function (file, enc, cb) { |
| 115 | + |
| 116 | + var _this = this; |
| 117 | + |
| 118 | + |
| 119 | + if (file.isNull()) { |
| 120 | + cb(null, file); |
| 121 | + } else { |
| 122 | + if (util.fileExist(file.path)) { |
| 123 | + var contents = file.contents.toString(); |
| 124 | + var regData; |
| 125 | + if(options.onlyInline){ |
| 126 | + regData = contents.match(marchRegInline); |
| 127 | + contents = run(regData,contents,marchRegInline); |
| 128 | + }else{ |
| 129 | + if(!mkdirone){ |
| 130 | + mkdirone = true; |
| 131 | + fs.exists(options.devPath +'/symboltemp', function(exists) { |
| 132 | + if(!exists){ |
| 133 | + fs.mkdir(options.devPath +'/symboltemp'); |
| 134 | + regData = contents.match(marchRegInline); |
| 135 | + contents = run(regData,contents,marchRegInline); |
| 136 | + regData = contents.match(marchRegSybmol); |
| 137 | + contents = run(regData,contents,marchRegSybmol); |
| 138 | + regData = contents.match(marchRegBase); |
| 139 | + contents = run(regData,contents,marchRegBase); |
| 140 | + } |
| 141 | + }); |
| 142 | + }else{ |
| 143 | + regData = contents.match(marchRegInline); |
| 144 | + contents = run(regData,contents,marchRegInline); |
| 145 | + regData = contents.match(marchRegSybmol); |
| 146 | + contents = run(regData,contents,marchRegSybmol); |
| 147 | + regData = contents.match(marchRegBase); |
| 148 | + contents = run(regData,contents,marchRegBase); |
| 149 | + } |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + _this.push(new File({ |
| 155 | + base: file.base, |
| 156 | + path: file.path, |
| 157 | + contents: new Buffer(contents) |
| 158 | + })); |
| 159 | + cb(null); |
| 160 | + } else { |
| 161 | + cb(null, file); |
| 162 | + } |
| 163 | + } |
| 164 | + }); |
| 165 | +} |
0 commit comments