1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-05 09:56:22 +00:00
HackMyResume/dist/core/fresh-theme.js

264 lines
8.5 KiB
JavaScript
Raw Normal View History

2016-01-27 10:29:26 +00:00
(function() {
2018-02-12 05:05:29 +00:00
/**
Definition of the FRESHTheme class.
@module core/fresh-theme
@license MIT. See LICENSE.md for details.
*/
/* Load and parse theme source files. */
/* Load a single theme file. */
/* Return a more friendly name for certain formats. */
2016-02-11 16:48:44 +00:00
var EXTEND, FRESHTheme, FS, HMSTATUS, PATH, READFILES, _, _load, _loadOne, friendlyName, loadSafeJson, moment, parsePath, pathExists, validator;
2016-01-27 10:29:26 +00:00
FS = require('fs');
validator = require('is-my-json-valid');
_ = require('underscore');
PATH = require('path');
parsePath = require('parse-filepath');
pathExists = require('path-exists').sync;
EXTEND = require('extend');
HMSTATUS = require('./status-codes');
moment = require('moment');
loadSafeJson = require('../utils/safe-json-loader');
READFILES = require('recursive-readdir-sync');
2016-02-11 16:48:44 +00:00
/* A representation of a FRESH theme asset.
2018-02-12 05:05:29 +00:00
@class FRESHTheme */
FRESHTheme = class FRESHTheme {
constructor() {
this.baseFolder = 'src';
return;
}
2016-01-27 10:29:26 +00:00
2016-02-11 16:48:44 +00:00
/* Open and parse the specified theme. */
2018-02-12 05:05:29 +00:00
open(themeFolder) {
2016-01-27 10:29:26 +00:00
var cached, formatsHash, pathInfo, that, themeFile, themeInfo;
this.folder = themeFolder;
2018-02-12 05:05:29 +00:00
// Open the [theme-name].json file; should have the same name as folder
2016-01-27 10:29:26 +00:00
pathInfo = parsePath(themeFolder);
2018-02-12 05:05:29 +00:00
// Set up a formats hash for the theme
2016-01-27 10:29:26 +00:00
formatsHash = {};
2018-02-12 05:05:29 +00:00
// Load the theme
2016-01-27 10:29:26 +00:00
themeFile = PATH.join(themeFolder, 'theme.json');
themeInfo = loadSafeJson(themeFile);
if (themeInfo.ex) {
throw {
2018-02-12 05:05:29 +00:00
fluenterror: themeInfo.ex.op === 'parse' ? HMSTATUS.parseError : HMSTATUS.readError
2016-01-27 10:29:26 +00:00
};
2018-02-12 05:05:29 +00:00
({
inner: themeInfo.ex.inner
});
2016-01-27 10:29:26 +00:00
}
that = this;
2018-02-12 05:05:29 +00:00
// Move properties from the theme JSON file to the theme object
2016-01-27 10:29:26 +00:00
EXTEND(true, this, themeInfo.json);
2018-02-12 05:05:29 +00:00
// Check for an "inherits" entry in the theme JSON.
2016-01-27 10:29:26 +00:00
if (this.inherits) {
cached = {};
_.each(this.inherits, function(th, key) {
var d, themePath, themesObj;
2018-02-12 05:05:29 +00:00
// First, see if this is one of the predefined FRESH themes. There are
// only a handful of these, but they may change over time, so we need to
// query the official source of truth: the fresh-themes repository, which
// mounts the themes conveniently by name to the module object, and which
// is embedded locally inside the HackMyResume installation.
// TODO: merge this code with
themesObj = require('fresh-themes');
if (_.has(themesObj.themes, th)) {
themePath = PATH.join(parsePath(require.resolve('fresh-themes')).dirname, '/themes/', th);
} else {
d = parsePath(th).dirname;
themePath = PATH.join(d, th);
}
2016-01-27 10:29:26 +00:00
cached[th] = cached[th] || new FRESHTheme().open(themePath);
return formatsHash[key] = cached[th].getFormat(key);
});
}
2018-02-12 05:05:29 +00:00
// Load theme files
formatsHash = _load.call(this, formatsHash);
2018-02-12 05:05:29 +00:00
// Cache
2016-01-27 10:29:26 +00:00
this.formats = formatsHash;
2018-02-12 05:05:29 +00:00
// Set the official theme name
2016-01-27 10:29:26 +00:00
this.name = parsePath(this.folder).name;
return this;
2018-02-12 05:05:29 +00:00
}
2016-01-27 10:29:26 +00:00
/* Determine if the theme supports the specified output format. */
2018-02-12 05:05:29 +00:00
hasFormat(fmt) {
2016-01-27 10:29:26 +00:00
return _.has(this.formats, fmt);
2018-02-12 05:05:29 +00:00
}
2016-01-27 10:29:26 +00:00
/* Determine if the theme supports the specified output format. */
2018-02-12 05:05:29 +00:00
getFormat(fmt) {
2016-01-27 10:29:26 +00:00
return this.formats[fmt];
2018-02-12 05:05:29 +00:00
}
2016-01-27 10:29:26 +00:00
2018-02-12 05:05:29 +00:00
};
2016-01-27 10:29:26 +00:00
_load = function(formatsHash) {
var copyOnly, fmts, jsFiles, major, that, tplFolder;
2016-01-27 10:29:26 +00:00
that = this;
major = false;
tplFolder = PATH.join(this.folder, this.baseFolder);
copyOnly = ['.ttf', '.otf', '.png', '.jpg', '.jpeg', '.pdf'];
2018-02-12 05:05:29 +00:00
// Iterate over all files in the theme folder, producing an array, fmts,
// containing info for each file. While we're doing that, also build up
// the formatsHash object.
2016-01-27 10:29:26 +00:00
fmts = READFILES(tplFolder).map(function(absPath) {
2016-02-11 16:48:44 +00:00
return _loadOne.call(this, absPath, formatsHash, tplFolder);
}, this);
2018-02-12 05:05:29 +00:00
// Now, get all the CSS files...
2016-01-27 10:29:26 +00:00
this.cssFiles = fmts.filter(function(fmt) {
return fmt && (fmt.ext === 'css');
});
2018-02-12 05:05:29 +00:00
// For each CSS file, get its corresponding HTML file. It's possible that
// a theme can have a CSS file but *no* HTML file, as when a theme author
// creates a pure CSS override of an existing theme.
2016-01-27 10:29:26 +00:00
this.cssFiles.forEach(function(cssf) {
var idx;
idx = _.findIndex(fmts, function(fmt) {
return fmt && fmt.pre === cssf.pre && fmt.ext === 'html';
});
cssf.major = false;
if (idx > -1) {
fmts[idx].css = cssf.data;
return fmts[idx].cssPath = cssf.path;
} else {
if (that.inherits) {
2018-02-12 05:05:29 +00:00
// Found a CSS file without an HTML file in a theme that inherits
// from another theme. This is the override CSS file.
2016-01-27 10:29:26 +00:00
return that.overrides = {
file: cssf.path,
data: cssf.data
};
}
}
});
2018-02-12 05:05:29 +00:00
// Now, save all the javascript file paths to a theme property.
jsFiles = fmts.filter(function(fmt) {
return fmt && (fmt.ext === 'js');
});
this.jsFiles = jsFiles.map(function(jsf) {
return jsf['path'];
});
2016-01-27 10:29:26 +00:00
return formatsHash;
};
2016-02-11 16:48:44 +00:00
_loadOne = function(absPath, formatsHash, tplFolder) {
var absPathSafe, act, defFormats, idx, isPrimary, obj, outFmt, pathInfo, portion, ref, ref1, reg, res;
2016-02-11 16:48:44 +00:00
pathInfo = parsePath(absPath);
if (pathInfo.basename.toLowerCase() === 'theme.json') {
return;
}
2016-02-11 16:48:44 +00:00
absPathSafe = absPath.trim().toLowerCase();
outFmt = '';
2016-02-12 03:04:11 +00:00
act = 'copy';
isPrimary = false;
2018-02-12 05:05:29 +00:00
// If this is an "explicit" theme, all files of importance are specified in
// the "transform" section of the theme.json file.
2016-02-11 16:48:44 +00:00
if (this.explicit) {
outFmt = _.find(Object.keys(this.formats), function(fmtKey) {
var fmtVal;
fmtVal = this.formats[fmtKey];
return _.some(fmtVal.transform, function(fpath) {
var absPathB;
absPathB = PATH.join(this.folder, fpath).trim().toLowerCase();
return absPathB === absPathSafe;
}, this);
}, this);
if (outFmt) {
act = 'transform';
}
}
if (!outFmt) {
2018-02-12 05:05:29 +00:00
// If this file lives in a specific format folder within the theme,
// such as "/latex" or "/html", then that format is the implicit output
// format for all files within the folder
2016-02-11 16:48:44 +00:00
portion = pathInfo.dirname.replace(tplFolder, '');
if (portion && portion.trim()) {
if (portion[1] === '_') {
return;
}
reg = /^(?:\/|\\)(html|latex|doc|pdf|png|partials)(?:\/|\\)?/ig;
res = reg.exec(portion);
if (res) {
if (res[1] !== 'partials') {
outFmt = res[1];
if (!this.explicit) {
act = 'transform';
}
} else {
this.partials = this.partials || [];
this.partials.push({
name: pathInfo.name,
path: absPath
});
return null;
}
}
}
}
if (!outFmt) {
idx = pathInfo.name.lastIndexOf('-');
outFmt = idx === -1 ? pathInfo.name : pathInfo.name.substr(idx + 1);
if (!this.explicit) {
act = 'transform';
}
defFormats = require('./default-formats');
isPrimary = _.some(defFormats, function(form) {
return form.name === outFmt && pathInfo.extname !== '.css';
});
2016-02-11 16:48:44 +00:00
}
2018-02-12 05:05:29 +00:00
// Make sure we have a valid formatsHash
2016-02-11 16:48:44 +00:00
formatsHash[outFmt] = formatsHash[outFmt] || {
outFormat: outFmt,
files: []
};
2018-02-12 05:05:29 +00:00
// Move symlink descriptions from theme.json to the format
2016-02-11 16:48:44 +00:00
if ((ref = this.formats) != null ? (ref1 = ref[outFmt]) != null ? ref1.symLinks : void 0 : void 0) {
formatsHash[outFmt].symLinks = this.formats[outFmt].symLinks;
}
2018-02-12 05:05:29 +00:00
// Create the file representation object
2016-02-11 16:48:44 +00:00
obj = {
action: act,
primary: isPrimary,
2016-02-11 16:48:44 +00:00
path: absPath,
orgPath: PATH.relative(tplFolder, absPath),
ext: pathInfo.extname.slice(1),
title: friendlyName(outFmt),
pre: outFmt,
2018-02-12 05:05:29 +00:00
// outFormat: outFmt || pathInfo.name,
2016-02-11 16:48:44 +00:00
data: FS.readFileSync(absPath, 'utf8'),
css: null
};
2018-02-12 05:05:29 +00:00
// Add this file to the list of files for this format type.
2016-02-11 16:48:44 +00:00
formatsHash[outFmt].files.push(obj);
return obj;
};
2016-01-27 10:29:26 +00:00
friendlyName = function(val) {
var friendly;
2016-02-11 16:48:44 +00:00
val = (val && val.trim().toLowerCase()) || '';
2016-01-27 10:29:26 +00:00
friendly = {
yml: 'yaml',
md: 'markdown',
txt: 'text'
};
return friendly[val] || val;
};
module.exports = FRESHTheme;
}).call(this);
2016-02-02 02:14:36 +00:00
//# sourceMappingURL=fresh-theme.js.map