diff --git a/.gitmodules b/.gitmodules index 94b0077..9e34276 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "external/md4c"] path = external/md4c url = https://github.com/mity/md4c +[submodule "external/stb"] + path = external/stb + url = https://github.com/nothings/stb diff --git a/Makefile b/Makefile index c3df66b..5d6b0f3 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC ?= cc -CFLAGS ?= -I external/xemil/include -I external/md4c/src -I src $(DEFS) +CFLAGS ?= -I external/xemil/include -I external/md4c/src -I external/stb -I src $(DEFS) LDFLAGS ?= LIBS ?= AFTER ?= diff --git a/external/stb b/external/stb new file mode 160000 index 0000000..2fb8c5a --- /dev/null +++ b/external/stb @@ -0,0 +1 @@ +Subproject commit 2fb8c5a3deb2110c89669f8d6f36e5833b556b44 diff --git a/objs.mk b/objs.mk index ae2fe44..62724f1 100644 --- a/objs.mk +++ b/objs.mk @@ -29,6 +29,9 @@ src/src_action_help.o: src/action/help.c OBJS += src/src_action_markdown.o src/src_action_markdown.o: src/action/markdown.c $(CC) $(CFLAGS) -c -o $@ src/action/markdown.c +OBJS += src/src_action_markdown_dir.o +src/src_action_markdown_dir.o: src/action/markdown_dir.c + $(CC) $(CFLAGS) -c -o $@ src/action/markdown_dir.c OBJS += src/src_action_seed.o src/src_action_seed.o: src/action/seed.c $(CC) $(CFLAGS) -c -o $@ src/action/seed.c @@ -80,6 +83,9 @@ src/src_skin_classic.o: src/skin/classic.c OBJS += src/src_skin_simple.o src/src_skin_simple.o: src/skin/simple.c $(CC) $(CFLAGS) -c -o $@ src/skin/simple.c +OBJS += src/src_stb_ds.o +src/src_stb_ds.o: src/stb_ds.c + $(CC) $(CFLAGS) -c -o $@ src/stb_ds.c OBJS += src/src_util.o src/src_util.o: src/util.c $(CC) $(CFLAGS) -c -o $@ src/util.c diff --git a/src/action/help.c b/src/action/help.c index f32cf58..9d810ea 100644 --- a/src/action/help.c +++ b/src/action/help.c @@ -2,9 +2,10 @@ int action_help(int argc, char** argv) { printf("Actions:\n"); - printf(" help Show this help\n"); - printf(" seed Creates a new website\n"); - printf(" site Builds an website (default action)\n"); - printf(" markdown Converts Markdown document to Taiga document\n"); + printf(" help Show this help\n"); + printf(" seed Creates a new website\n"); + printf(" site Builds an website (default action)\n"); + printf(" markdown Converts Markdown document to Taiga document\n"); + printf(" markdown-dir Converts Markdown document directory to Taiga document directory\n"); return 0; } diff --git a/src/action/markdown.c b/src/action/markdown.c index 57f1b15..dc97580 100644 --- a/src/action/markdown.c +++ b/src/action/markdown.c @@ -454,6 +454,7 @@ int action_markdown(int argc, char** argv) { indent--; if(title != NULL) free(title); + title = NULL; free(buffer); diff --git a/src/action/markdown_dir.c b/src/action/markdown_dir.c new file mode 100644 index 0000000..486b179 --- /dev/null +++ b/src/action/markdown_dir.c @@ -0,0 +1,104 @@ +#include + +static int scan(const char* inp, const char* path) { + char* in = u_strvacat(inp, "/", path, NULL); + char* out = u_strvacat("site/content/", path, NULL); + IO_DIR* dir; + + io_mkdir(out, 0755); + if((dir = io_opendir(in)) != NULL) { + struct io_dirent* d; + + while((d = io_readdir(dir)) != NULL) { + char* p; + struct io_stat s; + + if(strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) continue; + + p = u_strvacat(in, d->d_name, NULL); + if(io_stat(p, &s) == 0) { + char* str; + int is_md = 0; + + str = strrchr(d->d_name, '.'); + if(str != NULL && strcmp(str, ".md") == 0) is_md = 1; + + if(IO_S_ISDIR(s.st_mode)) { + char* p2 = u_strvacat(d->d_name, "/", NULL); + char* o = u_strvacat(out, d->d_name, NULL); + + io_mkdir(o, 0755); + free(o); + if(!scan(inp, p2)) { + free(p2); + free(p); + io_closedir(dir); + free(out); + free(in); + + return 0; + } + free(p2); + } else if(is_md) { + char** argv = NULL; + char* o = NULL; + + printf("%s:\n", p); + + arrput(argv, p); + if(strcmp(d->d_name, "README.md") == 0) { + o = u_strvacat(out, "index.xml", NULL); + } else { + char* n = u_strdup(d->d_name); + char* str; + char* d; + if((str = strchr(n, '.')) != NULL) str[0] = 0; + + d = u_strvacat(out, n, NULL); + o = u_strvacat(d, "/index.xml", NULL); + io_mkdir(d, 0755); + free(d); + + free(n); + } + + if(o != NULL) arrput(argv, o); + + if(action_markdown(arrlen(argv), argv)) { + free(o); + arrfree(argv); + free(p); + io_closedir(dir); + free(out); + free(in); + return 0; + } + free(o); + arrfree(argv); + } + } + free(p); + } + + io_closedir(dir); + } + + free(out); + free(in); + + return 1; +} + +int action_markdown_dir(int argc, char** argv) { + if(argc != 1) { + fprintf(stderr, "Usage: markdown-dir input\n"); + + return 1; + } + + if(!scan(argv[0], "")) { + return 1; + } + + return 0; +} diff --git a/src/main.c b/src/main.c index 424912a..f592a5e 100644 --- a/src/main.c +++ b/src/main.c @@ -32,6 +32,8 @@ int main(int argc, char** argv) { return action_seed(target_argc, target_argv); } else if(strcmp(action, "markdown") == 0) { return action_markdown(target_argc, target_argv); + } else if(strcmp(action, "markdown-dir") == 0) { + return action_markdown_dir(target_argc, target_argv); } fprintf(stderr, "Unknown action\n"); diff --git a/src/skin/classic.c b/src/skin/classic.c index 08220a6..dd15840 100644 --- a/src/skin/classic.c +++ b/src/skin/classic.c @@ -232,7 +232,7 @@ void classic_body(FILE* out, const char* top, const char* title, xl_node_t* body text = u_path(top, text); fprintf(out, " \"%s\"\n", link, text, name, size); - fprintf(out, " \"filling\"\n", top); + fprintf(out, " \"filling\"\n", top); free(text); } diff --git a/src/stb_ds.c b/src/stb_ds.c new file mode 100644 index 0000000..c6f2c2a --- /dev/null +++ b/src/stb_ds.c @@ -0,0 +1,2 @@ +#define STB_DS_IMPLEMENTATION +#include diff --git a/src/taiga.h b/src/taiga.h index 1973a3a..1311cbc 100644 --- a/src/taiga.h +++ b/src/taiga.h @@ -18,6 +18,7 @@ #include #include +#include #ifdef _WIN32 #define io_mkdir(x, y) _mkdir(x) @@ -65,6 +66,9 @@ int action_seed(int argc, char** argv); /* action/markdown.c */ int action_markdown(int argc, char** argv); +/* action/markdown_dir.c */ +int action_markdown_dir(int argc, char** argv); + /* util.c */ char* u_strvacat(const char* str, ...); char* u_strdup(const char* str);