taiga/src/process.c

98 lines
2.2 KiB
C
Raw Normal View History

2026-03-02 01:14:30 +09:00
#include <taiga.h>
2026-03-02 07:14:39 +09:00
static FILE* f;
2026-03-02 01:14:30 +09:00
2026-03-02 17:32:20 +09:00
static int parse_file(const char* top, const char* full) {
2026-03-02 07:14:39 +09:00
xemil_t* handle = xl_open_file(full);
2026-03-02 17:32:20 +09:00
int st = 1;
2026-03-02 01:14:30 +09:00
2026-03-02 07:14:39 +09:00
handle->new_text = 1;
2026-03-02 17:32:20 +09:00
if(handle != NULL && xl_parse(handle)) {
if(handle->root == NULL || strcmp(handle->root->name, "document") != 0) {
2026-03-02 07:14:39 +09:00
fprintf(stderr, "Root element has to be document\n");
st = 0;
}
2026-03-02 17:32:20 +09:00
if(st) {
char* title = NULL;
xl_node_t* node = NULL;
2026-03-02 07:14:39 +09:00
xl_node_t* header = NULL;
2026-03-02 17:32:20 +09:00
xl_node_t* body = NULL;
2026-03-02 07:14:39 +09:00
if(handle->root != NULL) node = handle->root->first_child;
2026-03-02 17:32:20 +09:00
while(node != NULL) {
if(node->type == XL_NODE_NODE && strcmp(node->name, "header") == 0) {
2026-03-02 07:14:39 +09:00
xl_node_t* n = node->first_child;
2026-03-02 17:32:20 +09:00
while(n != NULL) {
if(n->type == XL_NODE_NODE && strcmp(n->name, "title") == 0) {
2026-03-02 07:14:39 +09:00
if(title != NULL) free(title);
title = u_strdup(n->text);
}
n = n->next;
}
header = node;
2026-03-02 17:32:20 +09:00
} else if(node->type == XL_NODE_NODE && strcmp(node->name, "body") == 0) {
2026-03-02 07:14:39 +09:00
body = node;
}
node = node->next;
}
if(title == NULL) title = u_strdup("Untitled");
fprintf(f, "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
fprintf(f, "<html>\n");
fprintf(f, " <head>\n");
fprintf(f, " <meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">\n");
fprintf(f, " <title>%s</title>\n", title);
classic_head(f, top, header);
fprintf(f, " </head>\n");
fprintf(f, " <body>\n");
classic_body(f, top, title, body);
fprintf(f, " </body>\n");
fprintf(f, "</html>\n");
free(title);
}
2026-03-02 17:32:20 +09:00
} else {
2026-03-02 07:14:39 +09:00
fprintf(stderr, "Parse failed!\n", full);
st = 0;
}
if(handle != NULL) xl_close(handle);
return st;
}
2026-03-02 17:32:20 +09:00
int process(const char* top, const char* out, const char* full) {
2026-03-02 07:14:39 +09:00
char* fn = strrchr(full, '/');
if(fn != NULL) fn = fn + 1;
2026-03-02 17:32:20 +09:00
if(fn != NULL) {
2026-03-02 01:14:30 +09:00
char* n;
2026-03-02 07:14:39 +09:00
char* p;
2026-03-02 01:14:30 +09:00
2026-03-02 07:14:39 +09:00
fn = u_strdup(fn);
if((n = strrchr(fn, '.')) != NULL) n[0] = 0;
2026-03-02 01:14:30 +09:00
2026-03-02 07:14:39 +09:00
p = u_strvacat(out, fn, ".html", NULL);
2026-03-02 17:32:20 +09:00
if((f = fopen(p, "w")) != NULL) {
if(!parse_file(top, full)) {
2026-03-02 07:14:39 +09:00
fclose(f);
free(p);
free(fn);
return 0;
}
fclose(f);
}
free(p);
free(fn);
2026-03-02 01:14:30 +09:00
}
2026-03-02 07:14:39 +09:00
return 1;
2026-03-02 01:14:30 +09:00
}