xref: /openbsd-src/usr.bin/mandoc/roff_html.c (revision cefe897421c083ac43eb7ff12a99198529abd174)
1*cefe8974Sschwarze /*	$OpenBSD: roff_html.c,v 1.20 2019/04/30 15:52:42 schwarze Exp $ */
296a5de47Sschwarze /*
36561cb23Sschwarze  * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4275804acSschwarze  * Copyright (c) 2014, 2017, 2018, 2019 Ingo Schwarze <schwarze@openbsd.org>
596a5de47Sschwarze  *
696a5de47Sschwarze  * Permission to use, copy, modify, and distribute this software for any
796a5de47Sschwarze  * purpose with or without fee is hereby granted, provided that the above
896a5de47Sschwarze  * copyright notice and this permission notice appear in all copies.
996a5de47Sschwarze  *
1096a5de47Sschwarze  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1196a5de47Sschwarze  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1296a5de47Sschwarze  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1396a5de47Sschwarze  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1496a5de47Sschwarze  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1596a5de47Sschwarze  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1696a5de47Sschwarze  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1796a5de47Sschwarze  */
1896a5de47Sschwarze #include <sys/types.h>
1996a5de47Sschwarze 
2096a5de47Sschwarze #include <assert.h>
212e362670Sschwarze #include <stdio.h>
222e362670Sschwarze #include <string.h>
2396a5de47Sschwarze 
242e362670Sschwarze #include "mandoc.h"
2596a5de47Sschwarze #include "roff.h"
2696a5de47Sschwarze #include "out.h"
2796a5de47Sschwarze #include "html.h"
2896a5de47Sschwarze 
2996a5de47Sschwarze #define	ROFF_HTML_ARGS struct html *h, const struct roff_node *n
3096a5de47Sschwarze 
3196a5de47Sschwarze typedef	void	(*roff_html_pre_fp)(ROFF_HTML_ARGS);
3296a5de47Sschwarze 
3396a5de47Sschwarze static	void	  roff_html_pre_br(ROFF_HTML_ARGS);
34e13b4195Sschwarze static	void	  roff_html_pre_ce(ROFF_HTML_ARGS);
35275804acSschwarze static	void	  roff_html_pre_fi(ROFF_HTML_ARGS);
362e362670Sschwarze static	void	  roff_html_pre_ft(ROFF_HTML_ARGS);
37275804acSschwarze static	void	  roff_html_pre_nf(ROFF_HTML_ARGS);
386561cb23Sschwarze static	void	  roff_html_pre_sp(ROFF_HTML_ARGS);
3996a5de47Sschwarze 
4096a5de47Sschwarze static	const roff_html_pre_fp roff_html_pre_acts[ROFF_MAX] = {
4196a5de47Sschwarze 	roff_html_pre_br,  /* br */
42e13b4195Sschwarze 	roff_html_pre_ce,  /* ce */
43275804acSschwarze 	roff_html_pre_fi,  /* fi */
442e362670Sschwarze 	roff_html_pre_ft,  /* ft */
456561cb23Sschwarze 	NULL,  /* ll */
4624f1eaadSschwarze 	NULL,  /* mc */
47275804acSschwarze 	roff_html_pre_nf,  /* nf */
48af1e8f15Sschwarze 	NULL,  /* po */
496de096f4Sschwarze 	roff_html_pre_ce,  /* rj */
506561cb23Sschwarze 	roff_html_pre_sp,  /* sp */
51f7242c43Sschwarze 	NULL,  /* ta */
5211d70615Sschwarze 	NULL,  /* ti */
5396a5de47Sschwarze };
5496a5de47Sschwarze 
5596a5de47Sschwarze 
5696a5de47Sschwarze void
roff_html_pre(struct html * h,const struct roff_node * n)5796a5de47Sschwarze roff_html_pre(struct html *h, const struct roff_node *n)
5896a5de47Sschwarze {
5996a5de47Sschwarze 	assert(n->tok < ROFF_MAX);
60c4d3fa85Sschwarze 	if (roff_html_pre_acts[n->tok] != NULL)
6196a5de47Sschwarze 		(*roff_html_pre_acts[n->tok])(h, n);
6296a5de47Sschwarze }
6396a5de47Sschwarze 
6496a5de47Sschwarze static void
roff_html_pre_br(ROFF_HTML_ARGS)6596a5de47Sschwarze roff_html_pre_br(ROFF_HTML_ARGS)
6696a5de47Sschwarze {
673c7cc499Sschwarze 	print_otag(h, TAG_BR, "");
68e13b4195Sschwarze }
69e13b4195Sschwarze 
70e13b4195Sschwarze static void
roff_html_pre_ce(ROFF_HTML_ARGS)71e13b4195Sschwarze roff_html_pre_ce(ROFF_HTML_ARGS)
72e13b4195Sschwarze {
73e13b4195Sschwarze 	for (n = n->child->next; n != NULL; n = n->next) {
74e13b4195Sschwarze 		if (n->type == ROFFT_TEXT) {
75e13b4195Sschwarze 			if (n->flags & NODE_LINE)
76e13b4195Sschwarze 				roff_html_pre_br(h, n);
77e13b4195Sschwarze 			print_text(h, n->string);
78e13b4195Sschwarze 		} else
79e13b4195Sschwarze 			roff_html_pre(h, n);
80e13b4195Sschwarze 	}
81e13b4195Sschwarze 	roff_html_pre_br(h, n);
8296a5de47Sschwarze }
836561cb23Sschwarze 
846561cb23Sschwarze static void
roff_html_pre_fi(ROFF_HTML_ARGS)85275804acSschwarze roff_html_pre_fi(ROFF_HTML_ARGS)
86275804acSschwarze {
87275804acSschwarze 	if (html_fillmode(h, TOKEN_NONE) == ROFF_fi)
88275804acSschwarze 		print_otag(h, TAG_BR, "");
89275804acSschwarze }
90275804acSschwarze 
91275804acSschwarze static void
roff_html_pre_ft(ROFF_HTML_ARGS)922e362670Sschwarze roff_html_pre_ft(ROFF_HTML_ARGS)
932e362670Sschwarze {
942e362670Sschwarze 	const char	*cp;
952e362670Sschwarze 
962e362670Sschwarze 	cp = n->child->string;
97*cefe8974Sschwarze 	html_setfont(h, mandoc_font(cp, (int)strlen(cp)));
982e362670Sschwarze }
992e362670Sschwarze 
1002e362670Sschwarze static void
roff_html_pre_nf(ROFF_HTML_ARGS)101275804acSschwarze roff_html_pre_nf(ROFF_HTML_ARGS)
102275804acSschwarze {
103275804acSschwarze 	if (html_fillmode(h, TOKEN_NONE) == ROFF_nf)
104275804acSschwarze 		print_otag(h, TAG_BR, "");
105275804acSschwarze }
106275804acSschwarze 
107275804acSschwarze static void
roff_html_pre_sp(ROFF_HTML_ARGS)1086561cb23Sschwarze roff_html_pre_sp(ROFF_HTML_ARGS)
1096561cb23Sschwarze {
1107f442bfeSschwarze 	if (html_fillmode(h, TOKEN_NONE) == ROFF_nf) {
1117f442bfeSschwarze 		h->col++;
1127f442bfeSschwarze 		print_endline(h);
1132dd33770Sschwarze 	} else {
1142dd33770Sschwarze 		html_close_paragraph(h);
1157f442bfeSschwarze 		print_otag(h, TAG_P, "c", "Pp");
1162dd33770Sschwarze 	}
1176561cb23Sschwarze }
118