File: doc2web.c

package info (click to toggle)
gnuplot 6.0.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,940 kB
  • sloc: ansic: 95,319; cpp: 7,590; makefile: 2,470; javascript: 2,328; sh: 1,531; lisp: 664; perl: 304; pascal: 191; tcl: 88; python: 46
file content (615 lines) | stat: -rw-r--r-- 18,117 bytes parent folder | download | duplicates (3)
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/* GNUPLOT - doc2web.c */

/*[
 * Copyright 1986 - 1993, 1998, 2004   Thomas Williams, Colin Kelley
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the complete modified source code.  Modifications are to
 * be distributed as patches to the released version.  Permission to
 * distribute binaries produced by compiling modified sources is granted,
 * provided you
 *   1. distribute the corresponding source modifications from the
 *    released version in the form of a patch file along with the binaries,
 *   2. add special version identification to distinguish your version
 *    in addition to the base release version number,
 *   3. provide your name and address as the primary contact for the
 *    support of your modified version, and
 *   4. retain our contact information in regard to use of the base
 *    software.
 * Permission to distribute the released version of the source code along
 * with corresponding source modifications in the form of a patch file is
 * granted with same provisions 2 through 4 for binary distributions.
 *
 * This software is provided "as is" without express or implied warranty
 * to the extent permitted by applicable law.
]*/

/*
 * doc2web.c reformat gnuplot documentation into HTML for use as a 
 *		web resource.
 *
 * Derived from windows/doc2html by Ethan A Merritt (2022)
 * Derived from doc2rtf and doc2html (version 3.7.3) by B. Maerkisch
 *
 * usage:  doc2web file.doc outputdirectory [-d]
 *
 */

/* note that tables must begin in at least the second column to */
/* be formatted correctly and tabs are forbidden */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#define CREATE_INDEX

#include "syscfg.h"
#include "stdfn.h"
#define MAX_LINE_LEN 10230
#include "doc2x.h"
#include "xref.h"
#include "version.h"

static char path[PATH_MAX];
static const char name[] = "gnuplot6";
static char *sectionname = "";
static TBOOLEAN collapsing_terminal_docs = FALSE;
static TBOOLEAN processing_title_page = FALSE;
static TBOOLEAN file_has_sidebar = FALSE;

void convert(FILE *, FILE *, FILE *);
void process_line(char *, FILE *, FILE *);
void sidebar(FILE *a, int start);
void header(FILE *a, char * title);
void footer(FILE *a);

int
main (int argc, char **argv)
{
    FILE *infile;
    FILE *outfile;
    FILE *index = NULL;
    char filename[PATH_MAX];
    char *last_char;

    if (argc != 3) {
	fprintf(stderr, "Usage: %s infile outpath\n", argv[0]);
	exit(EXIT_FAILURE);
    }
    if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
	fprintf(stderr, "%s: Can't open %s for reading\n",
		argv[0], argv[1]);
	exit(EXIT_FAILURE);
    }
    strcpy(path, argv[2]);
    /* make sure there's a path separator at the end */
    last_char = path + strlen(path) - 1;
    if ((*last_char != DIRSEP1) && (*last_char != DIRSEP2)) {
        *(++last_char) = DIRSEP1;
        *(++last_char) = 0;
    }

    /* Wrap the title page text in the same format as the manual.
     * Assume that if we can create and write the file here,
     * we will be able to do the same for the manual pages later.
     */
    strcpy(filename, path);
    strcat(filename, name);
    strcat(filename, ".html");
    if ((outfile = fopen(filename, "w")) == (FILE *) NULL) {
	fprintf(stderr, "%s: Can't open %s for writing\n",
		argv[0], filename);
	fclose(infile);
	exit(EXIT_FAILURE);
    } else {
	char line[80];
	// fprintf(stderr,"Opening %s for output\n", filename);
	sprintf(line, "gnuplot %s", VERSION_MAJOR);
	header(outfile, line);
	fprintf(outfile, "<h1 align=\"center\">gnuplot %s</h1>\n", VERSION_MAJOR);
	footer(outfile);
    }

#ifdef CREATE_INDEX
    strcpy(filename, path);
    strcat(filename, "index");
    strcat(filename, ".hhk");
    if ((index = fopen(filename, "w")) == (FILE *) NULL) {
	fprintf(stderr, "%s: Can't open %s for writing\n",
		argv[0], filename);
	fclose(infile);
	fclose(outfile);
	exit(EXIT_FAILURE);
    }
#endif

    parse(infile);
    convert(infile, outfile, index);
    return EXIT_SUCCESS;
}


void
header(FILE *a, char * title)
{
    /* generate html header */
    fprintf(a, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
    fprintf(a, "<html>\n");
    fprintf(a, "<head>\n");
    fprintf(a, "<meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\">\n");
    fprintf(a, "<link text=\"text/css\" href=\"gnuplot_docs.css\" rel=\"stylesheet\">\n");
    fprintf(a, "<title>%s</title>\n", title);
    fprintf(a, "</head>\n");
    fprintf(a, "<body>\n");
    fputs( 
"<table class=\"center\" style=\"font-size:150%\">\n"
"<th class=\"center\"><a href=\"gnuplot6.html\">Credits</a></td>\n"
"<th class=\"center\"><a href=\"Overview.html\">Overview</a></td>\n"
"<th class=\"center\"><a href=\"Plotting_Styles.html\">Plotting Styles</a></td>\n"
"<th class=\"center\"><a href=\"Commands.html\">Commands</a></td>\n"
"<th class=\"center\"><a href=\"Terminals.html\">Terminals</a></td>\n"
"</th></table>\n", a);
}


void
footer(FILE *a)
{
    /* done with this HTML output page */
    fprintf(a, "</body>\n");
    fprintf(a, "</html>\n");
}

void
sidebar(FILE *a, int start)
{
    /* wrap the sidebar and the main page content in a table */
    if (start)
	fputs(
"<table><tr><td width=20%>\n"
"    <h1>Index</h1>\n"
"    <object type=\"text/html\" class=\"sidebar\" data=\"index.html\">\n"
"</td><td width=80%>\n", a);
    else
	fprintf(a, "</td></tr></table>\n");
}


/*
 * a is input (normally gnuplot.doc)
 * b is output (the html fragment we are working on)
 * d is index.html (unsorted at this point)
 */
void
convert(FILE *a, FILE *b, FILE *d)
{
    static char line[MAX_LINE_LEN+1];

    /* process each line of the file */
    while (get_line(line, sizeof(line), a)) {
	process_line(line, b, d);
    }

    list_free();
}

void
process_line(char *line, FILE *b, FILE *d)
{
    static int line_count = 0;
    static char line2[MAX_LINE_LEN+1];
    static int last_line;

    static TBOOLEAN startpage = TRUE;
    static TBOOLEAN tabl = FALSE;
    static TBOOLEAN intable = FALSE;
    static TBOOLEAN skiptable = FALSE;
    static TBOOLEAN forcetable = FALSE;
    static TBOOLEAN para = FALSE;
    static TBOOLEAN inhlink = FALSE;
    static TBOOLEAN inquote = FALSE;
    static int inref = 0;
    static int klink = 0;  /* link number counter */
    static char location[PATH_MAX+1] = {0};
    struct LIST *klist;
    int i, j, k, l;

    line_count++;
    i = 0;
    j = 0;
    while (line[i] != NUL) {
	switch (line[i]) {
	case '<':
	    line2[j++] = '&';
	    line2[j++] = 'l';
	    line2[j++] = 't';
	    line2[j] = ';';
	    break;
	case '>':
	    line2[j++] = '&';
	    line2[j++] = 'g';
	    line2[j++] = 't';
	    line2[j] = ';';
	    break;
	case '&':
	    line2[j++] = '&';
	    line2[j++] = 'a';
	    line2[j++] = 'm';
	    line2[j++] = 'p';
	    line2[j] = ';';
	    break;
	case '\r':
	case '\n':
	    break;
	case '`':		/* backquotes mean boldface or link */
	    if (line[1] == ' ')	/* tabular line */
		line2[j] = line[i];
	    else if ((!inref) && (!inquote)) {
                char topic[MAX_LINE_LEN+1];
		k = i + 1;	/* index into current string */
		l = 0;		/* index into topic string */
		while ((line[k] != '`') && (line[k] != NUL))
		    topic[l++] = line[k++];
		topic[l] = NUL;
		/* Do not turn `gnuplot` in to a self-reference hyperlink */
		if (!strcmp(&topic[1],"nuplot"))
		    klist = 0;
		else
		    klist = lookup(topic);
		if (klist && (k = klist->line) > 0 && (k != last_line)) {
                    char hyplink1[MAX_LINE_LEN+1];
		    (void)klink;	/* Otherwise compiler warning about unused variable */
                    /* explicit links */
                    if ((klist->line) > 1)
                        sprintf(hyplink1, "<a href=\"loc%d.html\">", klist->line);
                    else
                        sprintf(hyplink1, "<a href=\"%s.html\">", name);
                    strcpy(line2 + j, hyplink1);
		    j += strlen(hyplink1) - 1;

		    inref = k;
		} else {
		    line2[j++] = '<';
		    line2[j++] = 'b';
		    line2[j] = '>';
		    inquote = TRUE;
		}
	    } else {
		if (inquote && inref)
		    fprintf(stderr, "Warning: Reference Quote conflict line %d\n", line_count);
		if (inquote) {
		    line2[j++] = '<';
		    line2[j++] = '/';
		    line2[j++] = 'b';
		    line2[j] = '>';
		    inquote = FALSE;
		}
		if (inref) {
		    /* must be inref */
		    line2[j++] = '<';
		    line2[j++] = '/';
		    line2[j++] = 'a';
		    line2[j] = '>';
		    inref = 0;
		}
	    }
	    break;
	default:
	    line2[j] = line[i];
	}
	i++;
	j++;
	line2[j] = NUL;
    }

    i = 1;

    switch (line[0]) {		/* control character */
    case '=': 			/* latex index entry */
	    break;
    case '?':			/* interactive help entry */
            if ((line2[1] != NUL) && (line2[1] != ' ') && (line2[1] != '?')) {
#ifdef CREATE_INDEX
		/* The "term{inal}" index entries don't work because we move that section */
		if (!strcmp(&(line2[1]),"term") || !strcmp(&(line2[1]),"terminal"))
		    break;
		/* Only keep single-word entries */
		if (!strchr( &(line2[1]), ' ' )) {
		    fprintf(d, "<li><a href=\"%s.html\" target=\"_parent\">", location);
		    fprintf(d, " %s </a></li>\n", &line2[1]);
		}
#endif
            }
	    break;
    case '@':{			/* start/end table */
	    skiptable = !skiptable;
	    if (!skiptable) forcetable = FALSE;
	    intable = !intable; /* just for latex list support */
	    break;
	}
    case '^':{			/* html link escape */
	    if (!strncmp(line,"^figure_",8)) {
		/* Catch this again below */
            } else if ((!inhlink) && (line[3] == 'a') && (line[5] == 'h')) {
                char *str;
                inhlink = TRUE;
                /* remove trailing newline etc */
                str = line + strlen(line) - 1;
                while (*str=='\r' || *str=='\n') *str-- = NUL;
                fprintf(b, "%s", line + 2);
            } else if (inhlink) {
                inhlink = FALSE;
	        fputs(line + 2, b);	/* copy directly */


	    } else if (processing_title_page && !strncmp(&line[1], "<!-- end", 8)) {
		    /* Reached the end of the title page records.
		     * Jump to approximately where we would have been if there
		     * were no title records.
		     */
		    // fprintf(stderr, "Reached end of Title\n");
		    startpage = TRUE;
		    goto end_of_titlepage;

            } else {
                if (line[2] == '!') { /* hack for function sections */
                    const char magic[] = "<!-- INCLUDE_NEXT_TABLE -->";
                    if (strncmp(line+1, magic, strlen(magic)) == 0)
                        forcetable = TRUE;
                }
                inhlink = FALSE;
	        fputs(line + 1, b);	/* copy directly */
            }
	    if (!strncmp(line,"^figure_",8))
		; /* Fall through to embedded figure */
	    else
		break;
	}
    case 'F':			/* embedded figure */
            if (para) fprintf(b, "</p>");
            fprintf(b, "<p align=\"center\">\n");
            fprintf(b, "<img src=\"%s.svg\" alt=\"%s\">\n", line2+1, line2+1);
            fprintf(b, "</p>");
            if (para) fprintf(b, "<p align=\"justify\">\n");
            break;
    case '#':{			/* latex table entry */
	    if (!intable) {  /* HACK: we just handle itemized lists */
		/* Itemized list outside of table */
		if (line[1] == 's')
		    (void) fputs("<ul>\n", b);
		else if (line[1] == 'e')
		    (void) fputs("</ul>\n", b);
		else if (line[1] == 'b') {
		    /* Bullet */
		    fprintf(b, "<li class=\"shortlist\">%s\n", line2+2);
		}
		else if (line[1] == '#') {
		    /* Continuation of bulleted line */
		    fputs(line2+2, b);
		}
		else if (strncmp(line+1, "TeX", 3) != 0) {
		    if (strchr(line, '\n'))
			*(strchr(line, '\n')) = '\0';
		    fprintf(b, "<li><pre>%s</pre>\n", line + 1);
		}
	    }
	    break;
	}
    case '%':{			/* troff table entry */
	    break;		/* ignore */
	}
    case '\n':			/* empty text line */
	if (tabl)
	    fprintf(b, "</pre>\n");
	if (para)
	    fprintf(b, "</p>\n");
	para = FALSE;
	tabl = FALSE;
	break;
    case ' ':{			/* normal text line */
            if (skiptable && !forcetable) break; /* break */
	    if ((line2[1] == NUL) || (line2[1] == '\n')) {
		fprintf(b, "\n");
		if (para)
		    fprintf(b, "</p>\n");
	        para = FALSE;
	        tabl = FALSE;
	    } else if (line2[1] == ' ') { /* in table */
                if (inhlink) {
                    int numspaces = 0;
                    while (line2[numspaces+1] == ' ') numspaces++;
                    fprintf(b, "<tt>%s</tt>", line2+1+numspaces);
                } else {
                    if (!tabl) {
		        if (para)
			    fprintf(b, "</p>\n");
		        fprintf(b, "<pre>\n");
		    }
		    fprintf(b, "%s\n", &line2[1]);
		    para = FALSE;
		    tabl = TRUE;
                }
	    } else {
                if (inhlink) {
                    /* no newline here! */
                    fprintf(b, "%s", &line2[1]);
                } else {
                    if (tabl) {
        	        fprintf(b, "</pre>\n");
		        tabl = 0;
                    }
                    if (!para) {
		        para = TRUE;	/* not in para so start one */
		        tabl = FALSE;
		        fprintf(b, "<p align=\"justify\">");
		    }
                    fprintf(b, "%s\n", &line2[1]);
                }
	    }
	    break;
	}

    case 'D':		/* Link to figure in demo collection */
	{		/* D demo_name <n> */
			/* DB  inserts a break */
	    char *demo;
	    int figure = 1;

	    if (line[1] == 'B') {
		fprintf(b, "<br>\n");
		break;
	    }
	    if (!sscanf(&line[2], "%ms", &demo))
		break;
	    sscanf(&line[strlen(demo)+3], "%d", &figure);
	    fprintf(stderr, "Will include link to %s %d \n", demo, figure);

            if (para) {
		fprintf(b, "</p>");
		fprintf(b, "<p align=\"center\">\n");
		para = FALSE;
	    }
	    fprintf(b, "<a href=\"http://gnuplot.info/demo_6.0/%s.html\">\n", demo);
            fprintf(b, "<img class=\"demo\" src=\"http://gnuplot.info/demo_6.0/%s.%d.png\" ", demo, figure);
            fprintf(b, "alt=\"%s demo\" \\>\n", demo);
	    fprintf(b, "</a>\n");
            break;
	}

    default:{
	    if (isdigit((int)line[0])) {	/* start of section */
                int newlevel = line[0]-'0';

		if (newlevel == 1) {
		    startpage = TRUE;
		    intable = FALSE;
		    if (!strncmp(&line[2], "Gnuplot", 7)) {
			processing_title_page = TRUE;
			sectionname = "Overview";
		    } else if (!strncmp(&line[2], "Plot", 4))
			sectionname = "Plotting_Styles";
		    else if (!strncmp(&line[2], "Commands", 4))
			sectionname = "Commands";
		    else if (!strncmp(&line[2], "Term", 4))
			sectionname = "Terminals";
		    else if (!strncmp(&line[2], "Bugs", 4))
			sectionname = "Bugs";

		} else {
	            if (tabl)
	                fprintf(b, "</pre>\n");
	            if (para)
	                fprintf(b, "</p>\n");
		}

		/* Special case symlink for list of new features */
		if (newlevel == 2) {
		    if (!strncmp(&line[2], "New features", 12)) {
			startpage = TRUE;	/* just to get the symlink processing! */
			sectionname = "NewFeatures";
		    }
		}

		para = FALSE;	/* not in a paragraph */
		tabl = FALSE;

                /* output unique ID */
		sprintf(location, "loc%d", line_count);

		/* add list of subtopics */
		if (!collapsing_terminal_docs && !processing_title_page) {
		    reftable(last_line, b,
			"<table class=\"center\"><th colspan=3>Subtopics</th><tr><td width=33%><ul>\n",
		    "</ul></td></tr></table>\n",
		    "\t<li><a href=\"loc%d.html\">%s</a></li>\n",
		    3,   /* number of columns */
		    "</ul></td><td width=33%><ul>\n");
		}
		last_line = line_count;

		if (processing_title_page) {
		    /* Nothing to do here */

		} else if (collapsing_terminal_docs) {
		    collapsing_terminal_docs = FALSE;

		} else {
                    /* close current file and start a new one */
		    char newfile[PATH_MAX] = "";

    end_of_titlepage:

		    /* If we got here via "goto end_of_titlepage"
		     * dummy up the environment that would have been there if
		     * it were not for that detour past the titlepage records.
		     */
		    if (processing_title_page) {
			processing_title_page = FALSE;
			last_line = 1;
		    }

#ifdef CREATE_INDEX
		    if (file_has_sidebar) {
			sidebar(b, 0);
			file_has_sidebar = FALSE;
		    }
#endif
		    footer(b);
		    fclose(b);

                    /* open new file */
		    strncpy(newfile, path, PATH_MAX-1);
		    strncat(newfile, location, PATH_MAX-strlen(newfile)-6);
		    strcat(newfile,".html");
                    if (!(b = fopen(newfile, "w"))) {
                        fprintf(stderr, "doc2web: Can't open %s for writing\n",
                            newfile);
                        exit(EXIT_FAILURE);
                    }
		    if (startpage) {
			/* Make a symlink with a known name */
			char knownfile[PATH_MAX] = "";
			char locfile[16];
			strcpy(locfile, location);
			strcat(locfile, ".html");
			strncpy(knownfile, path, PATH_MAX-1);
			strncat(knownfile, sectionname, PATH_MAX-strlen(knownfile)-6);
			strcat(knownfile,".html");
			if (symlink(locfile, knownfile)) {
			    perror("doc2web: Can't create symlink ");
			    exit(EXIT_FAILURE);
			}
			fprintf(stderr,"Creating symlink %s for %s\n", knownfile, newfile);

			/* Start the new file */
			header(b, sectionname);
#ifdef CREATE_INDEX
			sidebar(b, 1);
			file_has_sidebar = TRUE;
#endif
			fprintf(b, "<h1>Gnuplot %s %s</h1>\n", VERSION_MAJOR, sectionname);
		    } else {
			header(b, &line2[2]);
			fprintf(b, "<h2>%s</h2>\n", &line2[2]);
		    }
		}

		if (startpage && !strncmp(sectionname,"Terminals",8)) {
		    collapsing_terminal_docs = TRUE;
		}
                startpage = FALSE;

            } else
		fprintf(stderr, "unknown control code '%c' in column 1, line %d\n",
			line[0], line_count);
	    break;
	}
    }
}