From 7037b98614e3048f682153c3842d9ce6f498b82b Mon Sep 17 00:00:00 2001 From: Rlidwka Date: Mon, 20 Feb 2012 04:15:14 +0400 Subject: [PATCH] include and include_str keywords added --- src/snockets.coffee | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/snockets.coffee b/src/snockets.coffee index 8f59248..b40c083 100644 --- a/src/snockets.coffee +++ b/src/snockets.coffee @@ -150,10 +150,38 @@ module.exports = class Snockets requireTree itemPath q.unwaitFor itemPath + escapeFile = (string) -> + string = string.replace(/\\/g, '\\\\').replace(/(['"])/g, '\\$1') + string = string.replace(/\n/, "\\\n") unless filePath.match(/\.coffee$/) + return string + + include = (relPath, substr, escape=off) => + # i'm setting random holder (just in case of unwanted collisions) + q.waitFor hold = Math.random() + onFound = (incPath) => + @readFile incPath, flags, (err, fileChanged) => + return callback err if err + if fileChanged then graphChanged = true + replaceTo = @cache[incPath].data.toString('utf8') + replaceTo = escapeFile(replaceTo) if escape + # here we are casting buffer to utf8... any need to cast it back? + @cache[filePath].data = @cache[filePath].data.toString('utf8'). + replace(substr, replaceTo) + q.unwaitFor hold + + relName = stripExt relPath + if relName.match EXPLICIT_PATH + onFound relPath + else + incName = path.join path.dirname(filePath), relName + @findMatchingFile incName, flags, (err, incPath) -> + return callback err if err + onFound incPath + @readFile filePath, flags, (err, fileChanged) => return callback err if err if fileChanged then graphChanged = true - for directive in parseDirectives(@cache[filePath].data.toString 'utf8') + for [substr, directive] in parseDirectives(@cache[filePath].data.toString 'utf8') words = directive.replace(/['"]/g, '').split /\s+/ [command, relPaths...] = words @@ -163,6 +191,11 @@ module.exports = class Snockets when 'require_tree' for relPath in relPaths requireTree path.join path.dirname(filePath), relPath + when 'include' + # multiple file including is not supported :( + include relPaths[0], substr + when 'include_str' + include relPaths[0], substr, on q.finalize() @@ -290,7 +323,7 @@ parseDirectives = (code) -> code = code.replace /[\r\t ]+$/gm, '\n' # fix for issue #2 return [] unless match = HEADER.exec(code) header = match[0] - match[1] while match = DIRECTIVE.exec header + match while match = DIRECTIVE.exec header stripExt = (filePath) -> if path.extname(filePath) in jsExts() @@ -310,4 +343,4 @@ minify = (js) -> pro.gen_code ast timeEq = (date1, date2) -> - date1? and date2? and date1.getTime() is date2.getTime() \ No newline at end of file + date1? and date2? and date1.getTime() is date2.getTime()