[BACK]Return to reorg.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / docbook2mdoc

File: [cvsweb.bsd.lv] / docbook2mdoc / reorg.c (download)

Revision 1.2, Sun Apr 28 19:05:11 2019 UTC (4 years, 11 months ago) by schwarze
Branch: MAIN
Changes since 1.1: +78 -5 lines

move default section titles to the reorg module

/* $Id: reorg.c,v 1.2 2019/04/28 19:05:11 schwarze Exp $ */
/*
 * Copyright (c) 2019 Ingo Schwarze <schwarze@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "node.h"
#include "reorg.h"

/*
 * The implementation of the tree reorganizer.
 */

static void
reorg_root(struct pnode *root)
{
	struct pnode	*date, *descr, *name, *vol, *nc;

	if (root == NULL)
		return;

	/* Collect prologue information. */

	if ((date = pnode_takefirst(root, NODE_PUBDATE)) == NULL &&
	    (date = pnode_takefirst(root, NODE_DATE)) == NULL) {
		date = pnode_alloc(NULL);
		pnode_alloc_text(date, "$Mdocdate" "$");
	}
	date->node = NODE_DATE;
	date->parent = root;

	name = vol = NULL;
	if ((nc = pnode_findfirst(root, NODE_REFMETA)) != NULL) {
		name = pnode_takefirst(nc, NODE_REFENTRYTITLE);
		vol = pnode_takefirst(nc, NODE_MANVOLNUM);
	}
	if (name == NULL) {
		name = pnode_alloc(NULL);
		name->node = NODE_REFENTRYTITLE;
		name->parent = root;
		pnode_alloc_text(name,
		    pnode_getattr_raw(root, ATTRKEY_ID, "UNKNOWN"));
	}
	if (vol == NULL) {
		vol = pnode_alloc(NULL);
		vol->node = NODE_MANVOLNUM;
		vol->parent = root;
		pnode_alloc_text(vol, "1");
	}

	/* Insert prologue information at the beginning. */

	if (pnode_findfirst(root, NODE_REFNAMEDIV) == NULL &&
	    ((nc = pnode_findfirst(root, NODE_BOOKINFO)) != NULL ||
	     (nc = pnode_findfirst(root, NODE_REFENTRYINFO)) != NULL) &&
	    (descr = pnode_takefirst(nc, NODE_TITLE)) != NULL)
		TAILQ_INSERT_HEAD(&root->childq, descr, child);
	TAILQ_INSERT_HEAD(&root->childq, vol, child);
	TAILQ_INSERT_HEAD(&root->childq, name, child);
	TAILQ_INSERT_HEAD(&root->childq, date, child);
}

static void
default_title(struct pnode *n, const char *title)
{
	struct pnode	*nc;

	if (n->parent == NULL)
		return;

	TAILQ_FOREACH(nc, &n->childq, child)
		if (nc->node == NODE_TITLE)
			return;

	nc = pnode_alloc(NULL);
	nc->node = NODE_TITLE;
	nc->parent = n;
	TAILQ_INSERT_HEAD(&n->childq, nc, child);
	pnode_alloc_text(nc, title);
}

static void
reorg_recurse(struct pnode *n)
{
	struct pnode	*nc;

	if (n == NULL)
		return;

	switch (n->node) {
	case NODE_APPENDIX:
		default_title(n, "Appendix");
		break;
	case NODE_CAUTION:
		default_title(n, "Caution");
		n->node = NODE_NOTE;
		break;
	case NODE_LEGALNOTICE:
		default_title(n, "Legal Notice");
		n->node = NODE_SIMPLESECT;
		break;
	case NODE_NOTE:
		default_title(n, "Note");
		break;
	case NODE_PREFACE:
		default_title(n, "Preface");
		n->node = NODE_SECTION;
		break;
	case NODE_SECTION:
	case NODE_SIMPLESECT:
		default_title(n, "Untitled");
		break;
	case NODE_TIP:
		default_title(n, "Tip");
		n->node = NODE_NOTE;
		break;
	case NODE_WARNING:
		default_title(n, "Warning");
		n->node = NODE_NOTE;
		break;
	default:
		break;
	}

	TAILQ_FOREACH(nc, &n->childq, child)
		reorg_recurse(nc);
}

void
ptree_reorg(struct ptree *tree)
{
	reorg_recurse(tree->root);
	reorg_root(tree->root);
}