@canopycanopycanopy/b-ber-lib
Advanced tools
Comparing version 0.1.0-alpha.c43f205d to 0.1.0-alpha.cb6e5f72
@@ -15,10 +15,2 @@ 'use strict'; | ||
var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray'); | ||
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2); | ||
var _entries = require('babel-runtime/core-js/object/entries'); | ||
var _entries2 = _interopRequireDefault(_entries); | ||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
@@ -64,4 +56,6 @@ | ||
var _helpers = require('./helpers'); | ||
var _Spine = require('./Spine'); | ||
var _Spine2 = _interopRequireDefault(_Spine); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -73,35 +67,61 @@ | ||
function ApplicationLoader() { | ||
var _this = this; | ||
(0, _classCallCheck3.default)(this, ApplicationLoader); | ||
var scriptPath = _path2.default.resolve(__dirname, 'package.json'); | ||
this.pkg = JSON.parse(_fsExtra2.default.readFileSync(scriptPath), 'utf8'); | ||
(0, _entries2.default)(ApplicationLoader.defaults).forEach(function (_ref) { | ||
var _ref2 = (0, _slicedToArray3.default)(_ref, 2), | ||
key = _ref2[0], | ||
val = _ref2[1]; | ||
return _this[key] = val; | ||
}); | ||
this.npmPackage = JSON.parse(_fsExtra2.default.readFileSync(scriptPath), 'utf8'); | ||
this.initialConfig = { | ||
env: process.env.NODE_ENV || 'development', | ||
src: '_project', | ||
dist: 'project', | ||
ibooks_specified_fonts: false, | ||
theme: 'b-ber-theme-serif', | ||
themes_directory: './themes', | ||
base_url: '/', | ||
remote_url: 'http://localhost:4000/', | ||
reader_url: 'http://localhost:4000/project-reader', | ||
builds: ['epub', 'mobi', 'pdf'], | ||
private: false, | ||
ignore: [], | ||
autoprefixer_options: { | ||
browsers: ['last 2 versions', '> 2%'], | ||
flexbox: 'no-2009' | ||
} | ||
}; | ||
this.config = (0, _extends3.default)({}, this.initialConfig); | ||
this.version = this.npmPackage.version; | ||
this.metadata = []; | ||
this.theme = {}; | ||
this.video = []; | ||
this.audio = []; | ||
this.buildTypes = { | ||
sample: {}, | ||
epub: {}, | ||
mobi: {}, | ||
pdf: {}, | ||
web: {} | ||
}; | ||
} | ||
(0, _createClass3.default)(ApplicationLoader, [{ | ||
key: '__version', | ||
value: function __version() { | ||
var version = this.pkg.version; | ||
this.version = version; | ||
key: '_resetConfig', | ||
value: function _resetConfig() { | ||
this.config = (0, _extends3.default)({}, this.initialConfig); | ||
} | ||
}, { | ||
key: '__config', | ||
value: function __config() { | ||
key: '_config', | ||
value: function _config() { | ||
if (!_fsExtra2.default.existsSync(_path2.default.join(cwd, 'config.yml'))) return; | ||
var baseConfig = _YamlAdaptor2.default.load(_path2.default.join(cwd, 'config.yml')); | ||
var userConfig = _YamlAdaptor2.default.load(_path2.default.join(cwd, 'config.yml')); | ||
this.config = (0, _extends3.default)({}, this.config, baseConfig); | ||
// updates initialConfig with data from config.yml | ||
this.initialConfig = (0, _extends3.default)({}, this.config, userConfig); | ||
this.config = (0, _extends3.default)({}, this.initialConfig); | ||
} | ||
}, { | ||
key: '__metadata', | ||
value: function __metadata() { | ||
key: '_metadata', | ||
value: function _metadata() { | ||
var fpath = _path2.default.join(cwd, this.config.src, 'metadata.yml'); | ||
@@ -112,8 +132,8 @@ if (!_fsExtra2.default.existsSync(fpath)) return; | ||
}, { | ||
key: '__theme', | ||
value: function __theme() { | ||
key: '_theme', | ||
value: function _theme() { | ||
var themeError = new Error('There was an error loading theme [' + this.theme + ']'); | ||
if ({}.hasOwnProperty.call(_bBerThemes2.default, this.theme)) { | ||
this.theme = _bBerThemes2.default[this.theme]; | ||
if ({}.hasOwnProperty.call(_bBerThemes2.default, this.config.theme)) { | ||
this.theme = _bBerThemes2.default[this.config.theme]; | ||
} else { | ||
@@ -145,5 +165,3 @@ if (!{}.hasOwnProperty.call(this.config, 'themes_directory')) { | ||
var userTheme = (0, _find2.default)(userThemes, { name: this.config.theme }); | ||
if (!userTheme) { | ||
_bBerLogger2.default.error('Could not find theme [' + this.config.theme + ']'); | ||
} | ||
if (!userTheme) _bBerLogger2.default.error('Could not find theme [' + this.config.theme + ']'); | ||
@@ -159,4 +177,4 @@ // exists! set it | ||
}, { | ||
key: '__media', | ||
value: function __media() { | ||
key: '_media', | ||
value: function _media() { | ||
var mediaPath = _path2.default.join(cwd, this.config.src, '_media'); | ||
@@ -183,44 +201,25 @@ try { | ||
}, { | ||
key: '__builds', | ||
value: function __builds() { | ||
key: '_builds', | ||
value: function _builds() { | ||
this.buildTypes = { | ||
sample: this.__loadBuildSettings('sample'), | ||
epub: this.__loadBuildSettings('epub'), | ||
mobi: this.__loadBuildSettings('mobi'), | ||
pdf: this.__loadBuildSettings('pdf'), | ||
web: this.__loadBuildSettings('web'), | ||
reader: this.__loadBuildSettings('reader') | ||
sample: this._loadBuildSettings('sample'), | ||
epub: this._loadBuildSettings('epub'), | ||
mobi: this._loadBuildSettings('mobi'), | ||
pdf: this._loadBuildSettings('pdf'), | ||
web: this._loadBuildSettings('web'), | ||
reader: this._loadBuildSettings('reader') | ||
}; | ||
} | ||
}, { | ||
key: '__createSpineListFromMarkdownFiles', | ||
value: function __createSpineListFromMarkdownFiles() { | ||
var markdownDir = _path2.default.join(cwd, this.config.src, '_markdown'); | ||
return _fsExtra2.default.readdirSync(markdownDir).filter(function (a) { | ||
return _path2.default.extname(a) === '.md'; | ||
}).map(function (a) { | ||
return _path2.default.basename(a, '.md'); | ||
}); | ||
} | ||
}, { | ||
key: '__loadBuildSettings', | ||
value: function __loadBuildSettings(type) { | ||
var _config = this.config, | ||
src = _config.src, | ||
dist = _config.dist; | ||
key: '_loadBuildSettings', | ||
value: function _loadBuildSettings(type) { | ||
var _config2 = this.config, | ||
src = _config2.src, | ||
dist = _config2.dist; | ||
var projectDir = _path2.default.join(cwd, src); | ||
var navigationConfigFile = _path2.default.join(cwd, src, type + '.yml'); | ||
var buildConfigFile = _path2.default.join(cwd, 'config.' + type + '.yml'); // build-specific config, i.e., loads from config.epub.yml, config.mobi.yml, etc | ||
var buildConfig = {}; | ||
var spineList = []; | ||
var spineEntries = []; | ||
var tocEntries = []; | ||
try { | ||
if (!_fsExtra2.default.existsSync(projectDir)) { | ||
throw new Error('Project directory [' + projectDir + '] does not exist'); | ||
} | ||
if (!_fsExtra2.default.existsSync(projectDir)) throw new Error('Project directory [' + projectDir + '] does not exist'); | ||
} catch (err) { | ||
@@ -231,61 +230,22 @@ // Starting a new project, noop | ||
dist: dist + '-' + type, | ||
config: buildConfig, | ||
spineList: spineList, | ||
spineEntries: spineEntries, | ||
tocEntries: tocEntries | ||
config: {}, | ||
spineList: [], | ||
spineEntries: [], | ||
tocEntries: [] | ||
}; | ||
} | ||
try { | ||
if (_fsExtra2.default.existsSync(navigationConfigFile)) { | ||
spineList = _YamlAdaptor2.default.load(navigationConfigFile) || []; | ||
} else { | ||
throw new Error('creating default file [' + type + '.yml]'); | ||
} | ||
} catch (err) { | ||
if (/creating default file/.test(err.message)) { | ||
_bBerLogger2.default.warn(err.message); | ||
_fsExtra2.default.writeFileSync(navigationConfigFile, ''); | ||
spineList = this.__createSpineListFromMarkdownFiles(); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
var spine = new _Spine2.default({ src: src, buildType: type }); | ||
tocEntries = (0, _helpers.createPageModelsFromYAML)(spineList, src); // nested navigation | ||
spineEntries = (0, _helpers.flattenNestedEntries)(tocEntries); // one-dimensional page flow | ||
var spineList = spine.create(navigationConfigFile); | ||
var tocEntries = spine.build(spineList, src); // nested navigation | ||
var spineEntries = spine.flatten(tocEntries); // one-dimensional page flow | ||
try { | ||
if (_fsExtra2.default.existsSync(buildConfigFile)) { | ||
buildConfig = _YamlAdaptor2.default.load(buildConfigFile); | ||
var ignore = []; | ||
// build-specific config. gets merged into base config during build step | ||
var config = this.config[type] ? (0, _extends3.default)({}, this.config[type]) : {}; | ||
// store in temp. var so we can map to object | ||
if (buildConfig.ignore) ignore = [].concat((0, _toConsumableArray3.default)(buildConfig.ignore)); | ||
if (ignore.length) { | ||
buildConfig.ignore = {}; | ||
ignore.forEach(function (a) { | ||
var file = _path2.default.resolve(cwd, src, a); | ||
var stats = _fsExtra2.default.statSync(file); | ||
buildConfig.ignore[file] = { | ||
type: stats.isDirectory() ? 'directory' : stats.isFile() ? 'file' : null | ||
}; | ||
}); | ||
} | ||
} else { | ||
throw new Error('[config.' + type + '.yml] not found. Using base configuration for [' + type + '] build'); | ||
} | ||
} catch (err) { | ||
if (/Using base configuration/.test(err.message)) { | ||
// noop | ||
} else { | ||
throw err; | ||
} | ||
} | ||
return { | ||
src: src, | ||
dist: dist + '-' + type, | ||
config: buildConfig, | ||
config: config, | ||
spineList: spineList, | ||
@@ -299,8 +259,7 @@ spineEntries: spineEntries, | ||
value: function load() { | ||
this.__version(); | ||
this.__config(); | ||
this.__metadata(); | ||
this.__theme(); | ||
this.__media(); | ||
this.__builds(); | ||
this._config(); | ||
this._metadata(); | ||
this._media(); | ||
this._builds(); | ||
this._theme(); | ||
} | ||
@@ -311,28 +270,2 @@ }]); | ||
ApplicationLoader.defaults = { | ||
version: '', | ||
config: { | ||
env: process.env.NODE_ENV || 'development', | ||
src: '_project', | ||
dist: 'project', | ||
ibooks_specified_fonts: false, | ||
theme: 'b-ber-theme-serif', | ||
themes_directory: './themes', | ||
base_url: '/', | ||
remote_url: 'http://localhost:4000/', | ||
reader_url: 'http://localhost:4000/project-reader', | ||
builds: ['epub', 'mobi', 'pdf'], | ||
private: false, | ||
ignore: {}, | ||
autoprefixer_options: { | ||
browsers: ['last 2 versions', '> 2%'], | ||
flexbox: 'no-2009' | ||
} | ||
}, | ||
metadata: [], | ||
video: [], | ||
audio: [], | ||
buildTypes: { sample: {}, epub: {}, mobi: {}, pdf: {}, web: {} } | ||
}; | ||
exports.default = ApplicationLoader; |
@@ -56,3 +56,3 @@ 'use strict'; | ||
if (err) return reject(err); | ||
if (stderr !== '') reject(new Error(stderr)); | ||
if (stderr !== '') process.stdout.write(stderr); | ||
if (stdout !== '') process.stdout.write(stdout); | ||
@@ -59,0 +59,0 @@ resolve(); |
42
index.js
@@ -79,11 +79,47 @@ 'use strict'; | ||
var _helpers = require('./helpers'); | ||
var _Url = require('./Url'); | ||
Object.defineProperty(exports, 'helpers', { | ||
Object.defineProperty(exports, 'Url', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_helpers).default; | ||
return _interopRequireDefault(_Url).default; | ||
} | ||
}); | ||
var _Html = require('./Html'); | ||
Object.defineProperty(exports, 'Html', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_Html).default; | ||
} | ||
}); | ||
var _SpineItem = require('./SpineItem'); | ||
Object.defineProperty(exports, 'SpineItem', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_SpineItem).default; | ||
} | ||
}); | ||
var _Spine = require('./Spine'); | ||
Object.defineProperty(exports, 'Spine', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_Spine).default; | ||
} | ||
}); | ||
var _Template = require('./Template'); | ||
Object.defineProperty(exports, 'Template', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_Template).default; | ||
} | ||
}); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
{ | ||
"name": "@canopycanopycanopy/b-ber-lib", | ||
"version": "0.1.0-alpha.c43f205d", | ||
"version": "0.1.0-alpha.cb6e5f72", | ||
"description": "", | ||
@@ -23,9 +23,8 @@ "main": "index.js", | ||
"mock-fs": "^4.4.2", | ||
"rimraf": "^2.6.2", | ||
"vinyl": "^2.1.0" | ||
"rimraf": "^2.6.2" | ||
}, | ||
"dependencies": { | ||
"@canopycanopycanopy/b-ber-logger": "canary", | ||
"@canopycanopycanopy/b-ber-shapes": "canary", | ||
"@canopycanopycanopy/b-ber-themes": "canary", | ||
"@canopycanopycanopy/b-ber-logger": "^0.1.0-alpha.cb6e5f72", | ||
"@canopycanopycanopy/b-ber-shapes": "^0.1.0-alpha.cb6e5f72", | ||
"@canopycanopycanopy/b-ber-themes": "^0.1.0-alpha.cb6e5f72", | ||
"babel-runtime": "^6.26.0", | ||
@@ -36,6 +35,8 @@ "command-exists": "^1.2.2", | ||
"js-yaml": "^3.11.0", | ||
"layouts": "^3.0.1", | ||
"lodash": "^4.17.5", | ||
"mime-types": "^2.1.18", | ||
"vinyl": "^2.2.0", | ||
"yargs": "^11.0.0" | ||
} | ||
} |
18
State.js
@@ -83,3 +83,4 @@ 'use strict'; | ||
_this.__updateConfig(); | ||
_this._resetEntries(); | ||
_this._resetConfig(); | ||
_this.load(); | ||
@@ -92,3 +93,4 @@ return _this; | ||
value: function reset() { | ||
this.__updateConfig(); | ||
this._resetEntries(); | ||
this._resetConfig(); | ||
@@ -99,4 +101,4 @@ this.templates = { dynamicPageTmpl: dynamicPageTmpl, dynamicPageHead: dynamicPageHead, dynamicPageTail: dynamicPageTail }; | ||
}, { | ||
key: '__updateConfig', | ||
value: function __updateConfig() { | ||
key: '_resetEntries', | ||
value: function _resetEntries() { | ||
var _this2 = this; | ||
@@ -241,10 +243,2 @@ | ||
}, { | ||
key: 'theme', | ||
get: function get() { | ||
return this.config.theme; | ||
}, | ||
set: function set(val) { | ||
this.config.theme = val; | ||
} | ||
}, { | ||
key: 'env', | ||
@@ -251,0 +245,0 @@ get: function get() { |
135
theme.js
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.setTheme = exports.theme = undefined; | ||
exports.theme = undefined; | ||
@@ -29,6 +29,2 @@ var _regenerator = require('babel-runtime/regenerator'); | ||
var _yargs = require('yargs'); | ||
var _yargs2 = _interopRequireDefault(_yargs); | ||
var _bBerThemes = require('@canopycanopycanopy/b-ber-themes'); | ||
@@ -42,6 +38,2 @@ | ||
var _YamlAdaptor = require('./YamlAdaptor'); | ||
var _YamlAdaptor2 = _interopRequireDefault(_YamlAdaptor); | ||
var _utils = require('./utils'); | ||
@@ -68,2 +60,11 @@ | ||
try { | ||
if (!_fsExtra2.default.existsSync(_path2.default.join(process.cwd(), config.themes_directory))) { | ||
throw new Error('Themes directory [' + config.themes_directory + '] does not exist.'); | ||
} | ||
} catch (err) { | ||
_bBerLogger2.default.warn(err); | ||
return resolve({ names: names, themes: userThemes }); | ||
} | ||
_fsExtra2.default.readdirSync(_path2.default.join(cwd, config.themes_directory)).forEach(function (a) { | ||
@@ -99,84 +100,12 @@ var modulePath = _path2.default.resolve(cwd, config.themes_directory, a); | ||
return themeList.reduce(function (acc, curr) { | ||
// eslint-disable-line prefer-template | ||
var icon = currentTheme && currentTheme === curr ? '✓' : '○'; | ||
return acc.concat(' ' + icon + ' ' + curr + '\n'); | ||
}, '\n').slice(0, -1); | ||
}, '\n').slice(0, -1) + '\n'; | ||
}; | ||
function setTheme(themeName, themeList, userThemes, cwd) { | ||
return new _promise2.default(function (resolve) { | ||
if (themeList.indexOf(themeName) < 0) { | ||
_bBerLogger2.default.error('Could not find theme matching [' + themeName + '].'); | ||
_bBerLogger2.default.info('Select one from the list of available themes:'); | ||
_bBerLogger2.default.info(printThemeList(themeList)); | ||
return; | ||
} | ||
var configPath = _path2.default.join(cwd, 'config.yml'); | ||
var configObj = _YamlAdaptor2.default.load(configPath); | ||
var promises = []; | ||
// save the new theme name to the config.yml | ||
configObj.theme = themeName; | ||
// write the updated config file | ||
promises.push(new _promise2.default(function (resolve) { | ||
return _fsExtra2.default.writeFile(configPath, _YamlAdaptor2.default.dump(configObj), function (err) { | ||
if (err) throw err; | ||
_bBerLogger2.default.info('Successfully set theme theme to [' + themeName + ']'); | ||
return resolve(); | ||
}); | ||
})); | ||
// add a theme dir with the same name to the src dir, copy over | ||
// the `settings` file, and create an overrides file | ||
promises.push(new _promise2.default(function (resolve) { | ||
var themeObject = {}.hasOwnProperty.call(_bBerThemes2.default, themeName) ? _bBerThemes2.default[themeName] : userThemes.themes[themeName]; | ||
var settingsFileName = '_settings.scss'; | ||
var overridesFileName = '_overrides.scss'; | ||
var settingsOutputPath = _path2.default.join(cwd, configObj.src || '_project', '_stylesheets', themeName); | ||
var settingsInputFile = _path2.default.join(_path2.default.dirname(themeObject.entry), settingsFileName); | ||
var settingsOutputFile = _path2.default.join(settingsOutputPath, settingsFileName); | ||
var overridesOutputFile = _path2.default.join(settingsOutputPath, overridesFileName); | ||
try { | ||
if (!_fsExtra2.default.existsSync(settingsOutputPath)) { | ||
_fsExtra2.default.mkdirsSync(settingsOutputPath); | ||
} | ||
} catch (err) { | ||
_bBerLogger2.default.error(err); | ||
process.exit(1); | ||
} | ||
try { | ||
if (_fsExtra2.default.existsSync(settingsOutputFile)) { | ||
throw new Error('[' + settingsOutputFile + '] already exists'); | ||
} else { | ||
_fsExtra2.default.copySync(settingsInputFile, settingsOutputFile, {}); | ||
_bBerLogger2.default.info('create [' + settingsOutputFile + ']'); | ||
} | ||
if (_fsExtra2.default.existsSync(overridesOutputFile)) { | ||
throw new Error('[' + overridesOutputFile + '] already exists'); | ||
} else { | ||
_fsExtra2.default.writeFileSync(overridesOutputFile, ''); | ||
} | ||
} catch (err) { | ||
if (/already exists/.test(err.message)) { | ||
_bBerLogger2.default.info(err.message); | ||
} else { | ||
_bBerLogger2.default.error(err); | ||
} | ||
} | ||
resolve(); | ||
})); | ||
return _promise2.default.all(promises).then(resolve); | ||
}); | ||
} | ||
var theme = function theme(_) { | ||
var theme = function theme(args) { | ||
return new _promise2.default(function () { | ||
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(resolve) { | ||
var cwd, config, themeList, userThemes, i, currentTheme, themeName; | ||
var config, themeList, userThemes, i, currentTheme; | ||
return _regenerator2.default.wrap(function _callee$(_context) { | ||
@@ -186,6 +115,2 @@ while (1) { | ||
case 0: | ||
_bBerLogger2.default.logLevel = 4; | ||
cwd = process.cwd(); | ||
config = _State2.default.config; | ||
@@ -200,8 +125,9 @@ themeList = []; | ||
// get user themes dir, if any, and merge with built-in b-ber themes | ||
_context.next = 7; | ||
_context.next = 5; | ||
return getUserDefinedThemes(); | ||
case 7: | ||
case 5: | ||
userThemes = _context.sent; | ||
for (i = 0; i < userThemes.names.length; i++) { | ||
@@ -211,26 +137,14 @@ themeList.push(userThemes.names[i]); | ||
currentTheme = config.theme && config.theme.name ? config.theme.name : ''; | ||
currentTheme = config.theme && config.theme ? config.theme : ''; | ||
if (!_yargs2.default.argv.list) { | ||
_context.next = 14; | ||
if (!args.list) { | ||
_context.next = 12; | ||
break; | ||
} | ||
_bBerLogger2.default.info('The following themes are available:'); | ||
_bBerLogger2.default.info(printThemeList(themeList, currentTheme)); | ||
_bBerLogger2.default.notice('The following themes are available:'); | ||
console.log(printThemeList(themeList, currentTheme)); | ||
return _context.abrupt('return', resolve()); | ||
case 14: | ||
if (!_yargs2.default.argv.set) { | ||
_context.next = 17; | ||
break; | ||
} | ||
themeName = _yargs2.default.argv.set; | ||
return _context.abrupt('return', setTheme(themeName, themeList, userThemes, cwd).then(resolve)); | ||
case 17: | ||
return _context.abrupt('return', resolve(_yargs2.default.showHelp())); | ||
case 18: | ||
case 12: | ||
case 'end': | ||
@@ -249,3 +163,2 @@ return _context.stop(); | ||
exports.theme = theme; | ||
exports.setTheme = setTheme; | ||
exports.theme = theme; // eslint-disable-line import/prefer-default-export |
@@ -6,20 +6,4 @@ 'use strict'; | ||
}); | ||
exports.getBookMetadata = exports.trimSlashes = exports.encodeQueryString = exports.ensureDecoded = exports.getTitleOrName = exports.parseHTMLFiles = exports.getPagebreakAttribute = exports.flattenSpineFromYAML = exports.nestedContentToYAML = exports.modelFromString = exports.modelFromObject = exports.spineModel = exports.escapeHTML = exports.passThrough = exports.htmlComment = exports.promiseAll = exports.forOf = exports.getImageOrientation = exports.hrtimeformat = exports.fileId = exports.opsPath = undefined; | ||
exports.getBookMetadata = exports.getTitleOrName = exports.forOf = exports.getImageOrientation = exports.fileId = exports.opsPath = undefined; | ||
var _defineProperty2 = require('babel-runtime/helpers/defineProperty'); | ||
var _defineProperty3 = _interopRequireDefault(_defineProperty2); | ||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
var _keys2 = _interopRequireDefault(_keys); | ||
var _extends2 = require('babel-runtime/helpers/extends'); | ||
var _extends3 = _interopRequireDefault(_extends2); | ||
var _promise = require('babel-runtime/core-js/promise'); | ||
var _promise2 = _interopRequireDefault(_promise); | ||
var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray'); | ||
@@ -33,6 +17,2 @@ | ||
var _fsExtra = require('fs-extra'); | ||
var _fsExtra2 = _interopRequireDefault(_fsExtra); | ||
var _path = require('path'); | ||
@@ -42,4 +22,6 @@ | ||
var _lodash = require('lodash'); | ||
var _find = require('lodash/find'); | ||
var _find2 = _interopRequireDefault(_find); | ||
var _bBerLogger = require('@canopycanopycanopy/b-ber-logger'); | ||
@@ -52,8 +34,2 @@ | ||
/** | ||
* @module utils | ||
*/ | ||
var cwd = process.cwd(); | ||
/** | ||
* Get a file's relative path to the OPS | ||
@@ -75,2 +51,6 @@ * @param {String} fpath File path | ||
// https://www.w3.org/TR/xml-names/#Conformance | ||
/** | ||
* @module utils | ||
*/ | ||
var fileId = function fileId(str) { | ||
@@ -81,12 +61,2 @@ return '_' + str.replace(/[^a-zA-Z0-9_]/g, '_'); | ||
/** | ||
* [description] | ||
* @param {Array} a [description] | ||
* @return {String} | ||
*/ | ||
var hrtimeformat = function hrtimeformat(a) { | ||
var s = a[0] * 1000 + a[1] / 1000000; | ||
return String(s).slice(0, -3) + 'ms'; | ||
}; | ||
/** | ||
* Determine an image's orientation | ||
@@ -125,179 +95,2 @@ * @param {Number} w Image width | ||
/** | ||
* [description] | ||
* @param {Array<Object<Promise>>} promiseArray [description] | ||
* @return {Object<Promise|Error>} | ||
*/ | ||
var promiseAll = function promiseAll(promiseArray) { | ||
return new _promise2.default(function (resolve) { | ||
return _promise2.default.all(promiseArray).then(resolve); | ||
}); | ||
}; | ||
var htmlComment = function htmlComment(str) { | ||
return '\n<!-- ' + str + ' -->\n'; | ||
}; | ||
var passThrough = function passThrough(args) { | ||
return args; | ||
}; | ||
var escapeHTML = function escapeHTML(str) { | ||
var map = { | ||
'&': '&', | ||
'<': '<', | ||
'>': '>', | ||
'"': '"', | ||
"'": ''' | ||
}; | ||
return str.replace(/[&<>"']/g, function (m) { | ||
return map[m]; | ||
}); | ||
}; | ||
// for generating page models used in navigation | ||
var spineModel = function spineModel() { | ||
return { | ||
relativePath: '', | ||
absolutePath: '', | ||
extension: '', | ||
fileName: '', | ||
name: '', | ||
// baseName: '', | ||
remotePath: '', | ||
type: '', | ||
title: '', | ||
// pageOrder: -1, | ||
linear: true, | ||
in_toc: true, | ||
nodes: [] | ||
}; | ||
}; | ||
/** | ||
* [description] | ||
* @param {String} _str File basename with extension | ||
* @param {String} _src Current `src` directory name | ||
* @return {Object} | ||
*/ | ||
var modelFromString = function modelFromString(_str, _src) { | ||
var str = String(_str); | ||
var pathFragment = /^(toc\.x?html|nav\.ncx)$/i.test(str) ? '' : 'text'; // TODO: clean this up | ||
var relativePath = _path2.default.join(pathFragment, str); // relative to OPS | ||
var absolutePath = _path2.default.join(cwd, _src, relativePath); | ||
var extension = _path2.default.extname(absolutePath); | ||
var fileName = _path2.default.basename(absolutePath); | ||
var name = _path2.default.basename(absolutePath, extension); | ||
// const baseName = path.basename(absolutePath, extension) | ||
var remotePath = ''; // TODO: add remote URL where applicable | ||
return (0, _extends3.default)({}, spineModel(), { | ||
relativePath: relativePath, | ||
absolutePath: absolutePath, | ||
extension: extension, | ||
fileName: fileName, | ||
name: name, | ||
remotePath: remotePath | ||
}); | ||
}; | ||
var modelFromObject = function modelFromObject(_obj, _src) { | ||
var _obj$Object$keys$ = _obj[(0, _keys2.default)(_obj)[0]], | ||
in_toc = _obj$Object$keys$.in_toc, | ||
linear = _obj$Object$keys$.linear; // eslint-disable-line camelcase | ||
var str = (0, _keys2.default)(_obj)[0]; | ||
var model = modelFromString(str, _src); | ||
return (0, _extends3.default)({}, model, { in_toc: in_toc, linear: linear }); | ||
}; | ||
var nestedContentToYAML = function nestedContentToYAML(arr) { | ||
var result = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | ||
arr.forEach(function (_) { | ||
var model = {}; | ||
// TODO: check for custom attrs somewhere else. | ||
if (_.linear === false || _.in_toc === false) { | ||
if (_.in_toc === false) model.in_toc = false; | ||
if (_.linear === false) model.linear = false; | ||
result.push((0, _defineProperty3.default)({}, _.fileName, model)); | ||
} else { | ||
result.push(_.fileName); | ||
if (_.nodes && _.nodes.length) { | ||
model.section = []; | ||
result.push(model); | ||
nestedContentToYAML(_.nodes, model.section); | ||
} | ||
} | ||
}); | ||
return result; | ||
}; | ||
var flattenSpineFromYAML = function flattenSpineFromYAML(arr) { | ||
return arr.reduce(function (acc, curr) { | ||
if ((0, _lodash.isPlainObject)(curr)) { | ||
if ((0, _keys2.default)(curr)[0] === 'section') { | ||
return acc.concat(flattenSpineFromYAML(curr.section)); | ||
} | ||
return acc.concat((0, _keys2.default)(curr)[0]); | ||
} | ||
return acc.concat(curr); | ||
}, []); | ||
}; | ||
function getPagebreakAttribute(_ref3) { | ||
var pagebreak = _ref3.pagebreak; | ||
if (!pagebreak || typeof pagebreak !== 'string') return ''; | ||
switch (pagebreak) { | ||
case 'before': | ||
case 'after': | ||
return ' style="page-break-' + pagebreak + ':always;"'; | ||
case 'both': | ||
return ' style="page-break-before:always; page-break-after:always;"'; | ||
default: | ||
return ''; | ||
} | ||
} | ||
// used by xml and pdf tasks | ||
// @param files Array List of `fileName` properties from the state.manifest object | ||
// @param parser Object Instance of the Parser/Printer class | ||
// @param dist String Absolute project path | ||
// @return Promise|Error Promise (An XML string) | ||
function parseHTMLFiles(files, parser, dist) { | ||
return new _promise2.default(function (resolve) { | ||
var dirname = _path2.default.join(dist, 'OPS', 'text'); | ||
var text = files.map(function (_, index, arr) { | ||
var data = void 0; | ||
var fname = (0, _lodash.isPlainObject)(_) ? (0, _keys2.default)(_)[0] : typeof _ === 'string' ? _ : null; | ||
var ext = '.xhtml'; | ||
if (!fname) return null; | ||
var fpath = _path2.default.join(dirname, '' + fname + ext); | ||
try { | ||
if (!_fsExtra2.default.existsSync(fpath)) return null; | ||
data = _fsExtra2.default.readFileSync(fpath, 'utf8'); | ||
} catch (err) { | ||
return _bBerLogger2.default.warn(err.message); | ||
} | ||
return parser.parse(data, index, arr); | ||
}).filter(Boolean); | ||
_promise2.default.all(text).catch(function (err) { | ||
return _bBerLogger2.default.error(err); | ||
}).then(function (docs) { | ||
return resolve(docs.join('\n')); | ||
}); | ||
}); | ||
} | ||
// TODO: the whole figures/generated pages/user-configurable YAML thing should | ||
@@ -317,28 +110,4 @@ // be worked out better. one reason is below, where we need the title of a | ||
var ensureDecoded = function ensureDecoded(str) { | ||
var str_ = str; | ||
while (decodeURIComponent(str_) !== str_) { | ||
str_ = decodeURIComponent(str_); | ||
}return str_; | ||
}; | ||
var encodeQueryString = function encodeQueryString(url) { | ||
var url_ = url.split('?'); | ||
var loc = url_[0]; | ||
var qs = url_[1]; | ||
if (!qs) return loc; | ||
qs = ensureDecoded(qs); | ||
qs = encodeURIComponent(qs); | ||
url_ = loc + '?' + qs; | ||
return url_; | ||
}; | ||
var trimSlashes = function trimSlashes(url) { | ||
return url.replace(/(^\/+|\/+$)/, ''); | ||
}; | ||
var getBookMetadata = function getBookMetadata(term, state) { | ||
var entry = (0, _lodash.find)(state.metadata, { term: term }); | ||
var entry = (0, _find2.default)(state.metadata, { term: term }); | ||
if (entry && entry.value) return entry.value; | ||
@@ -351,20 +120,5 @@ _bBerLogger2.default.warn('Could not find metadata value for ' + term); | ||
exports.fileId = fileId; | ||
exports.hrtimeformat = hrtimeformat; | ||
exports.getImageOrientation = getImageOrientation; | ||
exports.forOf = forOf; | ||
exports.promiseAll = promiseAll; | ||
exports.htmlComment = htmlComment; | ||
exports.passThrough = passThrough; | ||
exports.escapeHTML = escapeHTML; | ||
exports.spineModel = spineModel; | ||
exports.modelFromObject = modelFromObject; | ||
exports.modelFromString = modelFromString; | ||
exports.nestedContentToYAML = nestedContentToYAML; | ||
exports.flattenSpineFromYAML = flattenSpineFromYAML; | ||
exports.getPagebreakAttribute = getPagebreakAttribute; | ||
exports.parseHTMLFiles = parseHTMLFiles; | ||
exports.getTitleOrName = getTitleOrName; | ||
exports.ensureDecoded = ensureDecoded; | ||
exports.encodeQueryString = encodeQueryString; | ||
exports.trimSlashes = trimSlashes; | ||
exports.getBookMetadata = getBookMetadata; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3
17
12
75209
13
1731
+ Addedlayouts@^3.0.1
+ Addedvinyl@^2.2.0
+ Added@canopycanopycanopy/b-ber-logger@0.1.9(transitive)
+ Added@canopycanopycanopy/b-ber-shapes@0.1.9(transitive)
+ Added@canopycanopycanopy/b-ber-themes@0.1.9(transitive)
+ Addedclone@2.1.2(transitive)
+ Addedclone-buffer@1.0.0(transitive)
+ Addedclone-stats@1.0.0(transitive)
+ Addedcloneable-readable@1.1.3(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedget-view@2.0.0(transitive)
+ Addedlayouts@3.0.2(transitive)
+ Addedpath-ends-with@2.0.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedreplace-ext@1.0.1(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedvinyl@2.2.1(transitive)
- Removed@canopycanopycanopy/b-ber-logger@3.0.8-canary.0(transitive)
- Removed@canopycanopycanopy/b-ber-shapes@0.1.7-alpha.30(transitive)
- Removed@canopycanopycanopy/b-ber-themes@1.0.6-alpha.7(transitive)
- Removedchownr@2.0.0(transitive)
- Removedfs-extra@8.1.0(transitive)
- Removedfs-minipass@2.1.0(transitive)
- Removedlodash.has@4.5.2(transitive)
- Removedlodash.isplainobject@4.0.6(transitive)
- Removedlodash.isundefined@3.0.1(transitive)
- Removedminipass@3.3.65.0.0(transitive)
- Removedminizlib@2.1.2(transitive)
- Removedmkdirp@1.0.4(transitive)
- Removedtar@6.2.1(transitive)
- Removedyallist@4.0.0(transitive)
Updated@canopycanopycanopy/b-ber-logger@^0.1.0-alpha.cb6e5f72
Updated@canopycanopycanopy/b-ber-shapes@^0.1.0-alpha.cb6e5f72
Updated@canopycanopycanopy/b-ber-themes@^0.1.0-alpha.cb6e5f72