taiga/src/site.c

123 lines
2.5 KiB
C
Raw Normal View History

2026-03-01 22:43:41 +09:00
#include <taiga.h>
2026-03-02 07:14:39 +09:00
xemil_t* skinconf;
2026-03-08 22:32:33 +09:00
void (*site_stylesheet)(FILE* out, const char* top); /* also create files here if you need one */
void (*site_head)(FILE* out, const char* top, xl_node_t* header);
void (*site_body)(FILE* out, const char* top, const char* title, xl_node_t* body);
2026-03-02 07:14:39 +09:00
2026-03-02 17:32:20 +09:00
static int scan(const char* top, const char* path) {
char* in = u_strvacat("site/content/", path, NULL);
char* out = u_strvacat("build/", path, NULL);
2026-03-01 23:36:33 +09:00
IO_DIR* dir;
2026-03-02 17:32:20 +09:00
2026-03-02 01:08:36 +09:00
io_mkdir(out, 0755);
2026-03-02 17:32:20 +09:00
if((dir = io_opendir(in)) != NULL) {
2026-03-01 23:36:33 +09:00
struct io_dirent* d;
2026-03-02 17:32:20 +09:00
while((d = io_readdir(dir)) != NULL) {
char* p;
2026-03-01 23:36:33 +09:00
struct io_stat s;
2026-03-08 22:32:33 +09:00
if(strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) continue;
2026-03-01 23:36:33 +09:00
2026-03-02 01:08:36 +09:00
p = u_strvacat(in, d->d_name, NULL);
2026-03-02 17:32:20 +09:00
if(io_stat(p, &s) == 0) {
if(IO_S_ISDIR(s.st_mode)) {
2026-03-02 01:08:36 +09:00
char* p2 = u_strvacat(d->d_name, "/", NULL);
2026-03-02 17:32:20 +09:00
char* t = u_strvacat(top, "../", NULL);
2026-03-02 21:51:27 +09:00
char* o = u_strvacat(out, d->d_name, NULL);
io_mkdir(o, 0755);
free(o);
2026-03-02 17:32:20 +09:00
if(!scan(t, p2)) {
2026-03-02 07:14:39 +09:00
free(t);
free(p2);
free(p);
io_closedir(dir);
free(out);
free(in);
return 0;
}
free(t);
2026-03-02 01:08:36 +09:00
free(p2);
2026-03-02 17:32:20 +09:00
} else if(strcmp(d->d_name, "index.xml") == 0) {
2026-03-02 07:14:39 +09:00
printf("%s:\n", p);
2026-03-02 17:32:20 +09:00
if(!process(top, out, p)) {
2026-03-02 07:14:39 +09:00
free(p);
io_closedir(dir);
free(out);
free(in);
return 0;
}
2026-03-02 21:51:27 +09:00
} else {
char* po = u_strvacat(out, d->d_name, NULL);
FILE* fin = fopen(p, "rb");
FILE* fout = fopen(po, "wb");
char buffer[4096];
int s;
while((s = fread(buffer, 1, sizeof(buffer), fin)) != 0) {
fwrite(buffer, 1, s, fout);
}
fclose(fout);
fclose(fin);
2026-03-01 23:36:33 +09:00
}
}
free(p);
}
io_closedir(dir);
}
2026-03-02 17:32:20 +09:00
2026-03-01 23:36:33 +09:00
free(out);
free(in);
2026-03-02 07:14:39 +09:00
return 1;
2026-03-01 23:36:33 +09:00
}
2026-03-08 22:32:33 +09:00
#define USE_THEME(x) \
site_stylesheet = x##_stylesheet; \
site_head = x##_head; \
site_body = x##_body;
2026-03-02 17:32:20 +09:00
int action_site(int argc, char** argv) {
int st = 0;
2026-03-02 07:14:39 +09:00
FILE* css;
2026-03-01 23:36:33 +09:00
io_mkdir("build", 0755);
2026-03-08 22:32:33 +09:00
USE_THEME(simple);
2026-03-05 03:55:09 +09:00
if((skinconf = xl_open_file("site/skinconf.xml")) == NULL ||
!xl_parse(skinconf)) {
2026-03-02 07:14:39 +09:00
fprintf(stderr, "Failed to parse site/skinconf.xml!\n");
st = 1;
goto cleanup;
}
2026-03-02 17:32:20 +09:00
if((css = fopen("build/style.css", "w")) == NULL) {
2026-03-02 07:14:39 +09:00
st = 1;
goto cleanup;
}
2026-03-08 22:32:33 +09:00
site_stylesheet(css, "build/");
2026-03-02 07:14:39 +09:00
fclose(css);
2026-03-02 17:32:20 +09:00
if(!scan("", "")) {
2026-03-02 07:14:39 +09:00
st = 1;
goto cleanup;
}
cleanup:;
2026-03-08 22:32:33 +09:00
if(skinconf != NULL) xl_close(skinconf);
2026-03-02 07:14:39 +09:00
2026-03-02 17:32:20 +09:00
if(st == 0) {
2026-03-02 07:14:39 +09:00
fprintf(stderr, "\nBuild successful\n");
2026-03-02 17:32:20 +09:00
} else {
2026-03-02 07:14:39 +09:00
fprintf(stderr, "\nBuild failed!\n");
}
2026-03-01 23:36:33 +09:00
2026-03-01 22:43:41 +09:00
return 0;
}