summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKristaps Dzonsons <kristaps@bsd.lv>2009-02-25 15:12:26 +0000
committerKristaps Dzonsons <kristaps@bsd.lv>2009-02-25 15:12:26 +0000
commitf3d5c8db4c02e31a44ed81c1faa87a057f79585b (patch)
tree6199f440c82d1ae02d4b7214353571b469c0e983
parent4d021113ebf58cd7c14d5296060c615c99450f9c (diff)
downloadmandoc-f3d5c8db4c02e31a44ed81c1faa87a057f79585b.tar.gz
Added [almost] all list types.
-rw-r--r--mdocterm.17
-rw-r--r--mdocterm.c68
-rw-r--r--term.c112
-rw-r--r--term.h2
4 files changed, 128 insertions, 61 deletions
diff --git a/mdocterm.1 b/mdocterm.1
index 7ca80b43..5ea9d207 100644
--- a/mdocterm.1
+++ b/mdocterm.1
@@ -119,3 +119,10 @@ See
.Xr mdoc 3
for a list of bugs, caveats, and incomplete macros regarding the
document parse.
+.Pp
+For front-end formatting, the
+.Sq -hang ,
+.Sq -inset
+and
+.Sq -column
+list types aren't yet supported.
diff --git a/mdocterm.c b/mdocterm.c
index f1026ba8..3a07b32e 100644
--- a/mdocterm.c
+++ b/mdocterm.c
@@ -44,6 +44,7 @@ enum termstyle {
};
static void body(struct termp *,
+ struct termpair *,
const struct mdoc_meta *,
const struct mdoc_node *);
static void header(struct termp *,
@@ -54,6 +55,8 @@ static void footer(struct termp *,
static void pword(struct termp *, const char *, size_t);
static void pescape(struct termp *,
const char *, size_t *, size_t);
+static void nescape(struct termp *,
+ const char *, size_t);
static void chara(struct termp *, char);
static void stringa(struct termp *, const char *);
static void style(struct termp *, enum termstyle);
@@ -89,7 +92,7 @@ main(int argc, char *argv[])
err(1, "malloc");
header(&termp, mdoc_meta(mdoc));
- body(&termp, mdoc_meta(mdoc), mdoc_node(mdoc));
+ body(&termp, NULL, mdoc_meta(mdoc), mdoc_node(mdoc));
footer(&termp, mdoc_meta(mdoc));
free(termp.buf);
@@ -218,8 +221,10 @@ newln(struct termp *p)
* vertical space.
*/
p->flags |= TERMP_NOSPACE;
- if (0 == p->col)
+ if (0 == p->col) {
+ p->flags &= ~TERMP_NOLPAD;
return;
+ }
flushln(p);
p->flags &= ~TERMP_NOLPAD;
}
@@ -287,8 +292,36 @@ style(struct termp *p, enum termstyle esc)
static void
+nescape(struct termp *p, const char *word, size_t len)
+{
+
+ switch (len) {
+ case (2):
+ if ('r' == word[0] && 'B' == word[1])
+ chara(p, ']');
+ else if ('l' == word[0] && 'B' == word[1])
+ chara(p, '[');
+ else if ('<' == word[0] && '-' == word[1])
+ stringa(p, "<-");
+ else if ('-' == word[0] && '>' == word[1])
+ stringa(p, "->");
+ else if ('l' == word[0] && 'q' == word[1])
+ chara(p, '\"');
+ else if ('r' == word[0] && 'q' == word[1])
+ chara(p, '\"');
+ else if ('b' == word[0] && 'u' == word[1])
+ chara(p, 'o');
+ break;
+ default:
+ break;
+ }
+}
+
+
+static void
pescape(struct termp *p, const char *word, size_t *i, size_t len)
{
+ size_t j;
(*i)++;
assert(*i < len);
@@ -297,20 +330,7 @@ pescape(struct termp *p, const char *word, size_t *i, size_t len)
/* Two-character escapes. */
(*i)++;
assert(*i + 1 < len);
-
- if ('r' == word[*i] && 'B' == word[*i + 1])
- chara(p, ']');
- else if ('l' == word[*i] && 'B' == word[*i + 1])
- chara(p, '[');
- else if ('<' == word[*i] && '-' == word[*i + 1])
- stringa(p, "<-");
- else if ('-' == word[*i] && '>' == word[*i + 1])
- stringa(p, "->");
- else if ('l' == word[*i] && 'q' == word[*i + 1])
- chara(p, '\"');
- else if ('r' == word[*i] && 'q' == word[*i + 1])
- chara(p, '\"');
-
+ nescape(p, &word[*i], 2);
(*i)++;
return;
@@ -332,7 +352,12 @@ pescape(struct termp *p, const char *word, size_t *i, size_t len)
}
return;
}
- /* n-character escapes. */
+
+ (*i)++;
+ for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++)
+ /* Loop... */ ;
+
+ nescape(p, &word[*i - j], j);
}
@@ -407,7 +432,8 @@ word(struct termp *p, const char *word)
static void
-body(struct termp *p, const struct mdoc_meta *meta,
+body(struct termp *p, struct termpair *ppair,
+ const struct mdoc_meta *meta,
const struct mdoc_node *node)
{
int dochild;
@@ -416,9 +442,11 @@ body(struct termp *p, const struct mdoc_meta *meta,
/* Pre-processing. */
dochild = 1;
+ pair.ppair = ppair;
pair.type = 0;
pair.offset = pair.rmargin = 0;
pair.flag = 0;
+ pair.count = 0;
if (MDOC_TEXT != node->type) {
if (termacts[node->tok].pre)
@@ -433,7 +461,7 @@ body(struct termp *p, const struct mdoc_meta *meta,
p->flags |= pair.flag;
if (dochild && node->child)
- body(p, meta, node->child);
+ body(p, &pair, meta, node->child);
if (TERMPAIR_FLAG & pair.type)
p->flags &= ~pair.flag;
@@ -447,7 +475,7 @@ body(struct termp *p, const struct mdoc_meta *meta,
/* Siblings. */
if (node->next)
- body(p, meta, node->next);
+ body(p, ppair, meta, node->next);
}
diff --git a/term.c b/term.c
index d5e9d744..567da478 100644
--- a/term.c
+++ b/term.c
@@ -30,7 +30,7 @@
*/
/* FIXME: indent/tab. */
-/* FIXME: handle nested lists. */
+/* FIXME: macro arguments can be escaped. */
#define TTYPE_PROG 0
#define TTYPE_CMD_FLAG 1
@@ -348,6 +348,7 @@ termp_it_pre(DECL_ARGS)
{
const struct mdoc_node *n, *it;
const struct mdoc_block *bl;
+ char buf[7];
int i;
size_t width, offset;
@@ -364,17 +365,9 @@ termp_it_pre(DECL_ARGS)
return(1);
}
- assert(MDOC_BLOCK == it->type);
- assert(MDOC_It == it->tok);
-
- n = it->parent;
- assert(MDOC_BODY == n->type);
- assert(MDOC_Bl == n->tok);
- n = n->parent;
+ n = it->parent->parent;
bl = &n->data.block;
- /* If `-compact', don't assert vertical space. */
-
if (MDOC_BLOCK == node->type) {
if (arg_hasattr(MDOC_Compact, bl->argc, bl->argv))
newln(p);
@@ -398,21 +391,63 @@ termp_it_pre(DECL_ARGS)
if (arg_hasattr(MDOC_Tag, bl->argc, bl->argv)) {
p->flags |= TERMP_NOSPACE;
-
- if (MDOC_HEAD == node->type) {
- p->flags |= TERMP_NOBREAK;
- p->offset += offset;
- p->rmargin = p->offset + width;
- } else {
+ if (MDOC_BODY == node->type) {
p->flags |= TERMP_NOLPAD;
p->offset += width;
+ } else {
+ p->flags |= TERMP_NOBREAK;
+ p->rmargin = p->offset + offset + width;
}
} else if (arg_hasattr(MDOC_Ohang, bl->argc, bl->argv)) {
p->flags |= TERMP_NOSPACE;
- p->offset += offset;
- }
+ } else if (arg_hasattr(MDOC_Diag, bl->argc, bl->argv)) {
+ /* TODO. */
+
+ } else if (arg_hasattr(MDOC_Hang, bl->argc, bl->argv)) {
+ /* TODO. */
+
+ } else if (arg_hasattr(MDOC_Bullet, bl->argc, bl->argv)) {
+ p->flags |= TERMP_NOSPACE;
+ if (MDOC_BODY == node->type) {
+ p->flags |= TERMP_NOLPAD;
+ p->offset += 6;
+ } else {
+ word(p, "\\[bu]");
+ p->flags |= TERMP_NOBREAK;
+ p->rmargin = p->offset + offset + 6;
+ }
+
+ } else if (arg_hasattr(MDOC_Enum, bl->argc, bl->argv)) {
+ p->flags |= TERMP_NOSPACE;
+ if (MDOC_BODY == node->type) {
+ p->flags |= TERMP_NOLPAD;
+ p->offset += 6;
+ } else {
+ (pair->ppair->ppair->count)++;
+ (void)snprintf(buf, sizeof(buf), "%d.",
+ pair->ppair->ppair->count);
+ word(p, buf);
+ p->flags |= TERMP_NOBREAK;
+ p->rmargin = p->offset + offset + 6;
+ }
+
+ } else if (arg_hasattr(MDOC_Dash, bl->argc, bl->argv) ||
+ arg_hasattr(MDOC_Hyphen, bl->argc, bl->argv)) {
+ p->flags |= TERMP_NOSPACE;
+ if (MDOC_BODY == node->type) {
+ p->flags |= TERMP_NOLPAD;
+ p->offset += 6;
+ return(1);
+ } else {
+ word(p, "\\-");
+ p->flags |= TERMP_NOBREAK;
+ p->rmargin = p->offset + offset + 6;
+ }
+ }
+
+ p->offset += offset;
return(1);
}
@@ -424,43 +459,38 @@ termp_it_post(DECL_ARGS)
const struct mdoc_node *n, *it;
const struct mdoc_block *bl;
- switch (node->type) {
- case (MDOC_BODY):
- /* FALLTHROUGH */
- case (MDOC_HEAD):
- break;
- default:
+ if (MDOC_BODY != node->type && MDOC_HEAD != node->type)
return;
- }
it = node->parent;
- assert(MDOC_BLOCK == it->type);
- assert(MDOC_It == it->tok);
-
- n = it->parent;
- assert(MDOC_BODY == n->type);
- assert(MDOC_Bl == n->tok);
- n = n->parent;
+ n = it->parent->parent;
bl = &n->data.block;
- /* If `-tag', adjust our margins accordingly. */
-
- if (arg_hasattr(MDOC_Tag, bl->argc, bl->argv)) {
+ if (arg_hasattr(MDOC_Tag, bl->argc, bl->argv) ||
+ arg_hasattr(MDOC_Bullet, bl->argc, bl->argv) ||
+ arg_hasattr(MDOC_Dash, bl->argc, bl->argv) ||
+ arg_hasattr(MDOC_Enum, bl->argc, bl->argv) ||
+ arg_hasattr(MDOC_Hyphen, bl->argc, bl->argv)) {
flushln(p);
-
if (MDOC_HEAD == node->type) {
p->rmargin = pair->rmargin;
- p->offset = pair->offset;
p->flags &= ~TERMP_NOBREAK;
- } else {
- p->offset = pair->offset;
+ } else
p->flags &= ~TERMP_NOLPAD;
- }
} else if (arg_hasattr(MDOC_Ohang, bl->argc, bl->argv)) {
flushln(p);
- p->offset = pair->offset;
+
+ } else if (arg_hasattr(MDOC_Inset, bl->argc, bl->argv)) {
+ if (MDOC_BODY == node->type)
+ flushln(p);
+
+ } else if (arg_hasattr(MDOC_Item, bl->argc, bl->argv)) {
+ if (MDOC_BODY == node->type)
+ flushln(p);
}
+
+ p->offset = pair->offset;
}
diff --git a/term.h b/term.h
index 334d60ea..594329f1 100644
--- a/term.h
+++ b/term.h
@@ -41,11 +41,13 @@ struct termp {
};
struct termpair {
+ struct termpair *ppair;
int type;
#define TERMPAIR_FLAG (1 << 0)
int flag;
size_t offset;
size_t rmargin;
+ int count;
};
#define TERMPAIR_SETFLAG(p, fl) \