xref: /openbsd-src/usr.bin/mandoc/roff_html.c (revision f933361f20df4def12f9bc8391f68d6bfad3bb75)
1 /*	$OpenBSD: roff_html.c,v 1.10 2017/06/14 22:50:37 schwarze Exp $ */
2 /*
3  * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2014, 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 AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 <sys/types.h>
19 
20 #include <assert.h>
21 #include <stddef.h>
22 
23 #include "roff.h"
24 #include "out.h"
25 #include "html.h"
26 
27 #define	ROFF_HTML_ARGS struct html *h, const struct roff_node *n
28 
29 typedef	void	(*roff_html_pre_fp)(ROFF_HTML_ARGS);
30 
31 static	void	  roff_html_pre_br(ROFF_HTML_ARGS);
32 static	void	  roff_html_pre_ce(ROFF_HTML_ARGS);
33 static	void	  roff_html_pre_sp(ROFF_HTML_ARGS);
34 
35 static	const roff_html_pre_fp roff_html_pre_acts[ROFF_MAX] = {
36 	roff_html_pre_br,  /* br */
37 	roff_html_pre_ce,  /* ce */
38 	NULL,  /* ft */
39 	NULL,  /* ll */
40 	NULL,  /* mc */
41 	NULL,  /* po */
42 	roff_html_pre_ce,  /* rj */
43 	roff_html_pre_sp,  /* sp */
44 	NULL,  /* ta */
45 	NULL,  /* ti */
46 };
47 
48 
49 void
50 roff_html_pre(struct html *h, const struct roff_node *n)
51 {
52 	assert(n->tok < ROFF_MAX);
53 	if (roff_html_pre_acts[n->tok] != NULL)
54 		(*roff_html_pre_acts[n->tok])(h, n);
55 }
56 
57 static void
58 roff_html_pre_br(ROFF_HTML_ARGS)
59 {
60 	struct tag	*t;
61 
62 	t = print_otag(h, TAG_DIV, "");
63 	print_text(h, "\\~");  /* So the div isn't empty. */
64 	print_tagq(h, t);
65 }
66 
67 static void
68 roff_html_pre_ce(ROFF_HTML_ARGS)
69 {
70 	for (n = n->child->next; n != NULL; n = n->next) {
71 		if (n->type == ROFFT_TEXT) {
72 			if (n->flags & NODE_LINE)
73 				roff_html_pre_br(h, n);
74 			print_text(h, n->string);
75 		} else
76 			roff_html_pre(h, n);
77 	}
78 	roff_html_pre_br(h, n);
79 }
80 
81 static void
82 roff_html_pre_sp(ROFF_HTML_ARGS)
83 {
84 	struct roffsu	 su;
85 
86 	SCALE_VS_INIT(&su, 1);
87 	if ((n = n->child) != NULL) {
88 		if (a2roffsu(n->string, &su, SCALE_VS) == NULL)
89 			su.scale = 1.0;
90 		else if (su.scale < 0.0)
91 			su.scale = 0.0;
92 	}
93 	print_otag(h, TAG_DIV, "suh", &su);
94 	print_text(h, "\\~");  /* So the div isn't empty. */
95 }
96