UNPKG

simple-confluence-to-markdown

Version:
182 lines (155 loc) 5.3 kB
// Generated by CoffeeScript 1.12.7 (function() { var Utils; Utils = (function() { /** * @param {fs} _fs Required lib * @param {path} _path Required lib * @param {ncp} _ncp Required lib * @param {Logger} logger My lib */ function Utils(_fs, _path, _ncp, logger) { this._fs = _fs; this._path = _path; this._ncp = _ncp; this.logger = logger; } Utils.prototype.mkdirSync = function(path) { var e; this.logger.debug("Making dir: " + path); try { return this._fs.mkdirSync(path); } catch (error) { e = error; if (e.code !== 'EEXIST') { throw e; } } }; /** * Checks if given file exists and is a file. * @param {string} filePath Absolute/relative path to a file * @param {string|void} cwd Current working directory against which the path is built. * @return {bool} */ Utils.prototype.isFile = function(filePath, cwd) { var pathFull, stat; if (cwd == null) { cwd = ''; } pathFull = this._path.resolve(cwd, filePath); return this._fs.existsSync(pathFull) && ((stat = this._fs.statSync(pathFull)) && stat.isFile()); }; /** * Checks if given directory exists and is a directory. * @param {string} dirPath Absolute/relative path to a directory * @param {string|void} cwd Current working directory against which the path is built. * @return {bool} */ Utils.prototype.isDir = function(dirPath, cwd) { var pathFull, stat; if (cwd == null) { cwd = ''; } pathFull = this._path.resolve(cwd, dirPath); return this._fs.existsSync(pathFull) && ((stat = this._fs.statSync(pathFull)) && stat.isDirectory()); }; /** * Return list of files (and directories) in a given directory. * @param {string} path Absolute path to a directory. * @param {bool|void} filesOnly Whether to return only files. * @return {array} */ Utils.prototype.readDirRecursive = function(path, filesOnly) { var fileName, fullPath, fullPaths, i, len, ref; if (filesOnly == null) { filesOnly = true; } fullPaths = []; if (this.isFile(path)) { return [path]; } ref = this._fs.readdirSync(path); for (i = 0, len = ref.length; i < len; i++) { fileName = ref[i]; fullPath = this._path.join(path, fileName); if (this.isFile(fullPath)) { fullPaths.push(fullPath); } else { if (!filesOnly) { fullPaths.push(fullPath); } fullPaths.push.apply(fullPaths, this.readDirRecursive(fullPath, filesOnly)); } } return fullPaths; }; /** * Sanitize a filename, replacing invalid characters with an underscore * @param (string) filename Filename and extension, but not directory component * @return (string) */ Utils.prototype.sanitizeFilename = function(name) { return name.replace(/[\s<>()\[\]{}:;'`"\/\\|?\*~!@#$%^&,]/g, '_').replace(/__+/g, '_'); }; Utils.prototype.getBasename = function(path, extension) { return this._path.basename(path, extension); }; Utils.prototype.getDirname = function(path) { return this._path.dirname(path); }; Utils.prototype.readFile = function(path) { return this._fs.readFileSync(path, 'utf8'); }; Utils.prototype.getLinkToNewPageFile = function(href, pages, space) { var baseName, fileName, i, j, len, len1, matches, page, pageId; fileName = this.getBasename(href); if (fileName.endsWith('.html')) { baseName = fileName.replace('.html', ''); for (i = 0, len = pages.length; i < len; i++) { page = pages[i]; if (baseName === page.fileBaseName) { if (space === page.space) { return page.fileNameNew.replace('.md', ''); } else { return page.spacePath.replace('.md', ''); } } } } else if (matches = href.match(/.*pageId=(\d+).*/)) { pageId = matches[1]; for (j = 0, len1 = pages.length; j < len1; j++) { page = pages[j]; if (pageId === page.fileBaseName) { return page.spacePath.replace('.md', ''); } } } else { return void 0; } }; /** * Copies assets directories to path with MD files * @param {string} fullInPath Absolute path to file to convert * @param {string} dirOut Directory where to place converted MD files */ Utils.prototype.copyAssets = function(pathWithHtmlFiles, dirOut) { var asset, assetsDirIn, assetsDirOut, i, len, ref, results; ref = ['images', 'attachments']; results = []; for (i = 0, len = ref.length; i < len; i++) { asset = ref[i]; assetsDirIn = this._path.join(pathWithHtmlFiles, asset); assetsDirOut = this._path.join(dirOut, asset); if (this.isDir(assetsDirIn)) { results.push(this._ncp(assetsDirIn, assetsDirOut)); } else { results.push(void 0); } } return results; }; return Utils; })(); module.exports = Utils; }).call(this);