Annotation of docbook2mdoc/reorg.c, Revision 1.2
1.2 ! schwarze 1: /* $Id: reorg.c,v 1.1 2019/04/28 17:11:53 schwarze Exp $ */
1.1 schwarze 2: /*
3: * Copyright (c) 2019 Ingo Schwarze <schwarze@openbsd.org>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17:
18: #include "node.h"
19: #include "reorg.h"
20:
21: /*
22: * The implementation of the tree reorganizer.
23: */
24:
1.2 ! schwarze 25: static void
! 26: reorg_root(struct pnode *root)
1.1 schwarze 27: {
1.2 ! schwarze 28: struct pnode *date, *descr, *name, *vol, *nc;
1.1 schwarze 29:
1.2 ! schwarze 30: if (root == NULL)
1.1 schwarze 31: return;
32:
33: /* Collect prologue information. */
34:
35: if ((date = pnode_takefirst(root, NODE_PUBDATE)) == NULL &&
36: (date = pnode_takefirst(root, NODE_DATE)) == NULL) {
37: date = pnode_alloc(NULL);
38: pnode_alloc_text(date, "$Mdocdate" "$");
39: }
40: date->node = NODE_DATE;
41: date->parent = root;
42:
43: name = vol = NULL;
44: if ((nc = pnode_findfirst(root, NODE_REFMETA)) != NULL) {
45: name = pnode_takefirst(nc, NODE_REFENTRYTITLE);
46: vol = pnode_takefirst(nc, NODE_MANVOLNUM);
47: }
48: if (name == NULL) {
49: name = pnode_alloc(NULL);
50: name->node = NODE_REFENTRYTITLE;
51: name->parent = root;
52: pnode_alloc_text(name,
53: pnode_getattr_raw(root, ATTRKEY_ID, "UNKNOWN"));
54: }
55: if (vol == NULL) {
56: vol = pnode_alloc(NULL);
57: vol->node = NODE_MANVOLNUM;
58: vol->parent = root;
59: pnode_alloc_text(vol, "1");
60: }
61:
62: /* Insert prologue information at the beginning. */
63:
64: if (pnode_findfirst(root, NODE_REFNAMEDIV) == NULL &&
65: ((nc = pnode_findfirst(root, NODE_BOOKINFO)) != NULL ||
66: (nc = pnode_findfirst(root, NODE_REFENTRYINFO)) != NULL) &&
67: (descr = pnode_takefirst(nc, NODE_TITLE)) != NULL)
68: TAILQ_INSERT_HEAD(&root->childq, descr, child);
69: TAILQ_INSERT_HEAD(&root->childq, vol, child);
70: TAILQ_INSERT_HEAD(&root->childq, name, child);
71: TAILQ_INSERT_HEAD(&root->childq, date, child);
1.2 ! schwarze 72: }
! 73:
! 74: static void
! 75: default_title(struct pnode *n, const char *title)
! 76: {
! 77: struct pnode *nc;
! 78:
! 79: if (n->parent == NULL)
! 80: return;
! 81:
! 82: TAILQ_FOREACH(nc, &n->childq, child)
! 83: if (nc->node == NODE_TITLE)
! 84: return;
! 85:
! 86: nc = pnode_alloc(NULL);
! 87: nc->node = NODE_TITLE;
! 88: nc->parent = n;
! 89: TAILQ_INSERT_HEAD(&n->childq, nc, child);
! 90: pnode_alloc_text(nc, title);
! 91: }
! 92:
! 93: static void
! 94: reorg_recurse(struct pnode *n)
! 95: {
! 96: struct pnode *nc;
! 97:
! 98: if (n == NULL)
! 99: return;
! 100:
! 101: switch (n->node) {
! 102: case NODE_APPENDIX:
! 103: default_title(n, "Appendix");
! 104: break;
! 105: case NODE_CAUTION:
! 106: default_title(n, "Caution");
! 107: n->node = NODE_NOTE;
! 108: break;
! 109: case NODE_LEGALNOTICE:
! 110: default_title(n, "Legal Notice");
! 111: n->node = NODE_SIMPLESECT;
! 112: break;
! 113: case NODE_NOTE:
! 114: default_title(n, "Note");
! 115: break;
! 116: case NODE_PREFACE:
! 117: default_title(n, "Preface");
! 118: n->node = NODE_SECTION;
! 119: break;
! 120: case NODE_SECTION:
! 121: case NODE_SIMPLESECT:
! 122: default_title(n, "Untitled");
! 123: break;
! 124: case NODE_TIP:
! 125: default_title(n, "Tip");
! 126: n->node = NODE_NOTE;
! 127: break;
! 128: case NODE_WARNING:
! 129: default_title(n, "Warning");
! 130: n->node = NODE_NOTE;
! 131: break;
! 132: default:
! 133: break;
! 134: }
! 135:
! 136: TAILQ_FOREACH(nc, &n->childq, child)
! 137: reorg_recurse(nc);
! 138: }
! 139:
! 140: void
! 141: ptree_reorg(struct ptree *tree)
! 142: {
! 143: reorg_recurse(tree->root);
! 144: reorg_root(tree->root);
1.1 schwarze 145: }
CVSweb