Skip to content

Commit

Permalink
Merge pull request ksky521#143 from HakurouKen/master
Browse files Browse the repository at this point in the history
添加 asset-path 选项
  • Loading branch information
ksky521 authored Sep 27, 2016
2 parents b377c66 + 648f766 commit 5040361
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
21 changes: 20 additions & 1 deletion bin/nodeppt
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,52 @@ program
console.log();
});

function assertPathWarn(){
console.warn('The asset-path params will be ignored when output all files.');
}

program
.command('generate')
.alias('release')
.usage('[file_path] [save_path]')
.description('export html file')
.option('-a, --all [false]', 'output all style(include js,css) file', false)
.option('-p, --asset-path [path]','set the relative path of html to assets(in website), ignored when -a is specified','.')
.action(function (cmd, output, options) {
var filename = '';
var shouldAll = false;
var path = '';
if (typeof output !== 'string') {
options = output;
output = undefined;
}

if (typeof cmd === 'string') {
filename = cmd;
shouldAll = options.all;
if (options.assetPath && shouldAll ) {
assertPathWarn();
} else {
path = options.assetPath;
}
} else if (typeof cmd === 'object') {
shouldAll = cmd.all;
output = cmd.output;
if (cmd.assetPath && shouldAll ) {
assertPathWarn();
} else {
path = cmd.assetPath;
}
}
nodePPT.generate(filename, output, shouldAll, '');

nodePPT.generate(filename, output, shouldAll, path || '');
})
.on('--help', function () {
console.log(' Examples:');
console.log();
console.log(' nodeppt generate /markdown/path.md /output/path');
console.log(' nodeppt generate /markdown/path.md /output/path -a');
console.log(' nodeppt generate /markdown/path.md /output/path -p ../lib/nodeppt');
console.log();
});

Expand Down
18 changes: 11 additions & 7 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var nodeModules = path.normalize(path.join(__dirname, '../node_modules')) + path
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) {
Expand All @@ -31,11 +30,11 @@ module.exports = function (filepath, outputDir, isAll, rDir) {
console.log('生成结束!'.bold.green + require('path').relative('b:/', outputDir).yellow);
};

function parser(content, template) {
function parser(content,config) {
try {
var html = md_parser(content, null, null, null, {
generate: true
});
var html = md_parser(content, null, null, null, $.mix({
generate: true,
},config));
return html;
} catch (e) {
console.log('ERROR: '.bold.red + e.toString());
Expand All @@ -61,7 +60,9 @@ function generate(filepath, outputDir, rDir) {
$.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);
var html = parser(content,{
assetPath: rDir
});
if (html) {
var title = html.match(/<title>(.*?)<\/title>/);
if (title && title[1]) {
Expand Down Expand Up @@ -103,7 +104,9 @@ function generate(filepath, outputDir, rDir) {
}
filename = path.basename(filepath);
copyLinkToOutput(content, filepath, outputDir, rDir);
var html = parser(content);
var html = parser(content,{
assetPath: rDir
});
if (html) {
html = handlerHTML(html, rDir);
$.writeFile(path.join(outputDir, filename.replace(/\.(?:md|markdown)$/i, '.htm')), html);
Expand All @@ -113,6 +116,7 @@ function generate(filepath, outputDir, rDir) {

//处理绝对路径的url
function handlerHTML(html, rDir) {
rDir = rDir.replace(/\/$/,'');
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//')
Expand Down
6 changes: 6 additions & 0 deletions template/markdown.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<button class="fa fa-bars" id="_btn-bar" data-toggle="fa fa-close"></button>
</div>
<script src="/js/mixjs/lib/mix.0.3.0.min.js"></script>
<script>
var ASSET_RELATIVE_DIR = "<%= hasOwnProperty('assetPath') && assetPath || '' %>";
</script>
<script>
var base = location.protocol + '//' + location.host;
<%if (hasOwnProperty('generate') && generate) { %>
Expand All @@ -59,6 +62,9 @@ var path = (location.pathname + '#').split('/').filter(function(v){
});
path.pop();
path = path.join('/');
if (typeof ASSET_RELATIVE_DIR === 'string') {
path = path + '/' + ASSET_RELATIVE_DIR;
}
MixJS.config({
baseURL: [ base, path, 'js'].join('/')+'/'
});
Expand Down

0 comments on commit 5040361

Please sign in to comment.