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
= 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
= 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 indentb
, indenta
;
187 IGNORE(keywords
); /* we don't happen to need this */
188 IGNORE(idx
); /* or this */
190 conf
= text_configure(sourceform
);
193 * Determine the output file name, and open the output file
195 * FIXME: want configurable output file names here. For the
196 * moment, we'll just call it `output.txt'.
198 fp
= fopen("output.txt", "w");
200 error(err_cantopenw
, "output.txt");
205 for (p
= sourceform
; p
; p
= p
->next
)
206 if (p
->type
== para_Title
)
207 text_heading(fp
, NULL
, NULL
, p
->words
,
208 conf
.atitle
, conf
.indent
, conf
.width
);
210 /* Do the preamble and copyright */
211 for (p
= sourceform
; p
; p
= p
->next
)
212 if (p
->type
== para_Preamble
)
213 text_para(fp
, NULL
, NULL
, p
->words
,
214 conf
.indent_preambles ? conf
.indent
: 0, 0,
215 conf
.width
+ (conf
.indent_preambles ?
0 : conf
.indent
));
216 for (p
= sourceform
; p
; p
= p
->next
)
217 if (p
->type
== para_Copyright
)
218 text_para(fp
, NULL
, NULL
, p
->words
,
219 conf
.indent_preambles ? conf
.indent
: 0, 0,
220 conf
.width
+ (conf
.indent_preambles ?
0 : conf
.indent
));
222 /* Do the main document */
223 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
226 * Things we ignore because we've already processed them or
227 * aren't going to touch them in this pass.
231 case para_Biblio
: /* only touch BiblioCited */
244 case para_UnnumberedChapter
:
245 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
246 conf
.achapter
, conf
.indent
, conf
.width
);
251 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
252 conf
.asect
[p
->aux
>=conf
.nasect ? conf
.nasect
-1 : p
->aux
],
253 conf
.indent
, conf
.width
);
257 text_rule(fp
, conf
.indent
, conf
.width
);
261 case para_BiblioCited
:
263 case para_NumberedList
:
264 if (p
->type
== para_Bullet
) {
265 prefix
= &conf
.bullet
;
267 indentb
= conf
.listindentbefore
;
268 indenta
= conf
.listindentafter
;
269 } else if (p
->type
== para_NumberedList
) {
271 prefixextra
= "."; /* FIXME: configurability */
272 indentb
= conf
.listindentbefore
;
273 indenta
= conf
.listindentafter
;
277 indentb
= indenta
= 0;
279 if (p
->type
== para_BiblioCited
) {
280 body
= dup_word_list(p
->kwtext
);
281 for (wp
= body
; wp
->next
; wp
= wp
->next
);
282 wp
->next
= &spaceword
;
283 spaceword
.next
= p
->words
;
284 spaceword
.alt
= NULL
;
285 spaceword
.type
= word_WhiteSpace
;
286 spaceword
.text
= NULL
;
291 text_para(fp
, prefix
, prefixextra
, body
,
292 conf
.indent
+ indentb
, indenta
,
293 conf
.width
- indentb
- indenta
);
296 free_word_list(body
);
301 text_codepara(fp
, p
->words
, conf
.indent
+ conf
.indent_code
, conf
.width
- 2 * conf
.indent_code
);
305 /* Do the version ID */
306 if (conf
.include_version_id
) {
307 for (p
= sourceform
; p
; p
= p
->next
)
308 if (p
->type
== para_VersionID
)
309 text_versionid(fp
, p
->words
);
318 sfree(conf
.achapter
.number_suffix
);
319 for (i
= 0; i
< conf
.nasect
; i
++)
320 sfree(conf
.asect
[i
].number_suffix
);
322 sfree(conf
.bullet
.text
);
327 * Convert a wide string into a string of chars. If `result' is
328 * non-NULL, mallocs the resulting string and stores a pointer to
329 * it in `*result'. If `result' is NULL, merely checks whether all
330 * characters in the string are feasible for the output character
333 * Return is nonzero if all characters are OK. If not all
334 * characters are OK but `result' is non-NULL, a result _will_
335 * still be generated!
337 static int text_convert(wchar_t *s
, char **result
) {
339 * FIXME. Currently this is ISO8859-1 only.
341 int doing
= (result
!= 0);
344 int plen
= 0, psize
= 0;
350 if ((c
>= 32 && c
<= 126) ||
351 (c
>= 160 && c
<= 255)) {
355 /* Char is not OK. */
357 outc
= 0xBF; /* approximate the good old DEC `uh?' */
362 p
= resize(p
, psize
);
368 p
= resize(p
, plen
+1);
375 static void text_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
) {
378 for (; text
&& text
!= end
; text
= text
->next
) switch (text
->type
) {
391 case word_WhiteSpace
:
394 case word_WkCodeSpace
:
398 case word_WkCodeQuote
:
399 assert(text
->type
!= word_CodeQuote
&&
400 text
->type
!= word_WkCodeQuote
);
401 if (towordstyle(text
->type
) == word_Emph
&&
402 (attraux(text
->aux
) == attr_First
||
403 attraux(text
->aux
) == attr_Only
))
404 rdaddc(rs
, '_'); /* FIXME: configurability */
405 else if (towordstyle(text
->type
) == word_Code
&&
406 (attraux(text
->aux
) == attr_First
||
407 attraux(text
->aux
) == attr_Only
))
408 rdaddc(rs
, '`'); /* FIXME: configurability */
409 if (removeattr(text
->type
) == word_Normal
) {
410 if (text_convert(text
->text
, &c
))
413 text_rdaddwc(rs
, text
->alt
, NULL
);
415 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
417 } else if (removeattr(text
->type
) == word_Quote
) {
418 rdaddc(rs
, quoteaux(text
->aux
) == quote_Open ?
'`' : '\'');
419 /* FIXME: configurability */
421 if (towordstyle(text
->type
) == word_Emph
&&
422 (attraux(text
->aux
) == attr_Last
||
423 attraux(text
->aux
) == attr_Only
))
424 rdaddc(rs
, '_'); /* FIXME: configurability */
425 else if (towordstyle(text
->type
) == word_Code
&&
426 (attraux(text
->aux
) == attr_Last
||
427 attraux(text
->aux
) == attr_Only
))
428 rdaddc(rs
, '\''); /* FIXME: configurability */
433 static int text_width(word
*);
435 static int text_width_list(word
*text
) {
438 w
+= text_width(text
);
444 static int text_width(word
*text
) {
445 switch (text
->type
) {
458 return (((text
->type
== word_Emph
||
459 text
->type
== word_Code
)
460 ?
(attraux(text
->aux
) == attr_Only ?
2 :
461 attraux(text
->aux
) == attr_Always ?
0 : 1)
463 (text_convert(text
->text
, NULL
) ?
464 ustrlen(text
->text
) :
465 text_width_list(text
->alt
)));
467 case word_WhiteSpace
:
470 case word_WkCodeSpace
:
474 case word_WkCodeQuote
:
475 assert(text
->type
!= word_CodeQuote
&&
476 text
->type
!= word_WkCodeQuote
);
477 return (((towordstyle(text
->type
) == word_Emph
||
478 towordstyle(text
->type
) == word_Code
)
479 ?
(attraux(text
->aux
) == attr_Only ?
2 :
480 attraux(text
->aux
) == attr_Always ?
0 : 1)
483 return 0; /* should never happen */
486 static void text_heading(FILE *fp
, word
*tprefix
, word
*nprefix
, word
*text
,
487 alignstruct align
, int indent
, int width
) {
488 rdstringc t
= { 0, 0, NULL
};
490 int firstlinewidth
, wrapwidth
;
491 wrappedline
*wrapping
, *p
;
493 if (align
.just_numbers
&& nprefix
) {
495 text_rdaddwc(&t
, nprefix
, NULL
);
496 if (text_convert(align
.number_suffix
, &c
)) {
500 } else if (!align
.just_numbers
&& tprefix
) {
502 text_rdaddwc(&t
, tprefix
, NULL
);
503 if (text_convert(align
.number_suffix
, &c
)) {
508 margin
= length
= (t
.text ?
strlen(t
.text
) : 0);
510 if (align
.align
== LEFTPLUS
) {
511 margin
= indent
- margin
;
512 if (margin
< 0) margin
= 0;
513 firstlinewidth
= indent
+ width
- margin
- length
;
515 } else if (align
.align
== LEFT
|| align
.align
== CENTRE
) {
517 firstlinewidth
= indent
+ width
- length
;
518 wrapwidth
= indent
+ width
;
521 wrapping
= wrap_para(text
, firstlinewidth
, wrapwidth
, text_width
);
522 for (p
= wrapping
; p
; p
= p
->next
) {
523 text_rdaddwc(&t
, p
->begin
, p
->end
);
524 length
= (t
.text ?
strlen(t
.text
) : 0);
525 if (align
.align
== CENTRE
) {
526 margin
= (indent
+ width
- length
)/2;
527 if (margin
< 0) margin
= 0;
529 fprintf(fp
, "%*s%s\n", margin
, "", t
.text
);
530 if (align
.underline
!= L
'\0') {
533 uw
[0] = align
.underline
; uw
[1] = L
'\0';
534 text_convert(uw
, &u
);
537 fprintf(fp
, "%*s", margin
, "");
542 if (align
.align
== LEFTPLUS
)
555 static void text_rule(FILE *fp
, int indent
, int width
) {
556 while (indent
--) putc(' ', fp
);
557 while (width
--) putc('-', fp
); /* FIXME: configurability! */
562 static void text_para(FILE *fp
, word
*prefix
, char *prefixextra
, word
*text
,
563 int indent
, int extraindent
, int width
) {
564 wrappedline
*wrapping
, *p
;
565 rdstringc pfx
= { 0, 0, NULL
};
567 int firstlinewidth
= width
;
570 text_rdaddwc(&pfx
, prefix
, NULL
);
572 rdaddsc(&pfx
, prefixextra
);
573 fprintf(fp
, "%*s%s", indent
, "", pfx
.text
);
574 /* If the prefix is too long, shorten the first line to fit. */
575 e
= extraindent
- strlen(pfx
.text
);
577 firstlinewidth
+= e
; /* this decreases it, since e < 0 */
578 if (firstlinewidth
< 0) {
579 e
= indent
+ extraindent
;
580 firstlinewidth
= width
;
587 e
= indent
+ extraindent
;
589 wrapping
= wrap_para(text
, firstlinewidth
, width
, text_width
);
590 for (p
= wrapping
; p
; p
= p
->next
) {
591 rdstringc t
= { 0, 0, NULL
};
592 text_rdaddwc(&t
, p
->begin
, p
->end
);
593 fprintf(fp
, "%*s%s\n", e
, "", t
.text
);
594 e
= indent
+ extraindent
;
601 static void text_codepara(FILE *fp
, word
*text
, int indent
, int width
) {
602 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
604 text_convert(text
->text
, &c
);
605 if (strlen(c
) > (size_t)width
) {
608 fprintf(fp
, "%*s%s\n", indent
, "", c
);
615 static void text_versionid(FILE *fp
, word
*text
) {
616 rdstringc t
= { 0, 0, NULL
};
618 rdaddc(&t
, '['); /* FIXME: configurability */
619 text_rdaddwc(&t
, text
, NULL
);
620 rdaddc(&t
, ']'); /* FIXME: configurability */
622 fprintf(fp
, "%s\n", t
.text
);