/* ListPost.js Author: tobychui This script list all the post within the system */ //Require the file library requirelib("filelib"); //Require the internal library for handling the path of blog posting includes("config.js"); var blogPostStorage = getBlogPostStore(); function extractPostInfo(filepath){ var content = filelib.readFile(filepath); var postObject = JSON.parse(content); var title = "Untitled"; var content = "(This post has no content)"; var tags = []; if (typeof postObject.Title != "undefined"){ title = postObject.Title } if (typeof postObject.Content != "undefined"){ content = postObject.Content; if (content.split(" ").length > 100){ //Trim the content off if it is longer than 100 words var words = content.split(" "); var trimmedContent = ""; for (var i = 0; i < 100; i++){ trimmedContent = trimmedContent + words[i] + " "; } trimmedContent = trimmedContent + "..." content = trimmedContent; }else if (content.length > 300){ //To handle lang with no space, like Cantonese var trimmedContent = content.substr(0, 300) + "..." content = trimmedContent; } } if (typeof postObject.Tags != "undefined"){ tags = postObject.Tags } return [title, content, tags]; } function main(){ //Filter out the last / if exists if (blogPostStorage.substr(blogPostStorage.length - 1, 1) == "/"){ blogPostStorage = blogPostStorage.substr(0, blogPostStorage.length - 1); } //If it doesn't exists filelib.mkdir(blogPostStorage + "/public"); filelib.mkdir(blogPostStorage + "/private"); //List all the created post var publicPosts = filelib.aglob(blogPostStorage + "/public/*.json","reverse"); var privatePosts = filelib.aglob(blogPostStorage + "/private/*.json","reverse"); var posts = []; for(var i = 0; i < publicPosts.length; i++){ //Extract post information var postInfo = extractPostInfo( publicPosts[i]); //Get the poost modification time var modTime = filelib.mtime(publicPosts[i], false); posts.push({ "title": postInfo[0], "content":postInfo[1], "tags": postInfo[2], "modtime": modTime, "view": "public", "filepath": publicPosts[i] }); } for(var i = 0; i < privatePosts.length; i++){ var postInfo = extractPostInfo( privatePosts[i]); //Get the poost modification time var modTime = filelib.mtime(privatePosts[i], false); posts.push({ "title": postInfo[0], "content":postInfo[1], "tags": postInfo[2], "modtime": modTime, "view": "private", "filepath": privatePosts[i] }); } //Return the value of the posts sendJSONResp(JSON.stringify(posts)); } main();