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

291 lines
9.0 KiB
JavaScript
Raw Normal View History

2015-10-26 16:30:00 +00:00
/**
2015-12-17 15:15:59 +00:00
Definition of the Theme class.
@license MIT. Copyright (c) 2015 hacksalot / FluentDesk.
2015-12-17 15:15:59 +00:00
@module theme.js
2015-10-26 16:30:00 +00:00
*/
(function() {
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')
, RECURSIVE_READ_DIR = require('recursive-readdir-sync');
2015-10-26 16:30:00 +00:00
/**
2015-12-19 17:37:42 +00:00
The Theme class is a representation of a HackMyResume theme asset.
2015-10-26 16:30:00 +00:00
@class Theme
*/
function Theme() {
}
/**
Open and parse the specified theme.
*/
Theme.prototype.open = function( themeFolder ) {
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 = { };
// See if the theme has a package.json. If so, load it.
var packageJsonPath = PATH.join(themeFolder, 'package.json');
if( pathExists( packageJsonPath ) ) {
var themePack = require( themeFolder );
var themePkgJson = require( packageJsonPath );
this.name = themePkgJson.name;
this.render = (themePack && themePack.render) || undefined;
this.formats = { html: { title: 'html', outFormat: 'html', ext: 'html', path: null, data: null } };
return this;
}
// Otherwise, do a full theme load
var themeFile = PATH.join( themeFolder, pathInfo.basename + '.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 );
}
// Add freebie formats every theme gets
2015-12-10 02:44:35 +00:00
formatsHash.json = { title: 'json', outFormat: 'json', pre: 'json', ext: 'json', path: null, data: null };
formatsHash.yml = { title: 'yaml', outFormat: 'yml', pre: 'yml', ext: 'yml', path: null, data: null };
2015-12-07 21:39:59 +00:00
// 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;
};
/**
Determine if the theme supports the specified output format.
*/
Theme.prototype.hasFormat = function( fmt ) {
return _.has( this.formats, fmt );
};
/**
Determine if the theme supports the specified output format.
*/
Theme.prototype.getFormat = function( fmt ) {
return this.formats[ fmt ];
};
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.
var fmts = RECURSIVE_READ_DIR( tplFolder ).map( function( absPath ) {
// 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-17 01:13:27 +00:00
var reg = /^(?:\/|\\)(html|latex|doc|pdf|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-09 10:08:10 +00:00
outFmt = ( idx === -1 ) ? pathInfo.name : pathInfo.name.substr( idx + 1 );
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-17 01:13:27 +00:00
(this.cssFiles = fmts.filter(function( fmt ){ return fmt && (fmt.ext === 'css'); }))
2015-12-07 15:16:38 +00:00
.forEach(function( cssf ) {
2015-12-09 05:13:58 +00:00
// For each CSS file, get its corresponding HTML file
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-07 21:39:59 +00:00
function loadExplicit() {
2015-11-05 05:56:41 +00:00
2015-12-07 21:39:59 +00:00
var that = this;
2015-12-08 02:24:14 +00:00
// Set up a hash of formats supported by this theme.
var formatsHash = { };
2015-10-26 16:30:00 +00:00
2015-12-07 21:39:59 +00:00
// Establish the base theme folder
2015-12-08 02:24:14 +00:00
var tplFolder = PATH.join( this.folder, 'src' );
var act = null;
// 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 = RECURSIVE_READ_DIR( tplFolder ).map( function( absPath ) {
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();
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;
});
});
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-10 02:44:35 +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...
(this.cssFiles = fmts.filter(function( fmt ){ return fmt.ext === 'css'; }))
.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-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
module.exports = Theme;
}());