A static site generator, written in C
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "cms_util.h"
  5. #include "cms_err.h"
  6. #include "cms_files.h"
  7. #include "cms_page.h"
  8. #include "cms_build.h"
  9. //Temporary, for testing purposes
  10. static void print_page_tree(cms_page* page)
  11. {
  12. if (page->title)
  13. printf("Page: %s\n", page->title);
  14. //Loop over posts
  15. size_t i;
  16. for (i = 0; i < page->numposts; ++i)
  17. {
  18. printf("printing post\n");
  19. printf("\tPost: %s\n", (page->posts[i]).title);
  20. }
  21. //Loop over subs
  22. for (i = 0; i < page->numsubs; ++i)
  23. {
  24. print_page_tree(&page->subs[i]);
  25. }
  26. printf("---------\n");
  27. }
  28. int main(int argc, char** argv)
  29. {
  30. if (argc < 2)
  31. {
  32. fprintf(stderr, "Usage: %s <init|build>\n", argv[0]);
  33. return 1;
  34. }
  35. //Initiate
  36. if (strcmp(argv[1], "init") == 0)
  37. {
  38. if (argc != 3)
  39. {
  40. fprintf(stderr, "Usage: %s init <directory>\n", argv[0]);
  41. return 1;
  42. }
  43. char* dirname = argv[2];
  44. //Get the path of .cmsinited, which tells us
  45. //whether or not the directory is already inited
  46. char* initedPath = cms_util_path_join(dirname, CMS_FILE_INITED);
  47. if (initedPath == NULL)
  48. cms_err_panic(cms_err_create(CMS_ERR_ALLOC, NULL));
  49. //Panic if the directory is already initiated
  50. if (cms_util_file_exists(initedPath))
  51. cms_err_panic(cms_err_create(CMS_ERR_INITED, NULL));
  52. //Copy files from resources
  53. cms_err* err;
  54. err = cms_util_dir_copy_recursive(CMS_FILE_RESOURCES, dirname);
  55. if (err)
  56. cms_err_panic(err);
  57. //Create .cmsinited file
  58. err = cms_util_file_create(initedPath);
  59. if (err)
  60. cms_err_panic(err);
  61. }
  62. //Build
  63. else if (strcmp(argv[1], "build") == 0)
  64. {
  65. if (argc != 3)
  66. {
  67. fprintf(stderr, "Usage: %s build <directory>\n", argv[0]);
  68. return 1;
  69. }
  70. char* dirname = argv[2];
  71. //Get the path of .cmsinited, which tells us
  72. //whether or not the directory is already inited
  73. char* initedPath = cms_util_path_join(dirname, CMS_FILE_INITED);
  74. //Panic if the directory isn't initiated
  75. if (!cms_util_file_exists(initedPath))
  76. cms_err_panic(cms_err_create(CMS_ERR_NOTINITED, NULL));
  77. cms_page* root = cms_page_create();
  78. if (root == NULL)
  79. cms_err_panic(cms_err_create(CMS_ERR_ALLOC, NULL));
  80. char* path = cms_util_path_join(dirname, CMS_FILE_ROOT);
  81. if (path == NULL)
  82. cms_err_panic(cms_err_create(CMS_ERR_ALLOC, NULL));
  83. //Build tree of pages and posts
  84. cms_err* err;
  85. err = cms_build_make_tree(root, path, NULL);
  86. free(path);
  87. if (err)
  88. cms_err_panic(err);
  89. char* outpath = cms_util_path_join(dirname, CMS_FILE_OUT);
  90. if (outpath == NULL)
  91. cms_err_panic(cms_err_create(CMS_ERR_ALLOC, NULL));
  92. cms_build_write_files(root, outpath);
  93. print_page_tree(root);
  94. }
  95. //Nothing, print usage
  96. else
  97. {
  98. fprintf(stderr, "Usage: %s <init|build>\n", argv[0]);
  99. return 1;
  100. }
  101. }