Skip to content

Commit

Permalink
修复导出html所有相对路径问题
Browse files Browse the repository at this point in the history
修复导出markdown中的所有资源
  • Loading branch information
ksky521 committed Aug 6, 2014
1 parent fd6f399 commit c368594
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 25 deletions.
81 changes: 65 additions & 16 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ var $ = require('./helper');
var rootDir = path.join(__dirname, '../') + path.sep;
var assetsDir = path.join(rootDir, 'assets') + path.sep;
var templateDir = path.join(rootDir, 'template') + path.sep;

//1. 只导出文件nodeppt generate file.md
//2. 导出文件+目录 nodeppt generate ./ --all -o publish

module.exports = function(filepath, outputDir, isAll) {
filepath = fs.realpathSync(filepath);
outputDir = outputDir ? $.getDirPath(outputDir) : $.getDirPath(path.join(process.cwd(), './publish'));
isAll = !! isAll;
isAll = !!isAll;
if (isAll) {
//1.导出assets
$.copy(assetsDir, outputDir, function(filename, dir, subdir) {
Expand All @@ -28,18 +28,19 @@ module.exports = function(filepath, outputDir, isAll) {
console.log('生成结束!'.bold.green + require('path').relative('b:/', outputDir).yellow);
};

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: '.bold.red + e.toString());
}
} else {
console.log('ERROR: '.bold.red + realPath + ' is not exists!');
function parser(content, template) {



try {
var html = md_parser(content, null, null, null, {
generate: true
});
return html;
} catch (e) {
console.log('ERROR: '.bold.red + e.toString());
}

return false;
}
/**
Expand All @@ -53,13 +54,15 @@ function generate(filepath, outputDir) {
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) {

if (!subdir && /\.(?:md|markdown)$/i.test(filename)) {
var html = parser(path.join(filepath, filename), templateMd);
var content = $.readFile(path.join(filepath, filename));
var html = parser(content);
if (html) {
var title = html.match(/<title>(.*?)<\/title>/);
if (title[1]) {
Expand All @@ -71,6 +74,10 @@ 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);

html = handlerHTML(html);

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

Expand All @@ -92,11 +99,53 @@ function generate(filepath, outputDir) {
indexList = $.renderStr(templateList, data);
$.writeFile(path.join(outputDir, 'index.html'), indexList);
} else {
var content;
if ($.exists(filepath)) {
content = $.readFile(filepath);
} else {
return console.log('ERROR: '.bold.red + filepath + ' is not exists!');
}
filename = path.basename(filepath);

var html = parser(filepath);
copyLinkToOutput(content, filepath, outputDir);
var html = parser(content);
if (html) {
html = handlerHTML(html);
$.writeFile(path.join(outputDir, filename.replace(/\.(?:md|markdown)$/i, '.htm')), html);
}
}
}

//处理绝对路径的url
function handlerHTML(html) {
html = html.replace(/([src|href])=["']\//gi, '$1="./')
.replace("loadJS('/js", "loadJS('./js").replace("dir: '/js/',", "dir: './js/',");
return html;
}
//处理页面相对url,到目标文件夹
function copyLinkToOutput(content, filepath, outputDir) {
//[inline模式](/assets/box-fe-road/img/inline-mode.png)
var files = [];
content.replace(/\[.+?\]\(\s?(.*?)\s?\)/g, function(i, file) {
files.push(file);
}).replace(/href=(['"])(.+?)\1/g, function(i, q, file) {
files.push(file);
});
//解析cover
var json = md_parser.parseCover(content);

if (json.files) {
files = files.concat(json.files.split(/\s?,\s?/));
}

files.filter(function(f) {
return !/^http[s]?:\/\//.test(f);
}).forEach(function(f) {
var topath = path.join(outputDir, f);
var realpath = path.join(path.dirname(filepath), f);
if ($.exists(realpath)) {
var data = fs.readFileSync(String(realpath));
$.writeFile(topath, data);
}

});
}
5 changes: 3 additions & 2 deletions lib/md_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ marked.setOptions({
var emptyFn = function(str) {
// console.log(str);
};
var parser = function(string, callback, argvObj, queryObj) {
var parser = function(string, callback, argvObj, queryObj, commonData) {
if (typeof callback !== 'function') {
callback = emptyFn;
}
Expand All @@ -48,6 +48,7 @@ var parser = function(string, callback, argvObj, queryObj) {
//第一个是封面
var cover = contents.shift();
var json = parseCover(cover);
json = mix({}, json, commonData);
var config = require(path.join(libDir, '../package.json'));
json.nodeppt_version = config.version;
json.nodeppt_site = config.site;
Expand Down Expand Up @@ -208,7 +209,7 @@ function parse(str, note, sAttrs) {
tagStart = '<slide class="' + cls + '" ' + sAttrs + '>';
// console.log(tagStart);
}
return tagStart + note +'<section class="slide-wrapper">' + content + '</section></slide>';
return tagStart + note + '<section class="slide-wrapper">' + content + '</section></slide>';
}

function doAttr(str, isArticle, noHead) {
Expand Down
2 changes: 1 addition & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function startApp(port, dir, host, argvObj) {
return;
} else {
//优先选择pptDir的静态资源
url = url.indexOf("/") === 0 ? url.substring(1) : url;
url = url.indexOf('/') === 0 ? url.substring(1) : url;
realPath = pptDir + url;
if (!fs.existsSync(realPath)) {
realPath = staticDir + url;
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": "0.8.8",
"version": "0.8.9",
"site": "https://github.com/ksky521/nodePPT",
"author": {
"name": "Theo Wang",
Expand Down
22 changes: 17 additions & 5 deletions template/markdown.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,24 @@
<div class="progress"><span id="progress"></span></div>
<script src="/js/mixjs/lib/mix.0.3.0.min.js"></script>
<script>
var base = location.protocol + '//' + location.host + '/';
var base = location.protocol + '//' + location.host;
<%if (hasOwnProperty('generate') && generate) { %>
var path = location.pathname.split('/').filter(function(v){
return !!v;
});
path.pop();
path = path.join('/');
MixJS.config({
baseURL: [ base, path, 'js'].join('/')+'/'
});
<% }else{ %>
MixJS.config({
baseURL:base + 'js/'
baseURL: [ base, 'js'].join('/')+'/'
});
<% } %>
MixJS.use('event/broadcast', function($){
$.loadJS('/js/nodeppt.js',function(){
$.loadJS('nodeppt.js',function(){
Slide.init({
containerID: 'container',
drawBoardID: 'drawBoard',
Expand All @@ -49,7 +61,7 @@ MixJS.use('event/broadcast', function($){
progressID: 'progress',
transition: '<%- transition %>',
width: 1100,
dir: '/js/',
dir: './',
<% if (query && query.controller === 'socket'){ %>
control:{
type: 'socket',
Expand All @@ -70,7 +82,7 @@ MixJS.use('event/broadcast', function($){
<% } %>
tipID: 'tip'
});
}).loadJS('/js/highlight/hljs-0.8.js',function(){
}).loadJS('highlight/hljs-0.8.js',function(){
hljs.tabReplace = ' ';
hljs.initHighlightingOnLoad();
});
Expand Down

0 comments on commit c368594

Please sign in to comment.