我不得不编写一个Gruntfile,目的是动态地将一堆Markdown文件转换为PDF。提供当前文件夹层次结构:
root/
|_subfolder1
| |_filename1.md
|_subfolder2
|_filename2.md
...
|_node_modules
|_subfolderN
|filenameN.md我想运行一个Markdown to PDF任务,它将处理md文件,并在相同的输出目录中输出一个具有匹配文件名的PDF文件。
我确实创建了一个自定义任务,它解析当前目录,忽略mode_modules文件夹并获取标记文件,但我不知道如何使用良好的属性配置md2pdf任务以反映动态文件夹映射。
下面是我当前的Gruntfile:
module.exports = function(grunt) {
// 1 - Configuration
grunt.initConfig({
md2pdf: {
}
});
// 2 - Plugins
grunt.loadNpmTasks('grunt-md2pdf');
// 3 - Task registering
grunt.registerTask('default', 'Get Subfolders', function() {
grunt.file.recurse('.', callback);
function callback(abspath, rootdir, subdir, filename) {
var filenameOutExt;
// if current occurence is a file subdir == undefined
// checking subdir to true means it's not undefined and
// the current path is a directory
if(subdir) {
// excluding node_modules folder
if (!subdir.match('node_modules')) {
// only process markdown files
if(filename.match('.md')) {
filenameOutExt = filename.split('.')[0];
// now for each markdown files, run md2pdf task
// and ouput filenameOutExt.pdf in same folder
// as the input files
}
}
}
}
});
};我正在使用这个插件:https://www.npmjs.com/package/grunt-md2pdf
因此,我的问题是如何配置md2pdf任务,将其传递给标记文件,并在同一个目录中生成匹配的文件名pdf输出。
产出应是:
root/
|_subfolder1
| |_filename1.md
|_filename1.pdf
|_subfolder2
|_filename2.md
|_filename2.pdf
...
|_node_modules
|_subfolderN
|filenameN.md
|_filenameN.pdf非常感谢
发布于 2015-02-26 13:18:15
根据这条线的说法,这个任务使用grunt.files实用函数。这样对我们来说更容易了!
首先,创建一个不同的任务来查找其他任务中需要的文件并不常见。
也就是说,每个任务都应该接收它需要操作的文件。例如..。
coffee:
main:
files: [
expand: true
cwd: 'assets/script'
src: ['**/*.coffee']
dest: "assets/script"
ext: '.js'
](请注意,此配置位于Gruntfile.coffee文件中,因此采用CoffeeScript语法)
这种类型的文件配置使用glob扩展是最常见的文件之一。你可以找到文档中的详细信息。
很明显:
在每个目录(
**)中,从以.coffee结尾的assets/script/获取所有内容(/*)。把它放进assets/script。将扩展重命名为.js。
因此,您的任务可能可以这样配置:
md2pdf: {
main: {
files: [ {
expand: true,
src: ['**/*.md', '!node_modules/**/*'],
dest: "pdf/"
} ]
}
}发布于 2015-02-26 13:08:09
好吧,弄清楚了!
module.exports = function(grunt) {
// 2 - Plugins
grunt.loadNpmTasks('grunt-md2pdf');
grunt.registerTask('default', 'Dynamically generate PDF from MD', function() {
grunt.file.expand("./**/*.md").forEach( function(file) {
if(!file.match('./node_modules')) {
var md2pdf = grunt.config.get('md2pdf') || {};
md2pdf[file] = {
src: file,
dest: file + '.pdf'
};
grunt.config.set('md2pdf', md2pdf);
}
});
grunt.task.run('md2pdf');
});
};https://stackoverflow.com/questions/28742377
复制相似问题