=================================================================== RCS file: /cvs/mandoc/man_html.c,v retrieving revision 1.4 retrieving revision 1.6 diff -u -p -r1.4 -r1.6 --- mandoc/man_html.c 2009/10/04 09:35:26 1.4 +++ mandoc/man_html.c 2009/10/04 15:24:54 1.6 @@ -1,4 +1,4 @@ -/* $Id: man_html.c,v 1.4 2009/10/04 09:35:26 kristaps Exp $ */ +/* $Id: man_html.c,v 1.6 2009/10/04 15:24:54 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -17,6 +17,8 @@ #include #include +#include +#include #include #include #include @@ -25,7 +27,9 @@ #include "html.h" #include "man.h" -#define INDENT 7 +/* TODO: preserve ident widths. */ + +#define INDENT 5 #define HALFINDENT 3 #define MAN_ARGS const struct man_meta *m, \ @@ -42,7 +46,11 @@ static void print_man_head(MAN_ARGS); static void print_man_nodelist(MAN_ARGS); static void print_man_node(MAN_ARGS); +static int a2width(const struct man_node *); + static int man_br_pre(MAN_ARGS); +static int man_HP_pre(MAN_ARGS); +static int man_IP_pre(MAN_ARGS); static int man_PP_pre(MAN_ARGS); static void man_root_post(MAN_ARGS); static int man_root_pre(MAN_ARGS); @@ -59,12 +67,12 @@ static const struct htmlman mans[MAN_MAX] = { { NULL, NULL }, /* TH */ { man_SH_pre, NULL }, /* SH */ { man_SS_pre, NULL }, /* SS */ - { NULL, NULL }, /* TP */ + { man_IP_pre, NULL }, /* TP */ { man_PP_pre, NULL }, /* LP */ { man_PP_pre, NULL }, /* PP */ { man_PP_pre, NULL }, /* P */ - { NULL, NULL }, /* IP */ - { NULL, NULL }, /* HP */ + { man_IP_pre, NULL }, /* IP */ + { man_HP_pre, NULL }, /* HP */ { NULL, NULL }, /* SM */ { NULL, NULL }, /* SB */ { NULL, NULL }, /* BI */ @@ -198,6 +206,34 @@ print_man_node(MAN_ARGS) } +static int +a2width(const struct man_node *n) +{ + int i, len; + const char *p; + + if (MAN_TEXT != n->type) + return(-1); + + p = n->string; + + if (0 == (len = (int)strlen(p))) + return(-1); + + for (i = 0; i < len; i++) + if ( ! isdigit((u_char)p[i])) + break; + + if (i == len - 1) { + if ('n' == p[len - 1] || 'm' == p[len - 1]) + return(atoi(p)); + } else if (i == len) + return(atoi(p)); + + return(-1); +} + + /* ARGSUSED */ static int man_root_pre(MAN_ARGS) @@ -403,17 +439,125 @@ man_SS_pre(MAN_ARGS) static int man_PP_pre(MAN_ARGS) { - struct htmlpair tag; + struct htmlpair tag; + int i; if (MAN_BLOCK != n->type) return(1); - buffmt(h, "margin-left: %dem;", INDENT); - if (n->next && n->next->child) + i = 0; + + if (MAN_ROOT == n->parent->tok) { + buffmt(h, "margin-left: %dem;", INDENT); + i = 1; + } + if (n->next && n->next->child) { + i = 1; bufcat(h, "margin-bottom: 1em;"); + } tag.key = ATTR_STYLE; tag.val = h->buf; + print_otag(h, TAG_DIV, i, &tag); + return(1); +} + + +/* ARGSUSED */ +static int +man_IP_pre(MAN_ARGS) +{ + struct htmlpair tag; + int len, ival; + const struct man_node *nn; + + if (MAN_BODY == n->type) { + print_otag(h, TAG_DIV, 0, NULL); + return(1); + } + + nn = MAN_BLOCK == n->type ? + n->head->child : n->parent->head->child; + len = INDENT; + ival = -1; + + /* Calculate the indentation length. */ + + if (NULL != nn) + if (NULL != (nn = nn->next)) { + for ( ; nn->next; nn = nn->next) + /* Do nothing. */ ; + if ((ival = a2width(nn)) >= 0) + len = ival; + } + + if (MAN_BLOCK == n->type) { + buffmt(h, "clear: both; margin-left: %dem;", len); + tag.key = ATTR_STYLE; + tag.val = h->buf; + print_otag(h, TAG_DIV, 1, &tag); + return(1); + } + + /* If there's an indent string, print it out. */ + + buffmt(h, "margin-left: -%dem; min-width: %dem;", + len, len - 1); + bufcat(h, "clear: left; padding-right: 1em;"); + + if (n->next && n->next->child) + bufcat(h, "float: left;"); + + tag.key = ATTR_STYLE; + tag.val = h->buf; print_otag(h, TAG_DIV, 1, &tag); + + if (ival < 0) + return(1); + + /* With a length string, omit the last child. */ + + for (nn = n->child; nn->next; nn = nn->next) + print_man_node(m, nn, h); + + return(0); +} + + +/* ARGSUSED */ +static int +man_HP_pre(MAN_ARGS) +{ + int ival, len; + const struct man_node *nn; + struct htmlpair tag; + + if (MAN_HEAD == n->type) + return(0); + + nn = MAN_BLOCK == n->type ? + n->head->child : n->parent->head->child; + + len = INDENT; + + if (NULL != nn) + if ((ival = a2width(nn)) >= 0) + len = ival; + + if (MAN_BLOCK == n->type) { + buffmt(h, "clear: both; margin-left: %dem;", len); + tag.key = ATTR_STYLE; + tag.val = h->buf; + print_otag(h, TAG_DIV, 1, &tag); + return(1); + } + + buffmt(h, "text-indent: -%dem;", len); + + tag.key = ATTR_STYLE; + tag.val = h->buf; + print_otag(h, TAG_DIV, 1, &tag); + return(1); } +