listPosts.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. ListPost.js
  3. Author: tobychui
  4. This script list all the post within the system
  5. */
  6. //Require the file library
  7. requirelib("filelib");
  8. //Require the internal library for handling the path of blog posting
  9. includes("config.js");
  10. var blogPostStorage = getBlogPostStore();
  11. function extractPostInfo(filepath){
  12. var content = filelib.readFile(filepath);
  13. var postObject = JSON.parse(content);
  14. var title = "Untitled";
  15. var content = "(This post has no content)";
  16. var tags = [];
  17. if (typeof postObject.Title != "undefined"){
  18. title = postObject.Title
  19. }
  20. if (typeof postObject.Content != "undefined"){
  21. content = postObject.Content;
  22. if (content.split(" ").length > 100){
  23. //Trim the content off if it is longer than 100 words
  24. var words = content.split(" ");
  25. var trimmedContent = "";
  26. for (var i = 0; i < 100; i++){
  27. trimmedContent = trimmedContent + words[i] + " ";
  28. }
  29. trimmedContent = trimmedContent + "..."
  30. content = trimmedContent;
  31. }else if (content.length > 300){
  32. //To handle lang with no space, like Cantonese
  33. var trimmedContent = content.substr(0, 300) + "..."
  34. content = trimmedContent;
  35. }
  36. }
  37. if (typeof postObject.Tags != "undefined"){
  38. tags = postObject.Tags
  39. }
  40. return [title, content, tags];
  41. }
  42. function main(){
  43. //Filter out the last / if exists
  44. if (blogPostStorage.substr(blogPostStorage.length - 1, 1) == "/"){
  45. blogPostStorage = blogPostStorage.substr(0, blogPostStorage.length - 1);
  46. }
  47. //If it doesn't exists
  48. filelib.mkdir(blogPostStorage + "/public");
  49. filelib.mkdir(blogPostStorage + "/private");
  50. //List all the created post
  51. var publicPosts = filelib.aglob(blogPostStorage + "/public/*.json","reverse");
  52. var privatePosts = filelib.aglob(blogPostStorage + "/private/*.json","reverse");
  53. var posts = [];
  54. for(var i = 0; i < publicPosts.length; i++){
  55. //Extract post information
  56. var postInfo = extractPostInfo( publicPosts[i]);
  57. //Get the poost modification time
  58. var modTime = filelib.mtime(publicPosts[i], false);
  59. posts.push({
  60. "title": postInfo[0],
  61. "content":postInfo[1],
  62. "tags": postInfo[2],
  63. "modtime": modTime,
  64. "view": "public",
  65. "filepath": publicPosts[i]
  66. });
  67. }
  68. for(var i = 0; i < privatePosts.length; i++){
  69. var postInfo = extractPostInfo( privatePosts[i]);
  70. //Get the poost modification time
  71. var modTime = filelib.mtime(privatePosts[i], false);
  72. posts.push({
  73. "title": postInfo[0],
  74. "content":postInfo[1],
  75. "tags": postInfo[2],
  76. "modtime": modTime,
  77. "view": "private",
  78. "filepath": privatePosts[i]
  79. });
  80. }
  81. //Return the value of the posts
  82. sendJSONResp(JSON.stringify(posts));
  83. }
  84. main();