mirror of
https://github.com/JuanCanham/HackMyResume.git
synced 2025-05-10 07:47:07 +01:00
Refactor generation.
Merge implicit and explicit generation paths, start emitting file transform & copy signals, fix various bugs, introduce new bugs, support better --debug outputs in the future.
This commit is contained in:
@ -33,3 +33,6 @@ module.exports =
|
||||
afterInlineConvert: 23
|
||||
beforeValidate: 24
|
||||
afterValidate: 25
|
||||
beforeWrite: 26
|
||||
afterWrite: 27
|
||||
applyTheme: 28
|
||||
|
@ -69,13 +69,8 @@ class FRESHTheme
|
||||
cached[ th ] = cached[th] || new FRESHTheme().open( themePath )
|
||||
formatsHash[ key ] = cached[ th ].getFormat( key )
|
||||
|
||||
# Check for an explicit "formats" entry in the theme JSON. If it has one,
|
||||
# then this theme declares its files explicitly.
|
||||
if !!@formats
|
||||
formatsHash = loadExplicit.call this, formatsHash
|
||||
@explicit = true;
|
||||
else
|
||||
formatsHash = loadImplicit.call this, formatsHash
|
||||
# Load theme files
|
||||
formatsHash = _load.call @, formatsHash
|
||||
|
||||
# Cache
|
||||
@formats = formatsHash
|
||||
@ -91,9 +86,10 @@ class FRESHTheme
|
||||
getFormat: ( fmt ) -> @formats[ fmt ]
|
||||
|
||||
|
||||
|
||||
### Load the theme implicitly, by scanning the theme folder for files. TODO:
|
||||
Refactor duplicated code with loadExplicit. ###
|
||||
loadImplicit = (formatsHash) ->
|
||||
_load = (formatsHash) ->
|
||||
|
||||
# Set up a hash of formats supported by this theme.
|
||||
that = @
|
||||
@ -102,31 +98,43 @@ loadImplicit = (formatsHash) ->
|
||||
# Establish the base theme folder
|
||||
tplFolder = PATH.join @folder, 'src'
|
||||
|
||||
copyOnly = ['.ttf','.otf', '.png','.jpg','.jpeg','.pdf']
|
||||
|
||||
# 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.
|
||||
fmts = READFILES(tplFolder).map (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.
|
||||
pathInfo = parsePath absPath
|
||||
absPathSafe = absPath.trim().toLowerCase()
|
||||
outFmt = ''
|
||||
isMajor = false
|
||||
portion = pathInfo.dirname.replace tplFolder,''
|
||||
if portion && portion.trim()
|
||||
return if portion[1] == '_'
|
||||
reg = /^(?:\/|\\)(html|latex|doc|pdf|png|partials)(?:\/|\\)?/ig
|
||||
res = reg.exec( portion )
|
||||
if res
|
||||
if res[1] != 'partials'
|
||||
outFmt = res[1]
|
||||
else
|
||||
that.partials = that.partials || []
|
||||
that.partials.push( { name: pathInfo.name, path: absPath } )
|
||||
return null
|
||||
|
||||
# If this file is mentioned in the theme's JSON file under "transforms"
|
||||
if that.formats
|
||||
outFmt = _.find Object.keys( that.formats ), ( fmtKey ) ->
|
||||
fmtVal = that.formats[ fmtKey ]
|
||||
_.some fmtVal.transform, (fpath) ->
|
||||
absPathB = PATH.join( that.folder, fpath ).trim().toLowerCase()
|
||||
absPathB == absPathSafe
|
||||
isMajor = true if outFmt
|
||||
|
||||
if !outFmt
|
||||
# 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.
|
||||
portion = pathInfo.dirname.replace tplFolder,''
|
||||
if portion && portion.trim()
|
||||
return if portion[1] == '_'
|
||||
reg = /^(?:\/|\\)(html|latex|doc|pdf|png|partials)(?:\/|\\)?/ig
|
||||
res = reg.exec( portion )
|
||||
if res
|
||||
if res[1] != 'partials'
|
||||
outFmt = res[1]
|
||||
else
|
||||
that.partials = that.partials || []
|
||||
that.partials.push( { name: pathInfo.name, path: absPath } )
|
||||
return null
|
||||
|
||||
# Otherwise, the output format is inferred from the filename, as in
|
||||
# compact-[outputformat].[extension], for ex, compact-pdf.html.
|
||||
@ -135,23 +143,27 @@ loadImplicit = (formatsHash) ->
|
||||
outFmt = if idx == -1 then pathInfo.name else pathInfo.name.substr( idx + 1 )
|
||||
isMajor = true
|
||||
|
||||
act = if _.contains copyOnly, pathInfo.extname then 'copy' else 'transform'
|
||||
|
||||
# We should have a valid output format now.
|
||||
formatsHash[ outFmt ] = formatsHash[outFmt] || {
|
||||
outFormat: outFmt,
|
||||
files: []
|
||||
}
|
||||
if that.formats?[ outFmt ]?.symLinks
|
||||
formatsHash[ outFmt ].symLinks = that.formats[ outFmt ].symLinks
|
||||
|
||||
# Create the file representation object.
|
||||
obj =
|
||||
action: 'transform'
|
||||
action: act
|
||||
path: absPath
|
||||
major: isMajor
|
||||
orgPath: PATH.relative(tplFolder, absPath)
|
||||
ext: pathInfo.extname.slice(1)
|
||||
title: friendlyName( outFmt )
|
||||
orgPath: PATH.relative tplFolder, absPath
|
||||
ext: pathInfo.extname.slice 1
|
||||
title: friendlyName outFmt
|
||||
pre: outFmt
|
||||
# outFormat: outFmt || pathInfo.name,
|
||||
data: FS.readFileSync( absPath, 'utf8' )
|
||||
data: FS.readFileSync absPath, 'utf8'
|
||||
css: null
|
||||
|
||||
# Add this file to the list of files for this format type.
|
||||
@ -180,87 +192,90 @@ loadImplicit = (formatsHash) ->
|
||||
|
||||
|
||||
|
||||
###
|
||||
Load the theme explicitly, by following the 'formats' hash
|
||||
in the theme's JSON settings file.
|
||||
###
|
||||
loadExplicit = (formatsHash) ->
|
||||
|
||||
# Housekeeping
|
||||
tplFolder = PATH.join this.folder, 'src'
|
||||
act = null
|
||||
that = this
|
||||
|
||||
# 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.
|
||||
fmts = READFILES( tplFolder ).map (absPath) ->
|
||||
|
||||
act = null
|
||||
# If this file is mentioned in the theme's JSON file under "transforms"
|
||||
pathInfo = parsePath(absPath)
|
||||
absPathSafe = absPath.trim().toLowerCase()
|
||||
outFmt = _.find Object.keys( that.formats ), ( fmtKey ) ->
|
||||
fmtVal = that.formats[ fmtKey ]
|
||||
_.some fmtVal.transform, (fpath) ->
|
||||
absPathB = PATH.join( that.folder, fpath ).trim().toLowerCase()
|
||||
absPathB == absPathSafe
|
||||
|
||||
act = 'transform' if outFmt
|
||||
|
||||
# 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
|
||||
portion = pathInfo.dirname.replace tplFolder,''
|
||||
if portion && portion.trim()
|
||||
reg = /^(?:\/|\\)(html|latex|doc|pdf)(?:\/|\\)?/ig
|
||||
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
|
||||
idx = pathInfo.name.lastIndexOf '-'
|
||||
outFmt = if (idx == -1) then pathInfo.name else pathInfo.name.substr(idx + 1)
|
||||
|
||||
# We should have a valid output format now.
|
||||
formatsHash[ outFmt ] =
|
||||
formatsHash[ outFmt ] || {
|
||||
outFormat: outFmt,
|
||||
files: [],
|
||||
symLinks: that.formats[ outFmt ].symLinks
|
||||
};
|
||||
|
||||
# Create the file representation object.
|
||||
obj =
|
||||
action: act
|
||||
orgPath: PATH.relative(that.folder, absPath)
|
||||
path: absPath
|
||||
ext: pathInfo.extname.slice(1)
|
||||
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 )
|
||||
obj
|
||||
|
||||
# Now, get all the CSS files...
|
||||
@cssFiles = fmts.filter ( fmt ) -> fmt.ext == 'css'
|
||||
|
||||
# For each CSS file, get its corresponding HTML file
|
||||
@cssFiles.forEach ( cssf ) ->
|
||||
# For each CSS file, get its corresponding HTML file
|
||||
idx = _.findIndex fmts, ( fmt ) ->
|
||||
fmt.pre == cssf.pre && fmt.ext == 'html'
|
||||
fmts[ idx ].css = cssf.data;
|
||||
fmts[ idx ].cssPath = cssf.path;
|
||||
# Remove CSS files from the formats array
|
||||
fmts = fmts.filter ( fmt) -> fmt.ext != 'css'
|
||||
formatsHash
|
||||
# ###
|
||||
# Load the theme explicitly, by following the 'formats' hash
|
||||
# in the theme's JSON settings file.
|
||||
# ###
|
||||
# loadExplicit = (formatsHash) ->
|
||||
#
|
||||
# # Housekeeping
|
||||
# tplFolder = PATH.join this.folder, 'src'
|
||||
# act = null
|
||||
# that = this
|
||||
#
|
||||
# # 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.
|
||||
# fmts = READFILES( tplFolder ).map (absPath) ->
|
||||
#
|
||||
# act = null
|
||||
#
|
||||
# pathInfo = parsePath absPath
|
||||
# absPathSafe = absPath.trim().toLowerCase()
|
||||
#
|
||||
# # If this file is mentioned in the theme's JSON file under "transforms"
|
||||
# outFmt = _.find Object.keys( that.formats ), ( fmtKey ) ->
|
||||
# fmtVal = that.formats[ fmtKey ]
|
||||
# _.some fmtVal.transform, (fpath) ->
|
||||
# absPathB = PATH.join( that.folder, fpath ).trim().toLowerCase()
|
||||
# absPathB == absPathSafe
|
||||
#
|
||||
# act = 'transform' if outFmt
|
||||
#
|
||||
# # 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
|
||||
# portion = pathInfo.dirname.replace tplFolder,''
|
||||
# if portion && portion.trim()
|
||||
# reg = /^(?:\/|\\)(html|latex|doc|pdf)(?:\/|\\)?/ig
|
||||
# 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
|
||||
# idx = pathInfo.name.lastIndexOf '-'
|
||||
# outFmt = if (idx == -1) then pathInfo.name else pathInfo.name.substr(idx + 1)
|
||||
#
|
||||
# # We should have a valid output format now.
|
||||
# formatsHash[ outFmt ] =
|
||||
# formatsHash[ outFmt ] || {
|
||||
# outFormat: outFmt,
|
||||
# files: [],
|
||||
# symLinks: that.formats[ outFmt ].symLinks
|
||||
# };
|
||||
#
|
||||
# # Create the file representation object.
|
||||
# obj =
|
||||
# action: act
|
||||
# orgPath: PATH.relative(that.folder, absPath)
|
||||
# path: absPath
|
||||
# ext: pathInfo.extname.slice(1)
|
||||
# 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 )
|
||||
# obj
|
||||
#
|
||||
# # Now, get all the CSS files...
|
||||
# @cssFiles = fmts.filter ( fmt ) -> fmt.ext == 'css'
|
||||
#
|
||||
# # For each CSS file, get its corresponding HTML file
|
||||
# @cssFiles.forEach ( cssf ) ->
|
||||
# # For each CSS file, get its corresponding HTML file
|
||||
# idx = _.findIndex fmts, ( fmt ) ->
|
||||
# fmt.pre == cssf.pre && fmt.ext == 'html'
|
||||
# fmts[ idx ].css = cssf.data
|
||||
# fmts[ idx ].cssPath = cssf.path
|
||||
#
|
||||
# # Remove CSS files from the formats array
|
||||
# fmts = fmts.filter ( fmt) -> fmt.ext != 'css'
|
||||
# formatsHash
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user