Skip to content

Commit f576250

Browse files
committed
生成改成相对路径
1 parent 8b81835 commit f576250

File tree

7 files changed

+63
-60
lines changed

7 files changed

+63
-60
lines changed

README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ nodePPT - 让你爱上做分享!
3636
* 双屏控制:http://qdemo.sinaapp.com/?_multiscreen=1 记得允许弹窗哦~
3737
* 手机百度前端之路:http://qdemo.sinaapp.com/box-fe-road.htm
3838

39+
## 文件定位
40+
对于nodeppt内部的文件,定位需要用根目录的方式来写,例如项目路径是 `slide``demo.md`中的图片使用:
41+
```markdown
42+
![测试文件路径](/img/demo.png)
43+
```
44+
45+
对应的图片路径是 `slide/img/demo.png`
46+
47+
使用 `nodeppt generate demo.md output -a` 则生成后,图片路径是:`output/img/demo.png`
48+
49+
3950
## subslide
4051

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

176187
```bash
177-
nodeppt path -o output/path -a
188+
nodeppt path output/path -a
178189
```
179190

180191

bin/nodeppt

+13-15
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ program
1717
.usage('[filename] [option]')
1818
.description('create a slide')
1919
.option('-d, --dir [path]', 'set slide file destination path')
20-
.action(function(filename, options) {
20+
.action(function (filename, options) {
2121
if (typeof filename === 'object') {
2222
console.log('ERROR: please input filename!'.bold.red);
2323
this.outputHelp();
2424
return;
2525
}
2626

2727
nodePPT.create(filename, options)
28-
}).on('--help', function() {
28+
}).on('--help', function () {
2929
console.log(' Examples:');
3030
console.log();
3131
console.log(' nodeppt create myslide');
@@ -37,26 +37,24 @@ program
3737
.command('generate')
3838
.usage('[file_path] [save_path]')
3939
.description('export html file')
40-
.option('-o, --output [path]', 'output path')
4140
.option('-a, --all [false]', 'output all style(include js,css) file', false)
42-
.action(function(cmd, options) {
41+
.action(function (cmd, output, options) {
4342
var filename = '';
4443
var shouldAll = false;
45-
var output = '';
46-
44+
if (typeof output !== 'string') {
45+
options = output;
46+
output = undefined;
47+
}
4748
if (typeof cmd === 'string') {
4849
filename = cmd;
4950
shouldAll = options.all;
50-
output = options.output;
5151
} else if (typeof cmd === 'object') {
5252
shouldAll = cmd.all;
5353
output = cmd.output;
5454
}
55-
56-
nodePPT.generate(filename, output, shouldAll);
57-
55+
nodePPT.generate(filename, output, shouldAll, '');
5856
})
59-
.on('--help', function() {
57+
.on('--help', function () {
6058
console.log(' Examples:');
6159
console.log();
6260
console.log(' nodeppt generate D:/webppt/demo.md -o D:/output');
@@ -73,15 +71,15 @@ program
7371
.option('-c, --controller [socket]', 'support websocket mutil screen controller')
7472
.option('-H, --host [host]', 'set host address', ipv4 || '0.0.0.0')
7573
.option('-w, --watch', 'livereload')
76-
.action(function(cmd) {
74+
.action(function (cmd) {
7775
if (typeof cmd !== 'object') {
7876
this.outputHelp();
7977
return;
8078
}
8179

8280
nodePPT.start(cmd)
8381
})
84-
.on('--help', function() {
82+
.on('--help', function () {
8583
console.log(' Examples:');
8684
console.log();
8785
console.log(' nodeppt start -d D:/webppt -p 8080');
@@ -95,7 +93,7 @@ program
9593
.command('pdf')
9694
.usage('[http_url] [save_path.pdf]')
9795
.description('export pdf file. ' + 'Deprecated'.bold.red)
98-
.action(function(http_url, save_path) {
96+
.action(function (http_url, save_path) {
9997
if (typeof http_url !== 'string' || typeof save_path !== 'string') {
10098
console.log('ERROR: pdf need a URL'.bold.red);
10199
this.outputHelp();
@@ -104,7 +102,7 @@ program
104102
console.log(' Warning: '.bold.red + 'This command is ' + 'Deprecated'.bold.red);
105103
nodePPT.pdf([http_url, save_path])
106104
})
107-
.on('--help', function() {
105+
.on('--help', function () {
108106
console.log(' Examples:');
109107
console.log();
110108
console.log(' nodeppt pdf http://127.0.0.1:8080/md/demo.md demo.pdf\n');

lib/generate.js

+22-30
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,22 @@ var nodeModules = path.normalize(path.join(__dirname, '../node_modules')) + path
1111

1212
//1. 只导出文件nodeppt generate file.md
1313
//2. 导出文件+目录 nodeppt generate ./ --all -o publish
14-
module.exports = function(filepath, outputDir, isAll) {
14+
module.exports = function (filepath, outputDir, isAll, rDir) {
1515
filepath = fs.realpathSync(filepath);
1616
outputDir = outputDir ? $.getDirPath(outputDir) : $.getDirPath(path.join(process.cwd(), './publish'));
1717
isAll = !!isAll;
1818
if (isAll) {
1919
//1.导出默认的assets
20-
$.copy(assetsDir, outputDir, function(filename, dir, subdir) {
20+
$.copy(assetsDir, outputDir, function (filename, dir, subdir) {
2121
if (!subdir || subdir === 'scss') {
2222
//不复制scss
2323
return false;
2424
}
2525
return true;
2626
});
27-
//2.导出ppts,特别针对img
28-
// $.copy(pptsDir, outputDir, function(filename, dir, subdir) {
29-
// if (!subdir || ['css', 'js'].indexOf(subdir) !== -1) {
30-
// //不复制css,js
31-
// return false;
32-
// }
33-
// return true;
34-
// });
3527
}
3628
//2.导出复制filepath除根目录下img、css和js等到assets,遇见/*.md就解析
37-
generate(filepath, outputDir);
29+
generate(filepath, outputDir, rDir);
3830
console.log('生成结束!'.bold.green + require('path').relative('b:/', outputDir).yellow);
3931
};
4032

@@ -47,7 +39,6 @@ function parser(content, template) {
4739
} catch (e) {
4840
console.log('ERROR: '.bold.red + e.toString());
4941
}
50-
5142
return false;
5243
}
5344

@@ -57,22 +48,22 @@ function parser(content, template) {
5748
* @param {[type]} outputDir [description]
5849
* @return {[type]} [description]
5950
*/
60-
function generate(filepath, outputDir) {
51+
function generate(filepath, outputDir, rDir) {
52+
rDir = rDir || '.';
6153
var filename = '';
6254
var templateMd = $.readFile(templateDir + 'markdown.ejs');
6355
var templateList = $.readFile(templateDir + 'list.ejs');
6456

6557
if ($.isDir(filepath, true)) {
6658
//遍历目录生成htm
6759
var indexList = '';
68-
69-
$.copy(filepath, outputDir, function(filename, dir, subdir) {
60+
$.copy(filepath, outputDir, function (filename, dir, subdir) {
7061
if (!subdir && /\.(?:md|markdown)$/i.test(filename)) {
7162
var content = $.readFile(path.join(filepath, filename));
7263
var html = parser(content);
7364
if (html) {
7465
var title = html.match(/<title>(.*?)<\/title>/);
75-
if (title[1]) {
66+
if (title && title[1]) {
7667
title = title[1];
7768
} else {
7869
title = filename;
@@ -81,9 +72,8 @@ function generate(filepath, outputDir) {
8172
var url = filename.replace(/\.(?:md|markdown)$/i, '.htm');
8273
indexList += '<li><a class="star" href="' + url + '" target="_blank">' + title + '</a> &nbsp; [<a href="' + url + '?_multiscreen=1" target="_blank" title="多窗口打开">多窗口</a>]</li>';
8374

84-
copyLinkToOutput(content, filepath, outputDir);
85-
86-
75+
copyLinkToOutput(content, filepath, outputDir, rDir);
76+
html = handlerHTML(html, rDir);
8777
$.writeFile(path.join(outputDir, filename.replace(/\.(?:md|markdown)$/i, '.htm')), html);
8878
}
8979
return false;
@@ -111,43 +101,45 @@ function generate(filepath, outputDir) {
111101
return console.log('ERROR: '.bold.red + filepath + ' is not exists!');
112102
}
113103
filename = path.basename(filepath);
114-
copyLinkToOutput(content, filepath, outputDir);
104+
copyLinkToOutput(content, filepath, outputDir, rDir);
115105
var html = parser(content);
116106
if (html) {
117-
html = handlerHTML(html);
107+
html = handlerHTML(html, rDir);
118108
$.writeFile(path.join(outputDir, filename.replace(/\.(?:md|markdown)$/i, '.htm')), html);
119109
}
120110
}
121111
}
122112

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

128120
return html;
129121
}
130122

131123
//处理页面相对url,到目标文件夹
132124
function copyLinkToOutput(content, filepath, outputDir) {
133125
var files = [];
134-
content.replace(/(!)?\[.+?\]\(\s?(.*?)\s?\)/g, function(i, isImg, file) {
126+
content.replace(/(!)?\[.+?\]\(\s?(.*?)\s?\)/g, function (i, isImg, file) {
135127
//处理markdown内部,[inline模式](/assets/box-fe-road/img/inline-mode.png)
136128
if (isImg && file) {
137129
file = file.split(/\s+/)[0];
138130
}
131+
// console.log(file);
139132
files.push(file);
140-
}).replace(/(?:href|src|url)[=|\(](['"])?(.+?)\1/g, function(i, q, file) {
133+
}).replace(/(?:href|src|url)[=|\(](['"])?(.+?)\1/g, function (i, q, file) {
141134
files.push(file);
142135
});
143136
//解析cover
144137
var json = md_parser.parseCover(content.split(/\[slide.*\]/i)[0]);
145138
if (json.files) {
146-
147139
files = files.concat(json.files.split(/\s?,\s?/));
148140
}
149141
if (json.usemathjax === 'yes') {
150-
$.copy(nodeModules + 'mathjax/', outputDir + 'js/mathjax/', function(filename, dir, subdir) {
142+
$.copy(nodeModules + 'mathjax/', outputDir + 'js/mathjax/', function (filename, dir, subdir) {
151143
if (/^(?:docs|unpacked|test)/.test(subdir)) {
152144
//不复制
153145
return false;
@@ -160,13 +152,13 @@ function copyLinkToOutput(content, filepath, outputDir) {
160152
return true;
161153
});
162154
}
163-
files.filter(function(f) {
155+
files.filter(function (f) {
164156
if (/^http[s]?:\/\//.test(f) || /^\/\//.test(f) || ['#', '/'].indexOf(f) !== -1 || /^\?/.test(f) || /^about\:/.test(f)) {
165157
//过滤掉外链
166158
return false;
167159
}
168160
return true;
169-
}).forEach(function(f) {
161+
}).forEach(function (f) {
170162
var topath = path.join(outputDir, f);
171163
var realpath = path.join(path.dirname(filepath), f);
172164
if ($.exists(realpath) && $.isFile(realpath)) {

lib/md_parser.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ var defaultJSON = {
2424
files: '',
2525
highlightStyle: 'monokai_sublime',
2626
headFiles: '',
27-
usemathjax: ''
27+
usemathjax: '',
28+
date: ''
2829
};
2930

3031
marked.setOptions({

lib/nodePPT.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var templateQ = [{
2929
'default': 'speaker'
3030
}];
3131
var ppt = module.exports = {
32-
pdf: function(args) {
32+
pdf: function (args) {
3333
var self = this;
3434
var url = args[0];
3535
var output = args[1] ? args[1] : '';
@@ -47,21 +47,21 @@ var ppt = module.exports = {
4747
}
4848
var child = exec('phantomjs ' + libDir + '/pdf.js ' + url + ' ' + output);
4949
// child.stderr.setEncoding('utf8');
50-
child.stderr.on('data', function(data) {
50+
child.stderr.on('data', function (data) {
5151
console.log('please install phantomjs:npm install -g phantomjs'.red);
5252
console.log('nodeppt pdf depend phantomjs '.red);
5353
});
54-
child.stdout.on('data', function(data) {
54+
child.stdout.on('data', function (data) {
5555
console.log(data);
5656
});
5757
},
58-
start: function(argsObj) {
58+
start: function (argsObj) {
5959
//启动
6060
var curRoot = process.cwd();
6161

6262
var dir = argsObj.dir;
6363
if (dir === '') {
64-
dir = curRoot;//path.join(rootDir, 'ppts');
64+
dir = curRoot; //path.join(rootDir, 'ppts');
6565
}
6666

6767
if (!fs.existsSync(dir)) {
@@ -73,16 +73,16 @@ var ppt = module.exports = {
7373
} else {
7474
var stat = fs.statSync(dir);
7575
if (!stat.isDirectory()) {
76-
return console.log('\nERROR: '.bold.red + dir + ' not a right path');
76+
return console.log('\nERROR: '.bold.red + dir + ' not a right path');
7777
}
7878
}
7979

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

85-
if(options && options.dir){
85+
if (options && options.dir) {
8686
curRoot = options.dir;
8787
}
8888

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

112-
read(prompt, function(err, value) {
112+
read(prompt, function (err, value) {
113113
if (err) {
114114
return console.log('\nERROR: '.bold.red + '获取 "' + prompt.name + '" 输入信息失败');
115115
}
@@ -134,8 +134,8 @@ var ppt = module.exports = {
134134
// require(libDir + '/init')(curRoot);
135135
// }
136136
// },
137-
generate: function(filename, output, all) {
138-
require(libDir + '/generate')(filename, output, all);
137+
generate: function (filename, output, all, rDir) {
138+
require(libDir + '/generate')(filename, output, all, rDir);
139139
}
140140
};
141141

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nodeppt",
33
"jsname": "nodeppt",
44
"description": "A simple, in-browser, markdown-driven presentation framework",
5-
"version": "1.3.5",
5+
"version": "1.3.6",
66
"site": "https://github.com/ksky521/nodePPT",
77
"author": {
88
"name": "Theo Wang",

template/markdown.ejs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Powered By nodePPT - This is probably the best web presentation tool so far!
33
version: <%= nodeppt_version %>
44
site: <%= nodeppt_site %>
5+
date: <%= date %>
56
-->
67
<!doctype html>
78
<html>

0 commit comments

Comments
 (0)