D3 Patternifier
unpatternedCode = ` var defs = svg.append("defs"); // create filter with id #drop-shadow // height=130% so that the shadow is not clipped var filter = defs.append("filter") .attr("id", "drop-shadow") .attr("height", "130%"); // SourceAlpha refers to opacity of graphic that this filter will be applied to // convolve that with a Gaussian with standard deviation 3 and store result // in blur filter.append("feGaussianBlur") .attr("in", "SourceAlpha") .attr("stdDeviation", 5) .attr("result", "blur"); // translate output of Gaussian blur to the right and downwards with 2px // store result in offsetBlur filter.append("feOffset") .attr("in", "blur") .attr("dx", 5) .attr("dy", 5) .attr("result", "offsetBlur"); // overlay original SourceGraphic over translated blurred opacity by using // feMerge filter. Order of specifying inputs is important! var feMerge = filter.append("feMerge"); feMerge.append("feMergeNode") .attr("in", "offsetBlur") feMerge.append("feMergeNode") .attr("in", "SourceGraphic"); `
patternedCode = unpatternedCode.split('\n') .map((d,i)=>{ if(d.indexOf('append')!=-1){ const match = /(\w*)(.append.*\()(.*)\)/g.exec(d); const elem = match[1]; const tag = match[3] return d.replace(/\.append.*/g,'.patternify({tag:'+tag+',selector:'+tag+'+'+i+'})') } return d; }) .join('\n')
Test Area
str = 'const elem = defs.append("filter")'
regRes={ if(str.indexOf('append')!=-1){ return /(\w*)(.append.*\()(.*)\)/g.exec(str) } }
regRes[2]