publish.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Publish.js
  3. This script publish the blog post object to a static HTML into the webroot
  4. Require paramter:
  5. filepath //The filepath of the post object
  6. webroot //The webroot of the current ArozOS user
  7. */
  8. requirelib("filelib");
  9. requirelib("appdata");
  10. includes("config.js");
  11. includes("helper.js");
  12. includes("indexManager.js");
  13. //Inject content into template
  14. function applyTemplate(templateFilepath, templateObjects){
  15. var templateContent = appdata.readFile(templateFilepath);
  16. if (templateContent == false){
  17. return false;
  18. }
  19. for (var i = 0; i < templateObjects.length; i++){
  20. var thisObject = templateObjects[i];
  21. templateContent = templateContent.split("{{" + thisObject.key + "}}").join(thisObject.value);
  22. }
  23. return templateContent;
  24. }
  25. function loadFooter(){
  26. var footerTemplate = appdata.readFile("Blog/template/footer.html");
  27. if (footerTemplate == false){
  28. return "Powered by ArozOS Blog Engine";
  29. }else{
  30. return footerTemplate;
  31. }
  32. }
  33. function loadBlogInfo(){
  34. return ["My Blog", "An amazing blog written by me!"]
  35. }
  36. //Main function to publish the file to webroot
  37. function publish(){
  38. //Check if the blog post object exists
  39. if (!filelib.fileExists(filepath)){
  40. retrunErrorObject("Given post not exists: " + filepath);
  41. return
  42. }
  43. //Get its content. Validate it correctless
  44. var content = filelib.readFile(filepath);
  45. if (typeof(content) == "undefined" || typeof(content) == "null" || content == ""){
  46. retrunErrorObject("Invalid blog post object");
  47. return
  48. }
  49. //Get the modification time of the file as blog date
  50. var modtime = filelib.mtime(filepath, false);
  51. var blogInfo = loadBlogInfo();
  52. var blogObject = JSON.parse(content);
  53. //Apply template
  54. var renderedPage = applyTemplate("Blog/template/post.html", [
  55. {key: "blog_title", value: blogInfo[0]},
  56. {key: "blog_subtitle", value: blogInfo[1]},
  57. {key: "post_date", value: modtime},
  58. {key: "post_title", value: blogObject.Title},
  59. {key: "post_tags", value: blogObject.Tags.join(", ")},
  60. {key: "post_content", value: blogObject.Content},
  61. {key: "footer", value: loadFooter()}
  62. ]);
  63. //Create the posts folder if not exists
  64. if (!filelib.fileExists(webroot + "/blog/posts/")){
  65. filelib.mkdir(webroot + "/blog/posts/");
  66. }
  67. //Write out to the user web root
  68. webroot = filepathClean(webroot);
  69. var postObjectName = filepath.split("/").pop();
  70. postObjectName = postObjectName.replace(".json", "");
  71. var targetPostLocation = webroot + "/blog/posts/" + postObjectName + ".html";
  72. filelib.writeFile(targetPostLocation, renderedPage);
  73. //Update the index file
  74. injectTitleIntoPostList(webroot, postObjectName)
  75. }
  76. publish();