使用grunt替换替换指定替换的所有文本 [英] Replace all the text with specified replacement using grunt replace
问题描述
我有一个包含id =fixedtext的.html文件,我想用id =uniquetext替换所有这些id。
grunt-text-替换只是替换它找到的第一个ID,并不会解析整个文本。
任何想法如何使grunt-text-replace https://github.com/yoniholmes/grunt-text-replace
或
grunt-replace https://www.npmjs.com/package/grunt-replace
为此做整个文件,而不仅仅是第一次出现。
替换:{
dist:{
options:{
patterns:[{
match:'id =fixedtext',
replacement:'id =''+ something [i ++] +'''
}],
files:[
{
expand:true,
src:['./ source.html'],
dest:'./ dest.html'
}
]
$
解决方案如果要添加唯一的ID,这是可以做的。
假设您已经拥有一组您想要在运行任务时添加的所有ID。
在这种情况下,id是文件名称
创建您自己的包装任务
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','用自定义id封装页眉和页脚',function(){
var data = this.data;
getListOfFiles(data.src );
函数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(fu llSrcPath);
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);
}
}
}
});
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'
}
]
}
}
},
解决方案 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);
}
}
}
});
这篇关于使用grunt替换替换指定替换的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!