Skip to content

Commit

Permalink
生成改成相对路径
Browse files Browse the repository at this point in the history
  • Loading branch information
ksky521 committed Nov 29, 2015
1 parent 8b81835 commit f576250
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 60 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ nodePPT - 让你爱上做分享!
* 双屏控制:http://qdemo.sinaapp.com/?_multiscreen=1 记得允许弹窗哦~
* 手机百度前端之路:http://qdemo.sinaapp.com/box-fe-road.htm

## 文件定位
对于nodeppt内部的文件,定位需要用根目录的方式来写,例如项目路径是 `slide``demo.md`中的图片使用:
```markdown
![测试文件路径](/img/demo.png)
```

对应的图片路径是 `slide/img/demo.png`

使用 `nodeppt generate demo.md output -a` 则生成后,图片路径是:`output/img/demo.png`


## subslide

subslide是在一页幻灯片中播放多个子页面,使用`subslide`标签包裹,子页面之间使用`======`间隔
Expand Down Expand Up @@ -169,12 +180,12 @@ nodeppt generate filepath
# 默认导出在publish文件夹
nodeppt generate ./ppts/demo.md -a
# 指定导出文件夹
nodeppt generate ./ppts/demo.md -a -o output/path
nodeppt generate ./ppts/demo.md output/path -a
```
导出目录下所有ppt,并且生成ppt list首页:

```bash
nodeppt path -o output/path -a
nodeppt path output/path -a
```


Expand Down
28 changes: 13 additions & 15 deletions bin/nodeppt
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ program
.usage('[filename] [option]')
.description('create a slide')
.option('-d, --dir [path]', 'set slide file destination path')
.action(function(filename, options) {
.action(function (filename, options) {
if (typeof filename === 'object') {
console.log('ERROR: please input filename!'.bold.red);
this.outputHelp();
return;
}

nodePPT.create(filename, options)
}).on('--help', function() {
}).on('--help', function () {
console.log(' Examples:');
console.log();
console.log(' nodeppt create myslide');
Expand All @@ -37,26 +37,24 @@ program
.command('generate')
.usage('[file_path] [save_path]')
.description('export html file')
.option('-o, --output [path]', 'output path')
.option('-a, --all [false]', 'output all style(include js,css) file', false)
.action(function(cmd, options) {
.action(function (cmd, output, options) {
var filename = '';
var shouldAll = false;
var output = '';

if (typeof output !== 'string') {
options = output;
output = undefined;
}
if (typeof cmd === 'string') {
filename = cmd;
shouldAll = options.all;
output = options.output;
} else if (typeof cmd === 'object') {
shouldAll = cmd.all;
output = cmd.output;
}

nodePPT.generate(filename, output, shouldAll);

nodePPT.generate(filename, output, shouldAll, '');
})
.on('--help', function() {
.on('--help', function () {
console.log(' Examples:');
console.log();
console.log(' nodeppt generate D:/webppt/demo.md -o D:/output');
Expand All @@ -73,15 +71,15 @@ program
.option('-c, --controller [socket]', 'support websocket mutil screen controller')
.option('-H, --host [host]', 'set host address', ipv4 || '0.0.0.0')
.option('-w, --watch', 'livereload')
.action(function(cmd) {
.action(function (cmd) {
if (typeof cmd !== 'object') {
this.outputHelp();
return;
}

nodePPT.start(cmd)
})
.on('--help', function() {
.on('--help', function () {
console.log(' Examples:');
console.log();
console.log(' nodeppt start -d D:/webppt -p 8080');
Expand All @@ -95,7 +93,7 @@ program
.command('pdf')
.usage('[http_url] [save_path.pdf]')
.description('export pdf file. ' + 'Deprecated'.bold.red)
.action(function(http_url, save_path) {
.action(function (http_url, save_path) {
if (typeof http_url !== 'string' || typeof save_path !== 'string') {
console.log('ERROR: pdf need a URL'.bold.red);
this.outputHelp();
Expand All @@ -104,7 +102,7 @@ program
console.log(' Warning: '.bold.red + 'This command is ' + 'Deprecated'.bold.red);
nodePPT.pdf([http_url, save_path])
})
.on('--help', function() {
.on('--help', function () {
console.log(' Examples:');
console.log();
console.log(' nodeppt pdf http://127.0.0.1:8080/md/demo.md demo.pdf\n');
Expand Down
52 changes: 22 additions & 30 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,22 @@ var nodeModules = path.normalize(path.join(__dirname, '../node_modules')) + path

//1. 只导出文件nodeppt generate file.md
//2. 导出文件+目录 nodeppt generate ./ --all -o publish
module.exports = function(filepath, outputDir, isAll) {
module.exports = function (filepath, outputDir, isAll, rDir) {
filepath = fs.realpathSync(filepath);
outputDir = outputDir ? $.getDirPath(outputDir) : $.getDirPath(path.join(process.cwd(), './publish'));
isAll = !!isAll;
if (isAll) {
//1.导出默认的assets
$.copy(assetsDir, outputDir, function(filename, dir, subdir) {
$.copy(assetsDir, outputDir, function (filename, dir, subdir) {
if (!subdir || subdir === 'scss') {
//不复制scss
return false;
}
return true;
});
//2.导出ppts,特别针对img
// $.copy(pptsDir, outputDir, function(filename, dir, subdir) {
// if (!subdir || ['css', 'js'].indexOf(subdir) !== -1) {
// //不复制css,js
// return false;
// }
// return true;
// });
}
//2.导出复制filepath除根目录下img、css和js等到assets,遇见/*.md就解析
generate(filepath, outputDir);
generate(filepath, outputDir, rDir);
console.log('生成结束!'.bold.green + require('path').relative('b:/', outputDir).yellow);
};

Expand All @@ -47,7 +39,6 @@ function parser(content, template) {
} catch (e) {
console.log('ERROR: '.bold.red + e.toString());
}

return false;
}

Expand All @@ -57,22 +48,22 @@ function parser(content, template) {
* @param {[type]} outputDir [description]
* @return {[type]} [description]
*/
function generate(filepath, outputDir) {
function generate(filepath, outputDir, rDir) {
rDir = rDir || '.';
var filename = '';
var templateMd = $.readFile(templateDir + 'markdown.ejs');
var templateList = $.readFile(templateDir + 'list.ejs');

if ($.isDir(filepath, true)) {
//遍历目录生成htm
var indexList = '';

$.copy(filepath, outputDir, function(filename, dir, subdir) {
$.copy(filepath, outputDir, function (filename, dir, subdir) {
if (!subdir && /\.(?:md|markdown)$/i.test(filename)) {
var content = $.readFile(path.join(filepath, filename));
var html = parser(content);
if (html) {
var title = html.match(/<title>(.*?)<\/title>/);
if (title[1]) {
if (title && title[1]) {
title = title[1];
} else {
title = filename;
Expand All @@ -81,9 +72,8 @@ function generate(filepath, outputDir) {
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>';

copyLinkToOutput(content, filepath, outputDir);


copyLinkToOutput(content, filepath, outputDir, rDir);
html = handlerHTML(html, rDir);
$.writeFile(path.join(outputDir, filename.replace(/\.(?:md|markdown)$/i, '.htm')), html);
}
return false;
Expand Down Expand Up @@ -111,43 +101,45 @@ function generate(filepath, outputDir) {
return console.log('ERROR: '.bold.red + filepath + ' is not exists!');
}
filename = path.basename(filepath);
copyLinkToOutput(content, filepath, outputDir);
copyLinkToOutput(content, filepath, outputDir, rDir);
var html = parser(content);
if (html) {
html = handlerHTML(html);
html = handlerHTML(html, rDir);
$.writeFile(path.join(outputDir, filename.replace(/\.(?:md|markdown)$/i, '.htm')), html);
}
}
}

//处理绝对路径的url
function handlerHTML(html) {
html = html.replace(/(src|href|url)([=|\(])(["'])\//gi, '$1$2$3./')
.replace("loadJS('/js", "loadJS('./js").replace("dir: '/js/',", "dir: './js/',");
function handlerHTML(html, rDir) {
html = html.replace(/(src|href|url)([=|\(])(["'])\/\//gi, '$1$2$3<=PLACEHOLDER=>//')
.replace(/(src|href|url)([=|\(])(["'])\//gi, '$1$2$3' + rDir + '/')
.replace(/(src|href|url)([=|\(])(["'])<=PLACEHOLDER=>\//gi, '$1$2$3//')
.replace(/loadJS\(['"]\/js/g, "loadJS($1" + rDir + "/js").replace(/dir:\s*(["'])\/js\/\1,/g, "dir: $1" + rDir + "/js/$1,");

return html;
}

//处理页面相对url,到目标文件夹
function copyLinkToOutput(content, filepath, outputDir) {
var files = [];
content.replace(/(!)?\[.+?\]\(\s?(.*?)\s?\)/g, function(i, isImg, file) {
content.replace(/(!)?\[.+?\]\(\s?(.*?)\s?\)/g, function (i, isImg, file) {
//处理markdown内部,[inline模式](/assets/box-fe-road/img/inline-mode.png)
if (isImg && file) {
file = file.split(/\s+/)[0];
}
// console.log(file);
files.push(file);
}).replace(/(?:href|src|url)[=|\(](['"])?(.+?)\1/g, function(i, q, file) {
}).replace(/(?:href|src|url)[=|\(](['"])?(.+?)\1/g, function (i, q, file) {
files.push(file);
});
//解析cover
var json = md_parser.parseCover(content.split(/\[slide.*\]/i)[0]);
if (json.files) {

files = files.concat(json.files.split(/\s?,\s?/));
}
if (json.usemathjax === 'yes') {
$.copy(nodeModules + 'mathjax/', outputDir + 'js/mathjax/', function(filename, dir, subdir) {
$.copy(nodeModules + 'mathjax/', outputDir + 'js/mathjax/', function (filename, dir, subdir) {
if (/^(?:docs|unpacked|test)/.test(subdir)) {
//不复制
return false;
Expand All @@ -160,13 +152,13 @@ function copyLinkToOutput(content, filepath, outputDir) {
return true;
});
}
files.filter(function(f) {
files.filter(function (f) {
if (/^http[s]?:\/\//.test(f) || /^\/\//.test(f) || ['#', '/'].indexOf(f) !== -1 || /^\?/.test(f) || /^about\:/.test(f)) {
//过滤掉外链
return false;
}
return true;
}).forEach(function(f) {
}).forEach(function (f) {
var topath = path.join(outputDir, f);
var realpath = path.join(path.dirname(filepath), f);
if ($.exists(realpath) && $.isFile(realpath)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/md_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ var defaultJSON = {
files: '',
highlightStyle: 'monokai_sublime',
headFiles: '',
usemathjax: ''
usemathjax: '',
date: ''
};

marked.setOptions({
Expand Down
22 changes: 11 additions & 11 deletions lib/nodePPT.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var templateQ = [{
'default': 'speaker'
}];
var ppt = module.exports = {
pdf: function(args) {
pdf: function (args) {
var self = this;
var url = args[0];
var output = args[1] ? args[1] : '';
Expand All @@ -47,21 +47,21 @@ var ppt = module.exports = {
}
var child = exec('phantomjs ' + libDir + '/pdf.js ' + url + ' ' + output);
// child.stderr.setEncoding('utf8');
child.stderr.on('data', function(data) {
child.stderr.on('data', function (data) {
console.log('please install phantomjs:npm install -g phantomjs'.red);
console.log('nodeppt pdf depend phantomjs '.red);
});
child.stdout.on('data', function(data) {
child.stdout.on('data', function (data) {
console.log(data);
});
},
start: function(argsObj) {
start: function (argsObj) {
//启动
var curRoot = process.cwd();

var dir = argsObj.dir;
if (dir === '') {
dir = curRoot;//path.join(rootDir, 'ppts');
dir = curRoot; //path.join(rootDir, 'ppts');
}

if (!fs.existsSync(dir)) {
Expand All @@ -73,16 +73,16 @@ var ppt = module.exports = {
} else {
var stat = fs.statSync(dir);
if (!stat.isDirectory()) {
return console.log('\nERROR: '.bold.red + dir + ' not a right path');
return console.log('\nERROR: '.bold.red + dir + ' not a right path');
}
}

require(libDir + '/server').start(argsObj.port, dir, argsObj.host, argsObj);
},
create: function(filename, options) {
create: function (filename, options) {
var curRoot = process.cwd();

if(options && options.dir){
if (options && options.dir) {
curRoot = options.dir;
}

Expand All @@ -109,7 +109,7 @@ var ppt = module.exports = {
return doneTmpl(opts);
}

read(prompt, function(err, value) {
read(prompt, function (err, value) {
if (err) {
return console.log('\nERROR: '.bold.red + '获取 "' + prompt.name + '" 输入信息失败');
}
Expand All @@ -134,8 +134,8 @@ var ppt = module.exports = {
// require(libDir + '/init')(curRoot);
// }
// },
generate: function(filename, output, all) {
require(libDir + '/generate')(filename, output, all);
generate: function (filename, output, all, rDir) {
require(libDir + '/generate')(filename, output, all, rDir);
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nodeppt",
"jsname": "nodeppt",
"description": "A simple, in-browser, markdown-driven presentation framework",
"version": "1.3.5",
"version": "1.3.6",
"site": "https://github.com/ksky521/nodePPT",
"author": {
"name": "Theo Wang",
Expand Down
1 change: 1 addition & 0 deletions template/markdown.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Powered By nodePPT - This is probably the best web presentation tool so far!
version: <%= nodeppt_version %>
site: <%= nodeppt_site %>
date: <%= date %>
-->
<!doctype html>
<html>
Expand Down

0 comments on commit f576250

Please sign in to comment.