From: Kristaps Dzonsons Date: Thu, 4 Dec 2008 11:25:29 +0000 (+0000) Subject: More html-css. X-Git-Tag: VERSION_1_0_1~22 X-Git-Url: https://git.cameronkatri.com/mandoc.git/commitdiff_plain/ff889942f7c344c157152d8ed3c19809b62ff504?ds=sidebyside More html-css. --- diff --git a/Makefile b/Makefile index a5ec3972..163debe4 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS += -W -Wall -Wno-unused-parameter -g +CFLAGS += -W -Wall -Wno-unused-parameter -g -DDEBUG LINTFLAGS += -c -e -f -u diff --git a/html.c b/html.c index 16ed07e1..08d1e8f1 100644 --- a/html.c +++ b/html.c @@ -1,4 +1,4 @@ -/* $Id: html.c,v 1.3 2008/12/03 21:27:56 kristaps Exp $ */ +/* $Id: html.c,v 1.4 2008/12/04 11:25:29 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -16,15 +16,24 @@ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ +#include +#include + #include +#include +#include #include +#include #include +#include #include "libmdocml.h" #include "private.h" #include "ml.h" +static int html_loadcss(struct md_mbuf *, const char *); + static ssize_t html_endtag(struct md_mbuf *, const struct md_args *, enum md_ns, int); @@ -32,8 +41,11 @@ static ssize_t html_begintag(struct md_mbuf *, const struct md_args *, enum md_ns, int, const int *, const char **); -static int html_begin(struct md_mbuf *, - const struct md_args *); +static int html_begin(struct md_mbuf *, + const struct md_args *, + const struct tm *, + const char *, const char *, + const char *, const char *); static int html_end(struct md_mbuf *, const struct md_args *); static ssize_t html_blocktagname(struct md_mbuf *, @@ -58,33 +70,113 @@ static ssize_t html_inlinetagargs(struct md_mbuf *, const int *, const char **); +static int +html_loadcss(struct md_mbuf *mbuf, const char *css) +{ + size_t res, bufsz; + char *buf; + struct stat st; + int fd, c; + ssize_t ssz; + + c = 0; + res = 0; + buf = NULL; + + if (-1 == (fd = open(css, O_RDONLY, 0))) { + warn("%s", css); + return(0); + } + + if (-1 == fstat(fd, &st)) { + warn("%s", css); + goto out; + } + + bufsz = MAX(st.st_blksize, BUFSIZ); + if (NULL == (buf = malloc(bufsz))) { + warn("malloc"); + goto out; + } + + for (;;) { + if (-1 == (ssz = read(fd, buf, bufsz))) { + warn("%s", css); + goto out; + } else if (0 == ssz) + break; + if ( ! ml_nputs(mbuf, buf, (size_t)ssz, &res)) + goto out; + } + + c = 1; + +out: + if (-1 == close(fd)) { + warn("%s", css); + c = 0; + } + + if (buf) + free(buf); + + return(c); +} + + /* ARGSUSED */ static int -html_begin(struct md_mbuf *mbuf, const struct md_args *args) +html_begin(struct md_mbuf *mbuf, const struct md_args *args, + const struct tm *tm, const char *os, + const char *title, const char *section, + const char *vol) { + const char *preamble, *css, *trail; + char buf[512]; size_t res; + preamble = + "\n" + "\n" + "\n" + " \n" + " \n" + " Manual Page for %s(%s)\n"; + + css = + " \n"; + trail = + "\n" + "\n" + "
\n"; + res = 0; - if ( ! ml_puts(mbuf, "\n", &res)) - return(0); - if ( ! ml_puts(mbuf, "\n", &res)) - return(0); - if ( ! ml_puts(mbuf, "\n", &res)) - return(0); - if ( ! ml_puts(mbuf, " Manual page\n", &res)) - return(0); - if ( ! ml_puts(mbuf, " \n", &res)) - return(0); - if ( ! ml_puts(mbuf, " \n", &res)) - return(0); - if ( ! ml_puts(mbuf, "\n", &res)) + + (void)snprintf(buf, sizeof(buf) - 1, + preamble, title, section); + + if ( ! ml_puts(mbuf, buf, &res)) return(0); - if ( ! ml_puts(mbuf, "", &res)) + + assert(args->params.html.css); + if (HTML_CSS_EMBED & args->params.html.flags) { + if ( ! ml_puts(mbuf, " \n", &res)) + return(0); + } else { + (void)snprintf(buf, sizeof(buf) - 1, css, + args->params.html.css); + if ( ! ml_puts(mbuf, buf, &res)) + return(0); + } + + if ( ! ml_puts(mbuf, trail, &res)) return(0); return(1); @@ -98,7 +190,7 @@ html_end(struct md_mbuf *mbuf, const struct md_args *args) size_t res; res = 0; - if ( ! ml_puts(mbuf, "\n", &res)) + if ( ! ml_puts(mbuf, "
\n", &res)) return(0); return(1); @@ -113,17 +205,8 @@ html_blockbodytagname(struct md_mbuf *mbuf, size_t res; res = 0; - - switch (tok) { - case (ROFF_Sh): - if ( ! ml_puts(mbuf, "blockquote", &res)) - return(-1); - break; - default: - if ( ! ml_puts(mbuf, "div", &res)) - return(-1); - break; - } + if ( ! ml_puts(mbuf, "div", &res)) + return(-1); return((ssize_t)res); } @@ -139,21 +222,8 @@ html_blockheadtagname(struct md_mbuf *mbuf, size_t res; res = 0; - - switch (tok) { - case (ROFF_Sh): - if ( ! ml_puts(mbuf, "h1", &res)) - return(-1); - break; - case (ROFF_Ss): - if ( ! ml_puts(mbuf, "h2", &res)) - return(-1); - break; - default: - if ( ! ml_puts(mbuf, "div", &res)) - return(-1); - break; - } + if ( ! ml_puts(mbuf, "div", &res)) + return(-1); return((ssize_t)res); } @@ -167,25 +237,8 @@ html_blocktagname(struct md_mbuf *mbuf, size_t res; res = 0; - - switch (tok) { - case (ROFF_Bd): - if ( ! ml_puts(mbuf, "pre", &res)) - return(-1); - break; - case (ROFF_Bl): - if ( ! ml_puts(mbuf, "ul", &res)) - return(-1); - break; - case (ROFF_It): - if ( ! ml_puts(mbuf, "li", &res)) - return(-1); - break; - default: - if ( ! ml_puts(mbuf, "div", &res)) - return(-1); - break; - } + if ( ! ml_puts(mbuf, "div", &res)) + return(-1); return((ssize_t)res); } @@ -200,7 +253,7 @@ html_blockheadtagargs(struct md_mbuf *mbuf, const struct md_args *args, res = 0; - if ( ! ml_puts(mbuf, " class=\"head:", &res)) + if ( ! ml_puts(mbuf, " class=\"head-", &res)) return(0); if ( ! ml_puts(mbuf, toknames[tok], &res)) return(0); @@ -225,7 +278,7 @@ html_blockbodytagargs(struct md_mbuf *mbuf, const struct md_args *args, res = 0; - if ( ! ml_puts(mbuf, " class=\"body:", &res)) + if ( ! ml_puts(mbuf, " class=\"body-", &res)) return(0); if ( ! ml_puts(mbuf, toknames[tok], &res)) return(0); @@ -250,7 +303,7 @@ html_blocktagargs(struct md_mbuf *mbuf, const struct md_args *args, res = 0; - if ( ! ml_puts(mbuf, " class=\"block:", &res)) + if ( ! ml_puts(mbuf, " class=\"block-", &res)) return(0); if ( ! ml_puts(mbuf, toknames[tok], &res)) return(0); @@ -275,7 +328,7 @@ html_inlinetagargs(struct md_mbuf *mbuf, const struct md_args *args, res = 0; - if ( ! ml_puts(mbuf, " class=\"inline:", &res)) + if ( ! ml_puts(mbuf, " class=\"inline-", &res)) return(0); if ( ! ml_puts(mbuf, toknames[tok], &res)) return(0); @@ -302,6 +355,10 @@ html_inlinetagname(struct md_mbuf *mbuf, res = 0; switch (tok) { + case (ROFF_Pp): + if ( ! ml_puts(mbuf, "div", &res)) + return(-1); + break; default: if ( ! ml_puts(mbuf, "span", &res)) return(-1); diff --git a/libmdocml.h b/libmdocml.h index cbfda8e0..8788467d 100644 --- a/libmdocml.h +++ b/libmdocml.h @@ -1,4 +1,4 @@ -/* $Id: libmdocml.h,v 1.11 2008/12/03 14:39:59 kristaps Exp $ */ +/* $Id: libmdocml.h,v 1.12 2008/12/04 11:25:29 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -26,7 +26,9 @@ struct md_params_xml { }; struct md_params_html { - int dummy; + char *css; + int flags; +#define HTML_CSS_EMBED (1 << 0) }; union md_params { diff --git a/mdocml.1 b/mdocml.1 index 00a600da..c37c89e8 100644 --- a/mdocml.1 +++ b/mdocml.1 @@ -1,6 +1,6 @@ .\" -.Dd $Mdocdate: December 3 2008 $ -.Dt mdocml 1 +.Dd $Mdocdate: December 4 2008 $ +.Dt mdocml 1 alpha .Os .\" .Sh NAME @@ -9,7 +9,7 @@ .\" .Sh SYNOPSIS .Nm mdocml -.Op Fl W +.Op Fl vW .Op Fl f Ar filter .Op Fl o Ar outfile .Op Ar infile @@ -18,9 +18,11 @@ The .Nm utility parses mdoc formatted manual source and passes results into an -output filter. The only current output filter is +output filter. The current output filters are +.Ar html +and .Ar xml , -the default. The arguments are as follows: +the default. Arguments common to all filters follow: .Bl -tag -width "\-o outfile" .It Fl f Ar filter The output filter name. @@ -47,8 +49,10 @@ reads from stdin and writes to stdout using the xml filter. .Ss XML Filter The XML filter, specified by .Fl f Ar xml , -is the default filter. It creates an XML document where element names are -their respective roff macro names. Each element name has an associated +is the default filter. This filter has no additional arguments. +.Pp +The XML filter creates an XML document where element names are their respective +roff macro names. Each element name has an associated namespace, which is one of .Qq block , .Qq head , @@ -58,9 +62,50 @@ or corresponding to the display mode of a node. The document root is always the .Qq mdoc -element, in the default namespace. +element, in the default namespace; the +.Qq head +namespace is for block headers (such as +.Sq .Ss +and +.Sq .Sh ) ; +the +.Qq body +namespace is for block bodies; and the +.Qq inline +namespace is for in-line elements (such as +.Sq .Em ) . +.Ss HTML Filter +The HTML filter, specified by +.Fl f Ar html , +accepts the following filter-specific arguments: +.Bl -tag -width "\-c css" +.It Fl c Ar css +The CSS file location, which defaults to +.Ar mdocml.css . +.It Fl e +Whether to embed the CSS file into the HTML prologue. +.El .\" This next request is for sections 1, 6, 7 & 8 only. .\" .Sh ENVIRONMENT +.Sh EXAMPLES +To produce an HTML4-strict document +.Pa mdocml.html +for +.Pa mdocml.1 +with the default, embedded style-sheet: +.Pp +.D1 % mdocml -fhtml -e mdocml.1 -o mdocml.html +.Pp +To create an XML document on standard output from +.Pa mdocml.1 +with the default namespace identifiers +.Li head , +.Li body , +.Li block +and +.Li inline : +.Pp +.D1 % mdocml mdocml.1 .\" .Sh SEE ALSO .Xr groff 1 , diff --git a/mdocml.c b/mdocml.c index 70a02a2d..de52dac3 100644 --- a/mdocml.c +++ b/mdocml.c @@ -1,4 +1,4 @@ -/* $Id: mdocml.c,v 1.16 2008/12/03 19:21:58 kristaps Exp $ */ +/* $Id: mdocml.c,v 1.17 2008/12/04 11:25:29 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -33,6 +33,12 @@ #define BUFFER_IN_DEF BUFSIZ /* See begin_bufs. */ #define BUFFER_OUT_DEF BUFSIZ /* See begin_bufs. */ +#ifdef DEBUG +#define CSS "mdocml.css" +#else +#define CSS "/usr/local/share/mdocml/mdocml.css" +#endif + static void usage(void); static int begin_io(const struct md_args *, @@ -48,20 +54,37 @@ int main(int argc, char *argv[]) { int c; - char *out, *in, *filter; + char *out, *in; struct md_args args; extern char *optarg; extern int optind; - out = in = filter = NULL; + out = in = NULL; (void)memset(&args, 0, sizeof(struct md_args)); - - while (-1 != (c = getopt(argc, argv, "f:o:vW"))) + + args.type = MD_XML; + + while (-1 != (c = getopt(argc, argv, "c:ef:o:vW"))) switch (c) { + case ('c'): + if (args.type != MD_HTML) + errx(1, "-c only valid for -fhtml"); + args.params.html.css = optarg; + break; + case ('e'): + if (args.type != MD_HTML) + errx(1, "-e only valid for -fhtml"); + args.params.html.flags |= HTML_CSS_EMBED; + break; case ('f'): - filter = optarg; + if (0 == strcmp(optarg, "html")) + args.type = MD_HTML; + else if (0 == strcmp(optarg, "xml")) + args.type = MD_XML; + else + errx(1, "invalid filter type"); break; case ('o'): out = optarg; @@ -77,22 +100,16 @@ main(int argc, char *argv[]) return(1); } + if (MD_HTML == args.type) + if (NULL == args.params.html.css) + args.params.html.css = CSS; + argv += optind; argc -= optind; if (1 == argc) in = *argv++; - if (filter) { - if (0 == strcmp(filter, "html")) - args.type = MD_HTML; - else if (0 == strcmp(filter, "xml")) - args.type = MD_XML; - else - errx(1, "invalid filter type"); - } else - args.type = MD_XML; - return(begin_io(&args, out ? out : "-", in ? in : "-")); } diff --git a/mdocml.css b/mdocml.css new file mode 100644 index 00000000..3acf0d0e --- /dev/null +++ b/mdocml.css @@ -0,0 +1,92 @@ + body + { + margin: 0px; + font-family: Tahoma, sans-serif; + font-size: small; + } + + div.mdoc + { + width: 600px; + } + + div.block-Sh + { + margin-bottom: 20px; + } + + div.head-Sh + { + font-weight: bold; + font-size: larger; + } + + div.head-Ss + { + font-weight: bold; + margin-top: 10px; + text-align: justify; + } + + div.body-Sh + { + margin-left: 20px; + margin-top: 10px; + text-align: justify; + } + + span.inline-Nd:before + { + content: ' - '; + } + + span.inline-Fl:before + { + content: '-'; + } + + span.inline-Fl + { + font-weight: bolder; + } + + span.inline-Ar + { + text-decoration: underline; + } + + span.inline-Pa + { + text-decoration: underline; + } + + span.inline-Op:before + { + content: '['; + } + + span.inline-Op:after + { + content: ']'; + } + + div.block-Bl + { + margin-top: 10px; + margin-left: 20px; + } + + div.inline-Pp + { + margin-bottom: 10px; + } + + span.inline-D1 + { + margin-left: 20px; + } + + span.inline-Qq:before { content: '``'; } + span.inline-Qq:after { content: '\'\''; } + span.inline-Sq:before { content: '`'; } + span.inline-Sq:after { content: '\''; } diff --git a/ml.h b/ml.h index 7c6e9fac..07bc6ceb 100644 --- a/ml.h +++ b/ml.h @@ -1,4 +1,4 @@ -/* $Id: ml.h,v 1.2 2008/12/03 19:21:58 kristaps Exp $ */ +/* $Id: ml.h,v 1.3 2008/12/04 11:25:29 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -29,15 +29,16 @@ enum md_ns { MD_NS_DEFAULT, }; -typedef int (*ml_begin)(struct md_mbuf *, - const struct md_args *); +typedef int (*ml_begin)(struct md_mbuf *, const struct md_args *, + const struct tm *, const char *, const char *, + const char *, const char *); typedef int (*ml_end)(struct md_mbuf *, - const struct md_args *); + const struct md_args *); typedef ssize_t (*ml_endtag)(struct md_mbuf *, - const struct md_args *, enum md_ns, int); + const struct md_args *, enum md_ns, int); typedef ssize_t (*ml_begintag)(struct md_mbuf *, - const struct md_args *, enum md_ns, int, - const int *, const char **); + const struct md_args *, enum md_ns, int, + const int *, const char **); __BEGIN_DECLS diff --git a/mlg.c b/mlg.c index 17ef1efd..7e89a8f9 100644 --- a/mlg.c +++ b/mlg.c @@ -1,4 +1,4 @@ -/* $Id: mlg.c,v 1.3 2008/12/03 21:27:56 kristaps Exp $ */ +/* $Id: mlg.c,v 1.4 2008/12/04 11:25:29 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -63,7 +63,9 @@ struct md_mlg { static void mlg_roffmsg(void *arg, enum roffmsg, const char *, const char *, char *); -static int mlg_roffhead(void *); +static int mlg_roffhead(void *, const struct tm *, + const char *, const char *, + const char *, const char *); static int mlg_rofftail(void *); static int mlg_roffin(void *, int, int *, char **); static int mlg_roffdata(void *, int, char *); @@ -346,7 +348,8 @@ mlg_alloc(const struct md_args *args, static int -mlg_roffhead(void *arg) +mlg_roffhead(void *arg, const struct tm *tm, const char *os, + const char *title, const char *sec, const char *vol) { struct md_mlg *p; @@ -354,7 +357,7 @@ mlg_roffhead(void *arg) p = (struct md_mlg *)arg; mlg_mode(p, MD_BLK_IN); - if ( ! (*p->begin)(p->mbuf, p->args)) + if ( ! (*p->begin)(p->mbuf, p->args, tm, os, title, sec, vol)) return(0); p->indent++; diff --git a/private.h b/private.h index 53710f0c..d1ccd50d 100644 --- a/private.h +++ b/private.h @@ -1,4 +1,4 @@ -/* $Id: private.h,v 1.25 2008/12/03 19:21:58 kristaps Exp $ */ +/* $Id: private.h,v 1.26 2008/12/04 11:25:29 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -213,7 +213,8 @@ enum roffmsg { ROFF_WARN, ROFF_ERROR }; struct roffcb { void (*roffmsg)(void *, enum roffmsg, const char *, const char *, char *); - int (*roffhead)(void *); + int (*roffhead)(void *, const struct tm *, const char *, + const char *, const char *, const char *); int (*rofftail)(void *); int (*roffdata)(void *, int, char *); int (*roffin)(void *, int, int *, char **); diff --git a/roff.c b/roff.c index fb6c37a4..80089387 100644 --- a/roff.c +++ b/roff.c @@ -1,4 +1,4 @@ -/* $Id: roff.c,v 1.35 2008/12/03 19:21:58 kristaps Exp $ */ +/* $Id: roff.c,v 1.36 2008/12/04 11:25:29 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -40,6 +40,7 @@ /* TODO: (warn) NAME section has particular order. */ /* TODO: unify empty-content tags a la
. */ /* TODO: macros with a set number of arguments? */ +/* TODO: validate Dt macro arguments. */ #define ROFF_MAXARG 32 @@ -965,7 +966,7 @@ roff_Dd(ROFFCALL_ARGS) argv++; - if (0 == strcmp(*argv, "$Mdocdate: December 3 2008 $")) { + if (0 == strcmp(*argv, "$Mdocdate: December 4 2008 $")) { t = time(NULL); if (NULL == localtime_r(&t, &tree->tm)) err(1, "localtime_r"); @@ -1210,7 +1211,9 @@ roff_Os(ROFFCALL_ARGS) assert(NULL == tree->last); - return((*tree->cb.roffhead)(tree->arg)); + return((*tree->cb.roffhead)(tree->arg, &tree->tm, + tree->os, tree->title, tree->section, + tree->volume)); } diff --git a/xml.c b/xml.c index 854c42b8..d60bcddd 100644 --- a/xml.c +++ b/xml.c @@ -1,4 +1,4 @@ -/* $Id: xml.c,v 1.12 2008/12/03 21:27:56 kristaps Exp $ */ +/* $Id: xml.c,v 1.13 2008/12/04 11:25:29 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -31,15 +31,21 @@ static ssize_t xml_begintag(struct md_mbuf *, const struct md_args *, enum md_ns, int, const int *, const char **); -static int xml_begin(struct md_mbuf *, - const struct md_args *); +static int xml_begin(struct md_mbuf *, + const struct md_args *, + const struct tm *, + const char *, const char *, + const char *, const char *); static int xml_end(struct md_mbuf *, const struct md_args *); /* ARGSUSED */ static int -xml_begin(struct md_mbuf *mbuf, const struct md_args *args) +xml_begin(struct md_mbuf *mbuf, const struct md_args *args, + const struct tm *tm, const char *os, + const char *title, const char *section, + const char *vol) { size_t res;