aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/htmlroff/html.c
blob: 98b123f84838fe95d00515de5b665f101e805859 (plain)
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
/*
 * Emit html.  Keep track of tags so that user doesn't have to.
 */

#include "a.h"

typedef struct Tag Tag;
struct Tag
{
	Tag *next;
	Rune *id;
	Rune *open;
	Rune *close;
};

Tag *tagstack;
Tag *tagset;
int hidingset;

static Rune*
closingtag(Rune *s)
{
	Rune *t;
	Rune *p0, *p;

	t = runemalloc(sizeof(Rune));
	if(s == nil)
		return t;
	for(p=s; *p; p++){
		if(*p == Ult){
			p++;
			if(*p == '/'){
				while(*p && *p != Ugt)
					p++;
				goto close;
			}
			p0 = p;
			while(*p && !isspacerune(*p) && *p != Uspace && *p != Ugt)
				p++;
			t = runerealloc(t, 1+(p-p0)+2+runestrlen(t)+1);
			runemove(t+(p-p0)+3, t, runestrlen(t)+1);
			t[0] = Ult;
			t[1] = '/';
			runemove(t+2, p0, p-p0);
			t[2+(p-p0)] = Ugt;
		}

		if(*p == Ugt && p>s && *(p-1) == '/'){
		close:
			for(p0=t+1; *p0 && *p0 != Ult; p0++)
				;
			runemove(t, p0, runestrlen(p0)+1);
		}
	}
	return t;
}

void
html(Rune *id, Rune *s)
{
	Rune *es;
	Tag *t, *tt, *next;

	br();
	hideihtml();	/* br already did, but be paranoid */
	for(t=tagstack; t; t=t->next){
		if(runestrcmp(t->id, id) == 0){
			for(tt=tagstack;; tt=next){
				next = tt->next;
				free(tt->id);
				free(tt->open);
				out(tt->close);
				outrune('\n');
				free(tt->close);
				free(tt);
				if(tt == t){
					tagstack = next;
					goto cleared;
				}
			}
		}
	}

cleared:
	if(s == nil || s[0] == 0)
		return;
	out(s);
	outrune('\n');
	es = closingtag(s);
	if(es[0] == 0){
		free(es);
		return;
	}
	if(runestrcmp(id, L("-")) == 0){
		out(es);
		outrune('\n');
		free(es);
		return;
	}
	t = emalloc(sizeof *t);
	t->id = erunestrdup(id);
	t->close = es;
	t->next = tagstack;
	tagstack = t;
}

void
closehtml(void)
{
	Tag *t, *next;

	br();
	hideihtml();
	for(t=tagstack; t; t=next){
		next = t->next;
		out(t->close);
		outrune('\n');
		free(t->id);
		free(t->close);
		free(t);
	}
}

static void
rshow(Tag *t, Tag *end)
{
	if(t == nil || t == end)
		return;
	rshow(t->next, end);
	out(t->open);
}

void
ihtml(Rune *id, Rune *s)
{
	Tag *t, *tt, **l;

	for(t=tagset; t; t=t->next){
		if(runestrcmp(t->id, id) == 0){
			if(s && t->open && runestrcmp(t->open, s) == 0)
				return;
			for(l=&tagset; (tt=*l); l=&tt->next){
				if(!hidingset)
					out(tt->close);
				if(tt == t)
					break;
			}
			*l = t->next;
			free(t->id);
			free(t->close);
			free(t->open);
			free(t);
			if(!hidingset)
				rshow(tagset, *l);
			goto cleared;
		}
	}

cleared:
	if(s == nil || s[0] == 0)
		return;
	t = emalloc(sizeof *t);
	t->id = erunestrdup(id);
	t->open = erunestrdup(s);
	t->close = closingtag(s);
	if(!hidingset)
		out(s);
	t->next = tagset;
	tagset = t;
}

void
hideihtml(void)
{
	Tag *t;

	if(hidingset)
		return;
	hidingset = 1;
	for(t=tagset; t; t=t->next)
		out(t->close);
}

void
showihtml(void)
{
	if(!hidingset)
		return;
	hidingset = 0;
	rshow(tagset, nil);
}

int
e_lt(void)
{
	return Ult;
}

int
e_gt(void)
{
	return Ugt;
}

int
e_at(void)
{
	return Uamp;
}

int
e_tick(void)
{
	return Utick;
}

int
e_btick(void)
{
	return Ubtick;
}

int
e_minus(void)
{
	return Uminus;
}

void
r_html(Rune *name)
{
	Rune *id, *line, *p;

	id = copyarg();
	line = readline(HtmlMode);
	for(p=line; *p; p++){
		switch(*p){
		case '<':
			*p = Ult;
			break;
		case '>':
			*p = Ugt;
			break;
		case '&':
			*p = Uamp;
			break;
		case ' ':
			*p = Uspace;
			break;
		}
	}
	if(name[0] == 'i')
		ihtml(id, line);
	else
		html(id, line);
	free(id);
	free(line);
}

char defaultfont[] =
	".ihtml f1\n"
	".ihtml f\n"
	".ihtml f <span style=\"font-size: \\n(.spt\">\n"
	".if \\n(.f==2 .ihtml f1 <i>\n"
	".if \\n(.f==3 .ihtml f1 <b>\n"
	".if \\n(.f==4 .ihtml f1 <b><i>\n"
	".if \\n(.f==5 .ihtml f1 <tt>\n"
	".if \\n(.f==6 .ihtml f1 <tt><i>\n"
	"..\n"
;

void
htmlinit(void)
{
	addraw(L("html"), r_html);
	addraw(L("ihtml"), r_html);

	addesc('<', e_lt, CopyMode);
	addesc('>', e_gt, CopyMode);
	addesc('\'', e_tick, CopyMode);
	addesc('`', e_btick, CopyMode);
	addesc('-', e_minus, CopyMode);
	addesc('@', e_at, CopyMode);

	ds(L("font"), L(defaultfont));
}