|
@@ -4,6 +4,8 @@
|
|
|
|
|
|
This script list all the post within the system
|
|
|
|
|
|
+ Require paramter:
|
|
|
+ webroot //The web deploy root of this user
|
|
|
*/
|
|
|
|
|
|
//Require the file library
|
|
@@ -11,99 +13,47 @@ requirelib("filelib");
|
|
|
|
|
|
//Require the internal library for handling the path of blog posting
|
|
|
includes("config.js");
|
|
|
+includes("helper.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);
|
|
|
- }
|
|
|
+ blogPostStorage = filepathClean(blogPostStorage);
|
|
|
|
|
|
- //If it doesn't exists
|
|
|
- filelib.mkdir(blogPostStorage + "/public");
|
|
|
- filelib.mkdir(blogPostStorage + "/private");
|
|
|
+ filelib.mkdir(blogPostStorage + "/posts/");
|
|
|
|
|
|
//List all the created post
|
|
|
- var publicPosts = filelib.aglob(blogPostStorage + "/public/*.json","reverse");
|
|
|
- var privatePosts = filelib.aglob(blogPostStorage + "/private/*.json","reverse");
|
|
|
+ var posts = filelib.aglob(blogPostStorage + "/posts/*.json","reverse");
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- var posts = [];
|
|
|
- for(var i = 0; i < publicPosts.length; i++){
|
|
|
+ var renderedPostList = [];
|
|
|
+ for(var i = 0; i < posts.length; i++){
|
|
|
//Extract post information
|
|
|
- var postInfo = extractPostInfo( publicPosts[i]);
|
|
|
+ var postInfo = extractPostInfo( posts[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);
|
|
|
+ var modTime = filelib.mtime(posts[i], false);
|
|
|
+
|
|
|
+ //Check if this post is published
|
|
|
+ webroot = filepathClean(webroot);
|
|
|
+ var postView = "private";
|
|
|
+ var postfilename = posts[i].split("/").pop().replace(".json", ".html");
|
|
|
+ if (filelib.fileExists(webroot + "/blog/posts/" + postfilename)){
|
|
|
+ postView = "public";
|
|
|
+ }
|
|
|
|
|
|
- posts.push({
|
|
|
+ renderedPostList.push({
|
|
|
"title": postInfo[0],
|
|
|
"content":postInfo[1],
|
|
|
"tags": postInfo[2],
|
|
|
"modtime": modTime,
|
|
|
- "view": "private",
|
|
|
- "filepath": privatePosts[i]
|
|
|
+ "view": postView,
|
|
|
+ "filepath": posts[i]
|
|
|
});
|
|
|
}
|
|
|
|
|
|
//Return the value of the posts
|
|
|
- sendJSONResp(JSON.stringify(posts));
|
|
|
+ sendJSONResp(JSON.stringify(renderedPostList));
|
|
|
}
|
|
|
|
|
|
main();
|