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

108 lines
3.1 KiB
JavaScript
Raw Normal View History

2015-10-26 16:30:00 +00:00
/**
Abstract theme representation.
@license MIT. Copyright (c) 2015 James Devlin / FluentDesk.
*/
(function() {
var FS = require('fs')
, extend = require('../utils/extend')
, validator = require('is-my-json-valid')
, _ = require('underscore')
, PATH = require('path')
2015-12-06 21:19:55 +00:00
, EXTEND = require('../utils/extend')
, moment = require('moment')
, recursiveReadSync = require('recursive-readdir-sync');
2015-10-26 16:30:00 +00:00
/**
2015-12-06 21:19:55 +00:00
The Theme class is a representation of a FluentCV 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 ) {
function friendlyName( val ) {
val = val.trim().toLowerCase();
var friendly = { yml: 'yaml', md: 'markdown', txt: 'text' };
return friendly[val] || val;
}
2015-12-06 21:19:55 +00:00
// Open the theme.json file; should have the same name as folder
2015-11-05 05:56:06 +00:00
this.folder = themeFolder;
2015-12-06 21:19:55 +00:00
var pathInfo = PATH.parse( themeFolder );
var themeFile = PATH.join( themeFolder, pathInfo.base + '.json' );
var themeInfo = JSON.parse( FS.readFileSync( themeFile, 'utf8' ) );
EXTEND( true, this, themeInfo );
2015-11-05 05:56:41 +00:00
// Iterate over all files in the theme folder, producing an array, fmts,
// containing info for each file.
var tplFolder = PATH.join( themeFolder, 'src' );
var fmts = recursiveReadSync( tplFolder ).map( function( file ) {
2015-10-26 16:30:00 +00:00
var absPath = PATH.join( tplFolder, file );
var pathInfo = PATH.parse(absPath);
var temp = [ pathInfo.name, {
title: friendlyName(pathInfo.name),
pre: pathInfo.name,
ext: pathInfo.ext.slice(1),
path: absPath,
data: FS.readFileSync( absPath, 'utf8' ),
css: null
}];
return temp;
});
2015-11-05 05:56:41 +00:00
// Add freebie formats every theme gets
2015-10-26 16:30:00 +00:00
fmts.push( [ 'json', { title: 'json', pre: 'json', ext: 'json', path: null, data: null } ] );
fmts.push( [ 'yml', { title: 'yaml', pre: 'yml', ext: 'yml', path: null, data: null } ] );
2015-11-05 05:56:41 +00:00
// Now, get all the CSS files...
this.cssFiles = fmts.filter(function( fmt ){ return fmt[1].ext === 'css'; });
// ...and assemble information on them
this.cssFiles.forEach(function( cssf ) {
2015-10-26 16:30:00 +00:00
// For each CSS file, get its corresponding HTML file
2015-11-05 05:56:41 +00:00
var idx = _.findIndex(fmts, function( fmt ) {
return fmt[1].pre === cssf[1].pre && fmt[1].ext === 'html'
});
2015-10-26 16:30:00 +00:00
fmts[ idx ][1].css = cssf[1].data;
fmts[ idx ][1].cssPath = cssf[1].path;
});
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) {
return fmt[1].ext !== 'css';
});
// Create a hash out of the formats for this theme
this.formats = _.object( fmts );
2015-11-05 05:56:41 +00:00
// Set the official theme name
2015-10-26 16:30:00 +00:00
this.name = PATH.parse( themeFolder ).name;
2015-11-05 05:56:41 +00:00
2015-10-26 16:30:00 +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 ];
};
module.exports = Theme;
}());