File: hxtoc.c

package info (click to toggle)
html-xml-utils 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,620 kB
  • sloc: ansic: 10,027; sh: 2,135; lex: 189; yacc: 125; perl: 123; makefile: 122
file content (464 lines) | stat: -rw-r--r-- 13,327 bytes parent folder | download
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 * Insert an active ToC between "<!--begin-toc-->" and "<!--end-toc-->",
 * or replacing the comment "<!--toc-->"
 *
 * Headers with class "no-toc" will not be listed in the ToC.
 *
 * The ToC links to elements with ID attributes as well as with
 * empty <A NAME> elements.
 *
 * Tags for a <SPAN> with class "index" are assumed to be used by
 * a cross-reference generator and will not be copied to the ToC.
 *
 * Similarly, DFN tags are not copied to the ToC (but the element's
 * content is).
 *
 * Any <A> tags with a class of "bctarget" are not copied, but
 * regenerated. They are assumed to be backwards-compatible versions
 * of ID attributes on their parent elements. With the option -t or -x
 * they are removed.
 *
 * Copyright  1994-2000 World Wide Web Consortium
 * See http://www.w3.org/Consortium/Legal/copyright-software
 *
 * Author: Bert Bos <bert@w3.org>
 * Created Sep 1997
 * Version: $Id: hxtoc.c,v 1.5 2011/08/26 00:29:06 bbos Exp $
 *
 **/
#include <config.h>
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#if STDC_HEADERS
# include <string.h>
#else
# ifndef HAVE_STRCHR
#  define strchr index
#  define strrchr rindex
# endif
# ifndef HAVE_STRSTR
#  include "strstr.e"
# endif
#endif

#ifdef HAVE_ERRNO_H
#  include <errno.h>
#endif
#ifdef HAVE_SEARCH_H
#  include <search.h>
#else
#  include "search-freebsd.h"
#endif

#include "export.h"
#include "types.e"
#include "heap.e"
#include "tree.e"
#include "html.e"
#include "scan.e"
#include "dict.e"
#include "openurl.e"
#include "errexit.e"
#include "genid.e"
#include "class.e"

#define BEGIN_TOC "begin-toc"			/* <!--begin-toc--> */
#define END_TOC "end-toc"			/* <!--end-toc--> */
#define TOC "toc"				/* <!--toc--> */
#define NO_TOC "no-toc"				/* CLASS="... no-toc..." */
#define INDEX "index"				/* CLASS="... index..." */
#define TARGET "bctarget"			/* CLASS="...bctarget..." */

#define EXPAND True
#define NO_EXPAND False
#define KEEP_ANCHORS True
#define REMOVE_ANCHORS False

#define INDENT " "				/* Amount to indent ToC per level */

static Tree tree;
static int toc_low = 1, toc_high = 6;		/* Which headers to include */
static Boolean xml = False;			/* Use <empty /> convention */
static Boolean bctarget = True;			/* Generate <a name=> after IDs */
static string toc_class = "toc";		/* <ul class="..."> */
static Boolean use_div = False;			/* Option -d */


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

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

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

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

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

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

/* handle_starttag -- called after a start tag is parsed */
static void handle_starttag(void *clientdata, string name, pairlist attribs)
{
  conststring id;

  tree = html_push(tree, name, attribs);

  /* If it has an ID, store it (so we don't accidentally generate it) */
  if ((id = pairlist_get(attribs, "id"))) storeID(id);
}

/* handle_emptytag -- called after an empty tag is parsed */
static void handle_emptytag(void *clientdata, string name, pairlist attribs)
{
  handle_starttag(clientdata, name, attribs);
}

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

/* indent -- print level times a number of spaces */
static void indent(int level)
{
  for (; level > 0; level--) printf(INDENT);
}

/* is_first_child_of_div -- check if t is 1st non-whitespace child of a DIV */
static Boolean is_first_child_of_div(Tree t)
{
  Tree h;

  assert(t->tp == Element);
  assert(t->parent);
  if (t->parent->tp != Element) return False;
  if (!eq(t->parent->name, "div")) return False;
  for (h = t->parent->children; h != t; h = h->sister) {
    if (h->tp == Element) return False;
    if (h->tp == Text && !only_space(h->text)) return False;
  }
  return True;
}

/* first_child_heading_level -- type of 1st child, if it's an H?, or -1 */
static int first_child_heading_level(Tree t)
{
  Tree h;

  assert(t->tp == Element);
  for (h = t->children; h; h = h->sister) {
    switch (h->tp) {
    case Element:
      if (eq(h->name, "h1")) return 1;
      if (eq(h->name, "h2")) return 2;
      if (eq(h->name, "h3")) return 3;
      if (eq(h->name, "h4")) return 4;
      if (eq(h->name, "h5")) return 5;
      if (eq(h->name, "h6")) return 6;
      return -1;
    case Text:
      if (! only_space(h->text)) return -1;
      break;
    default:
      break;
    }
  }
  return -1;
}

static void expand(Tree t, Boolean *write, Boolean exp, Boolean keep_anchors);

/* toc -- create a table of contents */
static void toc(Tree t, int *curlevel, Boolean *item_is_open)
{
  conststring val, id;
  int level;
  Tree h;
  Boolean write = True;

  switch (t->tp) {
    case Text: break;
    case Comment: break;
    case Declaration: break;
    case Procins: break;
    case Element:
      /* Check if the element is a heading */
      if (eq(t->name, "h1")) level = 1;
      else if (eq(t->name, "h2")) level = 2;
      else if (eq(t->name, "h3")) level = 3;
      else if (eq(t->name, "h4")) level = 4;
      else if (eq(t->name, "h5")) level = 5;
      else if (eq(t->name, "h6")) level = 6;
      else level = -1;
      /* Check if it has a "no-toc" class, or if the parent DIV had one */
      if (level > 0 &&
	  (((val = get_attrib(t, "class")) && contains(val, NO_TOC)) ||
	   (use_div && is_first_child_of_div(t) &&
	    (val = get_attrib(t->parent, "class")) && contains(val, NO_TOC))))
	level = -1;
      /* If it's a header for the ToC, create a list item for it */
      if (level >= toc_low && level <= toc_high) {
	/* Ensure there is an ID to point to */
	h = use_div && is_first_child_of_div(t) ? t->parent : t;
	if (! (id = get_attrib(h, "id"))) {
	  id = gen_id(h);
	  set_attrib(h, "id", id);
	}
	assert(*curlevel <= level || *item_is_open);
	while (*curlevel > level) {
	  printf(xml ? "</li>\n" : "\n");
	  indent(*curlevel - toc_low);
	  printf("</ul>");
	  (*curlevel)--;
	}
	if (*curlevel == level && *item_is_open) {
	  printf(xml ? "</li>\n" : "\n");
	} else if (*item_is_open) {
	  printf("\n");
	  (*curlevel)++;
	  indent(*curlevel - toc_low);
	  printf("<ul class=\"%s\">\n", toc_class);
	}
	while (*curlevel < level) {
	  indent(*curlevel - toc_low);
 	  printf("<li>\n");
	  (*curlevel)++;
	  indent(*curlevel - toc_low);
	  printf("<ul class=\"%s\">\n", toc_class);
	}
	indent(*curlevel - toc_low);
	if ((val = get_attrib(t, "class"))) {
	  printf("<li class=\"%s\"><a href=\"#%s\">", val, id);
	} else {
	  printf("<li><a href=\"#%s\">", id);
	}
	expand(t, &write, NO_EXPAND, REMOVE_ANCHORS);
	printf("</a>");
	*item_is_open = True;
      } else {
	for (h = t->children; h != NULL; h = h->sister)
	  toc(h, curlevel, item_is_open);
      }
      break;
    case Root:
      for (h = t->children; h != NULL; h = h->sister)
	toc(h, curlevel, item_is_open);
      break;
    default: assert(! "Cannot happen");
  }
}

/* expand -- write the tree, inserting ID's at H* and inserting a toc */
static void expand(Tree t, Boolean *write, Boolean exp, Boolean keep_anchors)

{
  conststring val;
  Tree h;
  pairlist a;
  string s;
  int level;
  Boolean item_is_open = False;

  for (h = t->children; h != NULL; h = h->sister) {
    switch (h->tp) {
      case Text:
	if (*write) printf("%s", h->text);
	break;
      case Comment:
	for (s = h->text; isspace(*s); s++) ;
	if (exp && (!strncmp(s, TOC, sizeof(TOC) - 1)
		    || !strncmp(s, BEGIN_TOC, sizeof(BEGIN_TOC) - 1))) {
	  printf("<!--%s-->\n", BEGIN_TOC);
	  printf("<ul class=\"%s\">\n", toc_class);
	  level = toc_low;
	  toc(get_root(t), &level, &item_is_open);
	  while (level > toc_low) {
	    printf(xml ? "</li>\n" : "\n");
	    indent(level - toc_low);
	    printf("</ul>");
	    level--;
	  }
	  if (item_is_open && xml) printf("</li>\n");
	  printf("</ul>\n");
	  printf("<!--%s-->", END_TOC);
	  if (!strncmp(s, BEGIN_TOC, sizeof(BEGIN_TOC) - 1))
	    *write = False;			/* Suppress old ToC */
	} else if (exp && !strncmp(s, END_TOC, sizeof(END_TOC) - 1)) {
	  *write = True;
	} else {
	  printf("<!--%s-->", h->text);
	}
	break;
      case Declaration:
	printf("<!DOCTYPE %s", h->name);
	if (h->text) printf(" PUBLIC \"%s\"", h->text);
	if (h->url) printf(" %s\"%s\"", h->text ? "" : "SYSTEM ", h->url);
	printf(">");
	break;
      case Procins:
	if (*write) printf("<?%s>", h->text);
	break;
      case Element:
	/* Give DIVs and headers an ID, if they need one */
	if (eq(h->name, "h1"))
	  level = !use_div || !is_first_child_of_div(h) ? 1 : -1;
	else if (eq(h->name, "h2"))
	  level = !use_div || !is_first_child_of_div(h) ? 2 : -1;
	else if (eq(h->name, "h3"))
	  level = !use_div || !is_first_child_of_div(h) ? 3 : -1;
	else if (eq(h->name, "h4"))
	  level = !use_div || !is_first_child_of_div(h) ? 4 : -1;
	else if (eq(h->name, "h5"))
	  level = !use_div || !is_first_child_of_div(h) ? 5 : -1;
	else if (eq(h->name, "h6"))
	  level = !use_div || !is_first_child_of_div(h) ? 6 : -1;
	else if (eq(h->name, "div"))
	  level = use_div ? first_child_heading_level(h) : -1;
	else
	  level = -1;
	if (level >= toc_low && level <= toc_high && !get_attrib(h, "id"))
	  set_attrib(h, "id", gen_id(h));
	if (*write) {
	  if (! keep_anchors && eq(h->name, "a")) {
	    /* Don't write the <a> and </a> tags */
	    expand(h, write, exp, False);
	  } else if (! keep_anchors && eq(h->name, "span")
		     && has_class(h->attribs, INDEX)) {
	    /* Don't write <span.index>...</span> tags */
	    expand(h, write, exp, False);
	  } else if (! keep_anchors && eq(h->name, "dfn")) {
	    /* Don't copy dfn tags to the ToC */
	    expand(h, write, exp, False);
	  } else if (eq(h->name, "a") && (has_class(h->attribs, TARGET)
		     || has_class(h->attribs, TOC))) {
	    /* This <a> was inserted by toc itself; remove it */
	    expand(h, write, exp, False);
	  } else {
	    printf("<%s", h->name);
	    for (a = h->attribs; a != NULL; a = a->next) {
	      if (keep_anchors || !eq(a->name, "id")) {
		/* If we don't keep anchors, we don't keep IDs either */
		printf(" %s", a->name);
		if (a->value != NULL) printf("=\"%s\"", a->value);
	      }
	    }
	    if (is_empty(h->name)) {
	      assert(h->children == NULL);
	      printf(xml ? " />" : ">");
	    } else {
	      printf(">");
	      /* Insert an <A NAME> if element has an ID and is not <A> */
	      if (bctarget && is_mixed(h->name) && (val = get_attrib(h, "id"))
		  && !eq(h->name, "a") && ! xml)
		printf("<a class=\"%s\" name=\"%s\"></a>", TARGET, val);
	      expand(h, write, exp, keep_anchors);
	      printf("</%s>", h->name);
	    }
	  }
	}
	break;
      case Root:
	assert(! "Cannot happen");
	break;
      default:
	assert(! "Cannot happen");
    }
  }
}

/* usage -- print usage message and exit */
static void usage(string name)
{
  errexit("Version %s\nUsage: %s [-l low] [-h high] [-x] [-t] [-d] [-c class] [html-file]\n",
	  VERSION, name);
}


int main(int argc, char *argv[])
{
  int i, status;
  Boolean write = True;

  /* Bind the parser callback routines to our handlers */
  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);

  yyin = stdin;
  for (i = 1; i < argc; i++) {
    if (eq(argv[i], "-l")) {
      if (i >= argc - 1) usage(argv[0]);
      toc_low = atoi(argv[++i]);
    } else if (eq(argv[i], "-h")) {
      if (i >= argc - 1) usage(argv[0]);
      toc_high = atoi(argv[++i]);
    } else if (eq(argv[i], "-x")) {
      xml = True;
    } else if (eq(argv[i], "-t")) {
      bctarget = False;
    } else if (eq(argv[i], "-d")) {
      use_div = True;
    } else if (eq(argv[i], "-")) {
      /* yyin = stdin; */
    } else if (eq(argv[i], "-c")) {
      if (i >= argc - 1) usage(argv[0]);
      toc_class = newstring(argv[++i]);
    } else {
      yyin = fopenurl(argv[i], "r", &status);
      if (yyin == NULL) {perror(argv[i]); exit(2);}
      if (status != 200) errexit("%s : %s\n", argv[i], http_strerror(status));
    }
  }
  if (toc_low < 1) toc_low = 1;
  if (toc_high > 6) toc_high = 6;

  if (yyparse() != 0) exit(3);

  tree = get_root(tree);
  expand(tree, &write, EXPAND, KEEP_ANCHORS);
  tree_delete(tree);				/* Just to test memory mgmt */
  return 0;
}