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
;
189 IGNORE(keywords
); /* we don't happen to need this */
190 IGNORE(idx
); /* or this */
192 conf
= text_configure(sourceform
);
195 * Determine the output file name, and open the output file
197 * FIXME: want configurable output file names here. For the
198 * moment, we'll just call it `output.txt'.
200 fp
= fopen("output.txt", "w");
202 error(err_cantopenw
, "output.txt");
207 for (p
= sourceform
; p
; p
= p
->next
)
208 if (p
->type
== para_Title
)
209 text_heading(fp
, NULL
, NULL
, p
->words
,
210 conf
.atitle
, conf
.indent
, conf
.width
);
212 nestindent
= conf
.listindentbefore
+ conf
.listindentafter
;
213 nesting
= (conf
.indent_preambles ?
0 : -conf
.indent
);
214 done_copyright
= FALSE
;
216 /* Do the main document */
217 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
224 assert(nesting
>= 0);
228 nesting
+= nestindent
;
231 nesting
-= nestindent
;
232 assert(nesting
>= 0);
236 * Things we ignore because we've already processed them or
237 * aren't going to touch them in this pass.
241 case para_Biblio
: /* only touch BiblioCited */
253 case para_UnnumberedChapter
:
255 * The copyright should come after the preamble but before
256 * the first chapter title.
258 if (!done_copyright
) {
261 for (p
= sourceform
; p
; p
= p
->next
)
262 if (p
->type
== para_Copyright
)
263 text_para(fp
, NULL
, NULL
, p
->words
,
264 conf
.indent
+ nesting
, 0, conf
.width
- nesting
);
265 done_copyright
= TRUE
;
267 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
268 conf
.achapter
, conf
.indent
, conf
.width
);
274 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
275 conf
.asect
[p
->aux
>=conf
.nasect ? conf
.nasect
-1 : p
->aux
],
276 conf
.indent
, conf
.width
);
280 text_rule(fp
, conf
.indent
+ nesting
, conf
.width
- nesting
);
284 case para_DescribedThing
:
285 case para_Description
:
286 case para_BiblioCited
:
288 case para_NumberedList
:
289 if (p
->type
== para_Bullet
) {
290 prefix
= &conf
.bullet
;
292 indentb
= conf
.listindentbefore
;
293 indenta
= conf
.listindentafter
;
294 } else if (p
->type
== para_NumberedList
) {
296 prefixextra
= "."; /* FIXME: configurability */
297 indentb
= conf
.listindentbefore
;
298 indenta
= conf
.listindentafter
;
299 } else if (p
->type
== para_Description
) {
302 indentb
= conf
.listindentbefore
;
303 indenta
= conf
.listindentafter
;
307 indentb
= indenta
= 0;
309 if (p
->type
== para_BiblioCited
) {
310 body
= dup_word_list(p
->kwtext
);
311 for (wp
= body
; wp
->next
; wp
= wp
->next
);
312 wp
->next
= &spaceword
;
313 spaceword
.next
= p
->words
;
314 spaceword
.alt
= NULL
;
315 spaceword
.type
= word_WhiteSpace
;
316 spaceword
.text
= NULL
;
321 text_para(fp
, prefix
, prefixextra
, body
,
322 conf
.indent
+ nesting
+ indentb
, indenta
,
323 conf
.width
- nesting
- indentb
- indenta
);
326 free_word_list(body
);
331 text_codepara(fp
, p
->words
,
332 conf
.indent
+ nesting
+ conf
.indent_code
,
333 conf
.width
- nesting
- 2 * conf
.indent_code
);
337 /* Do the version ID */
338 if (conf
.include_version_id
) {
339 for (p
= sourceform
; p
; p
= p
->next
)
340 if (p
->type
== para_VersionID
)
341 text_versionid(fp
, p
->words
);
350 sfree(conf
.achapter
.number_suffix
);
351 for (i
= 0; i
< conf
.nasect
; i
++)
352 sfree(conf
.asect
[i
].number_suffix
);
354 sfree(conf
.bullet
.text
);
359 * Convert a wide string into a string of chars. If `result' is
360 * non-NULL, mallocs the resulting string and stores a pointer to
361 * it in `*result'. If `result' is NULL, merely checks whether all
362 * characters in the string are feasible for the output character
365 * Return is nonzero if all characters are OK. If not all
366 * characters are OK but `result' is non-NULL, a result _will_
367 * still be generated!
369 static int text_convert(wchar_t *s
, char **result
) {
371 * FIXME. Currently this is ISO8859-1 only.
373 int doing
= (result
!= 0);
376 int plen
= 0, psize
= 0;
382 if ((c
>= 32 && c
<= 126) ||
383 (c
>= 160 && c
<= 255)) {
387 /* Char is not OK. */
389 outc
= 0xBF; /* approximate the good old DEC `uh?' */
394 p
= resize(p
, psize
);
400 p
= resize(p
, plen
+1);
407 static void text_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
) {
410 for (; text
&& text
!= end
; text
= text
->next
) switch (text
->type
) {
423 case word_WhiteSpace
:
426 case word_WkCodeSpace
:
430 case word_WkCodeQuote
:
431 assert(text
->type
!= word_CodeQuote
&&
432 text
->type
!= word_WkCodeQuote
);
433 if (towordstyle(text
->type
) == word_Emph
&&
434 (attraux(text
->aux
) == attr_First
||
435 attraux(text
->aux
) == attr_Only
))
436 rdaddc(rs
, '_'); /* FIXME: configurability */
437 else if (towordstyle(text
->type
) == word_Code
&&
438 (attraux(text
->aux
) == attr_First
||
439 attraux(text
->aux
) == attr_Only
))
440 rdaddc(rs
, '`'); /* FIXME: configurability */
441 if (removeattr(text
->type
) == word_Normal
) {
442 if (text_convert(text
->text
, &c
))
445 text_rdaddwc(rs
, text
->alt
, NULL
);
447 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
449 } else if (removeattr(text
->type
) == word_Quote
) {
450 rdaddc(rs
, quoteaux(text
->aux
) == quote_Open ?
'`' : '\'');
451 /* FIXME: configurability */
453 if (towordstyle(text
->type
) == word_Emph
&&
454 (attraux(text
->aux
) == attr_Last
||
455 attraux(text
->aux
) == attr_Only
))
456 rdaddc(rs
, '_'); /* FIXME: configurability */
457 else if (towordstyle(text
->type
) == word_Code
&&
458 (attraux(text
->aux
) == attr_Last
||
459 attraux(text
->aux
) == attr_Only
))
460 rdaddc(rs
, '\''); /* FIXME: configurability */
465 static int text_width(word
*);
467 static int text_width_list(word
*text
) {
470 w
+= text_width(text
);
476 static int text_width(word
*text
) {
477 switch (text
->type
) {
490 return (((text
->type
== word_Emph
||
491 text
->type
== word_Code
)
492 ?
(attraux(text
->aux
) == attr_Only ?
2 :
493 attraux(text
->aux
) == attr_Always ?
0 : 1)
495 (text_convert(text
->text
, NULL
) ?
496 ustrlen(text
->text
) :
497 text_width_list(text
->alt
)));
499 case word_WhiteSpace
:
502 case word_WkCodeSpace
:
506 case word_WkCodeQuote
:
507 assert(text
->type
!= word_CodeQuote
&&
508 text
->type
!= word_WkCodeQuote
);
509 return (((towordstyle(text
->type
) == word_Emph
||
510 towordstyle(text
->type
) == word_Code
)
511 ?
(attraux(text
->aux
) == attr_Only ?
2 :
512 attraux(text
->aux
) == attr_Always ?
0 : 1)
515 return 0; /* should never happen */
518 static void text_heading(FILE *fp
, word
*tprefix
, word
*nprefix
, word
*text
,
519 alignstruct align
, int indent
, int width
) {
520 rdstringc t
= { 0, 0, NULL
};
522 int firstlinewidth
, wrapwidth
;
523 wrappedline
*wrapping
, *p
;
525 if (align
.just_numbers
&& nprefix
) {
527 text_rdaddwc(&t
, nprefix
, NULL
);
528 if (text_convert(align
.number_suffix
, &c
)) {
532 } else if (!align
.just_numbers
&& tprefix
) {
534 text_rdaddwc(&t
, tprefix
, NULL
);
535 if (text_convert(align
.number_suffix
, &c
)) {
540 margin
= length
= (t
.text ?
strlen(t
.text
) : 0);
542 if (align
.align
== LEFTPLUS
) {
543 margin
= indent
- margin
;
544 if (margin
< 0) margin
= 0;
545 firstlinewidth
= indent
+ width
- margin
- length
;
547 } else if (align
.align
== LEFT
|| align
.align
== CENTRE
) {
549 firstlinewidth
= indent
+ width
- length
;
550 wrapwidth
= indent
+ width
;
553 wrapping
= wrap_para(text
, firstlinewidth
, wrapwidth
, text_width
);
554 for (p
= wrapping
; p
; p
= p
->next
) {
555 text_rdaddwc(&t
, p
->begin
, p
->end
);
556 length
= (t
.text ?
strlen(t
.text
) : 0);
557 if (align
.align
== CENTRE
) {
558 margin
= (indent
+ width
- length
)/2;
559 if (margin
< 0) margin
= 0;
561 fprintf(fp
, "%*s%s\n", margin
, "", t
.text
);
562 if (align
.underline
!= L
'\0') {
565 uw
[0] = align
.underline
; uw
[1] = L
'\0';
566 text_convert(uw
, &u
);
569 fprintf(fp
, "%*s", margin
, "");
574 if (align
.align
== LEFTPLUS
)
587 static void text_rule(FILE *fp
, int indent
, int width
) {
588 while (indent
--) putc(' ', fp
);
589 while (width
--) putc('-', fp
); /* FIXME: configurability! */
594 static void text_para(FILE *fp
, word
*prefix
, char *prefixextra
, word
*text
,
595 int indent
, int extraindent
, int width
) {
596 wrappedline
*wrapping
, *p
;
597 rdstringc pfx
= { 0, 0, NULL
};
599 int firstlinewidth
= width
;
602 text_rdaddwc(&pfx
, prefix
, NULL
);
604 rdaddsc(&pfx
, prefixextra
);
605 fprintf(fp
, "%*s%s", indent
, "", pfx
.text
);
606 /* If the prefix is too long, shorten the first line to fit. */
607 e
= extraindent
- strlen(pfx
.text
);
609 firstlinewidth
+= e
; /* this decreases it, since e < 0 */
610 if (firstlinewidth
< 0) {
611 e
= indent
+ extraindent
;
612 firstlinewidth
= width
;
619 e
= indent
+ extraindent
;
621 wrapping
= wrap_para(text
, firstlinewidth
, width
, text_width
);
622 for (p
= wrapping
; p
; p
= p
->next
) {
623 rdstringc t
= { 0, 0, NULL
};
624 text_rdaddwc(&t
, p
->begin
, p
->end
);
625 fprintf(fp
, "%*s%s\n", e
, "", t
.text
);
626 e
= indent
+ extraindent
;
633 static void text_codepara(FILE *fp
, word
*text
, int indent
, int width
) {
634 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
636 text_convert(text
->text
, &c
);
637 if (strlen(c
) > (size_t)width
) {
640 fprintf(fp
, "%*s%s\n", indent
, "", c
);
647 static void text_versionid(FILE *fp
, word
*text
) {
648 rdstringc t
= { 0, 0, NULL
};
650 rdaddc(&t
, '['); /* FIXME: configurability */
651 text_rdaddwc(&t
, text
, NULL
);
652 rdaddc(&t
, ']'); /* FIXME: configurability */
654 fprintf(fp
, "%s\n", t
.text
);