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
;
30 static int text_convert(wchar_t *, char **);
32 static void text_heading(FILE *, word
*, word
*, word
*, alignstruct
, int,int);
33 static void text_rule(FILE *, int, int);
34 static void text_para(FILE *, word
*, char *, word
*, int, int, int);
35 static void text_codepara(FILE *, word
*, int, int);
36 static void text_versionid(FILE *, word
*);
38 static alignment
utoalign(wchar_t *p
) {
39 if (!ustricmp(p
, L
"centre") || !ustricmp(p
, L
"center"))
41 if (!ustricmp(p
, L
"leftplus"))
46 static textconfig
text_configure(paragraph
*source
) {
52 ret
.bullet
.next
= NULL
;
53 ret
.bullet
.alt
= NULL
;
54 ret
.bullet
.type
= word_Normal
;
55 ret
.atitle
.just_numbers
= FALSE
; /* ignored */
62 ret
.listindentbefore
= 1;
63 ret
.listindentafter
= 3;
65 ret
.atitle
.align
= CENTRE
;
66 ret
.atitle
.underline
= L
'=';
67 ret
.achapter
.align
= LEFT
;
68 ret
.achapter
.just_numbers
= FALSE
;
69 ret
.achapter
.number_suffix
= L
": ";
70 ret
.achapter
.underline
= L
'-';
72 ret
.asect
= mknewa(alignstruct
, ret
.nasect
);
73 ret
.asect
[0].align
= LEFTPLUS
;
74 ret
.asect
[0].just_numbers
= TRUE
;
75 ret
.asect
[0].number_suffix
= L
" ";
76 ret
.asect
[0].underline
= L
'\0';
77 ret
.include_version_id
= TRUE
;
78 ret
.indent_preambles
= FALSE
;
79 ret
.bullet
.text
= L
"-";
80 ret
.filename
= dupstr("output.txt");
82 for (; source
; source
= source
->next
) {
83 if (source
->type
== para_Config
) {
84 if (!ustricmp(source
->keyword
, L
"text-indent")) {
85 ret
.indent
= utoi(uadv(source
->keyword
));
86 } else if (!ustricmp(source
->keyword
, L
"text-filename")) {
88 ret
.filename
= utoa_dup(uadv(source
->keyword
));
89 } else if (!ustricmp(source
->keyword
, L
"text-indent-code")) {
90 ret
.indent_code
= utoi(uadv(source
->keyword
));
91 } else if (!ustricmp(source
->keyword
, L
"text-width")) {
92 ret
.width
= utoi(uadv(source
->keyword
));
93 } else if (!ustricmp(source
->keyword
, L
"text-list-indent")) {
94 ret
.listindentbefore
= utoi(uadv(source
->keyword
));
95 } else if (!ustricmp(source
->keyword
, L
"text-listitem-indent")) {
96 ret
.listindentafter
= utoi(uadv(source
->keyword
));
97 } else if (!ustricmp(source
->keyword
, L
"text-chapter-align")) {
98 ret
.achapter
.align
= utoalign(uadv(source
->keyword
));
99 } else if (!ustricmp(source
->keyword
, L
"text-chapter-underline")) {
100 ret
.achapter
.underline
= *uadv(source
->keyword
);
101 } else if (!ustricmp(source
->keyword
, L
"text-chapter-numeric")) {
102 ret
.achapter
.just_numbers
= utob(uadv(source
->keyword
));
103 } else if (!ustricmp(source
->keyword
, L
"text-chapter-suffix")) {
104 ret
.achapter
.number_suffix
= uadv(source
->keyword
);
105 } else if (!ustricmp(source
->keyword
, L
"text-section-align")) {
106 wchar_t *p
= uadv(source
->keyword
);
112 if (n
>= ret
.nasect
) {
114 ret
.asect
= resize(ret
.asect
, n
+1);
115 for (i
= ret
.nasect
; i
<= n
; i
++)
116 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
119 ret
.asect
[n
].align
= utoalign(p
);
120 } else if (!ustricmp(source
->keyword
, L
"text-section-underline")) {
121 wchar_t *p
= uadv(source
->keyword
);
127 if (n
>= ret
.nasect
) {
129 ret
.asect
= resize(ret
.asect
, n
+1);
130 for (i
= ret
.nasect
; i
<= n
; i
++)
131 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
134 ret
.asect
[n
].underline
= *p
;
135 } else if (!ustricmp(source
->keyword
, L
"text-section-numeric")) {
136 wchar_t *p
= uadv(source
->keyword
);
142 if (n
>= ret
.nasect
) {
144 ret
.asect
= resize(ret
.asect
, n
+1);
145 for (i
= ret
.nasect
; i
<= n
; i
++)
146 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
149 ret
.asect
[n
].just_numbers
= utob(p
);
150 } else if (!ustricmp(source
->keyword
, L
"text-section-suffix")) {
151 wchar_t *p
= uadv(source
->keyword
);
157 if (n
>= ret
.nasect
) {
159 ret
.asect
= resize(ret
.asect
, n
+1);
160 for (i
= ret
.nasect
; i
<= n
; i
++) {
161 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
165 ret
.asect
[n
].number_suffix
= p
;
166 } else if (!ustricmp(source
->keyword
, L
"text-title-align")) {
167 ret
.atitle
.align
= utoalign(uadv(source
->keyword
));
168 } else if (!ustricmp(source
->keyword
, L
"text-title-underline")) {
169 ret
.atitle
.underline
= *uadv(source
->keyword
);
170 } else if (!ustricmp(source
->keyword
, L
"text-versionid")) {
171 ret
.include_version_id
= utob(uadv(source
->keyword
));
172 } else if (!ustricmp(source
->keyword
, L
"text-indent-preamble")) {
173 ret
.indent_preambles
= utob(uadv(source
->keyword
));
174 } else if (!ustricmp(source
->keyword
, L
"text-bullet")) {
175 ret
.bullet
.text
= uadv(source
->keyword
);
183 void text_backend(paragraph
*sourceform
, keywordlist
*keywords
,
187 word
*prefix
, *body
, *wp
;
191 int nesting
, nestindent
;
192 int indentb
, indenta
;
194 IGNORE(keywords
); /* we don't happen to need this */
195 IGNORE(idx
); /* or this */
197 conf
= text_configure(sourceform
);
200 * Open the output file.
202 fp
= fopen(conf
.filename
, "w");
204 error(err_cantopenw
, conf
.filename
);
209 for (p
= sourceform
; p
; p
= p
->next
)
210 if (p
->type
== para_Title
)
211 text_heading(fp
, NULL
, NULL
, p
->words
,
212 conf
.atitle
, conf
.indent
, conf
.width
);
214 nestindent
= conf
.listindentbefore
+ conf
.listindentafter
;
215 nesting
= (conf
.indent_preambles ?
0 : -conf
.indent
);
217 /* Do the main document */
218 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
225 assert(nesting
>= 0);
229 nesting
+= nestindent
;
232 nesting
-= nestindent
;
233 assert(nesting
>= 0);
237 * Things we ignore because we've already processed them or
238 * aren't going to touch them in this pass.
242 case para_Biblio
: /* only touch BiblioCited */
253 case para_UnnumberedChapter
:
254 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
255 conf
.achapter
, conf
.indent
, conf
.width
);
261 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
262 conf
.asect
[p
->aux
>=conf
.nasect ? conf
.nasect
-1 : p
->aux
],
263 conf
.indent
, conf
.width
);
267 text_rule(fp
, conf
.indent
+ nesting
, conf
.width
- nesting
);
272 case para_DescribedThing
:
273 case para_Description
:
274 case para_BiblioCited
:
276 case para_NumberedList
:
277 if (p
->type
== para_Bullet
) {
278 prefix
= &conf
.bullet
;
280 indentb
= conf
.listindentbefore
;
281 indenta
= conf
.listindentafter
;
282 } else if (p
->type
== para_NumberedList
) {
284 prefixextra
= "."; /* FIXME: configurability */
285 indentb
= conf
.listindentbefore
;
286 indenta
= conf
.listindentafter
;
287 } else if (p
->type
== para_Description
) {
290 indentb
= conf
.listindentbefore
;
291 indenta
= conf
.listindentafter
;
295 indentb
= indenta
= 0;
297 if (p
->type
== para_BiblioCited
) {
298 body
= dup_word_list(p
->kwtext
);
299 for (wp
= body
; wp
->next
; wp
= wp
->next
);
300 wp
->next
= &spaceword
;
301 spaceword
.next
= p
->words
;
302 spaceword
.alt
= NULL
;
303 spaceword
.type
= word_WhiteSpace
;
304 spaceword
.text
= NULL
;
309 text_para(fp
, prefix
, prefixextra
, body
,
310 conf
.indent
+ nesting
+ indentb
, indenta
,
311 conf
.width
- nesting
- indentb
- indenta
);
314 free_word_list(body
);
319 text_codepara(fp
, p
->words
,
320 conf
.indent
+ nesting
+ conf
.indent_code
,
321 conf
.width
- nesting
- 2 * conf
.indent_code
);
325 /* Do the version ID */
326 if (conf
.include_version_id
) {
327 for (p
= sourceform
; p
; p
= p
->next
)
328 if (p
->type
== para_VersionID
)
329 text_versionid(fp
, p
->words
);
337 sfree(conf
.filename
);
341 * Convert a wide string into a string of chars. If `result' is
342 * non-NULL, mallocs the resulting string and stores a pointer to
343 * it in `*result'. If `result' is NULL, merely checks whether all
344 * characters in the string are feasible for the output character
347 * Return is nonzero if all characters are OK. If not all
348 * characters are OK but `result' is non-NULL, a result _will_
349 * still be generated!
351 static int text_convert(wchar_t *s
, char **result
) {
353 * FIXME. Currently this is ISO8859-1 only.
355 int doing
= (result
!= 0);
358 int plen
= 0, psize
= 0;
364 if ((c
>= 32 && c
<= 126) ||
365 (c
>= 160 && c
<= 255)) {
369 /* Char is not OK. */
371 outc
= 0xBF; /* approximate the good old DEC `uh?' */
376 p
= resize(p
, psize
);
382 p
= resize(p
, plen
+1);
389 static void text_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
) {
392 for (; text
&& text
!= end
; text
= text
->next
) switch (text
->type
) {
405 case word_WhiteSpace
:
408 case word_WkCodeSpace
:
412 case word_WkCodeQuote
:
413 assert(text
->type
!= word_CodeQuote
&&
414 text
->type
!= word_WkCodeQuote
);
415 if (towordstyle(text
->type
) == word_Emph
&&
416 (attraux(text
->aux
) == attr_First
||
417 attraux(text
->aux
) == attr_Only
))
418 rdaddc(rs
, '_'); /* FIXME: configurability */
419 else if (towordstyle(text
->type
) == word_Code
&&
420 (attraux(text
->aux
) == attr_First
||
421 attraux(text
->aux
) == attr_Only
))
422 rdaddc(rs
, '`'); /* FIXME: configurability */
423 if (removeattr(text
->type
) == word_Normal
) {
424 if (text_convert(text
->text
, &c
))
427 text_rdaddwc(rs
, text
->alt
, NULL
);
429 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
431 } else if (removeattr(text
->type
) == word_Quote
) {
432 rdaddc(rs
, quoteaux(text
->aux
) == quote_Open ?
'`' : '\'');
433 /* FIXME: configurability */
435 if (towordstyle(text
->type
) == word_Emph
&&
436 (attraux(text
->aux
) == attr_Last
||
437 attraux(text
->aux
) == attr_Only
))
438 rdaddc(rs
, '_'); /* FIXME: configurability */
439 else if (towordstyle(text
->type
) == word_Code
&&
440 (attraux(text
->aux
) == attr_Last
||
441 attraux(text
->aux
) == attr_Only
))
442 rdaddc(rs
, '\''); /* FIXME: configurability */
447 static int text_width(word
*);
449 static int text_width_list(word
*text
) {
452 w
+= text_width(text
);
458 static int text_width(word
*text
) {
459 switch (text
->type
) {
472 return (((text
->type
== word_Emph
||
473 text
->type
== word_Code
)
474 ?
(attraux(text
->aux
) == attr_Only ?
2 :
475 attraux(text
->aux
) == attr_Always ?
0 : 1)
477 (text_convert(text
->text
, NULL
) ?
478 ustrlen(text
->text
) :
479 text_width_list(text
->alt
)));
481 case word_WhiteSpace
:
484 case word_WkCodeSpace
:
488 case word_WkCodeQuote
:
489 assert(text
->type
!= word_CodeQuote
&&
490 text
->type
!= word_WkCodeQuote
);
491 return (((towordstyle(text
->type
) == word_Emph
||
492 towordstyle(text
->type
) == word_Code
)
493 ?
(attraux(text
->aux
) == attr_Only ?
2 :
494 attraux(text
->aux
) == attr_Always ?
0 : 1)
497 return 0; /* should never happen */
500 static void text_heading(FILE *fp
, word
*tprefix
, word
*nprefix
, word
*text
,
501 alignstruct align
, int indent
, int width
) {
502 rdstringc t
= { 0, 0, NULL
};
504 int firstlinewidth
, wrapwidth
;
505 wrappedline
*wrapping
, *p
;
507 if (align
.just_numbers
&& nprefix
) {
509 text_rdaddwc(&t
, nprefix
, NULL
);
510 if (text_convert(align
.number_suffix
, &c
)) {
514 } else if (!align
.just_numbers
&& tprefix
) {
516 text_rdaddwc(&t
, tprefix
, NULL
);
517 if (text_convert(align
.number_suffix
, &c
)) {
522 margin
= length
= (t
.text ?
strlen(t
.text
) : 0);
524 if (align
.align
== LEFTPLUS
) {
525 margin
= indent
- margin
;
526 if (margin
< 0) margin
= 0;
527 firstlinewidth
= indent
+ width
- margin
- length
;
529 } else if (align
.align
== LEFT
|| align
.align
== CENTRE
) {
531 firstlinewidth
= indent
+ width
- length
;
532 wrapwidth
= indent
+ width
;
535 wrapping
= wrap_para(text
, firstlinewidth
, wrapwidth
, text_width
);
536 for (p
= wrapping
; p
; p
= p
->next
) {
537 text_rdaddwc(&t
, p
->begin
, p
->end
);
538 length
= (t
.text ?
strlen(t
.text
) : 0);
539 if (align
.align
== CENTRE
) {
540 margin
= (indent
+ width
- length
)/2;
541 if (margin
< 0) margin
= 0;
543 fprintf(fp
, "%*s%s\n", margin
, "", t
.text
);
544 if (align
.underline
!= L
'\0') {
547 uw
[0] = align
.underline
; uw
[1] = L
'\0';
548 text_convert(uw
, &u
);
551 fprintf(fp
, "%*s", margin
, "");
556 if (align
.align
== LEFTPLUS
)
569 static void text_rule(FILE *fp
, int indent
, int width
) {
570 while (indent
--) putc(' ', fp
);
571 while (width
--) putc('-', fp
); /* FIXME: configurability! */
576 static void text_para(FILE *fp
, word
*prefix
, char *prefixextra
, word
*text
,
577 int indent
, int extraindent
, int width
) {
578 wrappedline
*wrapping
, *p
;
579 rdstringc pfx
= { 0, 0, NULL
};
581 int firstlinewidth
= width
;
584 text_rdaddwc(&pfx
, prefix
, NULL
);
586 rdaddsc(&pfx
, prefixextra
);
587 fprintf(fp
, "%*s%s", indent
, "", pfx
.text
);
588 /* If the prefix is too long, shorten the first line to fit. */
589 e
= extraindent
- strlen(pfx
.text
);
591 firstlinewidth
+= e
; /* this decreases it, since e < 0 */
592 if (firstlinewidth
< 0) {
593 e
= indent
+ extraindent
;
594 firstlinewidth
= width
;
601 e
= indent
+ extraindent
;
603 wrapping
= wrap_para(text
, firstlinewidth
, width
, text_width
);
604 for (p
= wrapping
; p
; p
= p
->next
) {
605 rdstringc t
= { 0, 0, NULL
};
606 text_rdaddwc(&t
, p
->begin
, p
->end
);
607 fprintf(fp
, "%*s%s\n", e
, "", t
.text
);
608 e
= indent
+ extraindent
;
615 static void text_codepara(FILE *fp
, word
*text
, int indent
, int width
) {
616 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
618 text_convert(text
->text
, &c
);
619 if (strlen(c
) > (size_t)width
) {
622 fprintf(fp
, "%*s%s\n", indent
, "", c
);
629 static void text_versionid(FILE *fp
, word
*text
) {
630 rdstringc t
= { 0, 0, NULL
};
632 rdaddc(&t
, '['); /* FIXME: configurability */
633 text_rdaddwc(&t
, text
, NULL
);
634 rdaddc(&t
, ']'); /* FIXME: configurability */
636 fprintf(fp
, "%s\n", t
.text
);