Codebase list html-xml-utils / upstream/7.9 hxnormalize.c
upstream/7.9

Tree @upstream/7.9 (Download .tar.gz)

hxnormalize.c @upstream/7.9raw · history · blame

  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
/*
 * Format an HTML source in a consistent manner.
 *
 * Copyright © 1994-2012 World Wide Web Consortium
 * See http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
 *
 * Created 9 May 1998
 * Bert Bos <bert@w3.org>
 * $Id: hxnormalize.c,v 1.25 2019/10/05 17:11:31 bbos Exp $
 */
#include "config.h"
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif
#ifdef HAVE_STRING_H
#  include <string.h>
#elif HAVE_STRINGS_H
#  include <strings.h>
#endif
#include <assert.h>
#include <stdbool.h>
#include "export.h"
#include "types.e"
#include "tree.e"
#include "html.e"
#include "scan.e"
#include "textwrap.e"
#include "dict.e"
#include "openurl.e"
#include "errexit.e"
#include "langinfo.e"

static Tree tree;
static bool do_xml = false;
static bool do_endtag = false;
static bool has_errors = false;
static bool do_doctype = true;
static bool clean_span = false;
static string long_comment = NULL;
static bool do_lang = false;
static bool input_is_xml = false;


/* handle_error -- called when a parse error occurred */
void handle_error(void *clientdata, const string s, int lineno)
{
  fprintf(stderr, "%d: %s\n", lineno, s);
  has_errors = true;
}

/* start -- called before the first event is reported */
void* start(void)
{
  tree = create();
  return NULL;
}
  
/* end -- called after the last event is reported */
void end(void *clientdata)
{
  /* skip */
}

/* handle_comment -- called after a comment is parsed */
void handle_comment(void *clientdata, string commenttext)
{
  tree = append_comment(tree, commenttext);
}

/* handle_html_text -- called after a text chunk is parsed */
void handle_html_text(void *clientdata, string text)
{
  tree = append_text(tree, text);
}

/* handle_text -- called after a text chunk is parsed */
void handle_text(void *clientdata, string text)
{
  tree = tree_append_text(tree, text);
}

/* handle_decl -- called after a declaration is parsed */
void handle_decl(void *clientdata, string gi,
		 string fpi, string url)
{
  tree = append_declaration(tree, gi, fpi, url);
}

/* handle_pi -- called after a PI is parsed */
void handle_pi(void *clientdata, string pi_text)
{
  tree = append_procins(tree, pi_text);
}

/* handle_html_starttag -- called after a start tag is parsed */
void handle_html_starttag(void *clientdata, string name, pairlist attribs)
{
  tree = html_push(tree, name, attribs);
  free(name);
}

/* handle_starttag -- called after a start tag is parsed */
void handle_starttag(void *clientdata, string name, pairlist attribs)
{
  tree = tree_push(tree, name, attribs);
  free(name);
}

/* handle_html_emptytag -- called after an empty tag is parsed */
void handle_html_emptytag(void *clientdata, 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, string name, pairlist attribs)
{
  tree = tree_push(tree, name, attribs);
  tree = tree_pop(tree, name);
  free(name);
}

/* handle_html_endtag -- called after an endtag is parsed (name may be "") */
void handle_html_endtag(void *clientdata, string name)
{
  tree = html_pop(tree, name);
  free(name);
}

/* handle_endtag -- called after an endtag is parsed (name may be "") */
void handle_endtag(void *clientdata, string name)
{
  tree = tree_pop(tree, name);
  free(name);
}

/* insert -- insert an attribute into a sorted list of attributes */
static pairlist insert(pairlist x, pairlist list)
{
  if (! list) {					/* Empty list */
    x->next = NULL;
    return x;
  } else if (strcmp(x->name, list->name) <= 0) { /* Insert at head */
    x->next = list;
    return x;
  } else {					/* Insert not at head */
    list->next = insert(x, list->next);
    return list;
  }
}

/* sort_list -- sort a linked list of attributes, return reordered list */
static pairlist sort_list(pairlist list)
{
  /* Insertion sort should be fast enough... */
  if (! list) return NULL;
  else return insert(list, sort_list(list->next));
}

/* next_ambiguous -- check if omitting end changes the meaning */
static bool next_ambiguous(Node *n)
{
  Node *h = n;

  /* Skip text nodes with only white space */
  while (h->sister && h->sister->tp == Text && only_space(h->sister->text))
    h = h->sister;

  if (h->sister == NULL) return false;
  if (h->sister->tp == Text) return true;
  if (h->sister->tp == Comment) return true;
  if (h->sister->tp == Procins) return true;
  if (h->sister->tp == Declaration) return false; /* Should not occur */
  assert(h->sister->tp == Element);		/* Cannot be Root */
  return has_parent(h->sister->name, n->name);
}

/* needs_quotes -- check if the attribute value can be printed unquoted */
static bool needs_quotes(const string s)
{
  int i;
  assert(s);
  if (!s[0]) return true;			/* Empty string */
  for (i = 0; s[i]; i++)
    if (!isalnum(s[i]) && (s[i] != '-') && (s[i] != '.')) return true;
  return false;
}

/* pp -- print the document normalized */
static void pp(Tree n, bool preformatted, bool allow_text,
	       conststring lang)
{
  bool pre, mixed, with_space;
  conststring lang2;
  string s;
  pairlist h;
  size_t i, j;
  Tree l;

  with_space = with_spaces(lang);	/* Language with spaces between words? */

  switch (n->tp) {
    case Text:
      if (!allow_text) {
	assert(only_space(n->text));
      } else {
	s = n->text;
	out(s, preformatted, with_space);
      }
      break;
    case Comment:
      if (long_comment && strstr(n->text, long_comment) && !preformatted) {
	/* Found a comment that should have an empty line before it */
	outbreak();
	outln(NULL, true, with_space);
      }
      out("<!--", true, true); out(n->text, true, with_space);
      if (allow_text || preformatted) out("-->", true, true);
      else outln("-->", preformatted, true);
      break;
    case Declaration:
      if (do_doctype) {
	out("<!DOCTYPE ", false, true);
	out(n->name, false, true);
	if (n->text) {
	  out(" PUBLIC \"", false, true);
	  out(n->text, false, true);
	  out("\"", false, true);
	}
	if (n->url) {
	  if (!n->text) out(" SYSTEM", false, true);
	  out(" \"", false, true);
	  out(n->url, false, true);
	  out("\"", false, true);
	} else if (n->text && do_xml) {	/* XML cannot omit the system literal */
	  out(" \"\"", false, true);
	}
	outln(">", false, true);
      }
      break;
    case Procins:
      out("<?", false, true); out(n->text, true, true);
      if (allow_text || preformatted) out(">", false, true);
      else outln(">", false, true);
      break;
    case Element:
      if (clean_span && eq(n->name, "span") && ! n->attribs) {
	/* Omit start and end tags, print just the children. */
	for (l = n->children; l != NULL; l = l->sister)
	  pp(l, preformatted, true, lang);
	break;
      }
      /* Check for language attribute. */
      lang2 = pairlist_get(n->attribs, "lang");
      if (!lang2) lang2 = pairlist_get(n->attribs, "xml:lang");

      /* Remove redundant language attribute */
      if (do_lang && lang && lang2 && eq(lang, lang2)) {
	pairlist_unset(&n->attribs, "lang");
	pairlist_unset(&n->attribs, "xml:lang");
      }

      /* Update inherited language. */
      if (lang2) lang = lang2;

      if (!preformatted && break_before(n->name)) outln(NULL, false, true);
      out("<", preformatted, true); out(n->name, preformatted, true);
      if (break_before(n->name)) inc_indent();
      n->attribs = sort_list(n->attribs);
      for (h = n->attribs; h != NULL; h = h->next) {
	out(" ", false, true); out(h->name, false, true);
	if (do_xml) {
	  out("=\"", false, true);
	  out(h->value ? h->value : h->name, true, true);
	  outc('"', false, true);
	} else if (h->value == NULL) {
	  /* The h->name *is* the value (and the attribute name is implicit) */
	} else if (!needs_quotes(h->value)) {
	  out("=", false, true); /* Omit the quotes */
	  out(h->value, true, true);
	} else {
	  out("=\"", false, true);
	  out(h->value, true, true);
	  outc('"', false, true);
	}
      }
      if (is_empty(n->name)) {
	assert(n->children == NULL);
	outbreakpoint();
	out(do_xml ? " />" : ">", true, true);
	if (break_before(n->name)) dec_indent();
	if (!preformatted && break_after(n->name)) outln(NULL, false, true);

      } else if (do_xml && !input_is_xml && is_cdata_elt(n->name)) {
	/* Escape '<' and '&', but only if input was HTML, not XML */
	if (!n->children) {
	  out(" />", true, true);
	  if (break_before(n->name)) dec_indent();
	} else {
	  outbreakpoint();
	  out(">", preformatted, true);
	  for (l = n->children; l; l = l->sister) {
	    assert(l->tp == Text);
	    for (s = l->text; *s; s++)
	      if (*s == '<') out("&lt;", true, true);
	      else if (*s == '&') out("&amp;", true, true);
	      else outc(*s, true, with_space);
	  }
	  if (break_before(n->name)) dec_indent();
	  out("</", true, true);
	  out(n->name, true, true);
	  outbreakpoint();
	  out(">", preformatted, true);
	}
	if (!preformatted && break_after(n->name)) outbreak();

      } else if (!do_xml && input_is_xml && is_cdata_elt(n->name) &&
		 n->children) {
	/* Remove "<![CDATA[" and "]]>", or unescape &lt; and &amp;,
	   but only if input was XML, not HTML */
	outbreakpoint();
	out(">", preformatted, true);
	for (l = n->children; l != NULL; l = l->sister) {
	  if (l->tp != Text) {
	    errexit("Cannot convert <%s> to HTML because it has children\n",
		    n->name);
	  } else if (hasprefix(l->text, "<![CDATA[")) {
	    assert(hasaffix(l->text, "]]>"));
	    s = l->text + 9;	/* Skip "<![CDATA[" */
	    i = strlen(s) - 3;	/* Omit "]]>" */
	    for (j = 0; j < i; j++) outc(s[j], true, with_space);
	  } else {		/* Unescape &lt; and &amp; */
	    for (s = l->text; *s; s++)
	      if (hasprefix(s, "&amp;")) {outc('&', true, true); s += 4;}
	      else if (hasprefix(s, "&lt;")) {outc('<', true, true); s += 3;}
	      else if (hasprefix(s, "&gt;")) {outc('>', true, true); s += 3;}
	      else if (hasprefix(s, "&quot;")) {outc('"', true, true); s += 5;}
	      else if (hasprefix(s, "&apos;")) {outc('\'', true, true); s += 5;}
	      else outc(*s, true, with_space);
	  }
	}
	if (break_before(n->name)) dec_indent();
	out("</", preformatted, true);
	out(n->name, preformatted, true);
	outbreakpoint();
	out(">", preformatted, true);
	if (!preformatted && break_after(n->name)) outbreak();

      } else {
	outbreakpoint();
	out(">", preformatted, true);
	pre = preformatted || is_pre(n->name);
	mixed = is_mixed(n->name);
	for (l = n->children; l != NULL; l = l->sister)
	  pp(l, pre, mixed, lang);
	if (break_before(n->name)) dec_indent();
	if (do_xml || do_endtag || need_etag(n->name) || next_ambiguous(n)) {
	  out("</", pre, true); out(n->name, pre, true);
	  outbreakpoint();
	  out(">", preformatted, true);
	}
	if (!preformatted && break_after(n->name)) outbreak();
      }
      break;
    default:
      assert(!"Cannot happen");
  }
}

/* prettyprint -- print the tree normalized */
static void prettyprint(Tree t)
{
  Tree h;
  assert(t->tp == Root);
  for (h = t->children; h != NULL; h = h->sister) pp(h, false, false, NULL);
  flush();
}

/* usage -- print usage message and exit */
static void usage(string prog)
{
  fprintf(stderr, "%s version %s\n\
Usage: %s [-e] [-d] [-x] [-L] [-i indent] [-l linelen] [-c commentmagic] [file_or_url]\n",
	  prog, VERSION, prog);
  exit(1);
}

/* main -- main body */
int main(int argc, char *argv[])
{
  int c, status = 200;

  while ((c = getopt(argc, argv, "edxXi:l:sc:L")) != -1)
    switch (c) {
    case 'e': do_endtag = true; break;
    case 'x': do_xml = true; break;
    case 'X': input_is_xml = true; break;
    case 'd': do_doctype = false; break;
    case 'i': set_indent(atoi(optarg)); break;
    case 'l': set_linelen(atoi(optarg)); break;
    case 's': clean_span = true; break;
    case 'c': long_comment = optarg; break;
    case 'L': do_lang = true; break;
    default: usage(argv[0]);
    }
  if (optind == argc) yyin = stdin;
  else if (optind == argc - 1) yyin = fopenurl(argv[optind], "r", &status);
  else usage(argv[0]);
  if (yyin == NULL) {perror(argv[optind]); exit(2);}
  if (status != 200) errexit("%s : %s\n", argv[optind], http_strerror(status));

  /* Bind the parser callback routines to our handlers */
  if (input_is_xml) {
    set_error_handler(handle_error);
    set_start_handler(start);
    set_end_handler(end);
    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);
  } else {			/* Input is HTML */
    set_error_handler(handle_error);
    set_start_handler(start);
    set_end_handler(end);
    set_comment_handler(handle_comment);
    set_text_handler(handle_html_text);
    set_decl_handler(handle_decl);
    set_pi_handler(handle_pi);
    set_starttag_handler(handle_html_starttag);
    set_emptytag_handler(handle_html_emptytag);
    set_endtag_handler(handle_html_endtag);
  }

  if (yyparse() != 0) {exit(3);}
  tree = get_root(tree);
  prettyprint(tree);
  return has_errors ? 1 : 0;
}