1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-10-05 23:15:12 +01:00
HackMyResume/src/core/fresh-theme.js

316 lines
8.7 KiB
JavaScript
Raw Normal View History

2015-10-26 16:30:00 +00:00
/**
2015-12-30 18:22:18 +00:00
Definition of the FRESHTheme class.
@module fresh-theme.js
@license MIT. See LICENSE.md for details.
2015-10-26 16:30:00 +00:00
*/
(function() {
2015-12-30 20:21:58 +00:00
2015-10-26 16:30:00 +00:00
var FS = require('fs')
, extend = require('../utils/extend')
, validator = require('is-my-json-valid')
, _ = require('underscore')
, PATH = require('path')
, parsePath = require('parse-filepath')
, pathExists = require('path-exists').sync
2015-12-06 21:19:55 +00:00
, EXTEND = require('../utils/extend')
, moment = require('moment')
2015-12-30 20:21:58 +00:00
, READFILES = require('recursive-readdir-sync');
2015-10-26 16:30:00 +00:00
/**
2015-12-30 20:21:58 +00:00
The FRESHTheme class is a representation of a FRESH theme
asset. See also: JRSTheme.
2015-12-30 18:22:18 +00:00
@class FRESHTheme
2015-10-26 16:30:00 +00:00
*/
2015-12-30 18:22:18 +00:00
function FRESHTheme() {
2015-10-26 16:30:00 +00:00
}
2015-12-30 20:21:58 +00:00
2015-10-26 16:30:00 +00:00
/**
Open and parse the specified theme.
*/
2015-12-30 18:22:18 +00:00
FRESHTheme.prototype.open = function( themeFolder ) {
2015-10-26 16:30:00 +00:00
2015-11-05 05:56:06 +00:00
this.folder = themeFolder;
// Open the [theme-name].json file; should have the same name as folder
var pathInfo = parsePath( themeFolder );
// Set up a formats hash for the theme
var formatsHash = { };
2015-12-30 20:21:58 +00:00
// Load the theme
2016-01-04 09:15:01 +00:00
var themeFile = PATH.join( themeFolder, 'theme.json' );
2015-12-06 21:19:55 +00:00
var themeInfo = JSON.parse( FS.readFileSync( themeFile, 'utf8' ) );
2015-12-07 21:39:59 +00:00
var that = this;
2015-12-07 15:16:38 +00:00
// Move properties from the theme JSON file to the theme object
2015-12-06 21:19:55 +00:00
EXTEND( true, this, themeInfo );
2015-11-05 05:56:41 +00:00
2015-12-07 21:39:59 +00:00
// Check for an explicit "formats" entry in the theme JSON. If it has one,
// then this theme declares its files explicitly.
if( !!this.formats ) {
formatsHash = loadExplicit.call( this );
2015-12-10 04:30:53 +00:00
this.explicit = true;
2015-12-07 21:39:59 +00:00
}
else {
formatsHash = loadImplicit.call( this );
}
// Cache
this.formats = formatsHash;
// Set the official theme name
this.name = parsePath( this.folder ).name;
2015-12-07 21:39:59 +00:00
return this;
};
2015-12-30 20:21:58 +00:00
2015-12-07 21:39:59 +00:00
/**
Determine if the theme supports the specified output format.
*/
2015-12-30 18:22:18 +00:00
FRESHTheme.prototype.hasFormat = function( fmt ) {
2015-12-07 21:39:59 +00:00
return _.has( this.formats, fmt );
};
2015-12-30 20:21:58 +00:00
2015-12-07 21:39:59 +00:00
/**
Determine if the theme supports the specified output format.
*/
2015-12-30 18:22:18 +00:00
FRESHTheme.prototype.getFormat = function( fmt ) {
2015-12-07 21:39:59 +00:00
return this.formats[ fmt ];
};
2015-12-30 20:21:58 +00:00
/**
Load the theme implicitly, by scanning the theme folder for
files. TODO: Refactor duplicated code with loadExplicit.
*/
2015-12-07 21:39:59 +00:00
function loadImplicit() {
2015-12-07 15:16:38 +00:00
// Set up a hash of formats supported by this theme.
var formatsHash = { };
2015-12-08 15:13:04 +00:00
var that = this;
2015-12-09 10:08:10 +00:00
var major = false;
2015-12-07 15:16:38 +00:00
// Establish the base theme folder
2015-12-07 21:39:59 +00:00
var tplFolder = PATH.join( this.folder, 'src' );
2015-12-07 15:16:38 +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.
2015-12-30 20:21:58 +00:00
var fmts = READFILES(tplFolder).map( function(absPath) {
2015-12-07 15:16:38 +00:00
// If this file lives in a specific format folder within the theme,
// such as "/latex" or "/html", then that format is the output format
// for all files within the folder.
var pathInfo = parsePath(absPath);
2015-12-09 10:08:10 +00:00
var outFmt = '', isMajor = false;
var portion = pathInfo.dirname.replace(tplFolder,'');
2015-12-07 15:16:38 +00:00
if( portion && portion.trim() ) {
if( portion[1] === '_' ) return;
2015-12-28 19:29:13 +00:00
var reg = /^(?:\/|\\)(html|latex|doc|pdf|png|partials)(?:\/|\\)?/ig;
2015-12-07 15:16:38 +00:00
var res = reg.exec( portion );
2015-12-17 01:13:27 +00:00
if( res ) {
if( res[1] !== 'partials' ) {
outFmt = res[1];
}
else {
that.partials = that.partials || [];
that.partials.push( { name: pathInfo.name, path: absPath } );
return null;
}
}
}
2015-12-07 15:16:38 +00:00
// Otherwise, the output format is inferred from the filename, as in
// compact-[outputformat].[extension], for ex, compact-pdf.html.
if( !outFmt ) {
var idx = pathInfo.name.lastIndexOf('-');
2015-12-30 20:21:58 +00:00
outFmt = (idx === -1) ? pathInfo.name : pathInfo.name.substr(idx + 1);
2015-12-09 10:08:10 +00:00
isMajor = true;
2015-12-07 15:16:38 +00:00
}
// We should have a valid output format now.
2015-12-09 03:22:14 +00:00
formatsHash[ outFmt ] = formatsHash[outFmt] || {
outFormat: outFmt,
files: []
};
2015-12-07 15:16:38 +00:00
// Create the file representation object.
var obj = {
2015-12-09 03:22:14 +00:00
action: 'transform',
2015-12-07 15:16:38 +00:00
path: absPath,
2015-12-09 10:08:10 +00:00
major: isMajor,
orgPath: PATH.relative(tplFolder, absPath),
ext: pathInfo.extname.slice(1),
2015-12-07 15:16:38 +00:00
title: friendlyName( outFmt ),
pre: outFmt,
// outFormat: outFmt || pathInfo.name,
data: FS.readFileSync( absPath, 'utf8' ),
css: null
};
// Add this file to the list of files for this format type.
formatsHash[ outFmt ].files.push( obj );
return obj;
});
2015-10-26 16:30:00 +00:00
2015-11-05 05:56:41 +00:00
// Now, get all the CSS files...
2015-12-30 20:21:58 +00:00
(this.cssFiles = fmts.filter(function( fmt ){
return fmt && (fmt.ext === 'css');
}))
// For each CSS file, get its corresponding HTML file
2015-12-07 15:16:38 +00:00
.forEach(function( cssf ) {
2015-12-30 20:21:58 +00:00
2015-12-09 05:13:58 +00:00
var idx = _.findIndex(fmts, function( fmt ) {
2015-12-18 15:10:19 +00:00
return fmt && fmt.pre === cssf.pre && fmt.ext === 'html';
2015-11-05 05:56:41 +00:00
});
2015-12-09 05:13:58 +00:00
cssf.action = null;
fmts[ idx ].css = cssf.data;
fmts[ idx ].cssPath = cssf.path;
2015-10-26 16:30:00 +00:00
});
2015-11-05 05:56:41 +00:00
// Remove CSS files from the formats array
2015-10-26 16:30:00 +00:00
fmts = fmts.filter( function( fmt) {
2015-12-17 01:13:27 +00:00
return fmt && (fmt.ext !== 'css');
2015-10-26 16:30:00 +00:00
});
2015-12-07 21:39:59 +00:00
return formatsHash;
}
2015-10-26 16:30:00 +00:00
2015-12-30 20:21:58 +00:00
/**
Load the theme explicitly, by following the 'formats' hash
in the theme's JSON settings file.
*/
2015-12-07 21:39:59 +00:00
function loadExplicit() {
2015-11-05 05:56:41 +00:00
2015-12-30 20:21:58 +00:00
// Housekeeping
2015-12-08 02:24:14 +00:00
var formatsHash = { };
var tplFolder = PATH.join( this.folder, 'src' );
var act = null;
2015-12-30 20:21:58 +00:00
var that = this;
2015-12-08 02:24:14 +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.
var fmts = READFILES( tplFolder ).map( function( absPath ) {
2015-12-08 02:24:14 +00:00
act = null;
// If this file is mentioned in the theme's JSON file under "transforms"
var pathInfo = parsePath(absPath);
2015-12-08 02:24:14 +00:00
var absPathSafe = absPath.trim().toLowerCase();
2015-12-30 20:21:58 +00:00
var outFmt = _.find(
Object.keys( that.formats ),
function( fmtKey ) {
var fmtVal = that.formats[ fmtKey ];
return _.some( fmtVal.transform, function(fpath) {
var absPathB = PATH.join( that.folder, fpath )
.trim().toLowerCase();
return absPathB === absPathSafe;
});
2015-12-08 02:24:14 +00:00
});
if( outFmt ) {
act = 'transform';
}
// If this file lives in a specific format folder within the theme,
// such as "/latex" or "/html", then that format is the output format
// for all files within the folder.
if( !outFmt ) {
var portion = pathInfo.dirname.replace(tplFolder,'');
2015-12-08 02:24:14 +00:00
if( portion && portion.trim() ) {
var reg = /^(?:\/|\\)(html|latex|doc|pdf)(?:\/|\\)?/ig;
var res = reg.exec( portion );
res && (outFmt = res[1]);
}
}
// Otherwise, the output format is inferred from the filename, as in
// compact-[outputformat].[extension], for ex, compact-pdf.html.
if( !outFmt ) {
var idx = pathInfo.name.lastIndexOf('-');
2015-12-30 20:21:58 +00:00
outFmt = (idx === -1) ? pathInfo.name : pathInfo.name.substr(idx + 1);
2015-12-08 02:24:14 +00:00
}
// We should have a valid output format now.
formatsHash[ outFmt ] =
2015-12-08 15:13:04 +00:00
formatsHash[ outFmt ] || {
outFormat: outFmt,
files: [],
symLinks: that.formats[ outFmt ].symLinks
};
2015-12-08 02:24:14 +00:00
// Create the file representation object.
var obj = {
action: act,
orgPath: PATH.relative(that.folder, absPath),
path: absPath,
ext: pathInfo.extname.slice(1),
2015-12-08 02:24:14 +00:00
title: friendlyName( outFmt ),
pre: outFmt,
// outFormat: outFmt || pathInfo.name,
data: FS.readFileSync( absPath, 'utf8' ),
css: null
2015-12-07 21:39:59 +00:00
};
2015-12-08 02:24:14 +00:00
// Add this file to the list of files for this format type.
formatsHash[ outFmt ].files.push( obj );
return obj;
2015-12-07 21:39:59 +00:00
});
2015-12-08 02:24:14 +00:00
// Now, get all the CSS files...
2015-12-30 20:21:58 +00:00
(this.cssFiles = fmts.filter(function( fmt ){
return fmt.ext === 'css';
}))
2015-12-30 20:21:58 +00:00
// For each CSS file, get its corresponding HTML file
2015-12-08 02:24:14 +00:00
.forEach(function( cssf ) {
// For each CSS file, get its corresponding HTML file
var idx = _.findIndex(fmts, function( fmt ) {
2015-12-10 02:44:35 +00:00
return fmt.pre === cssf.pre && fmt.ext === 'html';
2015-12-08 02:24:14 +00:00
});
fmts[ idx ].css = cssf.data;
fmts[ idx ].cssPath = cssf.path;
});
// Remove CSS files from the formats array
fmts = fmts.filter( function( fmt) {
return fmt.ext !== 'css';
});
2015-12-07 21:39:59 +00:00
return formatsHash;
}
2015-10-26 16:30:00 +00:00
2015-12-30 20:21:58 +00:00
/**
Return a more friendly name for certain formats.
TODO: Refactor
*/
2015-12-07 21:39:59 +00:00
function friendlyName( val ) {
val = val.trim().toLowerCase();
var friendly = { yml: 'yaml', md: 'markdown', txt: 'text' };
return friendly[val] || val;
}
2015-10-26 16:30:00 +00:00
2015-12-30 20:21:58 +00:00
2015-12-30 18:22:18 +00:00
module.exports = FRESHTheme;
2015-10-26 16:30:00 +00:00
2015-12-30 20:21:58 +00:00
2015-10-26 16:30:00 +00:00
}());