taiga/src/default.c

281 lines
8.3 KiB
C
Raw Normal View History

2026-03-02 20:27:59 +09:00
#include <taiga.h>
static void print(FILE* out, const char* txt, int indent) {
int i;
for(i = 0;; i++) {
if(i == 0 || txt[i] == '\n' || txt[i] == 0) {
int j;
2026-03-05 03:55:09 +09:00
if(i > 0)
fprintf(out, "\n");
if(txt[i] == 0)
break;
for(j = 0; j < indent; j++)
fprintf(out, "\t");
2026-03-02 20:27:59 +09:00
}
2026-03-08 22:32:33 +09:00
if(txt[i] != '\n') fprintf(out, "%c", txt[i]);
2026-03-02 20:27:59 +09:00
}
}
static void accept_attr(char* text, xl_node_t* element, ...) {
xl_attribute_t* attr = element->first_attribute;
text[0] = 0;
while(attr != NULL) {
va_list va;
const char* s;
va_start(va, element);
while((s = va_arg(va, const char*)) != NULL) {
2026-03-05 03:55:09 +09:00
if(strcmp(attr->key, s) != 0)
continue;
2026-03-02 20:27:59 +09:00
if(attr->value == NULL) {
sprintf(text + strlen(text), " %s", attr->key);
} else {
sprintf(text + strlen(text), " %s=\"%s\"", attr->key, attr->value);
}
}
va_end(va);
attr = attr->next;
}
}
2026-03-03 01:31:01 +09:00
void default_head(FILE* out, const char* top, xl_node_t* element, int indent) {
xl_node_t* child;
char tag[2048];
char end[128];
int add = 0;
tag[0] = end[0] = 0;
2026-03-08 22:32:33 +09:00
if(element->name == NULL) return;
2026-03-03 01:31:01 +09:00
if(strcmp(element->name, "link") == 0) {
char text[2048];
accept_attr(text, element, "rel", "href", NULL);
sprintf(tag, "<%s%s>", element->name, text);
}
2026-03-08 22:32:33 +09:00
if(tag[0] != 0) print(out, tag, indent);
2026-03-03 01:31:01 +09:00
child = element->first_child;
while(child != NULL) {
if(child->type == XL_NODE_NODE && child->name != NULL) {
default_head(out, top, child, indent + 1 + add);
} else if(child->type == XL_NODE_TEXT && child->text != NULL) {
/* most likely junk */
#if 0
print(out, child->text, indent + 1 + add);
#endif
}
child = child->next;
}
2026-03-05 03:55:09 +09:00
if(end[0] != 0)
print(out, end, indent);
2026-03-03 01:31:01 +09:00
}
void default_body(FILE* out, const char* top, xl_node_t* element, int indent) {
2026-03-02 20:27:59 +09:00
xl_node_t* child;
char tag[2048]; /* enough for most cases... :) */
char end[128];
2026-03-03 20:57:58 +09:00
char text[2048];
2026-03-02 20:27:59 +09:00
int add = 0;
tag[0] = end[0] = 0;
2026-03-08 22:32:33 +09:00
if(element->name == NULL) return;
2026-03-02 20:27:59 +09:00
if(strcmp(element->name, "section") == 0) {
char* title = xl_get_attribute(element, "title");
sprintf(tag, "<div class=\"section\">%s</div>", title);
} else if(strcmp(element->name, "fixme") == 0 || /**/
strcmp(element->name, "warning") == 0 || /**/
strcmp(element->name, "note") == 0) {
2026-03-03 20:57:58 +09:00
char* say = "";
char author[2048];
char* attr;
author[0] = 0;
accept_attr(text, element, "id", "class", NULL);
2026-03-02 20:27:59 +09:00
if(strcmp(element->name, "fixme") == 0) {
2026-03-03 20:57:58 +09:00
say = "Fixme";
strcpy(author, " ()");
2026-03-08 22:32:33 +09:00
if((attr = xl_get_attribute(element, "author")) != NULL) sprintf(author, " (%s)", attr);
2026-03-02 20:27:59 +09:00
} else if(strcmp(element->name, "warning") == 0) {
2026-03-03 20:57:58 +09:00
say = "Warning";
2026-03-02 20:27:59 +09:00
} else if(strcmp(element->name, "note") == 0) {
2026-03-03 20:57:58 +09:00
say = "Note";
2026-03-02 20:27:59 +09:00
}
2026-03-08 22:32:33 +09:00
sprintf(tag + strlen(tag), "<div class=\"%s-message\"%s>\n", element->name, text);
sprintf(tag + strlen(tag), " <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%%\">\n");
2026-03-02 20:27:59 +09:00
sprintf(tag + strlen(tag), " <tr>\n");
2026-03-08 22:32:33 +09:00
sprintf(tag + strlen(tag), " <td rowspan=\"2\" class=\"message-icon\">\n");
2026-03-09 00:09:35 +09:00
sprintf(tag + strlen(tag), " <img src=\"%simage/%s.png\" alt=\"%s\" width=\"32\" height=\"32\">\n", top, element->name, say);
2026-03-02 20:27:59 +09:00
sprintf(tag + strlen(tag), " </td>\n");
2026-03-09 00:09:35 +09:00
sprintf(tag + strlen(tag), " <td class=\"message-title\">\n");
2026-03-08 22:32:33 +09:00
sprintf(tag + strlen(tag), " <b>%s%s</b>\n", say, author);
2026-03-02 20:27:59 +09:00
sprintf(tag + strlen(tag), " </td>\n");
sprintf(tag + strlen(tag), " </tr>\n");
sprintf(tag + strlen(tag), " <tr>\n");
sprintf(tag + strlen(tag), " <td width=\"100%%\">");
sprintf(end + strlen(end), " </td>\n");
sprintf(end + strlen(end), " </tr>\n");
sprintf(end + strlen(end), " </table>\n");
sprintf(end + strlen(end), "</div>", element->name);
add = 3;
} else if(strcmp(element->name, "p") == 0 || /**/
strcmp(element->name, "code") == 0 || /**/
strcmp(element->name, "blockquote") == 0 || /**/
strcmp(element->name, "li") == 0 || /**/
strcmp(element->name, "dt") == 0 || /**/
strcmp(element->name, "dd") == 0 || /**/
strcmp(element->name, "strong") == 0 || /**/
strcmp(element->name, "sub") == 0 || /**/
strcmp(element->name, "sup") == 0 || /**/
2026-03-03 20:57:58 +09:00
strcmp(element->name, "tr") == 0 || /**/
2026-03-09 00:09:35 +09:00
strcmp(element->name, "em") == 0 || /**/
strcmp(element->name, "h1") == 0 || /**/
strcmp(element->name, "h2") == 0 || /**/
strcmp(element->name, "h3") == 0 || /**/
strcmp(element->name, "h4") == 0 || /**/
strcmp(element->name, "h5") == 0 || /**/
strcmp(element->name, "h6") == 0) {
2026-03-03 20:57:58 +09:00
accept_attr(text, element, "id", "class", NULL);
sprintf(tag, "<%s%s>", element->name, text);
2026-03-02 20:27:59 +09:00
sprintf(end, "</%s>", element->name);
2026-03-03 20:57:58 +09:00
} else if(strcmp(element->name, "ul") == 0 || /**/
strcmp(element->name, "ol") == 0) {
accept_attr(text, element, "id", "class", "spacing", NULL);
2026-03-02 20:27:59 +09:00
2026-03-03 20:57:58 +09:00
sprintf(tag, "<%s%s>", element->name, text);
sprintf(end, "</%s>", element->name);
} else if(strcmp(element->name, "a") == 0) {
accept_attr(text, element, "id", "class", "href", "title", "rel", NULL);
2026-03-02 20:27:59 +09:00
sprintf(tag, "<%s%s>", element->name, text);
sprintf(end, "</%s>", element->name);
} else if(strcmp(element->name, "td") == 0 || /**/
strcmp(element->name, "th") == 0) {
2026-03-03 20:57:58 +09:00
accept_attr(text, element, "id", "class", "rowspan", "colspan", NULL);
2026-03-02 20:27:59 +09:00
sprintf(tag, "<%s%s>", element->name, text);
sprintf(end, "</%s>", element->name);
2026-03-03 20:57:58 +09:00
} else if(strcmp(element->name, "img") == 0) {
2026-03-09 00:43:35 +09:00
char* r;
char* w;
char* h;
int ws, hs;
2026-03-02 20:27:59 +09:00
2026-03-09 00:09:35 +09:00
accept_attr(text, element, "src", "alt", "title", "height", "width", "id", "class", NULL);
2026-03-03 20:57:58 +09:00
2026-03-09 00:43:35 +09:00
w = xl_get_attribute(element, "width");
h = xl_get_attribute(element, "height");
if((w == NULL || h == NULL) && (r = xl_get_attribute(element, "src")) != NULL) {
if(u_image_size(top, r, u_http_path, &ws, &hs)) {
if(w == NULL && h == NULL) {
sprintf(text + strlen(text), "width=\"%d\" height=\"%d\"", ws, hs);
} else if(w != NULL) {
sprintf(text + strlen(text), "width=\"%d\" height=\"%d\"", atoi(w), hs * atoi(w) / ws);
} else if(h != NULL) {
sprintf(text + strlen(text), "width=\"%d\" height=\"%d\"", ws * atoi(h) / hs, atoi(h));
}
}
}
2026-03-09 00:09:35 +09:00
sprintf(tag, "<%s%s border=\"0\">", element->name, text);
2026-03-03 20:57:58 +09:00
} else if(strcmp(element->name, "script") == 0) {
accept_attr(text, element, "src", "type", NULL);
2026-03-02 20:27:59 +09:00
sprintf(tag, "<%s%s>", element->name, text);
sprintf(end, "</%s>", element->name);
} else if(strcmp(element->name, "table") == 0) {
2026-03-03 20:57:58 +09:00
accept_attr(text, element, "id", "class", NULL);
2026-03-08 22:32:33 +09:00
sprintf(tag, "<%s%s class=\"grid\" width=\"100%%\" cellpadding=\"3\" cellspacing=\"2\" border=\"1\">", element->name, text);
2026-03-02 20:27:59 +09:00
sprintf(end, "</%s>", element->name);
2026-03-03 20:57:58 +09:00
} else if(strcmp(element->name, "hr") == 0 || /**/
strcmp(element->name, "br") == 0) {
accept_attr(text, element, "id", "class", NULL);
sprintf(tag, "<%s%text>", element->name, text);
2026-03-02 20:27:59 +09:00
}
2026-03-08 22:32:33 +09:00
if(tag[0] != 0) print(out, tag, indent);
2026-03-02 20:27:59 +09:00
child = element->first_child;
while(child != NULL) {
if(child->type == XL_NODE_NODE && child->name != NULL) {
2026-03-03 01:31:01 +09:00
default_body(out, top, child, indent + 1 + add);
2026-03-02 20:27:59 +09:00
} else if(child->type == XL_NODE_TEXT && child->text != NULL) {
2026-03-02 21:05:55 +09:00
print(out, child->text, indent + 1 + add);
2026-03-02 20:27:59 +09:00
}
child = child->next;
}
2026-03-08 22:32:33 +09:00
if(end[0] != 0) print(out, end, indent);
2026-03-02 21:05:55 +09:00
}
void default_nav(FILE* out, const char* top, xl_node_t* element, int indent) {
int i;
2026-03-08 22:32:33 +09:00
if(element->name == NULL) return;
2026-03-02 21:05:55 +09:00
if(strcmp(element->name, "link") == 0) {
char* link;
char* name;
2026-03-08 22:32:33 +09:00
if((link = xl_get_attribute(element, "href")) == NULL) link = "https://invalid.link";
if((name = xl_get_attribute(element, "name")) == NULL) name = "";
2026-03-02 21:05:55 +09:00
2026-03-03 01:31:01 +09:00
link = u_path(top, link);
2026-03-08 22:32:33 +09:00
for(i = 0; i < indent; i++) fprintf(out, "\t");
2026-03-02 21:27:26 +09:00
fprintf(out, " - <a href=\"%s\">%s</a><br>\n", link, name);
2026-03-03 01:31:01 +09:00
free(link);
2026-03-02 21:05:55 +09:00
} else if(strcmp(element->name, "group") == 0) {
char* title;
xl_node_t* child;
2026-03-08 22:32:33 +09:00
if((title = xl_get_attribute(element, "title")) == NULL) title = "";
2026-03-02 21:05:55 +09:00
if(title != NULL) {
2026-03-08 22:32:33 +09:00
for(i = 0; i < indent; i++) fprintf(out, "\t");
2026-03-02 21:05:55 +09:00
fprintf(out, "<div class=\"linkgrouptitle\"> - %s</div>\n", title);
}
2026-03-08 22:32:33 +09:00
for(i = 0; i < indent; i++) fprintf(out, "\t");
2026-03-02 21:05:55 +09:00
fprintf(out, "<div class=\"linkgroup\">\n");
child = element->first_child;
while(child != NULL) {
default_nav(out, top, child, indent + 1);
child = child->next;
}
2026-03-08 22:32:33 +09:00
for(i = 0; i < indent; i++) fprintf(out, "\t");
2026-03-02 21:05:55 +09:00
fprintf(out, "</div>\n");
}
2026-03-02 20:27:59 +09:00
}