xref: /netbsd-src/external/bsd/mdocml/dist/roff_html.c (revision 544c191c349c1704c9d5e679d12ec15cff579663)
1*544c191cSchristos /*	Id: roff_html.c,v 1.19 2019/01/07 07:26:29 schwarze Exp  */
2c9bcef03Schristos /*
3c9bcef03Schristos  * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4*544c191cSchristos  * Copyright (c) 2014, 2017, 2018, 2019 Ingo Schwarze <schwarze@openbsd.org>
5c9bcef03Schristos  *
6c9bcef03Schristos  * Permission to use, copy, modify, and distribute this software for any
7c9bcef03Schristos  * purpose with or without fee is hereby granted, provided that the above
8c9bcef03Schristos  * copyright notice and this permission notice appear in all copies.
9c9bcef03Schristos  *
10c9bcef03Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11c9bcef03Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12c9bcef03Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13c9bcef03Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14c9bcef03Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15c9bcef03Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16c9bcef03Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17c9bcef03Schristos  */
18c9bcef03Schristos #include <sys/types.h>
19c9bcef03Schristos 
20c9bcef03Schristos #include <assert.h>
21*544c191cSchristos #include <stdio.h>
22*544c191cSchristos #include <string.h>
23c9bcef03Schristos 
24c9bcef03Schristos #include "mandoc.h"
25c9bcef03Schristos #include "roff.h"
26c9bcef03Schristos #include "out.h"
27c9bcef03Schristos #include "html.h"
28c9bcef03Schristos 
29c9bcef03Schristos #define	ROFF_HTML_ARGS struct html *h, const struct roff_node *n
30c9bcef03Schristos 
31c9bcef03Schristos typedef	void	(*roff_html_pre_fp)(ROFF_HTML_ARGS);
32c9bcef03Schristos 
33c9bcef03Schristos static	void	  roff_html_pre_br(ROFF_HTML_ARGS);
34c9bcef03Schristos static	void	  roff_html_pre_ce(ROFF_HTML_ARGS);
35*544c191cSchristos static	void	  roff_html_pre_fi(ROFF_HTML_ARGS);
36*544c191cSchristos static	void	  roff_html_pre_ft(ROFF_HTML_ARGS);
37*544c191cSchristos static	void	  roff_html_pre_nf(ROFF_HTML_ARGS);
38c9bcef03Schristos static	void	  roff_html_pre_sp(ROFF_HTML_ARGS);
39c9bcef03Schristos 
40c9bcef03Schristos static	const roff_html_pre_fp roff_html_pre_acts[ROFF_MAX] = {
41c9bcef03Schristos 	roff_html_pre_br,  /* br */
42c9bcef03Schristos 	roff_html_pre_ce,  /* ce */
43*544c191cSchristos 	roff_html_pre_fi,  /* fi */
44*544c191cSchristos 	roff_html_pre_ft,  /* ft */
45c9bcef03Schristos 	NULL,  /* ll */
46c9bcef03Schristos 	NULL,  /* mc */
47*544c191cSchristos 	roff_html_pre_nf,  /* nf */
48c9bcef03Schristos 	NULL,  /* po */
49c9bcef03Schristos 	roff_html_pre_ce,  /* rj */
50c9bcef03Schristos 	roff_html_pre_sp,  /* sp */
51c9bcef03Schristos 	NULL,  /* ta */
52c9bcef03Schristos 	NULL,  /* ti */
53c9bcef03Schristos };
54c9bcef03Schristos 
55c9bcef03Schristos 
56c9bcef03Schristos void
roff_html_pre(struct html * h,const struct roff_node * n)57c9bcef03Schristos roff_html_pre(struct html *h, const struct roff_node *n)
58c9bcef03Schristos {
59c9bcef03Schristos 	assert(n->tok < ROFF_MAX);
60c9bcef03Schristos 	if (roff_html_pre_acts[n->tok] != NULL)
61c9bcef03Schristos 		(*roff_html_pre_acts[n->tok])(h, n);
62c9bcef03Schristos }
63c9bcef03Schristos 
64c9bcef03Schristos static void
roff_html_pre_br(ROFF_HTML_ARGS)65c9bcef03Schristos roff_html_pre_br(ROFF_HTML_ARGS)
66c9bcef03Schristos {
67*544c191cSchristos 	print_otag(h, TAG_BR, "");
68c9bcef03Schristos }
69c9bcef03Schristos 
70c9bcef03Schristos static void
roff_html_pre_ce(ROFF_HTML_ARGS)71c9bcef03Schristos roff_html_pre_ce(ROFF_HTML_ARGS)
72c9bcef03Schristos {
73c9bcef03Schristos 	for (n = n->child->next; n != NULL; n = n->next) {
74c9bcef03Schristos 		if (n->type == ROFFT_TEXT) {
75c9bcef03Schristos 			if (n->flags & NODE_LINE)
76c9bcef03Schristos 				roff_html_pre_br(h, n);
77c9bcef03Schristos 			print_text(h, n->string);
78c9bcef03Schristos 		} else
79c9bcef03Schristos 			roff_html_pre(h, n);
80c9bcef03Schristos 	}
81c9bcef03Schristos 	roff_html_pre_br(h, n);
82c9bcef03Schristos }
83c9bcef03Schristos 
84c9bcef03Schristos static void
roff_html_pre_fi(ROFF_HTML_ARGS)85*544c191cSchristos roff_html_pre_fi(ROFF_HTML_ARGS)
86*544c191cSchristos {
87*544c191cSchristos 	if (html_fillmode(h, TOKEN_NONE) == ROFF_fi)
88*544c191cSchristos 		print_otag(h, TAG_BR, "");
89*544c191cSchristos }
90*544c191cSchristos 
91*544c191cSchristos static void
roff_html_pre_ft(ROFF_HTML_ARGS)92*544c191cSchristos roff_html_pre_ft(ROFF_HTML_ARGS)
93*544c191cSchristos {
94*544c191cSchristos 	const char	*cp;
95*544c191cSchristos 
96*544c191cSchristos 	cp = n->child->string;
97*544c191cSchristos 	print_metaf(h, mandoc_font(cp, (int)strlen(cp)));
98*544c191cSchristos }
99*544c191cSchristos 
100*544c191cSchristos static void
roff_html_pre_nf(ROFF_HTML_ARGS)101*544c191cSchristos roff_html_pre_nf(ROFF_HTML_ARGS)
102*544c191cSchristos {
103*544c191cSchristos 	if (html_fillmode(h, TOKEN_NONE) == ROFF_nf)
104*544c191cSchristos 		print_otag(h, TAG_BR, "");
105*544c191cSchristos }
106*544c191cSchristos 
107*544c191cSchristos static void
roff_html_pre_sp(ROFF_HTML_ARGS)108c9bcef03Schristos roff_html_pre_sp(ROFF_HTML_ARGS)
109c9bcef03Schristos {
110*544c191cSchristos 	if (html_fillmode(h, TOKEN_NONE) == ROFF_nf) {
111*544c191cSchristos 		h->col++;
112*544c191cSchristos 		print_endline(h);
113*544c191cSchristos 	} else {
114*544c191cSchristos 		html_close_paragraph(h);
115*544c191cSchristos 		print_otag(h, TAG_P, "c", "Pp");
116*544c191cSchristos 	}
117c9bcef03Schristos }
118