Replace all the text with specified replacement using grunt replace

為{幸葍}努か 提交于 2019-11-28 11:55:38

问题


I have a .html file which contains id="fixedtext", I want to replace all these id with id="uniquetext"

the grunt-text-replace just replaces the first id it finds and doesnot parse the entire text.

Any idea how can I make either grunt-text-replace https://github.com/yoniholmes/grunt-text-replace

or grunt-replace https://www.npmjs.com/package/grunt-replace to do this for the entire document and not just for the first occurrence.

replace: {
            dist: {
                options:{
                    patterns:[{
                        match:'id="fixedtext"',
                        replacement: 'id="'+something[i++] +'"'
                    }],
                    files:[
                        {
                            expand: true,
                            src:['./source.html'],
                            dest:'./dest.html'
                        }
                    ]
                }
            }
        },

回答1:


this is what can be done if unique ids are to be added. Assuming that you already have an array of all the id you want to add when you run your task.

In this case the id's are the file names

create your own wrapper task

var path = require('path');
var fs = require('fs');

    grunt.initconfig({
    wrap:{
            html:{
                header:'<script type="text/ng-template" ',
                footer:'</script>',
                src:'./yourPathToFile/',
                dest'./yourPathToDest/'
            }
        }
    });

grunt.registerMultiTask('wrap', 'wrap header and footer with custom id', function(){
 var data = this.data;
 getListOfFiles(data.src);

 function getListOfFiles(expand_path){
       var listOfFiles = fs.readdirSync(expand_path);
            for(var i=0; i<listOfFiles.length; i++){
                var completePath = expand_path + listOfFiles[i];
                var extension = path.extname(completePath);
                if(fs.lstatSync(completePath).isDirectory()){
                    var newDirPath = completePath + '/';
                    console.log('true------ : \n',newDirPath);
                    getListofFiles(newDirPath);
                }
                else if(extension == '.html'){
                    console.log('F:\n', completePath);
                    fullSrcPath = path.resolve(completePath);
                    content = grunt.file.read(fullSrcPath);
                    scriptId = 'id="' + listOfFiles[i]+'">';
                    header = (grunt.template.process(data.header));
                    footer = (grunt.template.process(data.footer));
                    wholeFile = header + scriptId + content + footer;
                    grunt.file.write(fullSrcPath, wholeFile);
                }
            } 
 }

});


来源:https://stackoverflow.com/questions/29662570/replace-all-the-text-with-specified-replacement-using-grunt-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!