+html_man(void *arg, const struct roff_man *man)
+{
+ struct html *h;
+ struct tag *t;
+
+ h = (struct html *)arg;
+
+ if ((h->oflags & HTML_FRAGMENT) == 0) {
+ print_gen_decls(h);
+ print_otag(h, TAG_HTML, "");
+ t = print_otag(h, TAG_HEAD, "");
+ print_man_head(&man->meta, man->first, h);
+ print_tagq(h, t);
+ print_otag(h, TAG_BODY, "");
+ }
+
+ man_root_pre(&man->meta, man->first, h);
+ t = print_otag(h, TAG_DIV, "c", "manual-text");
+ print_man_nodelist(&man->meta, man->first->child, h);
+ print_tagq(h, t);
+ man_root_post(&man->meta, man->first, h);
+ print_tagq(h, NULL);
+}
+
+static void
+print_man_head(MAN_ARGS)
+{
+ char *cp;
+
+ print_gen_head(h);
+ mandoc_asprintf(&cp, "%s(%s)", man->title, man->msec);
+ print_otag(h, TAG_TITLE, "");
+ print_text(h, cp);
+ free(cp);
+}
+
+static void
+print_man_nodelist(MAN_ARGS)
+{
+
+ while (n != NULL) {
+ print_man_node(man, n, h);
+ n = n->next;
+ }
+}
+
+static void
+print_man_node(MAN_ARGS)
+{
+ static int want_fillmode = MAN_fi;
+ static int save_fillmode;
+
+ struct tag *t;
+ int child;
+
+ /*
+ * Handle fill mode switch requests up front,
+ * they would just cause trouble in the subsequent code.
+ */
+
+ switch (n->tok) {
+ case MAN_nf:
+ case MAN_EX:
+ want_fillmode = MAN_nf;
+ return;
+ case MAN_fi:
+ case MAN_EE:
+ want_fillmode = MAN_fi;
+ if (fillmode(h, 0) == MAN_fi)
+ print_otag(h, TAG_BR, "");
+ return;
+ default:
+ break;
+ }
+
+ /* Set up fill mode for the upcoming node. */
+
+ switch (n->type) {
+ case ROFFT_BLOCK:
+ save_fillmode = 0;
+ /* Some block macros suspend or cancel .nf. */
+ switch (n->tok) {
+ case MAN_TP: /* Tagged paragraphs */
+ case MAN_IP: /* temporarily disable .nf */
+ case MAN_HP: /* for the head. */
+ save_fillmode = want_fillmode;
+ /* FALLTHROUGH */
+ case MAN_SH: /* Section headers */
+ case MAN_SS: /* permanently cancel .nf. */
+ want_fillmode = MAN_fi;
+ /* FALLTHROUGH */
+ case MAN_PP: /* These have no head. */
+ case MAN_LP: /* They will simply */
+ case MAN_P: /* reopen .nf in the body. */
+ case MAN_RS:
+ case MAN_UR:
+ fillmode(h, MAN_fi);
+ break;
+ default:
+ break;
+ }
+ break;
+ case ROFFT_TBL:
+ fillmode(h, MAN_fi);
+ break;
+ case ROFFT_ELEM:
+ /*
+ * Some in-line macros produce tags and/or text
+ * in the handler, so they require fill mode to be
+ * configured up front just like for text nodes.
+ * For the others, keep the traditional approach
+ * of doing the same, for now.
+ */
+ fillmode(h, want_fillmode);
+ break;
+ case ROFFT_TEXT:
+ if (fillmode(h, want_fillmode) == MAN_fi &&
+ want_fillmode == MAN_fi &&
+ n->flags & NODE_LINE && *n->string == ' ' &&
+ (h->flags & HTML_NONEWLINE) == 0)
+ print_otag(h, TAG_BR, "");
+ if (*n->string != '\0')
+ break;
+ print_paragraph(h);
+ return;
+ default:
+ break;
+ }
+
+ /* Produce output for this node. */
+
+ child = 1;
+ switch (n->type) {
+ case ROFFT_TEXT:
+ t = h->tag;
+ print_text(h, n->string);
+ break;
+ case ROFFT_EQN:
+ t = h->tag;
+ print_eqn(h, n->eqn);
+ break;
+ case ROFFT_TBL:
+ /*
+ * This will take care of initialising all of the table
+ * state data for the first table, then tearing it down
+ * for the last one.
+ */
+ print_tbl(h, n->span);
+ return;
+ default:
+ /*
+ * Close out scope of font prior to opening a macro
+ * scope.
+ */
+ if (HTMLFONT_NONE != h->metac) {
+ h->metal = h->metac;
+ h->metac = HTMLFONT_NONE;
+ }
+
+ /*
+ * Close out the current table, if it's open, and unset
+ * the "meta" table state. This will be reopened on the
+ * next table element.
+ */
+ if (h->tblt)
+ print_tblclose(h);
+
+ t = h->tag;
+ if (n->tok < ROFF_MAX) {
+ roff_html_pre(h, n);
+ child = 0;
+ break;
+ }
+
+ assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
+ if (mans[n->tok].pre)
+ child = (*mans[n->tok].pre)(man, n, h);
+
+ /* Some block macros resume .nf in the body. */
+ if (save_fillmode && n->type == ROFFT_BODY)
+ want_fillmode = save_fillmode;
+
+ break;
+ }
+
+ if (child && n->child)
+ print_man_nodelist(man, n->child, h);
+
+ /* This will automatically close out any font scope. */
+ print_stagq(h, t);
+
+ if (fillmode(h, 0) == MAN_nf &&
+ n->next != NULL && n->next->flags & NODE_LINE)
+ print_endline(h);
+}
+
+/*
+ * MAN_nf switches to no-fill mode, MAN_fi to fill mode.
+ * Other arguments do not switch.
+ * The old mode is returned.
+ */
+static int
+fillmode(struct html *h, int want)
+{
+ struct tag *pre;
+ int had;
+
+ for (pre = h->tag; pre != NULL; pre = pre->next)
+ if (pre->tag == TAG_PRE)
+ break;
+
+ had = pre == NULL ? MAN_fi : MAN_nf;
+
+ if (want && want != had) {
+ if (want == MAN_nf)
+ print_otag(h, TAG_PRE, "");
+ else
+ print_tagq(h, pre);
+ }
+ return had;
+}
+
+static int
+a2width(const struct roff_node *n, struct roffsu *su)
+{
+ if (n->type != ROFFT_TEXT)
+ return 0;
+ return a2roffsu(n->string, su, SCALE_EN) != NULL;
+}
+
+static void
+man_root_pre(MAN_ARGS)
+{
+ struct tag *t, *tt;
+ char *title;
+
+ assert(man->title);
+ assert(man->msec);
+ mandoc_asprintf(&title, "%s(%s)", man->title, man->msec);
+
+ t = print_otag(h, TAG_TABLE, "c", "head");
+ tt = print_otag(h, TAG_TR, "");
+
+ print_otag(h, TAG_TD, "c", "head-ltitle");
+ print_text(h, title);
+ print_stagq(h, tt);
+
+ print_otag(h, TAG_TD, "c", "head-vol");
+ if (NULL != man->vol)
+ print_text(h, man->vol);
+ print_stagq(h, tt);
+
+ print_otag(h, TAG_TD, "c", "head-rtitle");
+ print_text(h, title);
+ print_tagq(h, t);
+ free(title);
+}
+
+static void
+man_root_post(MAN_ARGS)
+{
+ struct tag *t, *tt;
+
+ t = print_otag(h, TAG_TABLE, "c", "foot");
+ tt = print_otag(h, TAG_TR, "");
+
+ print_otag(h, TAG_TD, "c", "foot-date");
+ print_text(h, man->date);
+ print_stagq(h, tt);
+
+ print_otag(h, TAG_TD, "c", "foot-os");
+ if (man->os)
+ print_text(h, man->os);
+ print_tagq(h, t);
+}
+
+static int
+man_SH_pre(MAN_ARGS)