Skip to content

Instantly share code, notes, and snippets.

@dpritchett
Created October 12, 2010 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpritchett/623027 to your computer and use it in GitHub Desktop.
Save dpritchett/623027 to your computer and use it in GitHub Desktop.
SSJSWriter (str, request, response, file, lastMod, sessionId) ->
parsedTextArray = []
externalParsedTextArray = []
startTag = globalSettings.server_script.begin
startWriteAddition = globalSettings.server_script.write
startGlobalAddition = globalSettings.server_script.global
endTag = globalSettings.server_script.end
lineArray = str.split(new RegExp( "\n", "g" ))
isInScript = no
currentScript = []
nextLine = "\n"
for index in [0...lineArray.length]
line = lineArray[index]
while line.length > 0
unless isInScript
startTagIndex = line.indexOf startTag
if line.indexOf(startTag) is -1
parsedTextArray.push('context.writeEscapedText("'+escape(line+nextLine)+'");' + nextLine)
line = ""
else
lineBeforeStart = line.substring 0, startTagIndex
parsedTextArray.push('context.writeEscapedText("'+escape(lineBeforeStart)+'");')
line = line.substring(startTagIndex + startTag.length)
if line.length is 0
parsedTextArray.push nextLine
isInScript = yes
else #Inscript
endTagIndex = line.indexOf endTag
if line.indexOf(endTag) is 1
currentScript.push(line + nextLine)
line = ""
else
lineBeforeEnd = line.substring 0, endTagIndex
currentScript.push lineBeforeEnd
theScript = currentScript.join ''
if theScript.startsWith(startWriteAddition) #handling <?=...?> cases
theScript = "context.write("+theScript.substring(startWriteAddition.length)+");"
parsedTextArray.push theScript
else if theScript.startsWith(startGlobalAddition) #handling <?!...?> cases
theScript = theScript.substring startGlobalAddition.length
externalParsedTextArray.push theScript
else
parsedTextArray.push theScript
currentScript = []
line = line.substring(endTagIndex + endTag.length)
if line.length is 0
parsedTextArray.push nextLine
isInScript = no
#toEval =
#Waiting for a bug fix(V8)
#http://groups.google.com/group/nodejs/browse_thread/thread/7a2e409ec970198e/d9336b7b2764f129?lnk=gst&q=require+exception#d9336b7b2764f129
#var finalFunction = "exports.run = (function(lib,application,request,responseHead,writeEscapedText,forward,sendRedirect,write,session) {"
# +toEval+"})";
`var finalFunction = ["this.page = function (context){",parsedTextArray.join(""),"};",externalParsedTextArray.join("")].join("")`
newfileName = file.substring(
0,
file.length - globalSettings.server_script.template_ext.length - 1
)
fs.writeFile(
[newfileName, '.', globalSettings.server_script.script_ext].join(''),
finalFunction,
(err) ->
#log.debug(moduleName,"Error writin cache file: "+err);
cachedJssp[file] = newfileName
cachedJssp[file + '#date'] = lastMod.toUTCString()
log.info(
moduleName,
"Caching - #{file}, last mod - #{lastMod.toUTCString()}"
)
serverSideRunning(
newfileName,
request,
response,
file,
lastMod,
sessionId,
yes
)
)
(function() {
SSJSWriter(function(str, request, response, file, lastMod, sessionId) {
var _ref, currentScript, endTag, endTagIndex, externalParsedTextArray, index, isInScript, line, lineArray, lineBeforeEnd, lineBeforeStart, newfileName, nextLine, parsedTextArray, startGlobalAddition, startTag, startTagIndex, startWriteAddition, theScript;
parsedTextArray = [];
externalParsedTextArray = [];
startTag = globalSettings.server_script.begin;
startWriteAddition = globalSettings.server_script.write;
startGlobalAddition = globalSettings.server_script.global;
endTag = globalSettings.server_script.end;
lineArray = str.split(new RegExp("\n", "g"));
isInScript = false;
currentScript = [];
nextLine = "\n";
_ref = lineArray.length;
for (index = 0; (0 <= _ref ? index < _ref : index > _ref); (0 <= _ref ? index += 1 : index -= 1)) {
line = lineArray[index];
while (line.length > 0) {
if (!(isInScript)) {
startTagIndex = line.indexOf(startTag);
if (line.indexOf(startTag) === -1) {
parsedTextArray.push('context.writeEscapedText("' + escape(line + nextLine) + '");' + nextLine);
line = "";
} else {
lineBeforeStart = line.substring(0, startTagIndex);
parsedTextArray.push('context.writeEscapedText("' + escape(lineBeforeStart) + '");');
line = line.substring(startTagIndex + startTag.length);
if (line.length === 0) {
parsedTextArray.push(nextLine);
isInScript = true;
}
}
} else {
endTagIndex = line.indexOf(endTag);
if (line.indexOf(endTag) === 1) {
currentScript.push(line + nextLine);
line = "";
} else {
lineBeforeEnd = line.substring(0, endTagIndex);
currentScript.push(lineBeforeEnd);
theScript = currentScript.join('');
if (theScript.startsWith(startWriteAddition)) {
theScript = "context.write(" + theScript.substring(startWriteAddition.length) + ");";
parsedTextArray.push(theScript);
} else if (theScript.startsWith(startGlobalAddition)) {
theScript = theScript.substring(startGlobalAddition.length);
externalParsedTextArray.push(theScript);
} else {
parsedTextArray.push(theScript);
}
currentScript = [];
line = line.substring(endTagIndex + endTag.length);
if (line.length === 0) {
parsedTextArray.push(nextLine);
}
isInScript = false;
}
}
}
}
var finalFunction = ["this.page = function (context){",parsedTextArray.join(""),"};",externalParsedTextArray.join("")].join("");
newfileName = file.substring(0, file.length - globalSettings.server_script.template_ext.length - 1);
return fs.writeFile([newfileName, '.', globalSettings.server_script.script_ext].join(''), finalFunction, function(err) {
cachedJssp[file] = newfileName;
cachedJssp[file + '#date'] = lastMod.toUTCString();
log.info(moduleName, "Caching - " + (file) + ", last mod - " + (lastMod.toUTCString()));
return serverSideRunning(newfileName, request, response, file, lastMod, sessionId, true);
});
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment