-CFLAGS += -W -Wall -Wno-unused-parameter -g
+CFLAGS += -W -Wall -Wno-unused-parameter -g -DDEBUG
LINTFLAGS += -c -e -f -u
-/* $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 <kristaps@kth.se>
*
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
+#include <sys/param.h>
+#include <sys/stat.h>
+
#include <assert.h>
+#include <err.h>
+#include <fcntl.h>
#include <stdlib.h>
+#include <stdio.h>
#include <string.h>
+#include <unistd.h>
#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);
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 *,
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 =
+ "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n"
+ " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
+ "<html>\n"
+ "<head>\n"
+ " <meta http-equiv=\"Content-Type\"\n"
+ " content=\"text/html;charset=utf-8\">\n"
+ " <meta name=\"resource-type\" content=\"document\">\n"
+ " <title>Manual Page for %s(%s)</title>\n";
+
+ css =
+ " <link rel=\"stylesheet\" type=\"text/css\"\n"
+ " href=\"%s\">\n";
+ trail =
+ "</head>\n"
+ "<body>\n"
+ "<div class=\"mdoc\">\n";
+
res = 0;
- if ( ! ml_puts(mbuf, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
- "HTML 4.01//EN\" \"http://www.w3.org"
- "/TR/html4/strict.dtd\">\n", &res))
- return(0);
- if ( ! ml_puts(mbuf, "<html>\n", &res))
- return(0);
- if ( ! ml_puts(mbuf, "<head>\n", &res))
- return(0);
- if ( ! ml_puts(mbuf, " <title>Manual page</title>\n", &res))
- return(0);
- if ( ! ml_puts(mbuf, " <meta http-equiv=\"Content-Type\" "
- "content=\"text/html; "
- "charset=utf-8\">\n", &res))
- return(0);
- if ( ! ml_puts(mbuf, " <meta name=\"resource-type\" "
- "content=\"document\">\n", &res))
- return(0);
- if ( ! ml_puts(mbuf, "</head>\n", &res))
+
+ (void)snprintf(buf, sizeof(buf) - 1,
+ preamble, title, section);
+
+ if ( ! ml_puts(mbuf, buf, &res))
return(0);
- if ( ! ml_puts(mbuf, "<body>", &res))
+
+ assert(args->params.html.css);
+ if (HTML_CSS_EMBED & args->params.html.flags) {
+ if ( ! ml_puts(mbuf, " <style><!--\n", &res))
+ return(0);
+ if ( ! html_loadcss(mbuf, args->params.html.css))
+ return(0);
+ if ( ! ml_puts(mbuf, " --!></style>\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);
size_t res;
res = 0;
- if ( ! ml_puts(mbuf, "</body>\n</html>", &res))
+ if ( ! ml_puts(mbuf, "</div></body>\n</html>", &res))
return(0);
return(1);
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);
}
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);
}
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);
}
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);
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);
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);
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);
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);
-/* $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 <kristaps@kth.se>
*
};
struct md_params_html {
- int dummy;
+ char *css;
+ int flags;
+#define HTML_CSS_EMBED (1 << 0)
};
union md_params {
.\"
-.Dd $Mdocdate: December 3 2008 $
-.Dt mdocml 1
+.Dd $Mdocdate: December 4 2008 $
+.Dt mdocml 1 alpha
.Os
.\"
.Sh NAME
.\"
.Sh SYNOPSIS
.Nm mdocml
-.Op Fl W
+.Op Fl vW
.Op Fl f Ar filter
.Op Fl o Ar outfile
.Op Ar infile
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.
.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 ,
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 ,
-/* $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 <kristaps@kth.se>
*
#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 *,
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;
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 : "-"));
}
--- /dev/null
+ 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: '\''; }
-/* $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 <kristaps@kth.se>
*
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
-/* $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 <kristaps@kth.se>
*
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 *);
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;
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++;
-/* $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 <kristaps@kth.se>
*
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 **);
-/* $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 <kristaps@kth.se>
*
/* TODO: (warn) NAME section has particular order. */
/* TODO: unify empty-content tags a la <br />. */
/* TODO: macros with a set number of arguments? */
+/* TODO: validate Dt macro arguments. */
#define ROFF_MAXARG 32
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");
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));
}
-/* $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 <kristaps@kth.se>
*
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;