summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>1999-11-07 18:49:58 +0000
committerSimon Tatham <anakin@pobox.com>1999-11-07 18:49:58 +0000
commitb28dd9a135da2d62073840d05de430ec8dd8a58d (patch)
tree4aaecfbbdbefb5085a3b50e39d0b1511fb6e8240
parent6dff7b9ee671925f8c5dae1cd34bdc55f3236c38 (diff)
downloadhalibut-b28dd9a135da2d62073840d05de430ec8dd8a58d.zip
halibut-b28dd9a135da2d62073840d05de430ec8dd8a58d.tar.gz
halibut-b28dd9a135da2d62073840d05de430ec8dd8a58d.tar.bz2
halibut-b28dd9a135da2d62073840d05de430ec8dd8a58d.tar.xz
First stab at text backend configurability
[originally from svn r276]
-rw-r--r--bk_text.c190
-rw-r--r--buttress.h4
-rw-r--r--input.c4
-rw-r--r--ustring.c30
4 files changed, 168 insertions, 60 deletions
diff --git a/bk_text.c b/bk_text.c
index b7bdac7..aa47424 100644
--- a/bk_text.c
+++ b/bk_text.c
@@ -7,14 +7,19 @@
#include <assert.h>
#include "buttress.h"
-typedef enum { LEFT, CENTRE } alignment;
+typedef enum { LEFT, LEFTPLUS, CENTRE } alignment;
+typedef struct {
+ alignment align;
+ int just_numbers;
+ wchar_t underline;
+} alignstruct;
typedef struct {
int indent;
int listindentbefore, listindentafter;
int width;
- alignment titlealign, chapteralign;
- wchar_t titleunderline, chapterunderline;
+ alignstruct atitle, achapter, *asect;
+ int nasect;
int include_version_id;
int indent_preambles;
word bullet;
@@ -22,14 +27,21 @@ typedef struct {
static int text_convert(wchar_t *, char **);
-static void text_title(FILE *, word *, word *, alignment, wchar_t, int, int);
-static void text_heading(FILE *, word *, word *, int, int);
+static void text_heading(FILE *, word *, word *, word *, alignstruct, int,int);
static void text_rule(FILE *, int, int);
static void text_para(FILE *, word *, char *, word *, int, int, int);
static void text_codepara(FILE *, word *, int, int);
static void text_versionid(FILE *, word *);
-static textconfig text_configure(paragraph *sourceform) {
+static alignment utoalign(wchar_t *p) {
+ if (!ustricmp(p, L"centre") || !ustricmp(p, L"center"))
+ return CENTRE;
+ if (!ustricmp(p, L"leftplus"))
+ return LEFTPLUS;
+ return LEFT;
+}
+
+static textconfig text_configure(paragraph *source) {
textconfig ret;
/*
@@ -38,6 +50,7 @@ static textconfig text_configure(paragraph *sourceform) {
ret.bullet.next = NULL;
ret.bullet.alt = NULL;
ret.bullet.type = word_Normal;
+ ret.atitle.just_numbers = FALSE; /* ignored */
/*
* Defaults.
@@ -46,19 +59,94 @@ static textconfig text_configure(paragraph *sourceform) {
ret.listindentbefore = 1;
ret.listindentafter = 3;
ret.width = 68;
- ret.titlealign = CENTRE;
- ret.titleunderline = L'=';
- ret.chapteralign = LEFT;
- ret.chapterunderline = L'-';
+ ret.atitle.align = CENTRE;
+ ret.atitle.underline = L'=';
+ ret.achapter.align = LEFT;
+ ret.achapter.just_numbers = FALSE;
+ ret.achapter.underline = L'-';
+ ret.nasect = 1;
+ ret.asect = mknewa(alignstruct, ret.nasect);
+ ret.asect[0].align = LEFTPLUS;
+ ret.asect[0].just_numbers = TRUE;
+ ret.asect[0].underline = L'\0';
ret.include_version_id = TRUE;
ret.indent_preambles = FALSE;
ret.bullet.text = ustrdup(L"-");
- /*
- * FIXME: must walk the source form gleaning configuration from
- * \config paragraphs.
- */
- IGNORE(sourceform); /* for now */
+ for (; source; source = source->next) {
+ if (source->type == para_Config) {
+ if (!ustricmp(source->keyword, L"text-indent")) {
+ ret.indent = utoi(uadv(source->keyword));
+ } else if (!ustricmp(source->keyword, L"text-width")) {
+ ret.width = utoi(uadv(source->keyword));
+ } else if (!ustricmp(source->keyword, L"text-list-indent")) {
+ ret.listindentbefore = utoi(uadv(source->keyword));
+ } else if (!ustricmp(source->keyword, L"text-listitem-indent")) {
+ ret.listindentafter = utoi(uadv(source->keyword));
+ } else if (!ustricmp(source->keyword, L"text-chapter-align")) {
+ ret.achapter.align = utoalign(uadv(source->keyword));
+ } else if (!ustricmp(source->keyword, L"text-chapter-underline")) {
+ ret.achapter.underline = *uadv(source->keyword);
+ } else if (!ustricmp(source->keyword, L"text-chapter-numeric")) {
+ ret.achapter.underline = utob(uadv(source->keyword));
+ } else if (!ustricmp(source->keyword, L"text-section-align")) {
+ wchar_t *p = uadv(source->keyword);
+ int n = 0;
+ if (uisdigit(*p)) {
+ n = utoi(p);
+ p = uadv(p);
+ }
+ if (n >= ret.nasect) {
+ int i;
+ ret.asect = resize(ret.asect, n+1);
+ for (i = ret.nasect; i <= n; i++)
+ ret.asect[i] = ret.asect[ret.nasect-1];
+ ret.nasect = n+1;
+ }
+ ret.asect[n].align = utoalign(p);
+ } else if (!ustricmp(source->keyword, L"text-section-underline")) {
+ wchar_t *p = uadv(source->keyword);
+ int n = 0;
+ if (uisdigit(*p)) {
+ n = utoi(p);
+ p = uadv(p);
+ }
+ if (n >= ret.nasect) {
+ int i;
+ ret.asect = resize(ret.asect, n+1);
+ for (i = ret.nasect; i <= n; i++)
+ ret.asect[i] = ret.asect[ret.nasect-1];
+ ret.nasect = n+1;
+ }
+ ret.asect[n].underline = *p;
+ } else if (!ustricmp(source->keyword, L"text-section-numeric")) {
+ wchar_t *p = uadv(source->keyword);
+ int n = 0;
+ if (uisdigit(*p)) {
+ n = utoi(p);
+ p = uadv(p);
+ }
+ if (n >= ret.nasect) {
+ int i;
+ ret.asect = resize(ret.asect, n+1);
+ for (i = ret.nasect; i <= n; i++)
+ ret.asect[i] = ret.asect[ret.nasect-1];
+ ret.nasect = n+1;
+ }
+ ret.asect[n].just_numbers = utob(p);
+ } else if (!ustricmp(source->keyword, L"text-title-align")) {
+ ret.atitle.align = utoalign(uadv(source->keyword));
+ } else if (!ustricmp(source->keyword, L"text-title-underline")) {
+ ret.atitle.underline = *uadv(source->keyword);
+ } else if (!ustricmp(source->keyword, L"text-versionid")) {
+ ret.include_version_id = utob(uadv(source->keyword));
+ } else if (!ustricmp(source->keyword, L"text-indent-preamble")) {
+ ret.indent_preambles = utob(uadv(source->keyword));
+ } else if (!ustricmp(source->keyword, L"text-bullet")) {
+ ret.bullet.text = uadv(source->keyword);
+ }
+ }
+ }
return ret;
}
@@ -92,9 +180,8 @@ void text_backend(paragraph *sourceform, keywordlist *keywords, index *idx) {
/* Do the title */
for (p = sourceform; p; p = p->next)
if (p->type == para_Title)
- text_title(fp, NULL, p->words,
- conf.titlealign, conf.titleunderline,
- conf.indent, conf.width);
+ text_heading(fp, NULL, NULL, p->words,
+ conf.atitle, conf.indent, conf.width);
/* Do the preamble and copyright */
for (p = sourceform; p; p = p->next)
@@ -131,14 +218,15 @@ void text_backend(paragraph *sourceform, keywordlist *keywords, index *idx) {
case para_Chapter:
case para_Appendix:
case para_UnnumberedChapter:
- text_title(fp, p->kwtext, p->words,
- conf.chapteralign, conf.chapterunderline,
- conf.indent, conf.width);
+ text_heading(fp, p->kwtext, p->kwtext2, p->words,
+ conf.achapter, conf.indent, conf.width);
break;
case para_Heading:
case para_Subsect:
- text_heading(fp, p->kwtext2, p->words, conf.indent, conf.width);
+ text_heading(fp, p->kwtext, p->kwtext2, p->words,
+ conf.asect[p->aux>=conf.nasect ? conf.nasect-1 : p->aux],
+ conf.indent, conf.width);
break;
case para_Rule:
@@ -146,7 +234,7 @@ void text_backend(paragraph *sourceform, keywordlist *keywords, index *idx) {
break;
case para_Normal:
- case para_BiblioCited: /* FIXME: put the citation on front */
+ case para_BiblioCited:
case para_Bullet:
case para_NumberedList:
if (p->type == para_Bullet) {
@@ -156,7 +244,7 @@ void text_backend(paragraph *sourceform, keywordlist *keywords, index *idx) {
indenta = conf.listindentafter;
} else if (p->type == para_NumberedList) {
prefix = p->kwtext;
- prefixextra = ".";
+ prefixextra = "."; /* FIXME: configurability */
indentb = conf.listindentbefore;
indenta = conf.listindentafter;
} else {
@@ -363,30 +451,38 @@ static int text_width(word *text) {
return 0; /* should never happen */
}
-static void text_title(FILE *fp, word *prefix, word *text,
- alignment align, wchar_t underline,
- int indent, int width) {
+static void text_heading(FILE *fp, word *tprefix, word *nprefix, word *text,
+ alignstruct align, int indent, int width) {
rdstringc t = { 0, 0, NULL };
int margin, length;
- if (prefix) {
- text_rdaddwc(&t, prefix, NULL);
- rdaddsc(&t, ": ");
+ if (align.just_numbers && nprefix) {
+ text_rdaddwc(&t, nprefix, NULL);
+ rdaddc(&t, ' '); /* FIXME: as below */
+ margin = strlen(t.text);
+ } else if (!align.just_numbers && tprefix) {
+ text_rdaddwc(&t, tprefix, NULL);
+ rdaddsc(&t, ": "); /* FIXME: configurability */
+ margin = strlen(t.text);
}
text_rdaddwc(&t, text, NULL);
length = strlen(t.text);
- if (align == CENTRE) {
+
+ if (align.align == LEFTPLUS)
+ margin = indent - margin;
+ else if (align.align == LEFT)
+ margin = 0;
+ else if (align.align == CENTRE) {
margin = (indent + width - length)/2;
if (margin < 0) margin = 0;
- } else
- margin = 0;
+ }
fprintf(fp, "%*s%s\n", margin, "", t.text);
- if (underline != L'\0') {
+ if (align.underline != L'\0') {
char *u, uc;
wchar_t uw[2];
- uw[0] = underline; uw[1] = L'\0';
+ uw[0] = align.underline; uw[1] = L'\0';
text_convert(uw, &u);
uc = u[0];
sfree(u);
@@ -400,30 +496,6 @@ static void text_title(FILE *fp, word *prefix, word *text,
sfree(t.text);
}
-static void text_heading(FILE *fp, word *prefix, word *text,
- int indent, int width) {
- rdstringc t = { 0, 0, NULL };
- int margin;
-
- if (prefix) {
- text_rdaddwc(&t, prefix, NULL);
- rdaddc(&t, ' ');
- margin = strlen(t.text);
- }
- text_rdaddwc(&t, text, NULL);
-
- margin = indent - margin;
- if (margin < 0) margin = 0;
-
- fprintf(fp, "%*s%s\n\n", margin, "", t.text);
-
- if (strlen(t.text) > (size_t)width) {
- /* FIXME: warn */
- }
-
- sfree(t.text);
-}
-
static void text_rule(FILE *fp, int indent, int width) {
while (indent--) putc(' ', fp);
while (width--) putc('-', fp); /* FIXME: configurability! */
diff --git a/buttress.h b/buttress.h
index 7c357fb..6d4d593 100644
--- a/buttress.h
+++ b/buttress.h
@@ -231,6 +231,9 @@ wchar_t *ustrcpy(wchar_t *dest, wchar_t *source);
wchar_t utolower(wchar_t);
int ustrcmp(wchar_t *lhs, wchar_t *rhs);
int ustricmp(wchar_t *lhs, wchar_t *rhs);
+int utoi(wchar_t *);
+int utob(wchar_t *);
+int uisdigit(wchar_t);
wchar_t *ustrlow(wchar_t *s);
wchar_t *ustrftime(wchar_t *fmt, struct tm *timespec);
@@ -333,6 +336,7 @@ void index_debug(index *);
* contents.c
*/
numberstate *number_init(void);
+void number_cfg(numberstate *, paragraph *);
word *number_mktext(numberstate *, int, int, int, word **);
void number_free(numberstate *);
diff --git a/input.c b/input.c
index 0b25580..6b71d4e 100644
--- a/input.c
+++ b/input.c
@@ -617,7 +617,9 @@ static void read_file(paragraph ***ret, input *in, index *idx) {
case c_B: needkw = 2; par.type = para_Biblio; break;
case c_BR: needkw = 1; par.type = para_BR; break;
case c_C: needkw = 2; par.type = para_Chapter; break;
- case c_H: needkw = 2; par.type = para_Heading; break;
+ case c_H: needkw = 2; par.type = para_Heading;
+ par.aux = 0;
+ break;
case c_IM: needkw = 2; par.type = para_IM; break;
case c_S: needkw = 2; par.type = para_Subsect;
par.aux = t.aux; break;
diff --git a/ustring.c b/ustring.c
index f00a476..a6690c1 100644
--- a/ustring.c
+++ b/ustring.c
@@ -94,6 +94,36 @@ wchar_t *ustrlow(wchar_t *s) {
return s;
}
+int utoi(wchar_t *s) {
+ int sign = +1;
+ int n;
+
+ if (*s == L'-') {
+ s++;
+ sign = -1;
+ }
+
+ n = 0;
+ while (*s && *s >= L'0' && *s <= L'9') {
+ n *= 10;
+ n += (*s - '0');
+ s++;
+ }
+
+ return n;
+}
+
+int utob(wchar_t *s) {
+ if (!ustricmp(s, L"yes") || !ustricmp(s, L"y") ||
+ !ustricmp(s, L"true") || !ustricmp(s, L"t"))
+ return TRUE;
+ return FALSE;
+}
+
+int uisdigit(wchar_t c) {
+ return c >= L'0' && c <= L'9';
+}
+
#define USTRFTIME_DELTA 128
wchar_t *ustrftime(wchar_t *wfmt, struct tm *timespec) {
void *blk = NULL;