simple-confluence-to-markdown
Version:
Convert Confluence Pages to Markdown
150 lines (127 loc) • 5 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var App;
App = (function() {
App.outputTypesAdd = ['markdown_github', 'blank_before_header'];
App.outputTypesRemove = [];
App.extraOptions = ['--markdown-headings=atx'];
/**
* @param {fs} _fs Required lib
* @param {sync-exec} _exec Required lib
* @param {path} _path Required lib
* @param {mkdirp} _mkdirp Required lib
* @param {Utils} utils My lib
* @param {Formatter} formatter My lib
* @param {PageFactory} pageFactory My lib
* @param {Logger} logger My lib
*/
function App(_fs, _exec, _path, _mkdirp, utils, formatter, pageFactory, logger) {
var types, typesAdd, typesRemove;
this._fs = _fs;
this._exec = _exec;
this._path = _path;
this._mkdirp = _mkdirp;
this.utils = utils;
this.formatter = formatter;
this.pageFactory = pageFactory;
this.logger = logger;
typesAdd = App.outputTypesAdd.join('+');
typesRemove = App.outputTypesRemove.join('-');
typesRemove = typesRemove ? '-' + typesRemove : '';
types = typesAdd + typesRemove;
this.pandocOptions = [types ? '-t ' + types : '', App.extraOptions.join(' ')].join(' ');
}
/**
* Converts HTML files to MD files.
* @param {string} dirIn Directory to go through
* @param {string} dirOut Directory where to place converted MD files
*/
App.prototype.convert = function(dirIn, dirOut) {
var filePath, filePaths, fn, i, indexHtmlFiles, len, page, pages;
filePaths = this.utils.readDirRecursive(dirIn);
pages = (function() {
var i, len, results;
results = [];
for (i = 0, len = filePaths.length; i < len; i++) {
filePath = filePaths[i];
if (filePath.endsWith('.html')) {
results.push(this.pageFactory.create(filePath));
}
}
return results;
}).call(this);
indexHtmlFiles = [];
fn = (function(_this) {
return function(page) {
if (page.fileName === 'index.html') {
indexHtmlFiles.push(_this._path.join(page.space, 'index'));
}
return _this.convertPage(page, dirIn, dirOut, pages);
};
})(this);
for (i = 0, len = pages.length; i < len; i++) {
page = pages[i];
fn(page);
}
if (!this.utils.isFile(dirIn)) {
this.writeGlobalIndexFile(indexHtmlFiles, dirOut);
}
return this.logger.info('Conversion done');
};
/**
* Converts HTML file at given path to MD.
* @param {Page} page Page entity of HTML file
* @param {string} dirOut Directory where to place converted MD files
*/
App.prototype.convertPage = function(page, dirIn, dirOut, pages) {
var fullOutFileName, text;
this.logger.info('Parsing ... ' + page.path);
text = page.getTextToConvert(pages);
fullOutFileName = this._path.join(dirOut, page.space, page.fileNameNew);
this.logger.info('Making Markdown ... ' + fullOutFileName);
this.writeMarkdownFile(text, fullOutFileName);
this.utils.copyAssets(this.utils.getDirname(page.path), this.utils.getDirname(fullOutFileName));
return this.logger.info('Done\n');
};
/**
* @param {string} text Makdown content of file
* @param {string} fullOutFileName Absolute path to resulting file
* @return {string} Absolute path to created MD file
*/
App.prototype.writeMarkdownFile = function(text, fullOutFileName) {
var command, fullOutDirName, out, tempInputFile;
fullOutDirName = this.utils.getDirname(fullOutFileName);
this._mkdirp.sync(fullOutDirName, function(error) {
if (error) {
return this.logger.error('Unable to create directory #{fullOutDirName}');
}
});
tempInputFile = fullOutFileName + '~';
this._fs.writeFileSync(tempInputFile, text, {
flag: 'w'
});
command = 'pandoc -f html ' + this.pandocOptions + ' -o "' + fullOutFileName + '"' + ' "' + tempInputFile + '"';
this.logger.error('Running command: ' + command);
out = this._exec(command, {
cwd: fullOutDirName
});
if (out.status > 0) {
this.logger.error(out.stderr);
}
return this._fs.unlinkSync(tempInputFile); // 删除缓存文件
};
/**
* @param {array} indexHtmlFiles Relative paths of index.html files from all parsed Confluence spaces
* @param {string} dirOut Absolute path to a directory where to place converted MD files
*/
App.prototype.writeGlobalIndexFile = function(indexHtmlFiles, dirOut) {
var $content, globalIndex, text;
globalIndex = this._path.join(dirOut, 'index.md');
$content = this.formatter.createListFromArray(indexHtmlFiles);
text = this.formatter.getHtml($content);
return this.writeMarkdownFile(text, globalIndex);
};
return App;
})();
module.exports = App;
}).call(this);