/* Publish.js This script publish the blog post object to a static HTML into the webroot Require paramter: filepath //The filepath of the post object webroot //The webroot of the current ArozOS user */ requirelib("filelib"); requirelib("appdata"); includes("config.js"); includes("helper.js"); includes("indexManager.js"); //Inject content into template function applyTemplate(templateFilepath, templateObjects){ var templateContent = appdata.readFile(templateFilepath); if (templateContent == false){ return false; } for (var i = 0; i < templateObjects.length; i++){ var thisObject = templateObjects[i]; templateContent = templateContent.split("{{" + thisObject.key + "}}").join(thisObject.value); } return templateContent; } function loadFooter(){ var footerTemplate = appdata.readFile("Blog/template/footer.html"); if (footerTemplate == false){ return "Powered by ArozOS Blog Engine"; }else{ return footerTemplate; } } function loadBlogInfo(){ return ["My Blog", "An amazing blog written by me!"] } //Main function to publish the file to webroot function publish(){ //Check if the blog post object exists if (!filelib.fileExists(filepath)){ retrunErrorObject("Given post not exists: " + filepath); return } //Get its content. Validate it correctless var content = filelib.readFile(filepath); if (typeof(content) == "undefined" || typeof(content) == "null" || content == ""){ retrunErrorObject("Invalid blog post object"); return } //Get the modification time of the file as blog date var modtime = filelib.mtime(filepath, false); var blogInfo = loadBlogInfo(); var blogObject = JSON.parse(content); //Apply template var renderedPage = applyTemplate("Blog/template/post.html", [ {key: "blog_title", value: blogInfo[0]}, {key: "blog_subtitle", value: blogInfo[1]}, {key: "post_date", value: modtime}, {key: "post_title", value: blogObject.Title}, {key: "post_tags", value: blogObject.Tags.join(", ")}, {key: "post_content", value: blogObject.Content}, {key: "footer", value: loadFooter()} ]); //Create the posts folder if not exists if (!filelib.fileExists(webroot + "/blog/posts/")){ filelib.mkdir(webroot + "/blog/posts/"); } //Write out to the user web root webroot = filepathClean(webroot); var postObjectName = filepath.split("/").pop(); postObjectName = postObjectName.replace(".json", ""); var targetPostLocation = webroot + "/blog/posts/" + postObjectName + ".html"; filelib.writeFile(targetPostLocation, renderedPage); //Update the index file injectTitleIntoPostList(webroot, postObjectName) } publish();