diff options
Diffstat (limited to 'troff/troff.d/dhtml')
-rw-r--r-- | troff/troff.d/dhtml/Makefile.mk | 29 | ||||
-rw-r--r-- | troff/troff.d/dhtml/char.c | 115 | ||||
-rw-r--r-- | troff/troff.d/dhtml/char.h | 15 | ||||
-rw-r--r-- | troff/troff.d/dhtml/dhtml.1 | 107 | ||||
-rw-r--r-- | troff/troff.d/dhtml/dhtml.h | 1 | ||||
-rw-r--r-- | troff/troff.d/dhtml/dhtml.l | 87 | ||||
-rw-r--r-- | troff/troff.d/dhtml/lib.c | 148 | ||||
-rw-r--r-- | troff/troff.d/dhtml/lib.h | 9 | ||||
-rw-r--r-- | troff/troff.d/dhtml/main.c | 79 | ||||
-rw-r--r-- | troff/troff.d/dhtml/main.h | 1 | ||||
-rw-r--r-- | troff/troff.d/dhtml/roff.c | 27 | ||||
-rw-r--r-- | troff/troff.d/dhtml/roff.h | 2 | ||||
-rw-r--r-- | troff/troff.d/dhtml/tr_out.c | 221 | ||||
-rw-r--r-- | troff/troff.d/dhtml/tr_out.h | 17 |
14 files changed, 858 insertions, 0 deletions
diff --git a/troff/troff.d/dhtml/Makefile.mk b/troff/troff.d/dhtml/Makefile.mk new file mode 100644 index 0000000000000..2df615f95fb44 --- /dev/null +++ b/troff/troff.d/dhtml/Makefile.mk @@ -0,0 +1,29 @@ +BST= ../../../stuff/bst +BIN= dhtml +OBJ= main.o dhtml.o tr_out.o char.o lib.o $(BST)/bst.o +CPPFLAGS= -DFNTDIR='"$(FNTDIR)"' -I$(BST) + +all: $(BIN) + +install: + $(STRIP) $(BIN) + $(INSTALL) $(BIN) $(ROOT)$(BINDIR)/ + sed 's"$$FNTDIR"$(FNTDIR)"g' $(BIN).1 > \ + $(ROOT)$(MANDIR)/man1/$(BIN).1 + +clean: + rm -f $(OBJ) $(BIN) + +mrproper: clean + +$(BIN): $(OBJ) + $(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o $@ + +.c.o: + $(CC) $(CFLAGS) $(CPPFLAGS) -c $< + +char.o: char.h main.h $(BST)/bst.h lib.h tr_out.h +dhtml.o: dhtml.h tr_out.h char.h main.h +lib.o: main.h $(BST)/bst.h +main.o: dhtml.h char.h +tr_out.o: tr_out.h main.h $(BST)/bst.h lib.h diff --git a/troff/troff.d/dhtml/char.c b/troff/troff.d/dhtml/char.c new file mode 100644 index 0000000000000..491ded38f3a10 --- /dev/null +++ b/troff/troff.d/dhtml/char.c @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2015, Carsten Kunze + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "char.h" +#include "main.h" +#include "bst.h" +#include "lib.h" +#include "tr_out.h" + +int prevchar; + +static struct bst namdat = { NULL, bst_scmp }; +static struct bst chrdat = { NULL, bst_icmp }; +static struct bst numdat = { NULL, bst_icmp }; + +void +char_open(void) { + char *b; + ssize_t s; + if (!(b = file2ram(FNTDIR "/devhtml/CHAR", &s))) exit(1); + while (1) { + char *w, *l; + size_t n; + int t; + if (!(s = lineskip(&b, s))) return; + if (!(w = get_word(&b, &s, &n, &t))) return; + if (!(l = get_line(&b, &s, NULL))) return; + if (n == 1) + avl_add(&chrdat, I2BST(*w), S2BST(l)); + else if (n != 2 && !(t & ~1)) + avl_add(&numdat, I2BST(atoi(w)), S2BST(l)); + else + avl_add(&namdat, S2BST(w), S2BST(l)); + } +} + +void +char_c(int c) { + if (c == '`' || c == '\'') { + if (!prevchar) prevchar = c; + else if (c != prevchar) { + chrout(prevchar); + prevchar = c; + } else { + switch (c) { + case '`' : fputs("“", stdout); break; + case '\'': fputs("”", stdout); break; + } + prevchar = 0; + } + } else { + clslig(); + chrout(c); + } + hdecr(); +} + +void +char_N(int i) { + struct bst_node *n; + clslig(); + if (!bst_srch(&numdat, I2BST(i), &n)) + fputs(n->data.p, stdout); + else + printf("&#%d;", i); + hdecr(); +} + +void +char_C(char *s) { + struct bst_node *n; + clslig(); + if (!bst_srch(&namdat, S2BST(s), &n)) + fputs(n->data.p, stdout); + else + fprintf(stderr, "%s: Unknown character name \"%s\"\n", + progname, s); + hdecr(); +} + +void +chrout(int c) { + struct bst_node *n; + if (!bst_srch(&chrdat, I2BST(c), &n)) + fputs(n->data.p, stdout); + else + putchar(c); +} diff --git a/troff/troff.d/dhtml/char.h b/troff/troff.d/dhtml/char.h new file mode 100644 index 0000000000000..cd3aefd6cf3b5 --- /dev/null +++ b/troff/troff.d/dhtml/char.h @@ -0,0 +1,15 @@ +#define clslig() \ + do { \ + if (prevchar) { \ + chrout(prevchar); \ + prevchar = 0; \ + } \ + } while (0) + +void char_open(void); +void char_c(int); +void char_C(char *); +void char_N(int); +void chrout(int); + +extern int prevchar; diff --git a/troff/troff.d/dhtml/dhtml.1 b/troff/troff.d/dhtml/dhtml.1 new file mode 100644 index 0000000000000..beb09286f1af5 --- /dev/null +++ b/troff/troff.d/dhtml/dhtml.1 @@ -0,0 +1,107 @@ +.\" Copyright (c) 2015, Carsten Kunze +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions are met: +.\" +.\" 1. Redistributions of source code must retain the above copyright notice, +.\" this list of conditions and the following disclaimer. +.\" +.\" 2. Redistributions in binary form must reproduce the above copyright notice, +.\" this list of conditions and the following disclaimer in the documentation +.\" and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +.\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +.\" POSSIBILITY OF SUCH DAMAGE. +.Dd September 18, 2015 +.Dt DHTML 1 +.Sh NAME +.Nm dhtml +.Nd HTML postprocessor for troff +.Sh SYNOPSIS +.Nm +.Op Fl t Ar title +.Li < Ar ditroff_output Li > Ar html_output +.Sh DESCRIPTION +.Nm +is a HTML postprocessor for +.Xr troff 1 . +It reads the ditroff output from +.Dv STDIN +and writes HTML5 output to +.Dv STDOUT . +.Nm troff +must in this case be called with option +.Fl Thtml . +Missing charaters can be added by changing the files +.Pa charset +and +.Pa CHAR +(see section +.Sx FILES ) . +For adding fonts the tool needs to be recompiled (currently the +legacy fonts +.Li R I B BI C CW CR CI CB H HI HB S +are available). +.Pp +Options: +.Bl -tag -width ".Fl t Ar title" +.It Fl t Ar title +Set text for the HTML +.Li <title> +tag. +.El +.Sh FILES +.Bl -tag +.It Ar source_dir Ns Pa /troff/troff.d/font/devhtml/charset +.Sy charset +which is appended to each font description file at installation +of the tool (run +.Pp +.Dl # make install +.Pp +after changes of this file). +.It Pa $FNTDIR/devhtml/CHAR +Character to HTML name mapping which is read at each run of +.Nm . +.El +.Sh EXAMPLES +.Bd -unfilled -offset indent +.Li tbl Ar manpage Li | troff -Thtml -mandoc \(rs +.Li " |" dhtml Li -t Ar html_title Li > Ar html_file +.Ed +.Pp +Convert +.Ar manpage +into HTML format with title +.Ar html_title +and save it as +.Ar html_file . +.Sh SEE ALSO +.Xr troff 1 , +.Xr dpost 1 +.Sh BUGS +For the current version no changes to the ditroff interface are done. +All information for formatting the HTML page is taken from legacy ditroff +commands. +As a consequence the alignment cannot be as accurate as in the +.Xr dpost 1 +output since the character widths of HTML fonts must be supposed to be +unknown. +.Pp +At the moment no HTML tables are generated. +.Xr tbl 1 +(without options) can be used for preprocessing but mayor alingment issues +may result. +There is no support for equations, graphics, images, and links in this +versions. +These features are subject to be implemented later. diff --git a/troff/troff.d/dhtml/dhtml.h b/troff/troff.d/dhtml/dhtml.h new file mode 100644 index 0000000000000..1c4d1be54f51f --- /dev/null +++ b/troff/troff.d/dhtml/dhtml.h @@ -0,0 +1 @@ +void run_lex(void); diff --git a/troff/troff.d/dhtml/dhtml.l b/troff/troff.d/dhtml/dhtml.l new file mode 100644 index 0000000000000..c99db26a88a19 --- /dev/null +++ b/troff/troff.d/dhtml/dhtml.l @@ -0,0 +1,87 @@ +%{ +/* + * Copyright (c) 2015, Carsten Kunze + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <string.h> +#include "dhtml.h" +#include "tr_out.h" +#include "char.h" +#include "main.h" +static int i0; +%} +%x X FONT_NUM FONT_NAM T X_X LINK ULINK ANCHOR +WS [ ] +NWS [^ \n] +%% +<X>f{NWS}*{WS}+ { BEGIN FONT_NUM; } +<X>T{NWS}*{WS}+ { BEGIN T; } +<X>X{NWS}*{WS}+ { BEGIN X_X; } +<X>[^fTX].* { BEGIN 0; } +<FONT_NUM>[0-9]+{WS}+ { i0 = atoi(yytext); + BEGIN FONT_NAM; } +<FONT_NAM>{NWS}+ { out_x_f(i0, yytext); + BEGIN 0; } +<T>{NWS}+ { out_x_T(yytext); + BEGIN 0; } +<X_X>Link{WS}+ { BEGIN LINK; } +<X_X>ULink{WS}+ { BEGIN ULINK; } +<X_X>U?Link{WS}*\n { out_end_link(); + BEGIN 0; } +<X_X>Anchor{WS}+ { BEGIN ANCHOR; } +<X_X>LC_TYPE.* { BEGIN 0; } +<X_X>[^AL].* { BEGIN 0; } +<LINK>.+ { out_begin_link(yytext); + BEGIN 0; } +<ULINK>.+ { out_begin_ulink(yytext); + BEGIN 0; } +<ANCHOR>.+ { out_anchor(yytext); + BEGIN 0; } +c. { char_c(yytext[1]); } +[0-9][0-9]. { char_c(yytext[2]); } +C{NWS}+ { char_C(yytext+1); } +w { out_w(); } +H-?[0-9]+ { ; } +h-?[0-9]+ { out_h(atoi(yytext+1)); } +V-?[0-9]+ { out_V(atoi(yytext+1)); } +v-?[0-9]+ { fprintf(stderr, "Ignore v %s\n", yytext+1); } +f[0-9]+ { out_f(atoi(yytext+1)); } +N[0-9]+ { char_N(atoi(yytext+1)); } +n[0-9]+{WS}+[0-9]+ { out_n(atoi(yytext+1)); } +s[0-9]+ { out_s(atoi(yytext+1)); } +p[0-9]+ { ; } +x{WS}+ { BEGIN X; } +D.+ { ; } +\n { ; } +. { fprintf(stderr, "Ignore '%c'\n", *yytext); } +%% +int +yywrap(void) { + return 1; +} +void +run_lex(void) { + yylex(); +} diff --git a/troff/troff.d/dhtml/lib.c b/troff/troff.d/dhtml/lib.c new file mode 100644 index 0000000000000..251223627e05c --- /dev/null +++ b/troff/troff.d/dhtml/lib.c @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2015, Carsten Kunze + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <unistd.h> +#include <string.h> +#include "main.h" +#include "bst.h" + +void * /* pointer to \0 terminated file contents */ +file2ram(char *p, /* path to file */ + ssize_t *l) { /* pointer to buffer size variable */ + int f; + struct stat s; + char *b = NULL; + if ((f = open(p, O_RDONLY)) == -1) { + fprintf(stderr, "%s: open(%s) failed: ", progname, p); + perror(NULL); + goto r; + } + if (fstat(f, &s) == -1) { + fprintf(stderr, "%s: fstat(%s) failed: ", progname, p); + perror(NULL); + goto c; + } + if (!(b = malloc(s.st_size + 1))) goto c; + if ((*l = read(f, b, s.st_size)) == -1) { + free(b); + fprintf(stderr, "%s: read(%s) failed: ", progname, p); + perror(NULL); + goto c; + } + b[*l] = 0; +c: + close(f); +r: + return b; +} + +ssize_t +lineskip(char **b, ssize_t n) { + char *p = *b; + int i = 0; + int c; + while (1) { + for (; n && (!(c = *p) || c == ' ' || c == '\t' || c == '\n'); + n--, p++) { + if (c == '\n') i = 0; + else i++; + } + if (!n || !i || c != '#') goto r; + for (; n && (c = *p) && c != '\n'; n--, p++); + } +r: + *b = p; + return n; +} + +char * /* pointer to word */ +get_word(char **b, /* begin of buffer, then end of buffer */ + ssize_t *l, /* buffer length */ + size_t *s, /* word length (if not NULL) */ + int *t) { /* type (if not NULL) */ + char *w = NULL; + char *p = *b; + ssize_t n = *l; + int c; + size_t i = 0; + int f = 0; + for (; n && (!(c = *p) || c == ' ' || c == '\t' || c == '\n'); + n--, p++); + if (!n) goto r; + w = p; + for (; n && (c = *p) && c != ' ' && c != '\t' && c != '\n'; + i++, n--, p++) { + if (c >= '0' && c <= '9') f |= 1; + else f |= 2; + } + if (!n) goto r; + *p = 0; +r: + *b = p; + *l = n; + if (s) *s = i; + if (t) *t = f; + return w; +} + +char * /* pointer to line */ +get_line(char **b, /* begin of buffer, then end of buffer */ + ssize_t *l, /* buffer length */ + size_t *s) { /* line length (if not NULL) */ + char *w = NULL; + char *p = *b; + ssize_t n = *l; + int c; + size_t i = 0; + for (; n && (!(c = *p) || c == ' ' || c == '\t'); + n--, p++); + if (!n) goto r; + w = p; + for (; n && (c = *p) && c != '\n'; i++, n--, p++); + if (!n) goto r; + *p = 0; +r: + *b = p; + *l = n; + if (s) *s = i; + return w; +} + +int +bst_scmp(union bst_val a, union bst_val b) { + return strcmp(a.p, b.p); +} +int +bst_icmp(union bst_val a, union bst_val b) { + return a.i < b.i ? -1 : + a.i > b.i ? 1 : + 0 ; +} diff --git a/troff/troff.d/dhtml/lib.h b/troff/troff.d/dhtml/lib.h new file mode 100644 index 0000000000000..7f8d5b591ffaf --- /dev/null +++ b/troff/troff.d/dhtml/lib.h @@ -0,0 +1,9 @@ +#define I2BST(i) ((union bst_val)(int)i) +#define S2BST(s) ((union bst_val)(void *)s) + +void *file2ram(char *, ssize_t *); +ssize_t lineskip(char **, ssize_t); +char *get_word(char **, ssize_t *, size_t *, int *); +char *get_line(char **, ssize_t *, size_t *); +int bst_scmp(union bst_val, union bst_val); +int bst_icmp(union bst_val, union bst_val); diff --git a/troff/troff.d/dhtml/main.c b/troff/troff.d/dhtml/main.c new file mode 100644 index 0000000000000..ece7903327943 --- /dev/null +++ b/troff/troff.d/dhtml/main.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2015, Carsten Kunze + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include "dhtml.h" +#include "char.h" + +char *progname; + +static void begin_html(void); +static void end_html(void); + +static char *title; + +int +main(int argc, char **argv) { + int opt; + progname = argv[0]; + while ((opt = getopt(argc, argv, "t:")) != -1) { + switch (opt) { + case 't': + title = optarg; + break; + default: + fprintf(stderr, "Usage: %s [-t title] < " + "ditroff_output > html_output\n", progname); + exit(EXIT_FAILURE); + } + } + begin_html(); + char_open(); + run_lex(); + end_html(); + return 0; +} + +static void +begin_html(void) { + puts("<!DOCTYPE html>"); + puts("<html lang=\"en\">"); + puts(" <head>"); + puts(" <meta charset=\"utf-8\">"); + printf(" <title>%s</title>", title ? title : ""); + puts(" </head>"); + puts(" <body>"); +} + +static void +end_html(void) { + puts(""); + puts(" </body>"); + puts("</html>"); +} diff --git a/troff/troff.d/dhtml/main.h b/troff/troff.d/dhtml/main.h new file mode 100644 index 0000000000000..0aca5f172b38c --- /dev/null +++ b/troff/troff.d/dhtml/main.h @@ -0,0 +1 @@ +extern char *progname; diff --git a/troff/troff.d/dhtml/roff.c b/troff/troff.d/dhtml/roff.c new file mode 100644 index 0000000000000..760893d24a054 --- /dev/null +++ b/troff/troff.d/dhtml/roff.c @@ -0,0 +1,27 @@ +#include <stdio.h> +#include "roff.h" + +static struct req_in_data { + int not_1st; + int last_i; +} req_in_data; + +void +req_in(int i) { +#if 0 + if (req_in_data.not_1st) { + puts("\n</div>"); + req_in_data.not_1st = 0; + } + if (i) { + printf("\n<div style=\"padding-left: %dpx;\">\n", i); + req_in_data.not_1st = 1; + } +#endif + puts("<br>"); +} + +void +req_sp(int i) { + printf("\n<div style=\"height: %dpx;\"></div>\n", (i*12)/40); +} diff --git a/troff/troff.d/dhtml/roff.h b/troff/troff.d/dhtml/roff.h new file mode 100644 index 0000000000000..93150486a60b0 --- /dev/null +++ b/troff/troff.d/dhtml/roff.h @@ -0,0 +1,2 @@ +void req_in(int); +void req_sp(int); diff --git a/troff/troff.d/dhtml/tr_out.c b/troff/troff.d/dhtml/tr_out.c new file mode 100644 index 0000000000000..7fd52c901dd61 --- /dev/null +++ b/troff/troff.d/dhtml/tr_out.c @@ -0,0 +1,221 @@ +/* + * Copyright (c) 2015, Carsten Kunze + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "tr_out.h" +#include "main.h" +#include "bst.h" +#include "lib.h" +#include "char.h" + +int fontsize; +int hdec; + +static void closesize(void); + +static int lineheight; +static int totalh; +static char *size_end; +static struct bst fonts = { NULL, bst_icmp }; + +void +out_x_T(char *dev) { + if (!strcmp(dev, "html")) return; + fprintf(stderr, "%s: Invalid troff output device \"%s\"." + "Please use troff with option \"-Thtml\".\n", progname, dev); +} + +void +out_f(int num) { + static char *prevfont = "R"; + static char *closefont; + struct bst_node *n; + char *nam; + clslig(); + if (!bst_srch(&fonts, I2BST(num), &n)) { + nam = n->data.p; + } else { + nam = "R"; + if (num != 1) + fprintf(stderr, "%s: Unknown font %d\n", progname, + num); + } + if (!strcmp(nam, prevfont)) return; + closesize(); + if (closefont) { + fputs(closefont, stdout); + closefont = NULL; + } + prevfont = nam; + if (!strcmp(nam, "R") || + !strcmp(nam, "S")) return; + else + if (!strcmp(nam, "I")) { + fputs("<span style=\"font-style: italic\">", stdout); + closefont = "</span>"; + } else + if (!strcmp(nam, "B")) { + fputs("<span style=\"font-weight: bold\">", stdout); + closefont = "</span>"; + } else + if (!strcmp(nam, "BI")) { + fputs("<span style=\"font-weight: bold;" + " font-style: italic\">", stdout); + closefont = "</span>"; + } else + if (!strcmp(nam, "CW") || + !strcmp(nam, "CR") || + !strcmp(nam, "C")) { + fputs("<span style=\"font-family: monospace\">", stdout); + closefont = "</span>"; + } else + if (!strcmp(nam, "CI")) { + fputs("<span style=\"font-family: monospace;" + " font-style: italic\">", stdout); + closefont = "</span>"; + } else + if (!strcmp(nam, "CB")) { + fputs("<span style=\"font-family: monospace;" + " font-weight: bold\">", stdout); + closefont = "</span>"; + } else + if (!strcmp(nam, "H")) { + fputs("<span style=\"font-family: sans-serif\">", stdout); + closefont = "</span>"; + } else + if (!strcmp(nam, "HI")) { + fputs("<span style=\"font-family: sans-serif;" + " font-style: italic\">", stdout); + closefont = "</span>"; + } else + if (!strcmp(nam, "HB")) { + fputs("<span style=\"font-family: sans-serif;" + " font-weight: bold\">", stdout); + closefont = "</span>"; + } else + fprintf(stderr, "%s: Unknown font name \"%s\"\n", + progname, nam); + out_s(fontsize); +} + +void +out_s(int i) { + fontsize = i; + clslig(); + closesize(); + if (fontsize == 10) return; + printf("<span style=\"font-size: %gem\">", fontsize / 10.0); + size_end = "</span>"; +} + +static void +closesize(void) { + if (size_end) { + fputs(size_end, stdout); + size_end = NULL; + } +} + +void +out_n(int i) { + clslig(); + lineheight = i; +} + +void +out_V(int i) { + static int prevv; + int d; + clslig(); + if (i < 0) return; + if (i < prevv) { + prevv = i; + return; + } + if (!lineheight) return; + prevv += lineheight; + if ((d = i - prevv) > 0) { + printf("<p style=\"heigh: %dpx\"></p>\n", d); + prevv = i; + } else { + puts("<br>"); + } + lineheight = 0; + totalh = 0; + hdec = 0; +} + +void +out_h(int i) { + if (lineheight) i -= totalh; + else totalh += i; + i -= hdec; + hdec = 0; + if (i <= 0) return; + printf("<span style=\"display: inline-block; width: %gpt\"></span>", + i / 20.0); +} + +void +out_w(void) { + clslig(); + putchar(' '); + hdec += 10 * fontsize; +} + +void +out_x_f(int num, char *nam) { + struct bst_node *n; + if (bst_srch(&fonts, I2BST(num), &n)) { + avl_add(&fonts, I2BST(num), S2BST(strdup(nam))); + } else { + free(n->data.p); + n->data.p = strdup(nam); + } +} + +void +out_begin_link(char *l) { + printf("<a href=\"#%s\">", l); +} + +void +out_begin_ulink(char *l) { + printf("<a href=\"%s\">", l); +} + +void +out_end_link(void) { + fputs("</a>", stdout); +} + +void +out_anchor(char *a) { + printf("<span id=\"%s\"></span>", a); +} diff --git a/troff/troff.d/dhtml/tr_out.h b/troff/troff.d/dhtml/tr_out.h new file mode 100644 index 0000000000000..e040bd84d1bca --- /dev/null +++ b/troff/troff.d/dhtml/tr_out.h @@ -0,0 +1,17 @@ +#define hdecr() hdec += 10 * fontsize + +void out_anchor(char *); +void out_begin_link(char *); +void out_begin_ulink(char *); +void out_end_link(void); +void out_f(int); +void out_h(int); +void out_n(int); +void out_s(int); +void out_V(int); +void out_w(void); +void out_x_f(int, char *); +void out_x_T(char *); + +extern int fontsize; +extern int hdec; |