Skip to content

Commit

Permalink
0.6 目录
Browse files Browse the repository at this point in the history
  • Loading branch information
ksky521 committed Nov 15, 2013
1 parent 798ce1b commit a789054
Show file tree
Hide file tree
Showing 13 changed files with 419 additions and 85 deletions.
3 changes: 0 additions & 3 deletions build/js/nodePPT.js

This file was deleted.

5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 0.6.0
* 添加导出功能:```nodeppt generate```
* 添加固定目录作为开发目录:```nodeppt init```
* 去除grunt依赖
* 模板升级到ejs
7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"url": "http://js8.in",
"speaker": "nodeppt demo",
"assetsDir": "./assets",
"pptDir": "./ppts",
"port": 8080
}
108 changes: 108 additions & 0 deletions lib/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
var fs = require('fs');
var path = require('path');

var md_parser = require('./md_parser');
var $ = require('./helper');
var rootDir = path.join(__dirname, '../') + path.sep;
var staticDir = path.join(rootDir, 'assets') + path.sep;
var templateDir = path.join(rootDir, 'template') + path.sep;

module.exports = {
dir: function(destDir, outputDir) {
destDir = $.getDirPath(destDir);
if (!fs.existsSync(destDir + 'config.json')) {
console.log('ERROR: '.red + ' not found config.json!');
return;
}
outputDir = outputDir ? $.getDirPath(outputDir) : $.getDirPath(path.join(process.cwd(), './publish'));
var config = require(destDir + 'config.json');
var defaultConfig = require(rootDir + 'config.json');
var assetsDir = config.assetsDir ? path.join(destDir, config.assetsDir) : defaultConfig.assetsDir;
var pptDir = config.pptDir ? path.join(destDir, config.pptDir) : defaultConfig.pptDir;
var template = {
'markdown.ejs': '',
'list.ejs': ''
};
//复制assets文件夹内容
$.copy(assetsDir, outputDir, function(filename, dir, subdir) {
if (subdir === 'scss') {
//不复制scss
return false
}
if (!subdir && /\.tpl$/i.test(filename)) {
if (filename in template) {
template[filename] = fs.readFileSync(path.join(assetsDir, filename), 'utf-8').toString();
}

//不复制根目录下面的tpl文件
return false;
}
return true;
});

var indexList = '';
//复制ppts内容
$.copy(pptDir, outputDir, function(filename, dir, subdir) {

if (!subdir && /\.(?:md|markdown)$/i.test(filename)) {
var html = parser(path.join(pptDir, filename), template['markdown.ejs']);
if (html) {
var title = html.match(/<title>(.*?)<\/title>/);
if (title[1]) {
title = title[1];
} else {
title = filename;
}

var url = filename.replace(/\.(?:md|markdown)$/i, '.htm');
indexList += '<li><a class="star" href="' + url + '" target="_blank">' + title + '</a> &nbsp; [<a href="' + url + '?_multiscreen=1" target="_blank" title="多窗口打开">多窗口</a>]</li>';

$.writeFile(path.join(outputDir, filename.replace(/\.(?:md|markdown)$/i, '.htm')), html);
}

//不复制根目录下面的tpl文件
return false;
}
return true;
});

//输出index文件
var packageJson = require(path.join(__dirname, '../package.json'));

var data = {
version: packageJson.version,
site: packageJson.site,
date: Date.now(),
list: indexList,
dir: '/'
};

indexList = $.renderStr(template['list.ejs'] || $.readFile(templateDir + 'list.ejs'), {
data: data
});
$.writeFile(path.join(outputDir, 'index.html'), indexList);

console.log('生成结束!'.green);
},
output: function(filename) {
var html = parser(filename);
if (html) {
$.writeFile(filename.replace(/\.(?:md|markdown)$/i, '.htm'), html);
}
}
};

function parser(realPath, template) {
if ($.exists(realPath)) {
var content = $.readFile(realPath);
try {
var html = md_parser(content, null, template);
return html;
} catch (e) {
console.log('ERROR: '.red + e.toString());
}
} else {
console.log('ERROR: '.red + realPath + ' is not exists!');
}
return false;
}
184 changes: 184 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
var fs = require('fs');
var path = require('path');
var ejs = require('ejs');

var win32 = process.platform === 'win32';
var pathSeparatorRe = /[\/\\]/g;

var $ = {
getDirPath: function(dir) {
dir += path.sep;
try {
dir = fs.realpathSync(dir) + path.sep;
} catch (e) {}
return dir;
}
};

var unixifyPath = function(filepath) {
if (win32) {
return filepath.replace(/\\/g, '/');
} else {
return filepath;
}
};

/**
* 遍历
* @param {[type]} rootdir [description]
* @param {Function} callback [description]
* @param {[type]} subdir [description]
* @param {[type]} destDir [description]
* @param {[type]} judgeFunction [description]
* @return {[type]} [description]
*/
$.recurse = function(rootdir, callback, subdir, destDir, judgeFunction) {
var abspath = subdir ? path.join(rootdir, subdir) : rootdir;
judgeFunction = typeof judgeFunction === 'function' ? judgeFunction : function() {
return true;
};
fs.readdirSync(abspath).forEach(function(filename) {
var filepath = path.join(abspath, filename);
if (fs.statSync(filepath).isDirectory() && judgeFunction(filename)) {
$.mkdir(path.join(destDir, filename));
$.recurse(rootdir, callback, unixifyPath(path.join(subdir, filename)), unixifyPath(path.join(destDir, filename)), judgeFunction);
} else {
judgeFunction(filename) && callback(unixifyPath(filepath), rootdir, subdir, filename, destDir, judgeFunction);
}
});

};

/**
* 是否是目录
* @param {[type]} dir [description]
* @param {[type]} notwarning [description]
* @return {Boolean} [description]
*/
$.isDir = function(dir, notwarning) {
if (!fs.existsSync(dir)) {
dir = path.join(process.cwd(), dir);
if (!fs.existsSync(dir)) {

if (!notwarning) {
console.log('ERROR: '.bold.red + dir + ' 不是一个正确路径');
}
return false;
}

} else {
var stat = fs.statSync(dir);
if (!stat.isDirectory()) {
if (!notwarning) {
console.log('ERROR: '.bold.red + dir + ' 不是一个正确路径');
}
return false;
}
}
return true;
};

/**
* 写文件
* @param {[type]} path [description]
* @param {[type]} content [description]
* @return {[type]} [description]
*/
$.writeFile = function(path, content) {
fs.writeFileSync(path, content, {
encoding: 'utf-8'
});
};
/**
* 读取文件
* @param {[type]} filepath [description]
* @return {[type]} [description]
*/
$.readFile = function(filepath) {
fs.readFileSync(filepath, 'utf8').toString();
};
/**
* 路径复制
* @param {[type]} distDir [description]
* @param {[type]} destDir [description]
* @param {[type]} judgeFunction [description]
* @return {[type]} [description]
*/
$.copy = function(distDir, destDir, judgeFunction) {
if (!(typeof judgeFunction === 'function')) {
judgeFunction = function() {
return true
};
}
if ($.isDir(distDir, true)) {
$.recurse(distDir, function(abspath, rootdir, subdir, filename) {
var dest = path.join(destDir, (subdir ? subdir : ''));
if (judgeFunction(filename, dest, subdir)) {
if (!$.isDir(dest, true)) {
$.mkdir(dest);
}
if (filename) {
$.copy(abspath, path.join(dest, filename));
} else {}
}


});
} else {
console.log(('路径 "' + distDir + '" 不存在!').red);
}
};

/**
* ejs渲染file
* @param {[type]} filepath [description]
* @param {[type]} data [description]
* @return {[type]} [description]
*/
$.renderFile = function(filepath, data) {
var str = $.readFile(filepath);
return ejs.render(str, data || {});
};
/**
* ejs渲染string
* @param {[type]} str [description]
* @param {[type]} data [description]
* @return {[type]} [description]
*/
$.renderStr = function(str, data) {
return ejs.render(str, data);
};
/**
* 创建目录
* @param {[type]} dirpath [description]
* @param {[type]} mode [description]
* @return {[type]} [description]
*/
$.mkdir = function(dirpath, mode) {
if (mode == null) {
mode = parseInt('0777', 8) & (~process.umask());
}
dirpath.split(pathSeparatorRe).reduce(function(parts, part) {
parts += part + '/';
var subpath = path.resolve(parts);
if (!$.exists(subpath)) {
try {
fs.mkdirSync(subpath, mode);
} catch (e) {
console.log('Unable to create directory "' + subpath + '" (Error code: ' + e.code + ').', e);
}
}
return parts;
}, '');
};

/**
* 判断是否存在
* @return {[type]} [description]
*/
$.exists = function() {
var filepath = path.join.apply(path, arguments);
return fs.existsSync(filepath);
};

module.exports = $;
39 changes: 39 additions & 0 deletions lib/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var fs = require('fs');
var path = require('path');

var libDir = __dirname + path.sep;
var $ = require('./helper');
var rootDir = path.join(libDir, '../') + path.sep;

module.exports = function(destDir) {
destDir = $.getDirPath(destDir);
var assetsDir = rootDir + 'assets';
//复制assets文件夹
$.copy(assetsDir, path.join(destDir, 'assets'), function(filename, dir, subdir) {
if (!subdir) {

return false;
}
return true;
});
//复制template文件夹
$.copy(rootDir + 'template', path.join(destDir, 'template'), function(filename, dir, subdir) {
if (!subdir && filename === 'default.ejs') {
return false;
}
return true;
});
//复制ppts文件夹
$.copy(rootDir + 'ppts', path.join(destDir, 'ppts'), function(filename, dir, subdir) {
if (!subdir && filename === 'demo.html') {
return false;
}
return true;
});

//复制config.json
['config.json'].map(function(filename) {
filepath = rootDir + filename;
$.copy(filepath, path.join(destDir, filename));
});
}
15 changes: 5 additions & 10 deletions lib/md_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ var path = require('path');

//非系统
var marked = require('marked');
var grunt = require('grunt');

var gFile = grunt.file;
var gTpl = grunt.template;
var $ = require('./helper');
var libDir = __dirname;
var rootDir = path.join(libDir, '../');
var assetsDir = path.join(rootDir, 'assets');
var assetsDir = path.join(rootDir, 'assets') + path.sep;
var templateDir = path.join(rootDir, 'template') + path.sep;
var EOL = '\n';
var templateFile = assetsDir + '/markdown.tpl';
var templateFile = templateDir + 'markdown.ejs';
var defaultJSON = {
title: 'nodeppt markdown',
url: '',
Expand Down Expand Up @@ -100,10 +98,7 @@ var parser = function(string, callback) {
json.content = slides.join(EOL);

//解析
var html = fs.readFileSync(templateFile, 'utf-8').toString();
html = gTpl.process(html, {
data: json
});
html = $.renderFile(templateFile, json);
callback(html);
return html;
}
Expand Down
Loading

0 comments on commit a789054

Please sign in to comment.