1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
/*
wml-tools
Copyright (C) 1999 Thomas Neill (tneill@pwot.co.uk)
This file is part of the wml-tools package and it's usage is subject
to the terms and conditions as given in the license. See the file
LICENSE in the root directory of the distribution for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
FILE *out;
int debug = 0;
int dumpNodeIndent = 0;
int htmlIndent = 0;
void dumpNode(xmlNodePtr node)
{
while(node) {
fprintf(stderr, "%*s == %s\n", dumpNodeIndent, "", node->name);
if(node->children) {
dumpNodeIndent += 2;
dumpNode(node->children);
dumpNodeIndent -= 2;
}
node = node->next;
}
}
void showHtml(xmlNodePtr node)
{
xmlNodePtr val;
xmlAttrPtr attr;
while(node) {
if(strcmp("text", node->name) == 0) {
fprintf(out, "%*s%s\n", htmlIndent, "", node->content);
} else if(strcmp("wml", node->name) == 0) {
fprintf(out, "<html>\n");
fprintf(out, "<body>\n");
} else if(strcmp("card", node->name) == 0) {
fprintf(out, "%*s<table border=\"1\" width=\"100%%\">\n", htmlIndent, "");
fprintf(out, "%*s<tr><td><b><a name=\"%s\">%s</a></b></td></tr>\n",
(htmlIndent + 2), "", xmlGetProp(node, "id"), xmlGetProp(node, "title"));
fprintf(out, "%*s</table>\n", htmlIndent, "");
} else if(strcmp("table", node->name) == 0) {
fprintf(out, "%*s<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n",
htmlIndent, "");
} else {
fprintf(out, "%*s<%s", htmlIndent, "", node->name);
attr = node->properties;
while(attr) {
val = attr->children;
if(strcmp(val->name, "text") != 0)
continue;
else
fprintf(out, " %s=\"%s\"", attr->name, val->content);
attr = attr->next;
}
fprintf(out, ">\n");
}
if(node->children) {
htmlIndent += 2;
showHtml(node->children);
htmlIndent -= 2;
}
if(strcmp("wml", node->name) == 0) {
fprintf(out, "</body>\n");
fprintf(out, "</html>\n");
} else if(strcmp("card", node->name) == 0) {
fprintf(out, "%*s<hr>\n", htmlIndent, "");
} else if(strcmp("text", node->name) != 0)
fprintf(out, "%*s</%s>\n", htmlIndent, "", node->name);
node = node->next;
}
}
int main(int argc, char **argv)
{
char *inf, *outf;
xmlDocPtr doc;
if(argc == 2) {
inf = argv[1];
out = stdout;
} else {
fprintf(stderr, "Usage: %s file.wml [output.html]\n", argv[0]);
exit(1);
}
doc = xmlParseFile(inf);
if(!doc) {
fprintf(stderr, "Couldn't parse %s as a valid XML document\n", inf);
exit(1);
}
if(debug)
dumpNode(doc->children);
showHtml(doc->children);
return 0;
}
|