123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <dirent.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
-
- #include "cms_build.h"
- #include "cms_template.h"
- #include "cms_err.h"
- #include "cms_files.h"
- #include "cms_util.h"
- #include "cms_page.h"
- #include "cms_post.h"
-
- #include <stdio.h>
-
- static char* make_post(cms_page* parent, cms_post post, char* rootpath)
- {
- char* fstr;
- char* fname;
- cms_template_args* args;
- cms_err* err;
-
- char* str;
-
- //Template index.html
- fname = cms_util_path_join(rootpath, CMS_FILE_THEME "/html/index.html");
- if (fname == NULL) return NULL;
-
- fstr = cms_util_file_read(fname);
- if (fstr == NULL) return NULL;
-
- args = cms_template_args_create();
-
- err = cms_template_args_append(args, "articles", post.html);
- if (err) return NULL;
-
- str = cms_templateify(fstr, args);
- if (str == NULL) return NULL;
-
- free(fstr);
- free(fname);
- free(args);
- free(err);
-
- return str;
- }
-
- cms_err* cms_build_write_files(cms_page* root, char* path, char* rootpath)
- {
- printf(
- "page: '%s', n posts: %i, n subs: %i\n",
- root->title,
- (int)root->numposts,
- (int)root->numsubs
- );
-
- char* dirpath = cms_util_path_join(path, root->slug);
- if (dirpath == NULL)
- return cms_err_create(CMS_ERR_ALLOC, NULL);
-
- if (mkdir(dirpath, 0777) == -1 && errno != EEXIST)
- return cms_err_from_std_err(errno, dirpath);
-
- //Go through each post in the page, writing it to disk
- size_t i;
- for (i = 0; i < (root->numposts); ++i)
- {
- cms_post post = root->posts[i];
-
- char* postdirpath = cms_util_path_join(dirpath, post.slug);
- if (postdirpath == NULL)
- return cms_err_create(CMS_ERR_ALLOC, NULL);
-
- if (mkdir(postdirpath, 0777) == -1 && errno != EEXIST)
- return cms_err_from_std_err(errno, postdirpath);
-
- char* filepath = cms_util_path_join(postdirpath, CMS_FILE_INDEX);
- if (postdirpath == NULL)
- return cms_err_create(CMS_ERR_ALLOC, NULL);
-
- int file = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0777);
- if (file == -1)
- return cms_err_from_std_err(errno, NULL);
-
- //Create the HTML for the post
- char* html = make_post(root, post, rootpath);
- if (html == NULL)
- return cms_err_create(CMS_ERR_ALLOC, NULL);
-
- //Write post's HTML file
- ssize_t nbytes = write(file, html, strlen(html));
- if (nbytes == -1)
- return cms_err_from_std_err(errno, NULL);
-
- close(file);
- free(filepath);
- free(postdirpath);
- }
-
- //Go through each sub in the page recursively
- for (i = 0; i < root->numsubs; ++i)
- {
- cms_page sub = root->subs[i];
-
- char* subdirpath = cms_util_path_join(dirpath, sub.slug);
- if (subdirpath == NULL)
- return cms_err_create(CMS_ERR_ALLOC, NULL);
-
- if (mkdir(subdirpath, 0777) == -1 && errno != EEXIST)
- return cms_err_from_std_err(errno, subdirpath);
-
- cms_err* err = cms_build_write_files(&sub, subdirpath, rootpath);
- if (err)
- return err;
-
- free(subdirpath);
- }
-
- free(dirpath);
-
- return cms_err_create(CMS_ERR_NONE, NULL);
- }
-
- cms_err* cms_build_make_tree(cms_page* root, char* path, char* dirname)
- {
- struct dirent** namelist;
- int n = scandir(path, &namelist, 0, alphasort);
- if (n == -1)
- return cms_err_from_std_err(errno, NULL);
-
- struct dirent* ep;
-
- struct stat* st = malloc(sizeof(struct stat));
- if (st == NULL)
- return cms_err_create(CMS_ERR_ALLOC, NULL);
-
- for (int i = 0; i < n; ++i)
- {
- ep = namelist[i];
- if (ep->d_name[0] == '.')
- continue;
-
- char* entpath = cms_util_path_join(path, ep->d_name);
- stat(entpath, st);
-
- //Entry is directory, recurse
- if (S_ISDIR(st->st_mode))
- {
- cms_page* sub = cms_page_create();
- if (sub == NULL)
- return cms_err_create(CMS_ERR_ALLOC, NULL);
-
- cms_err* err;
- err = cms_build_make_tree(sub, entpath, ep->d_name);
- if (err)
- return err;
-
- err = cms_page_add_sub(root, sub);
- if (err)
- return err;
- }
-
- //Entry is the file which contains metadata
- //about a page, parse
- else if (strcmp(ep->d_name, CMS_FILE_PAGE) == 0)
- {
- char* content = cms_util_file_read(entpath);
- if (content == NULL)
- return cms_err_create(CMS_ERR_FILEREAD, entpath);
-
- cms_err* err = cms_page_parse(root, content, dirname);
- free(content);
- if (err)
- return err;
- }
-
- //Entry is a post, read it and add to page
- else
- {
- cms_post* post = cms_post_create();
-
- char* content = cms_util_file_read(entpath);
- if (content == NULL)
- return cms_err_create(CMS_ERR_FILEREAD, entpath);
-
- cms_err* err;
- err = cms_post_parse(post, content, ep->d_name);
- free(content);
- if (err)
- return err;
-
- err = cms_page_add_post(root, post);
- if (err)
- return err;
- }
-
- free(entpath);
- }
-
- free(st);
- free(namelist);
-
- return cms_err_create(CMS_ERR_NONE, NULL);
- }
|