]> git.cameronkatri.com Git - mandoc.git/blob - man_html.c
Implement line breaking of the generated HTML code at space characters
[mandoc.git] / man_html.c
1 /* $Id: man_html.c,v 1.125 2017/01/19 01:00:14 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "mandoc_aux.h"
29 #include "roff.h"
30 #include "man.h"
31 #include "out.h"
32 #include "html.h"
33 #include "main.h"
34
35 /* TODO: preserve ident widths. */
36 /* FIXME: have PD set the default vspace width. */
37
38 #define INDENT 5
39
40 #define MAN_ARGS const struct roff_meta *man, \
41 const struct roff_node *n, \
42 struct mhtml *mh, \
43 struct html *h
44
45 struct mhtml {
46 int fl;
47 #define MANH_LITERAL (1 << 0) /* literal context */
48 };
49
50 struct htmlman {
51 int (*pre)(MAN_ARGS);
52 int (*post)(MAN_ARGS);
53 };
54
55 static void print_bvspace(struct html *,
56 const struct roff_node *);
57 static void print_man_head(MAN_ARGS);
58 static void print_man_nodelist(MAN_ARGS);
59 static void print_man_node(MAN_ARGS);
60 static int a2width(const struct roff_node *,
61 struct roffsu *);
62 static int man_B_pre(MAN_ARGS);
63 static int man_HP_pre(MAN_ARGS);
64 static int man_IP_pre(MAN_ARGS);
65 static int man_I_pre(MAN_ARGS);
66 static int man_OP_pre(MAN_ARGS);
67 static int man_PP_pre(MAN_ARGS);
68 static int man_RS_pre(MAN_ARGS);
69 static int man_SH_pre(MAN_ARGS);
70 static int man_SM_pre(MAN_ARGS);
71 static int man_SS_pre(MAN_ARGS);
72 static int man_UR_pre(MAN_ARGS);
73 static int man_alt_pre(MAN_ARGS);
74 static int man_br_pre(MAN_ARGS);
75 static int man_ign_pre(MAN_ARGS);
76 static int man_in_pre(MAN_ARGS);
77 static int man_literal_pre(MAN_ARGS);
78 static void man_root_post(MAN_ARGS);
79 static void man_root_pre(MAN_ARGS);
80
81 static const struct htmlman mans[MAN_MAX] = {
82 { man_br_pre, NULL }, /* br */
83 { NULL, NULL }, /* TH */
84 { man_SH_pre, NULL }, /* SH */
85 { man_SS_pre, NULL }, /* SS */
86 { man_IP_pre, NULL }, /* TP */
87 { man_PP_pre, NULL }, /* LP */
88 { man_PP_pre, NULL }, /* PP */
89 { man_PP_pre, NULL }, /* P */
90 { man_IP_pre, NULL }, /* IP */
91 { man_HP_pre, NULL }, /* HP */
92 { man_SM_pre, NULL }, /* SM */
93 { man_SM_pre, NULL }, /* SB */
94 { man_alt_pre, NULL }, /* BI */
95 { man_alt_pre, NULL }, /* IB */
96 { man_alt_pre, NULL }, /* BR */
97 { man_alt_pre, NULL }, /* RB */
98 { NULL, NULL }, /* R */
99 { man_B_pre, NULL }, /* B */
100 { man_I_pre, NULL }, /* I */
101 { man_alt_pre, NULL }, /* IR */
102 { man_alt_pre, NULL }, /* RI */
103 { man_br_pre, NULL }, /* sp */
104 { man_literal_pre, NULL }, /* nf */
105 { man_literal_pre, NULL }, /* fi */
106 { NULL, NULL }, /* RE */
107 { man_RS_pre, NULL }, /* RS */
108 { man_ign_pre, NULL }, /* DT */
109 { man_ign_pre, NULL }, /* UC */
110 { man_ign_pre, NULL }, /* PD */
111 { man_ign_pre, NULL }, /* AT */
112 { man_in_pre, NULL }, /* in */
113 { man_ign_pre, NULL }, /* ft */
114 { man_OP_pre, NULL }, /* OP */
115 { man_literal_pre, NULL }, /* EX */
116 { man_literal_pre, NULL }, /* EE */
117 { man_UR_pre, NULL }, /* UR */
118 { NULL, NULL }, /* UE */
119 { man_ign_pre, NULL }, /* ll */
120 };
121
122
123 /*
124 * Printing leading vertical space before a block.
125 * This is used for the paragraph macros.
126 * The rules are pretty simple, since there's very little nesting going
127 * on here. Basically, if we're the first within another block (SS/SH),
128 * then don't emit vertical space. If we are (RS), then do. If not the
129 * first, print it.
130 */
131 static void
132 print_bvspace(struct html *h, const struct roff_node *n)
133 {
134
135 if (n->body && n->body->child)
136 if (n->body->child->type == ROFFT_TBL)
137 return;
138
139 if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS)
140 if (NULL == n->prev)
141 return;
142
143 print_paragraph(h);
144 }
145
146 void
147 html_man(void *arg, const struct roff_man *man)
148 {
149 struct mhtml mh;
150 struct html *h;
151 struct tag *t, *tt;
152
153 memset(&mh, 0, sizeof(mh));
154 h = (struct html *)arg;
155
156 if ( ! (HTML_FRAGMENT & h->oflags)) {
157 print_gen_decls(h);
158 t = print_otag(h, TAG_HTML, "");
159 tt = print_otag(h, TAG_HEAD, "");
160 print_man_head(&man->meta, man->first, &mh, h);
161 print_tagq(h, tt);
162 print_otag(h, TAG_BODY, "");
163 print_otag(h, TAG_DIV, "c", "mandoc");
164 } else
165 t = print_otag(h, TAG_DIV, "c", "mandoc");
166
167 print_man_nodelist(&man->meta, man->first, &mh, h);
168 print_tagq(h, t);
169 }
170
171 static void
172 print_man_head(MAN_ARGS)
173 {
174 char *cp;
175
176 print_gen_head(h);
177 mandoc_asprintf(&cp, "%s(%s)", man->title, man->msec);
178 print_otag(h, TAG_TITLE, "");
179 print_text(h, cp);
180 free(cp);
181 }
182
183 static void
184 print_man_nodelist(MAN_ARGS)
185 {
186
187 while (n != NULL) {
188 print_man_node(man, n, mh, h);
189 n = n->next;
190 }
191 }
192
193 static void
194 print_man_node(MAN_ARGS)
195 {
196 int child;
197 struct tag *t;
198
199 child = 1;
200 t = h->tags.head;
201
202 switch (n->type) {
203 case ROFFT_ROOT:
204 man_root_pre(man, n, mh, h);
205 break;
206 case ROFFT_TEXT:
207 if ('\0' == *n->string) {
208 print_paragraph(h);
209 return;
210 }
211 if (n->flags & NODE_LINE && (*n->string == ' ' ||
212 (n->prev != NULL && mh->fl & MANH_LITERAL &&
213 ! (h->flags & HTML_NONEWLINE))))
214 print_otag(h, TAG_BR, "");
215 print_text(h, n->string);
216 return;
217 case ROFFT_EQN:
218 print_eqn(h, n->eqn);
219 break;
220 case ROFFT_TBL:
221 /*
222 * This will take care of initialising all of the table
223 * state data for the first table, then tearing it down
224 * for the last one.
225 */
226 print_tbl(h, n->span);
227 return;
228 default:
229 /*
230 * Close out scope of font prior to opening a macro
231 * scope.
232 */
233 if (HTMLFONT_NONE != h->metac) {
234 h->metal = h->metac;
235 h->metac = HTMLFONT_NONE;
236 }
237
238 /*
239 * Close out the current table, if it's open, and unset
240 * the "meta" table state. This will be reopened on the
241 * next table element.
242 */
243 if (h->tblt) {
244 print_tblclose(h);
245 t = h->tags.head;
246 }
247 if (mans[n->tok].pre)
248 child = (*mans[n->tok].pre)(man, n, mh, h);
249 break;
250 }
251
252 if (child && n->child)
253 print_man_nodelist(man, n->child, mh, h);
254
255 /* This will automatically close out any font scope. */
256 print_stagq(h, t);
257
258 switch (n->type) {
259 case ROFFT_ROOT:
260 man_root_post(man, n, mh, h);
261 break;
262 case ROFFT_EQN:
263 break;
264 default:
265 if (mans[n->tok].post)
266 (*mans[n->tok].post)(man, n, mh, h);
267 break;
268 }
269 }
270
271 static int
272 a2width(const struct roff_node *n, struct roffsu *su)
273 {
274
275 if (n->type != ROFFT_TEXT)
276 return 0;
277 if (a2roffsu(n->string, su, SCALE_EN))
278 return 1;
279
280 return 0;
281 }
282
283 static void
284 man_root_pre(MAN_ARGS)
285 {
286 struct tag *t, *tt;
287 char *title;
288
289 assert(man->title);
290 assert(man->msec);
291 mandoc_asprintf(&title, "%s(%s)", man->title, man->msec);
292
293 t = print_otag(h, TAG_TABLE, "c", "head");
294 print_otag(h, TAG_TBODY, "");
295 tt = print_otag(h, TAG_TR, "");
296
297 print_otag(h, TAG_TD, "c", "head-ltitle");
298 print_text(h, title);
299 print_stagq(h, tt);
300
301 print_otag(h, TAG_TD, "c", "head-vol");
302 if (NULL != man->vol)
303 print_text(h, man->vol);
304 print_stagq(h, tt);
305
306 print_otag(h, TAG_TD, "c", "head-rtitle");
307 print_text(h, title);
308 print_tagq(h, t);
309 free(title);
310 }
311
312 static void
313 man_root_post(MAN_ARGS)
314 {
315 struct tag *t, *tt;
316
317 t = print_otag(h, TAG_TABLE, "c", "foot");
318 tt = print_otag(h, TAG_TR, "");
319
320 print_otag(h, TAG_TD, "c", "foot-date");
321 print_text(h, man->date);
322 print_stagq(h, tt);
323
324 print_otag(h, TAG_TD, "c", "foot-os");
325 if (man->os)
326 print_text(h, man->os);
327 print_tagq(h, t);
328 }
329
330
331 static int
332 man_br_pre(MAN_ARGS)
333 {
334 struct roffsu su;
335
336 SCALE_VS_INIT(&su, 1);
337
338 if (MAN_sp == n->tok) {
339 if (NULL != (n = n->child))
340 if ( ! a2roffsu(n->string, &su, SCALE_VS))
341 su.scale = 1.0;
342 } else
343 su.scale = 0.0;
344
345 print_otag(h, TAG_DIV, "suh", &su);
346
347 /* So the div isn't empty: */
348 print_text(h, "\\~");
349
350 return 0;
351 }
352
353 static int
354 man_SH_pre(MAN_ARGS)
355 {
356 if (n->type == ROFFT_BLOCK) {
357 mh->fl &= ~MANH_LITERAL;
358 print_otag(h, TAG_DIV, "c", "section");
359 return 1;
360 } else if (n->type == ROFFT_BODY)
361 return 1;
362
363 print_otag(h, TAG_H1, "");
364 return 1;
365 }
366
367 static int
368 man_alt_pre(MAN_ARGS)
369 {
370 const struct roff_node *nn;
371 int i, savelit;
372 enum htmltag fp;
373 struct tag *t;
374
375 if ((savelit = mh->fl & MANH_LITERAL))
376 print_otag(h, TAG_BR, "");
377
378 mh->fl &= ~MANH_LITERAL;
379
380 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
381 t = NULL;
382 switch (n->tok) {
383 case MAN_BI:
384 fp = i % 2 ? TAG_I : TAG_B;
385 break;
386 case MAN_IB:
387 fp = i % 2 ? TAG_B : TAG_I;
388 break;
389 case MAN_RI:
390 fp = i % 2 ? TAG_I : TAG_MAX;
391 break;
392 case MAN_IR:
393 fp = i % 2 ? TAG_MAX : TAG_I;
394 break;
395 case MAN_BR:
396 fp = i % 2 ? TAG_MAX : TAG_B;
397 break;
398 case MAN_RB:
399 fp = i % 2 ? TAG_B : TAG_MAX;
400 break;
401 default:
402 abort();
403 }
404
405 if (i)
406 h->flags |= HTML_NOSPACE;
407
408 if (TAG_MAX != fp)
409 t = print_otag(h, fp, "");
410
411 print_man_node(man, nn, mh, h);
412
413 if (t)
414 print_tagq(h, t);
415 }
416
417 if (savelit)
418 mh->fl |= MANH_LITERAL;
419
420 return 0;
421 }
422
423 static int
424 man_SM_pre(MAN_ARGS)
425 {
426 print_otag(h, TAG_SMALL, "");
427 if (MAN_SB == n->tok)
428 print_otag(h, TAG_B, "");
429 return 1;
430 }
431
432 static int
433 man_SS_pre(MAN_ARGS)
434 {
435 if (n->type == ROFFT_BLOCK) {
436 mh->fl &= ~MANH_LITERAL;
437 print_otag(h, TAG_DIV, "c", "subsection");
438 return 1;
439 } else if (n->type == ROFFT_BODY)
440 return 1;
441
442 print_otag(h, TAG_H2, "");
443 return 1;
444 }
445
446 static int
447 man_PP_pre(MAN_ARGS)
448 {
449
450 if (n->type == ROFFT_HEAD)
451 return 0;
452 else if (n->type == ROFFT_BLOCK)
453 print_bvspace(h, n);
454
455 return 1;
456 }
457
458 static int
459 man_IP_pre(MAN_ARGS)
460 {
461 const struct roff_node *nn;
462
463 if (n->type == ROFFT_BODY) {
464 print_otag(h, TAG_DD, "");
465 return 1;
466 } else if (n->type != ROFFT_HEAD) {
467 print_otag(h, TAG_DL, "");
468 return 1;
469 }
470
471 /* FIXME: width specification. */
472
473 print_otag(h, TAG_DT, "");
474
475 /* For IP, only print the first header element. */
476
477 if (MAN_IP == n->tok && n->child)
478 print_man_node(man, n->child, mh, h);
479
480 /* For TP, only print next-line header elements. */
481
482 if (MAN_TP == n->tok) {
483 nn = n->child;
484 while (NULL != nn && 0 == (NODE_LINE & nn->flags))
485 nn = nn->next;
486 while (NULL != nn) {
487 print_man_node(man, nn, mh, h);
488 nn = nn->next;
489 }
490 }
491
492 return 0;
493 }
494
495 static int
496 man_HP_pre(MAN_ARGS)
497 {
498 struct roffsu sum, sui;
499 const struct roff_node *np;
500
501 if (n->type == ROFFT_HEAD)
502 return 0;
503 else if (n->type != ROFFT_BLOCK)
504 return 1;
505
506 np = n->head->child;
507
508 if (np == NULL || !a2width(np, &sum))
509 SCALE_HS_INIT(&sum, INDENT);
510
511 sui.unit = sum.unit;
512 sui.scale = -sum.scale;
513
514 print_bvspace(h, n);
515 print_otag(h, TAG_DIV, "csului", "spacer", &sum, &sui);
516 return 1;
517 }
518
519 static int
520 man_OP_pre(MAN_ARGS)
521 {
522 struct tag *tt;
523
524 print_text(h, "[");
525 h->flags |= HTML_NOSPACE;
526 tt = print_otag(h, TAG_SPAN, "c", "opt");
527
528 if (NULL != (n = n->child)) {
529 print_otag(h, TAG_B, "");
530 print_text(h, n->string);
531 }
532
533 print_stagq(h, tt);
534
535 if (NULL != n && NULL != n->next) {
536 print_otag(h, TAG_I, "");
537 print_text(h, n->next->string);
538 }
539
540 print_stagq(h, tt);
541 h->flags |= HTML_NOSPACE;
542 print_text(h, "]");
543 return 0;
544 }
545
546 static int
547 man_B_pre(MAN_ARGS)
548 {
549 print_otag(h, TAG_B, "");
550 return 1;
551 }
552
553 static int
554 man_I_pre(MAN_ARGS)
555 {
556 print_otag(h, TAG_I, "");
557 return 1;
558 }
559
560 static int
561 man_literal_pre(MAN_ARGS)
562 {
563
564 if (MAN_fi == n->tok || MAN_EE == n->tok) {
565 print_otag(h, TAG_BR, "");
566 mh->fl &= ~MANH_LITERAL;
567 } else
568 mh->fl |= MANH_LITERAL;
569
570 return 0;
571 }
572
573 static int
574 man_in_pre(MAN_ARGS)
575 {
576 print_otag(h, TAG_BR, "");
577 return 0;
578 }
579
580 static int
581 man_ign_pre(MAN_ARGS)
582 {
583
584 return 0;
585 }
586
587 static int
588 man_RS_pre(MAN_ARGS)
589 {
590 struct roffsu su;
591
592 if (n->type == ROFFT_HEAD)
593 return 0;
594 else if (n->type == ROFFT_BODY)
595 return 1;
596
597 SCALE_HS_INIT(&su, INDENT);
598 if (n->head->child)
599 a2width(n->head->child, &su);
600
601 print_otag(h, TAG_DIV, "sul", &su);
602 return 1;
603 }
604
605 static int
606 man_UR_pre(MAN_ARGS)
607 {
608 n = n->child;
609 assert(n->type == ROFFT_HEAD);
610 if (n->child != NULL) {
611 assert(n->child->type == ROFFT_TEXT);
612 print_otag(h, TAG_A, "ch", "link-ext", n->child->string);
613 }
614
615 assert(n->next->type == ROFFT_BODY);
616 if (n->next->child != NULL)
617 n = n->next;
618
619 print_man_nodelist(man, n->child, mh, h);
620
621 return 0;
622 }