//OpenForum/Javascript/PageBuilder/default-page-builder.jsvar htmlContent = "";
var PageBuilder = function(pageName) {
if(pageName.charAt(0)!="/") pageName = "/"+pageName;
var VERSION = "0.0.6 July Storm";
var contentBuilder = "/OpenForum/Javascript/PageBuilder/default-page.build.js "+VERSION;
var renderer = js.getObject("/OpenForum/Javascript/Renderer","DefaultRenderer.sjs");
try{
var applyFields = function( pageName, timestamp, htmlContent ) {
var fields = [];
fields["pageName"] = pageName;
fields["title"] = openForum.wikiToTitleName(pageName);
fields["shortTitle"] = openForum.wikiToTitleName(pageName);
fields["author"] = "unknown";
fields["lastChangedBy"] = "unknown";
fields["referringPages"] = "";
fields["attachments"] = "";
fields["tags"] = "";
fields["time"] = timestamp;
try {
var data = JSON.parse( file.getAttachment(pageName,"data.json") );
for(var i in data) {
fields[i] = data[i];
}
} catch( e ) {}
// Replace template fields
var stepCount = 0;
var MAX_STEP_COUNTS = 10000;
var MAX_PAGE_LENGTH = 1000000;
//openForum.postMessageToQueue("test","Buidling Page:(" +pageName+ ") File:(page.html)");
var searchData = htmlContent;
htmlContent = "";
while( searchData.indexOf("&")!=-1 ) {
stepCount++;
if(stepCount>MAX_STEP_COUNTS) break;
var indexStart = searchData.indexOf("&");
htmlContent += searchData.substring(0,indexStart);
searchData = searchData.substring(indexStart+1);
var endIndex = searchData.indexOf(";");
if(endIndex==-1) {
htmlContent += "&";
break;
}
var fieldName = searchData.substring(0,endIndex);
if( typeof(fields[fieldName])!=="undefined" ) {
htmlContent += fields[fieldName];
searchData = searchData.substring(endIndex+1);
} else if(fieldName.indexOf("insert:")===0) {
var insertFile = fieldName.substring(7);
if(insertFile.indexOf("/")!=0) insertFile = pageName +"/"+ insertFile;
var insertPageName = ""+insertFile.substring(0,insertFile.lastIndexOf("/"));
var insertFileName = ""+insertFile.substring(insertFile.lastIndexOf("/")+1);
searchData = searchData.substring(endIndex+1);
if(file.attachmentExists(insertFile,"page.html.fragment")) {
searchData = "" + file.getAttachment(insertFile,"page.html.fragment") + "" + searchData;
} else if(file.attachmentExists(insertPageName,insertFileName)) {
searchData = "" + file.getAttachment(insertPageName,insertFileName) + "" + searchData;
} else {
searchData = "" + searchData;
}
} else {
htmlContent += "&";
}
if(htmlContent>MAX_PAGE_LENGTH) {
htmlContent += "";
break;
}
}
htmlContent += searchData;
try{
htmlContent = renderer.render(pageName,htmlContent);
} catch(e) {
log.error("Failed to render renderer "+renderer.getType()+" on "+pageName+" using "+contentBuilder+". Exception "+e+" on line number "+e.lineNumber);
}
return htmlContent;
};
// --- Build content fragments
var attachments=[];
//var pageName = "/Sandbox";
var matching = ".*\\.wiki\\.fragment"; //Regex include all wiki fragments
var list = file.getAttachmentsForPage( pageName );
if(pageName.charAt(0)!='/')
{
pageName = "/"+pageName;
}
var iterator= list.keySet().iterator();
while(iterator.hasNext())
{
var key = ""+iterator.next();
var item;
if(key.charAt(0)==='+') { // ignore sub pages
continue;
} else if(key.search( matching )==-1 ) {
continue;
} else {
var name = key.substring(0,key.indexOf(".wiki.fragment"));
var content = file.getAttachment(pageName,key);
file.saveAttachment(pageName,name+".html.fragment",renderer.render(pageName,content));
}
}
// --- End Build
var pageTemplate = file.getPageInheritedFileAsString(pageName,"page.html.template");
if( pageTemplate===null ) {
// Get header content
var headerTemplate = ""+file.getPageInheritedFileAsString(pageName,"header.html.template");
// Get footer content
var footerTemplate = ""+file.getPageInheritedFileAsString(pageName,"footer.html.template");
pageTemplate = headerTemplate + "" + "&content;" + "" + footerTemplate;
// Insert inserts
} else {
pageTemplate = ""+pageTemplate;
}
var savePage = true;
var errors = "";
var timestamp = new Date().getTime();
if (file.attachmentExists(pageName,"page.content")===false) {
file.saveAttachment(pageName,"page.content","");
log.info("Created empty page.content for "+pageName);
}
if (file.attachmentExists(pageName,"page.js")===false) {
file.saveAttachment(pageName,"page.js","");
log.info("Created empty page.js for "+pageName);
}
if (file.attachmentExists(pageName,"data.json")===false) {
file.saveAttachment(pageName,"data.json","{}");
log.info("Created empty data.json for "+pageName);
}
//if(typeof(content)==="undefined") { // Not sure what this is for.
errors += "No content supplied by caller";
content = file.getAttachment(pageName,"page.content");
timestamp = file.getAttachmentTimestamp(pageName,"page.content");
if(content===null) {
errors += "
No content found at "+pageName+"/page.content";
content = file.getAttachment(pageName,"page.wiki"); // deprecated
timestamp = file.getAttachmentTimestamp(pageName,"page.wiki");
log.warning("Content in page.wiki for "+pageName+" should be moved to page.content. page.wiki deprecated");
if(content===null) {
errors += "
No content found at "+pageName+"/page.wiki (deprecated)";
log.warning("No content for "+pageName);
}
}
/*} else {
savePage = false;
content = ""+content;
}*/
if(!content) {
htmlContent = "Page content missing. Errors:
Content:["+content+"]";
} else {
htmlContent = ""+content;
if(htmlContent.indexOf("\n")>0) {
var firstLine = htmlContent.substring(0,htmlContent.indexOf("\n"));
if(firstLine.substring(0,19)===""));
}
}
}
//log.debug("Content loaded for "+pageName);
var htmlFragment = "\n" + htmlContent;
htmlFragment = applyFields( pageName, timestamp, htmlFragment );
if(savePage===true) {
file.saveAttachment(pageName,"page.html.fragment",renderer.render(pageName,htmlFragment));
}
htmlContent = htmlContent + "\n";
htmlContent = pageTemplate.replace("&content;","\n"+htmlContent+"\n");
htmlContent = applyFields( pageName, timestamp, htmlContent );
//log.debug("Content built for "+pageName);
if(savePage===true) {
file.saveAttachment(pageName,"page.html",htmlContent);
}
log.info("Built "+pageName+" save:"+savePage);
} catch(e) {
message = "Failed to build "+pageName+" using "+contentBuilder+". Exception "+e+" on line number "+e.lineNumber;
log.error(message);
htmlContent += "\n";
htmlContent += "\n";
}
};
new PageBuilder(pageName);
htmlContent = ""+htmlContent;