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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
/*
* hxtabletrans -- transpose an HTML or XHTML table
*
* The input is an HTML or XHTML table, the output is the same table
* transposed, i.e., rows become columns and columns become rows, as
* if the table was flipped along the main diagonal.
*
* Any TBODY, THEAD, COLGROUP are lost. Comments and processing
* instructions occurring outside of table cells are also lost.
*
* TODO: reinsert comments (at some reasonable place).
*
* TODO: handle rows that have too few cells. (Requires adding up
* colspans and rowspans, possibly discounting overlap because of
* incorrect rowspans.)
*
* TODO: Turn thead, tfoot and tbody into colgroup?
*
* Copyright © 2012 World Wide Web Consortium
* See http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
*
* Author: Bert Box <bert@w3.org>
* Created: 23 Oct 2012
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <unistd.h>
#include <assert.h>
#include <stdbool.h>
#if HAVE_STRING_H
# include <string.h>
#elif HAVE_STRINGS_H
# include <strings.h>
#endif
#include <ctype.h>
#include "heap.e"
#include "types.e"
#include "tree.e"
#include "scan.e"
#include "html.e"
#include "dict.e"
#include "openurl.e"
static Tree tree;
static bool has_error = false;
static bool do_xml = false;
static bool do_content = false; /* Whether to try and transpose characters */
/*======================== Parser callbacks =======================*/
/* handle_error -- called when a parse error occurred */
void handle_error(void *clientdata, const string s, int lineno)
{
warnx("%s:%d: %s\n", get_yyin_name(), lineno, s);
has_error = true;
}
/* start -- called before the first event is reported */
/* void* handle_start(void) */
/* end -- called after the last event is reported */
/* void handle_end(void *clientdata) */
/* handle_comment -- called after a comment is parsed */
void handle_comment(void *clientdata, const string commenttext)
{
tree = append_comment(tree, commenttext);
}
/* handle_text -- called after a text chunk is parsed */
void handle_text(void *clientdata, const string text)
{
tree = append_text(tree, text);
}
/* handle_decl -- called after a declaration is parsed */
void handle_decl(void *clientdata, const string gi, const string fpi,
const string url)
{
tree = append_declaration(tree, gi, fpi, url);
}
/* handle_pi -- called after a Processing Instruction is parsed */
void handle_pi(void *clientdata, const string pi_text)
{
tree = append_procins(tree, pi_text);
}
/* handle_starttag -- called after a start tag is parsed */
void handle_starttag(void *clientdata, const string name, pairlist attribs)
{
tree = html_push(tree, name, attribs);
free(name);
}
/* handle_emptytag -- called after an empty tag is parsed */
void handle_emptytag(void *clientdata, const string name, pairlist attribs)
{
tree = html_push(tree, name, attribs);
free(name);
}
/* handle_endtag -- called after an endtag is parsed (name may be "") */
void handle_endtag(void *clientdata, const string name)
{
tree = html_pop(tree, name);
free(name);
}
/* handle_endincl -- called at the end of an included file */
/* void handle_endincl(void *clientdata) */
/*==================== End of parser callbacks ====================*/
/* print_start -- print a start tag */
static void print_start(Tree t)
{
pairlist p;
printf("<%s", t->name);
for (p = t->attribs; p; p = p->next)
if (p->value) printf(" %s=\"%s\"", p->name, p->value);
else printf(" %s", p->name);
if (do_xml && is_empty(t->name)) printf("/>"); else printf(">");
}
/* print_tree -- print the subtree t */
static void print_tree(Tree t)
{
if (t) {
switch (t->tp) {
case Element:
print_start(t);
if (!is_empty(t->name)) {
print_tree(t->children);
printf("</%s>", t->name);
}
print_tree(t->sister);
break;
case Text:
printf("%s", t->text);
print_tree(t->sister);
break;
case Procins:
printf("<?%s>", t->text);
print_tree(t->sister);
break;
case Declaration:
printf("<!DOCTYPE %s", t->name);
if (t->text && t->url) printf(" PUBLIC \"%s\" \"%s\"", t->text, t->url);
else if (t->text) printf(" PUBLIC \"%s\"", t->text);
else if (t->url) printf(" SYSTEM \"%s\"", t->url);
printf(">");
print_tree(t->sister);
break;
case Comment:
printf("<!--%s-->", t->text);
print_tree(t->sister);
break;
case Root:
default:
assert(!"Cannot happen!");
break;
}
}
}
/* find_all_rows -- create an array pointing to the 1st child of each row */
static int find_all_rows(Tree t, Tree **rows)
{
int nrows = 0;
Tree g, h;
assert(t->tp == Element && eq(t->name, "table"));
*rows = NULL;
for (h = t->children; h; h = h->sister) {
if (h->tp != Element) continue;
if (eq(h->name, "tr")) {
renewarray(*rows, ++nrows);
(*rows)[nrows-1] = h->children;
} else if (eq(h->name, "thead") ||
eq(h->name, "tbody") ||
eq(h->name, "tfoot")) {
for (g = h->children; g; g = g->sister) {
if (h->tp != Element) continue;
assert(eq(g->name, "tr"));
renewarray(*rows, ++nrows);
(*rows)[nrows-1] = g->children;
}
} else {
assert(eq(h->name, "colgroup") || eq(h->name, "col"));
}
}
return nrows;
}
/* transpose_char -- if c has a transposed form, return it; otherwise NULL */
static conststring transpose_char(conststring *c)
{
static conststring pairs[] = {
"⋮", "⋯",
"…", "︙",
"←", "↑",
"→", "↓",
"⇐", "⇑",
"⇒", "⇓",
"⇠", "⇡",
"⇢", "⇣",
"⇦", "⇧",
"⇨", "⇩",
"⇄", "⇅",
"⇆", "⇅",
"⇇", "⇈",
"⇉", "⇊",
NULL,
};
int i;
for (i = 0; pairs[i]; i++)
if (hasprefix(*c, pairs[i])) {
*c += strlen(pairs[i]);
return pairs[i % 2 ? i - 1 : i + 1];
}
return NULL;
}
/* appendc -- reallocate and append one character */
static void appendc(string *s, int c)
{
int i;
if (!s) {new(s); *s = NULL;}
i = *s ? strlen(*s) : 0;
renewarray(*s, i + 2);
(*s)[i] = c;
(*s)[i+1] = '\0';
}
/* transpose_content -- try to transpose character content, true if success */
static bool transpose_content(const Tree t, Tree *result)
{
Tree q = NULL, r;
conststring h, c;
string s = NULL;
*result = NULL;
if (!t) return true;
if (t->tp == Element && !transpose_content(t->children, &q)) return false;
if (!transpose_content(t->sister, &r)) {tree_delete(q); return false;}
if (t->tp == Text) {
for (h = t->text; *h;)
if (isspace(*h)) appendc(&s, *(h++));
else if ((c = transpose_char(&h))) strapp(&s, c, NULL);
else {dispose(s); tree_delete(q); tree_delete(r); return false;}
}
assert(t->tp != Root);
new(*result);
(*result)->tp = t->tp;
(*result)->parent = t->parent;
(*result)->children = q;
(*result)->sister = r;
if (t->tp == Comment || t->tp == Procins || t->tp == Declaration)
(*result)->text = newstring(t->text);
else
(*result)->text = s;
if (t->tp == Declaration) (*result)->url = newstring(t->url);
if (t->tp == Element || t->tp == Declaration)
(*result)->name = newstring(t->name);
if (t->tp == Element) (*result)->attribs = pairlist_copy(t->attribs);
return true;
}
/* trans -- print the transposition of table t */
static void trans(const Tree t)
{
Tree *rows;
int nrows, i;
pairlist p;
static Node _SPANNED;
static Tree SPANNED = &_SPANNED;
Tree **table, h;
int j, k, l, colspan, rowspan, maxcols, *ncols;
conststring s;
Tree transposed;
assert(t->tp == Element && eq(t->name, "table"));
nrows = find_all_rows(t, &rows);
/* Find the maximum number of columns */
/* TODO: deal with overlapping cells */
newarray(ncols, nrows);
for (i = 0; i < nrows; i++) ncols[i] = 0;
for (i = 0; i < nrows; i++) {
for (h = rows[i]; h; h = h->sister) {
assert(h->tp == Element || h->tp == Comment || h->tp == Procins);
if (h->tp == Element) {
assert(eq(h->name, "td") || eq(h->name, "th"));
if (! (s = get_attrib(h, "colspan"))) colspan = 1;
else if ((colspan = atoi(s)) < 1) colspan = 1;
if (! (s = get_attrib(h, "rowspan"))) rowspan = 1;
else if ((rowspan = atoi(s)) < 1) rowspan = 1;
for (j = i; j < i + rowspan && j < nrows; j++) ncols[j] += colspan;
}
}
}
for (maxcols = 0, i = 0; i < nrows; i++)
if (maxcols < ncols[i]) maxcols = ncols[i];
/* Make an empty table */
newarray(table, nrows);
for (i = 0; i < nrows; i++) {
newarray(table[i], maxcols);
for (j = 0; j < maxcols; j++) table[i][j] = NULL;
}
/* Put the cells at their correct coordinates in the table */
for (i = 0; i < nrows; i++) {
for (j = 0, h = rows[i]; h; h = h->sister) {
if (h->tp == Element) {
while (table[i][j]) j++; /* Find next free cell in this row */
assert(j < maxcols);
if (! (s = get_attrib(h, "colspan"))) colspan = 1;
else if ((colspan = atoi(s)) < 1) colspan = 1;
if (! (s = get_attrib(h, "rowspan"))) rowspan = 1;
else if ((rowspan = atoi(s)) < 1) rowspan = 1;
for (k = i; k < i + rowspan; k++)
for (l = j; l < j + colspan; l++)
table[k][l] = SPANNED; /* Mark cells as occupied */
table[i][j] = h;
j += colspan;
}
}
}
/* Print the table transposed */
print_start(t);
printf("\n");
for (j = 0; j < maxcols; j++) {
printf("<tr>\n");
for (i = 0; i < nrows; i++) {
if (table[i][j] == NULL) {
printf("<td></td>\n");
} else if (table[i][j] != SPANNED) {
/* Swap colspan and rowspan attributes */
for (p = table[i][j]->attribs; p; p = p->next)
if (eq(p->name, "colspan")) strcpy(p->name, "rowspan");
else if (eq(p->name, "rowspan")) strcpy(p->name, "colspan");
print_start(table[i][j]);
if (do_content && transpose_content(table[i][j]->children, &transposed))
print_tree(transposed);
else
print_tree(table[i][j]->children);
printf("</%s>\n", table[i][j]->name);
}
}
printf("</tr>\n");
}
printf("</table>\n");
}
/* transpose -- look for the first table in the tree and output it transposed */
static bool transpose(const Tree t)
{
if (!t) return false;
else if (t->tp == Element && eq(t->name, "table")) {trans(t); return true;}
else if (transpose(t->sister)) return true;
else return transpose(t->children);
}
/* usage -- print usage message and exit */
static void usage(const string prog)
{
fprintf(stderr, "Usage: %s [-c] [-x] [-v] [file-or-url]\n", prog);
exit(1);
}
/* main -- main body */
int main(int argc, char *argv[])
{
int c, status = 200;
/* Parse command line */
while ((c = getopt(argc, argv, "cxv")) != -1)
switch (c) {
case 'c': do_content = true; break;
case 'x': do_xml = true; break;
case 'v': printf("Version: %s %s\n", PACKAGE, VERSION); return 0;
default: usage(argv[0]);
}
if (optind == argc)
set_yyin(stdin, "stdin");
else if (optind == argc - 1)
set_yyin(fopenurl(argv[optind], "r", &status), argv[optind]);
else
usage(argv[0]);
if (!yyin) err(1, "%s", argv[argc-1]);
if (status != 200) errx(1, "%s: %s", argv[argc-1], http_strerror(status));
/* Bind the parser callback routines to our handlers */
set_error_handler(handle_error);
set_comment_handler(handle_comment);
set_text_handler(handle_text);
set_decl_handler(handle_decl);
set_pi_handler(handle_pi);
set_starttag_handler(handle_starttag);
set_emptytag_handler(handle_emptytag);
set_endtag_handler(handle_endtag);
/* Parse input and transpose it as a table, if possible */
tree = create();
if (yyparse() != 0) exit(1);
if (!transpose(get_root(tree))) errx(1, "Found no table to transpose");
return has_error ? 1 : 0;
}
|