#include 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; if(i > 0) fprintf(out, "\n"); if(txt[i] == 0) break; for(j = 0; j < indent; j++) fprintf(out, "\t"); } if(txt[i] != '\n') fprintf(out, "%c", txt[i]); } } static void accept_attr(const char* top, char* text, int spec, 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) { if(strcmp(attr->key, s) != 0) continue; if(attr->value == NULL) { sprintf(text + strlen(text), " %s", attr->key); } else { char* t; if(spec && (strcmp(attr->key, "href") == 0 || strcmp(attr->key, "src") == 0)) { t = u_path(top, attr->value); } else { t = u_strdup(attr->value); } sprintf(text + strlen(text), " %s=\"%s\"", attr->key, t); free(t); } } va_end(va); attr = attr->next; } } 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; if(element->name == NULL) return; if(strcmp(element->name, "link") == 0) { char text[2048]; accept_attr(top, text, 0, element, "rel", "href", NULL); sprintf(tag, "<%s%s>", element->name, text); } if(tag[0] != 0) print(out, tag, indent); 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; } if(end[0] != 0) print(out, end, indent); } void default_body(FILE* out, const char* top, xl_node_t* element, int spec, int indent) { xl_node_t* child; char tag[2048]; /* enough for most cases... :) */ char end[128]; char text[2048]; int add = 0; tag[0] = end[0] = 0; if(element->name == NULL) return; if(strcmp(element->name, "section") == 0) { char* title = xl_get_attribute(element, "title"); sprintf(tag, "
%s
", title); } else if(strcmp(element->name, "fixme") == 0 || /**/ strcmp(element->name, "warning") == 0 || /**/ strcmp(element->name, "note") == 0) { char* say = ""; char author[2048]; char* attr; author[0] = 0; accept_attr(top, text, 0, element, "id", "class", NULL); if(strcmp(element->name, "fixme") == 0) { say = "Fixme"; strcpy(author, " ()"); if((attr = xl_get_attribute(element, "author")) != NULL) sprintf(author, " (%s)", attr); } else if(strcmp(element->name, "warning") == 0) { say = "Warning"; } else if(strcmp(element->name, "note") == 0) { say = "Note"; } sprintf(tag + strlen(tag), "
\n", element->name, text); sprintf(tag + strlen(tag), " \n"); sprintf(tag + strlen(tag), " \n"); sprintf(tag + strlen(tag), " \n"); sprintf(tag + strlen(tag), " \n"); sprintf(tag + strlen(tag), " \n"); sprintf(tag + strlen(tag), " \n"); sprintf(tag + strlen(tag), " \n"); sprintf(end + strlen(end), " \n"); sprintf(end + strlen(end), "
\n"); sprintf(tag + strlen(tag), " \"%s\"\n", top, element->name, say); sprintf(tag + strlen(tag), " \n"); sprintf(tag + strlen(tag), " %s%s\n", say, author); sprintf(tag + strlen(tag), "
"); sprintf(end + strlen(end), "
\n"); sprintf(end + strlen(end), "
", 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 || /**/ strcmp(element->name, "tr") == 0 || /**/ 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) { accept_attr(top, text, 0, element, "id", "class", NULL); sprintf(tag, "<%s%s>", element->name, text); sprintf(end, "", element->name); } else if(strcmp(element->name, "ul") == 0 || /**/ strcmp(element->name, "ol") == 0) { accept_attr(top, text, 0, element, "id", "class", "spacing", NULL); sprintf(tag, "<%s%s>", element->name, text); sprintf(end, "", element->name); } else if(strcmp(element->name, "a") == 0) { accept_attr(top, text, 0, element, "id", "class", "href", "title", "rel", NULL); sprintf(tag, "<%s%s>", element->name, text); sprintf(end, "", element->name); } else if(strcmp(element->name, "td") == 0 || /**/ strcmp(element->name, "th") == 0) { accept_attr(top, text, 0, element, "id", "class", "rowspan", "colspan", NULL); sprintf(tag, "<%s%s>", element->name, text); sprintf(end, "", element->name); } else if(strcmp(element->name, "img") == 0) { char* r; char* w; char* h; int ws, hs; accept_attr(top, text, 0, element, "src", "alt", "title", "height", "width", "id", "class", NULL); 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)); } } } sprintf(tag, "<%s%s border=\"0\">", element->name, text); } else if(strcmp(element->name, "script") == 0) { accept_attr(top, text, 0, element, "src", "type", NULL); sprintf(tag, "<%s%s>", element->name, text); sprintf(end, "", element->name); } else if(strcmp(element->name, "table") == 0) { accept_attr(top, text, 0, element, "id", "class", NULL); sprintf(tag, "<%s%s class=\"grid\" width=\"100%%\" cellpadding=\"3\" cellspacing=\"2\" border=\"1\">", element->name, text); sprintf(end, "", element->name); } else if(strcmp(element->name, "hr") == 0 || /**/ strcmp(element->name, "br") == 0) { accept_attr(top, text, 0, element, "id", "class", NULL); sprintf(tag, "<%s%s>", element->name, text); } if(tag[0] != 0) print(out, tag, indent); child = element->first_child; while(child != NULL) { if(child->type == XL_NODE_NODE && child->name != NULL) { default_body(out, top, child, spec, indent + 1 + add); } else if(child->type == XL_NODE_TEXT && child->text != NULL) { print(out, child->text, indent + 1 + add); } child = child->next; } if(end[0] != 0) print(out, end, indent); } void default_nav(FILE* out, const char* top, xl_node_t* element, int indent) { int i; if(element->name == NULL) return; if(strcmp(element->name, "link") == 0) { char* link; char* name; if((link = xl_get_attribute(element, "href")) == NULL) link = "https://invalid.link"; if((name = xl_get_attribute(element, "name")) == NULL) name = ""; link = u_path(top, link); for(i = 0; i < indent; i++) fprintf(out, "\t"); fprintf(out, " - %s
\n", link, name); free(link); } else if(strcmp(element->name, "group") == 0) { char* title; xl_node_t* child; if((title = xl_get_attribute(element, "title")) == NULL) title = ""; if(title != NULL) { for(i = 0; i < indent; i++) fprintf(out, "\t"); fprintf(out, "
- %s
\n", title); } for(i = 0; i < indent; i++) fprintf(out, "\t"); fprintf(out, "
\n"); child = element->first_child; while(child != NULL) { default_nav(out, top, child, indent + 1); child = child->next; } for(i = 0; i < indent; i++) fprintf(out, "\t"); fprintf(out, "
\n"); } }