Skip to content

Instantly share code, notes, and snippets.

@wizardforcel
Created April 30, 2016 02:24
Show Gist options
  • Save wizardforcel/f3ccb734e7b5936cd86148df9f021171 to your computer and use it in GitHub Desktop.
Save wizardforcel/f3ccb734e7b5936cd86148df9f021171 to your computer and use it in GitHub Desktop.
Generate archive for my ebook blog
var fs = require('fs');
var path = 'D:\\it-ebooks\\source\\_posts';
var files = fs.readdirSync(path);
var result = '# 计算机开放电子书汇总\n\n' +
'+ [站点](http://it-ebooks.flygon.net/)\n' +
'+ [站点源码](https://github.com/it-ebooks/blog)\n\n';
var reTitle = /^title: (.+?)$/m;
var reDL = /^[\+\-\*]\s+\[.+?\]\(.+?\)$/mg;
for(var i in files)
{
var fName = files[i];
if(!fName.endsWith('.md'))
continue;
console.log(fName);
var content = fs.readFileSync(path + '\\' + fName, 'utf-8');
var rmTitle = content.match(reTitle);
if(!rmTitle) continue;
var title = rmTitle[1];
var pos = content.indexOf('## 在线阅读');
if(pos == -1) {
pos = content.indexOf('## 下载地址');
if (pos == -1) continue;
}
content = content.substring(pos);
var links = '';
while(rm = reDL.exec(content))
{
links += rm[0] + '\n';
}
if(links == '')
continue;
result += '## ' + title + '\n\n';
result += links;
result += '\n';
}
fs.writeFileSync('result.md', result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment