2 * text backend for Halibut
10 typedef enum { LEFT
, LEFTPLUS
, CENTRE
} alignment
;
15 wchar_t *number_suffix
;
19 int indent
, indent_code
;
20 int listindentbefore
, listindentafter
;
22 alignstruct atitle
, achapter
, *asect
;
24 int include_version_id
;
29 static int text_convert(wchar_t *, char **);
31 static void text_heading(FILE *, word
*, word
*, word
*, alignstruct
, int,int);
32 static void text_rule(FILE *, int, int);
33 static void text_para(FILE *, word
*, char *, word
*, int, int, int);
34 static void text_codepara(FILE *, word
*, int, int);
35 static void text_versionid(FILE *, word
*);
37 static alignment
utoalign(wchar_t *p
) {
38 if (!ustricmp(p
, L
"centre") || !ustricmp(p
, L
"center"))
40 if (!ustricmp(p
, L
"leftplus"))
45 static textconfig
text_configure(paragraph
*source
) {
51 ret
.bullet
.next
= NULL
;
52 ret
.bullet
.alt
= NULL
;
53 ret
.bullet
.type
= word_Normal
;
54 ret
.atitle
.just_numbers
= FALSE
; /* ignored */
61 ret
.listindentbefore
= 1;
62 ret
.listindentafter
= 3;
64 ret
.atitle
.align
= CENTRE
;
65 ret
.atitle
.underline
= L
'=';
66 ret
.achapter
.align
= LEFT
;
67 ret
.achapter
.just_numbers
= FALSE
;
68 ret
.achapter
.number_suffix
= ustrdup(L
": ");
69 ret
.achapter
.underline
= L
'-';
71 ret
.asect
= mknewa(alignstruct
, ret
.nasect
);
72 ret
.asect
[0].align
= LEFTPLUS
;
73 ret
.asect
[0].just_numbers
= TRUE
;
74 ret
.asect
[0].number_suffix
= ustrdup(L
" ");
75 ret
.asect
[0].underline
= L
'\0';
76 ret
.include_version_id
= TRUE
;
77 ret
.indent_preambles
= FALSE
;
78 ret
.bullet
.text
= ustrdup(L
"-");
80 for (; source
; source
= source
->next
) {
81 if (source
->type
== para_Config
) {
82 if (!ustricmp(source
->keyword
, L
"text-indent")) {
83 ret
.indent
= utoi(uadv(source
->keyword
));
84 } else if (!ustricmp(source
->keyword
, L
"text-indent-code")) {
85 ret
.indent_code
= utoi(uadv(source
->keyword
));
86 } else if (!ustricmp(source
->keyword
, L
"text-width")) {
87 ret
.width
= utoi(uadv(source
->keyword
));
88 } else if (!ustricmp(source
->keyword
, L
"text-list-indent")) {
89 ret
.listindentbefore
= utoi(uadv(source
->keyword
));
90 } else if (!ustricmp(source
->keyword
, L
"text-listitem-indent")) {
91 ret
.listindentafter
= utoi(uadv(source
->keyword
));
92 } else if (!ustricmp(source
->keyword
, L
"text-chapter-align")) {
93 ret
.achapter
.align
= utoalign(uadv(source
->keyword
));
94 } else if (!ustricmp(source
->keyword
, L
"text-chapter-underline")) {
95 ret
.achapter
.underline
= *uadv(source
->keyword
);
96 } else if (!ustricmp(source
->keyword
, L
"text-chapter-numeric")) {
97 ret
.achapter
.just_numbers
= utob(uadv(source
->keyword
));
98 } else if (!ustricmp(source
->keyword
, L
"text-chapter-suffix")) {
99 ret
.achapter
.number_suffix
= ustrdup(uadv(source
->keyword
));
100 } else if (!ustricmp(source
->keyword
, L
"text-section-align")) {
101 wchar_t *p
= uadv(source
->keyword
);
107 if (n
>= ret
.nasect
) {
109 ret
.asect
= resize(ret
.asect
, n
+1);
110 for (i
= ret
.nasect
; i
<= n
; i
++)
111 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
114 ret
.asect
[n
].align
= utoalign(p
);
115 } else if (!ustricmp(source
->keyword
, L
"text-section-underline")) {
116 wchar_t *p
= uadv(source
->keyword
);
122 if (n
>= ret
.nasect
) {
124 ret
.asect
= resize(ret
.asect
, n
+1);
125 for (i
= ret
.nasect
; i
<= n
; i
++)
126 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
129 ret
.asect
[n
].underline
= *p
;
130 } else if (!ustricmp(source
->keyword
, L
"text-section-numeric")) {
131 wchar_t *p
= uadv(source
->keyword
);
137 if (n
>= ret
.nasect
) {
139 ret
.asect
= resize(ret
.asect
, n
+1);
140 for (i
= ret
.nasect
; i
<= n
; i
++)
141 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
144 ret
.asect
[n
].just_numbers
= utob(p
);
145 } else if (!ustricmp(source
->keyword
, L
"text-section-suffix")) {
146 wchar_t *p
= uadv(source
->keyword
);
152 if (n
>= ret
.nasect
) {
154 ret
.asect
= resize(ret
.asect
, n
+1);
155 for (i
= ret
.nasect
; i
<= n
; i
++)
156 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
159 ret
.asect
[n
].number_suffix
= ustrdup(p
);
160 } else if (!ustricmp(source
->keyword
, L
"text-title-align")) {
161 ret
.atitle
.align
= utoalign(uadv(source
->keyword
));
162 } else if (!ustricmp(source
->keyword
, L
"text-title-underline")) {
163 ret
.atitle
.underline
= *uadv(source
->keyword
);
164 } else if (!ustricmp(source
->keyword
, L
"text-versionid")) {
165 ret
.include_version_id
= utob(uadv(source
->keyword
));
166 } else if (!ustricmp(source
->keyword
, L
"text-indent-preamble")) {
167 ret
.indent_preambles
= utob(uadv(source
->keyword
));
168 } else if (!ustricmp(source
->keyword
, L
"text-bullet")) {
169 ret
.bullet
.text
= uadv(source
->keyword
);
177 void text_backend(paragraph
*sourceform
, keywordlist
*keywords
,
181 word
*prefix
, *body
, *wp
;
185 int nesting
, nestindent
;
186 int indentb
, indenta
;
188 IGNORE(keywords
); /* we don't happen to need this */
189 IGNORE(idx
); /* or this */
191 conf
= text_configure(sourceform
);
194 * Determine the output file name, and open the output file
196 * FIXME: want configurable output file names here. For the
197 * moment, we'll just call it `output.txt'.
199 fp
= fopen("output.txt", "w");
201 error(err_cantopenw
, "output.txt");
206 for (p
= sourceform
; p
; p
= p
->next
)
207 if (p
->type
== para_Title
)
208 text_heading(fp
, NULL
, NULL
, p
->words
,
209 conf
.atitle
, conf
.indent
, conf
.width
);
211 nestindent
= conf
.listindentbefore
+ conf
.listindentafter
;
212 nesting
= (conf
.indent_preambles ?
0 : -conf
.indent
);
214 /* Do the main document */
215 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
222 assert(nesting
>= 0);
226 nesting
+= nestindent
;
229 nesting
-= nestindent
;
230 assert(nesting
>= 0);
234 * Things we ignore because we've already processed them or
235 * aren't going to touch them in this pass.
239 case para_Biblio
: /* only touch BiblioCited */
250 case para_UnnumberedChapter
:
251 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
252 conf
.achapter
, conf
.indent
, conf
.width
);
258 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
259 conf
.asect
[p
->aux
>=conf
.nasect ? conf
.nasect
-1 : p
->aux
],
260 conf
.indent
, conf
.width
);
264 text_rule(fp
, conf
.indent
+ nesting
, conf
.width
- nesting
);
269 case para_DescribedThing
:
270 case para_Description
:
271 case para_BiblioCited
:
273 case para_NumberedList
:
274 if (p
->type
== para_Bullet
) {
275 prefix
= &conf
.bullet
;
277 indentb
= conf
.listindentbefore
;
278 indenta
= conf
.listindentafter
;
279 } else if (p
->type
== para_NumberedList
) {
281 prefixextra
= "."; /* FIXME: configurability */
282 indentb
= conf
.listindentbefore
;
283 indenta
= conf
.listindentafter
;
284 } else if (p
->type
== para_Description
) {
287 indentb
= conf
.listindentbefore
;
288 indenta
= conf
.listindentafter
;
292 indentb
= indenta
= 0;
294 if (p
->type
== para_BiblioCited
) {
295 body
= dup_word_list(p
->kwtext
);
296 for (wp
= body
; wp
->next
; wp
= wp
->next
);
297 wp
->next
= &spaceword
;
298 spaceword
.next
= p
->words
;
299 spaceword
.alt
= NULL
;
300 spaceword
.type
= word_WhiteSpace
;
301 spaceword
.text
= NULL
;
306 text_para(fp
, prefix
, prefixextra
, body
,
307 conf
.indent
+ nesting
+ indentb
, indenta
,
308 conf
.width
- nesting
- indentb
- indenta
);
311 free_word_list(body
);
316 text_codepara(fp
, p
->words
,
317 conf
.indent
+ nesting
+ conf
.indent_code
,
318 conf
.width
- nesting
- 2 * conf
.indent_code
);
322 /* Do the version ID */
323 if (conf
.include_version_id
) {
324 for (p
= sourceform
; p
; p
= p
->next
)
325 if (p
->type
== para_VersionID
)
326 text_versionid(fp
, p
->words
);
335 sfree(conf
.achapter
.number_suffix
);
336 for (i
= 0; i
< conf
.nasect
; i
++)
337 sfree(conf
.asect
[i
].number_suffix
);
339 sfree(conf
.bullet
.text
);
344 * Convert a wide string into a string of chars. If `result' is
345 * non-NULL, mallocs the resulting string and stores a pointer to
346 * it in `*result'. If `result' is NULL, merely checks whether all
347 * characters in the string are feasible for the output character
350 * Return is nonzero if all characters are OK. If not all
351 * characters are OK but `result' is non-NULL, a result _will_
352 * still be generated!
354 static int text_convert(wchar_t *s
, char **result
) {
356 * FIXME. Currently this is ISO8859-1 only.
358 int doing
= (result
!= 0);
361 int plen
= 0, psize
= 0;
367 if ((c
>= 32 && c
<= 126) ||
368 (c
>= 160 && c
<= 255)) {
372 /* Char is not OK. */
374 outc
= 0xBF; /* approximate the good old DEC `uh?' */
379 p
= resize(p
, psize
);
385 p
= resize(p
, plen
+1);
392 static void text_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
) {
395 for (; text
&& text
!= end
; text
= text
->next
) switch (text
->type
) {
408 case word_WhiteSpace
:
411 case word_WkCodeSpace
:
415 case word_WkCodeQuote
:
416 assert(text
->type
!= word_CodeQuote
&&
417 text
->type
!= word_WkCodeQuote
);
418 if (towordstyle(text
->type
) == word_Emph
&&
419 (attraux(text
->aux
) == attr_First
||
420 attraux(text
->aux
) == attr_Only
))
421 rdaddc(rs
, '_'); /* FIXME: configurability */
422 else if (towordstyle(text
->type
) == word_Code
&&
423 (attraux(text
->aux
) == attr_First
||
424 attraux(text
->aux
) == attr_Only
))
425 rdaddc(rs
, '`'); /* FIXME: configurability */
426 if (removeattr(text
->type
) == word_Normal
) {
427 if (text_convert(text
->text
, &c
))
430 text_rdaddwc(rs
, text
->alt
, NULL
);
432 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
434 } else if (removeattr(text
->type
) == word_Quote
) {
435 rdaddc(rs
, quoteaux(text
->aux
) == quote_Open ?
'`' : '\'');
436 /* FIXME: configurability */
438 if (towordstyle(text
->type
) == word_Emph
&&
439 (attraux(text
->aux
) == attr_Last
||
440 attraux(text
->aux
) == attr_Only
))
441 rdaddc(rs
, '_'); /* FIXME: configurability */
442 else if (towordstyle(text
->type
) == word_Code
&&
443 (attraux(text
->aux
) == attr_Last
||
444 attraux(text
->aux
) == attr_Only
))
445 rdaddc(rs
, '\''); /* FIXME: configurability */
450 static int text_width(word
*);
452 static int text_width_list(word
*text
) {
455 w
+= text_width(text
);
461 static int text_width(word
*text
) {
462 switch (text
->type
) {
475 return (((text
->type
== word_Emph
||
476 text
->type
== word_Code
)
477 ?
(attraux(text
->aux
) == attr_Only ?
2 :
478 attraux(text
->aux
) == attr_Always ?
0 : 1)
480 (text_convert(text
->text
, NULL
) ?
481 ustrlen(text
->text
) :
482 text_width_list(text
->alt
)));
484 case word_WhiteSpace
:
487 case word_WkCodeSpace
:
491 case word_WkCodeQuote
:
492 assert(text
->type
!= word_CodeQuote
&&
493 text
->type
!= word_WkCodeQuote
);
494 return (((towordstyle(text
->type
) == word_Emph
||
495 towordstyle(text
->type
) == word_Code
)
496 ?
(attraux(text
->aux
) == attr_Only ?
2 :
497 attraux(text
->aux
) == attr_Always ?
0 : 1)
500 return 0; /* should never happen */
503 static void text_heading(FILE *fp
, word
*tprefix
, word
*nprefix
, word
*text
,
504 alignstruct align
, int indent
, int width
) {
505 rdstringc t
= { 0, 0, NULL
};
507 int firstlinewidth
, wrapwidth
;
508 wrappedline
*wrapping
, *p
;
510 if (align
.just_numbers
&& nprefix
) {
512 text_rdaddwc(&t
, nprefix
, NULL
);
513 if (text_convert(align
.number_suffix
, &c
)) {
517 } else if (!align
.just_numbers
&& tprefix
) {
519 text_rdaddwc(&t
, tprefix
, NULL
);
520 if (text_convert(align
.number_suffix
, &c
)) {
525 margin
= length
= (t
.text ?
strlen(t
.text
) : 0);
527 if (align
.align
== LEFTPLUS
) {
528 margin
= indent
- margin
;
529 if (margin
< 0) margin
= 0;
530 firstlinewidth
= indent
+ width
- margin
- length
;
532 } else if (align
.align
== LEFT
|| align
.align
== CENTRE
) {
534 firstlinewidth
= indent
+ width
- length
;
535 wrapwidth
= indent
+ width
;
538 wrapping
= wrap_para(text
, firstlinewidth
, wrapwidth
, text_width
);
539 for (p
= wrapping
; p
; p
= p
->next
) {
540 text_rdaddwc(&t
, p
->begin
, p
->end
);
541 length
= (t
.text ?
strlen(t
.text
) : 0);
542 if (align
.align
== CENTRE
) {
543 margin
= (indent
+ width
- length
)/2;
544 if (margin
< 0) margin
= 0;
546 fprintf(fp
, "%*s%s\n", margin
, "", t
.text
);
547 if (align
.underline
!= L
'\0') {
550 uw
[0] = align
.underline
; uw
[1] = L
'\0';
551 text_convert(uw
, &u
);
554 fprintf(fp
, "%*s", margin
, "");
559 if (align
.align
== LEFTPLUS
)
572 static void text_rule(FILE *fp
, int indent
, int width
) {
573 while (indent
--) putc(' ', fp
);
574 while (width
--) putc('-', fp
); /* FIXME: configurability! */
579 static void text_para(FILE *fp
, word
*prefix
, char *prefixextra
, word
*text
,
580 int indent
, int extraindent
, int width
) {
581 wrappedline
*wrapping
, *p
;
582 rdstringc pfx
= { 0, 0, NULL
};
584 int firstlinewidth
= width
;
587 text_rdaddwc(&pfx
, prefix
, NULL
);
589 rdaddsc(&pfx
, prefixextra
);
590 fprintf(fp
, "%*s%s", indent
, "", pfx
.text
);
591 /* If the prefix is too long, shorten the first line to fit. */
592 e
= extraindent
- strlen(pfx
.text
);
594 firstlinewidth
+= e
; /* this decreases it, since e < 0 */
595 if (firstlinewidth
< 0) {
596 e
= indent
+ extraindent
;
597 firstlinewidth
= width
;
604 e
= indent
+ extraindent
;
606 wrapping
= wrap_para(text
, firstlinewidth
, width
, text_width
);
607 for (p
= wrapping
; p
; p
= p
->next
) {
608 rdstringc t
= { 0, 0, NULL
};
609 text_rdaddwc(&t
, p
->begin
, p
->end
);
610 fprintf(fp
, "%*s%s\n", e
, "", t
.text
);
611 e
= indent
+ extraindent
;
618 static void text_codepara(FILE *fp
, word
*text
, int indent
, int width
) {
619 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
621 text_convert(text
->text
, &c
);
622 if (strlen(c
) > (size_t)width
) {
625 fprintf(fp
, "%*s%s\n", indent
, "", c
);
632 static void text_versionid(FILE *fp
, word
*text
) {
633 rdstringc t
= { 0, 0, NULL
};
635 rdaddc(&t
, '['); /* FIXME: configurability */
636 text_rdaddwc(&t
, text
, NULL
);
637 rdaddc(&t
, ']'); /* FIXME: configurability */
639 fprintf(fp
, "%s\n", t
.text
);