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 /* Do the preamble and copyright */
212 for (p
= sourceform
; p
; p
= p
->next
)
213 if (p
->type
== para_Preamble
)
214 text_para(fp
, NULL
, NULL
, p
->words
,
215 conf
.indent_preambles ? conf
.indent
: 0, 0,
216 conf
.width
+ (conf
.indent_preambles ?
0 : conf
.indent
));
217 for (p
= sourceform
; p
; p
= p
->next
)
218 if (p
->type
== para_Copyright
)
219 text_para(fp
, NULL
, NULL
, p
->words
,
220 conf
.indent_preambles ? conf
.indent
: 0, 0,
221 conf
.width
+ (conf
.indent_preambles ?
0 : conf
.indent
));
223 nestindent
= conf
.listindentbefore
+ conf
.listindentafter
;
226 /* Do the main document */
227 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
238 * Things we ignore because we've already processed them or
239 * aren't going to touch them in this pass.
243 case para_Biblio
: /* only touch BiblioCited */
256 case para_UnnumberedChapter
:
257 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
258 conf
.achapter
, conf
.indent
, conf
.width
);
263 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
264 conf
.asect
[p
->aux
>=conf
.nasect ? conf
.nasect
-1 : p
->aux
],
265 conf
.indent
, conf
.width
);
269 text_rule(fp
, conf
.indent
+ nestindent
*nesting
,
270 conf
.width
- nestindent
*nesting
);
274 case para_DescribedThing
:
275 case para_Description
:
276 case para_BiblioCited
:
278 case para_NumberedList
:
279 if (p
->type
== para_Bullet
) {
280 prefix
= &conf
.bullet
;
282 indentb
= conf
.listindentbefore
;
283 indenta
= conf
.listindentafter
;
284 } else if (p
->type
== para_NumberedList
) {
286 prefixextra
= "."; /* FIXME: configurability */
287 indentb
= conf
.listindentbefore
;
288 indenta
= conf
.listindentafter
;
289 } else if (p
->type
== para_Description
) {
292 indentb
= conf
.listindentbefore
;
293 indenta
= conf
.listindentafter
;
297 indentb
= indenta
= 0;
299 if (p
->type
== para_BiblioCited
) {
300 body
= dup_word_list(p
->kwtext
);
301 for (wp
= body
; wp
->next
; wp
= wp
->next
);
302 wp
->next
= &spaceword
;
303 spaceword
.next
= p
->words
;
304 spaceword
.alt
= NULL
;
305 spaceword
.type
= word_WhiteSpace
;
306 spaceword
.text
= NULL
;
311 text_para(fp
, prefix
, prefixextra
, body
,
312 conf
.indent
+ nestindent
*nesting
+ indentb
, indenta
,
313 conf
.width
- nestindent
*nesting
- indentb
- indenta
);
316 free_word_list(body
);
321 text_codepara(fp
, p
->words
,
322 conf
.indent
+ nestindent
*nesting
+ conf
.indent_code
,
323 conf
.width
- nestindent
*nesting
- 2 * conf
.indent_code
);
327 /* Do the version ID */
328 if (conf
.include_version_id
) {
329 for (p
= sourceform
; p
; p
= p
->next
)
330 if (p
->type
== para_VersionID
)
331 text_versionid(fp
, p
->words
);
340 sfree(conf
.achapter
.number_suffix
);
341 for (i
= 0; i
< conf
.nasect
; i
++)
342 sfree(conf
.asect
[i
].number_suffix
);
344 sfree(conf
.bullet
.text
);
349 * Convert a wide string into a string of chars. If `result' is
350 * non-NULL, mallocs the resulting string and stores a pointer to
351 * it in `*result'. If `result' is NULL, merely checks whether all
352 * characters in the string are feasible for the output character
355 * Return is nonzero if all characters are OK. If not all
356 * characters are OK but `result' is non-NULL, a result _will_
357 * still be generated!
359 static int text_convert(wchar_t *s
, char **result
) {
361 * FIXME. Currently this is ISO8859-1 only.
363 int doing
= (result
!= 0);
366 int plen
= 0, psize
= 0;
372 if ((c
>= 32 && c
<= 126) ||
373 (c
>= 160 && c
<= 255)) {
377 /* Char is not OK. */
379 outc
= 0xBF; /* approximate the good old DEC `uh?' */
384 p
= resize(p
, psize
);
390 p
= resize(p
, plen
+1);
397 static void text_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
) {
400 for (; text
&& text
!= end
; text
= text
->next
) switch (text
->type
) {
413 case word_WhiteSpace
:
416 case word_WkCodeSpace
:
420 case word_WkCodeQuote
:
421 assert(text
->type
!= word_CodeQuote
&&
422 text
->type
!= word_WkCodeQuote
);
423 if (towordstyle(text
->type
) == word_Emph
&&
424 (attraux(text
->aux
) == attr_First
||
425 attraux(text
->aux
) == attr_Only
))
426 rdaddc(rs
, '_'); /* FIXME: configurability */
427 else if (towordstyle(text
->type
) == word_Code
&&
428 (attraux(text
->aux
) == attr_First
||
429 attraux(text
->aux
) == attr_Only
))
430 rdaddc(rs
, '`'); /* FIXME: configurability */
431 if (removeattr(text
->type
) == word_Normal
) {
432 if (text_convert(text
->text
, &c
))
435 text_rdaddwc(rs
, text
->alt
, NULL
);
437 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
439 } else if (removeattr(text
->type
) == word_Quote
) {
440 rdaddc(rs
, quoteaux(text
->aux
) == quote_Open ?
'`' : '\'');
441 /* FIXME: configurability */
443 if (towordstyle(text
->type
) == word_Emph
&&
444 (attraux(text
->aux
) == attr_Last
||
445 attraux(text
->aux
) == attr_Only
))
446 rdaddc(rs
, '_'); /* FIXME: configurability */
447 else if (towordstyle(text
->type
) == word_Code
&&
448 (attraux(text
->aux
) == attr_Last
||
449 attraux(text
->aux
) == attr_Only
))
450 rdaddc(rs
, '\''); /* FIXME: configurability */
455 static int text_width(word
*);
457 static int text_width_list(word
*text
) {
460 w
+= text_width(text
);
466 static int text_width(word
*text
) {
467 switch (text
->type
) {
480 return (((text
->type
== word_Emph
||
481 text
->type
== word_Code
)
482 ?
(attraux(text
->aux
) == attr_Only ?
2 :
483 attraux(text
->aux
) == attr_Always ?
0 : 1)
485 (text_convert(text
->text
, NULL
) ?
486 ustrlen(text
->text
) :
487 text_width_list(text
->alt
)));
489 case word_WhiteSpace
:
492 case word_WkCodeSpace
:
496 case word_WkCodeQuote
:
497 assert(text
->type
!= word_CodeQuote
&&
498 text
->type
!= word_WkCodeQuote
);
499 return (((towordstyle(text
->type
) == word_Emph
||
500 towordstyle(text
->type
) == word_Code
)
501 ?
(attraux(text
->aux
) == attr_Only ?
2 :
502 attraux(text
->aux
) == attr_Always ?
0 : 1)
505 return 0; /* should never happen */
508 static void text_heading(FILE *fp
, word
*tprefix
, word
*nprefix
, word
*text
,
509 alignstruct align
, int indent
, int width
) {
510 rdstringc t
= { 0, 0, NULL
};
512 int firstlinewidth
, wrapwidth
;
513 wrappedline
*wrapping
, *p
;
515 if (align
.just_numbers
&& nprefix
) {
517 text_rdaddwc(&t
, nprefix
, NULL
);
518 if (text_convert(align
.number_suffix
, &c
)) {
522 } else if (!align
.just_numbers
&& tprefix
) {
524 text_rdaddwc(&t
, tprefix
, NULL
);
525 if (text_convert(align
.number_suffix
, &c
)) {
530 margin
= length
= (t
.text ?
strlen(t
.text
) : 0);
532 if (align
.align
== LEFTPLUS
) {
533 margin
= indent
- margin
;
534 if (margin
< 0) margin
= 0;
535 firstlinewidth
= indent
+ width
- margin
- length
;
537 } else if (align
.align
== LEFT
|| align
.align
== CENTRE
) {
539 firstlinewidth
= indent
+ width
- length
;
540 wrapwidth
= indent
+ width
;
543 wrapping
= wrap_para(text
, firstlinewidth
, wrapwidth
, text_width
);
544 for (p
= wrapping
; p
; p
= p
->next
) {
545 text_rdaddwc(&t
, p
->begin
, p
->end
);
546 length
= (t
.text ?
strlen(t
.text
) : 0);
547 if (align
.align
== CENTRE
) {
548 margin
= (indent
+ width
- length
)/2;
549 if (margin
< 0) margin
= 0;
551 fprintf(fp
, "%*s%s\n", margin
, "", t
.text
);
552 if (align
.underline
!= L
'\0') {
555 uw
[0] = align
.underline
; uw
[1] = L
'\0';
556 text_convert(uw
, &u
);
559 fprintf(fp
, "%*s", margin
, "");
564 if (align
.align
== LEFTPLUS
)
577 static void text_rule(FILE *fp
, int indent
, int width
) {
578 while (indent
--) putc(' ', fp
);
579 while (width
--) putc('-', fp
); /* FIXME: configurability! */
584 static void text_para(FILE *fp
, word
*prefix
, char *prefixextra
, word
*text
,
585 int indent
, int extraindent
, int width
) {
586 wrappedline
*wrapping
, *p
;
587 rdstringc pfx
= { 0, 0, NULL
};
589 int firstlinewidth
= width
;
592 text_rdaddwc(&pfx
, prefix
, NULL
);
594 rdaddsc(&pfx
, prefixextra
);
595 fprintf(fp
, "%*s%s", indent
, "", pfx
.text
);
596 /* If the prefix is too long, shorten the first line to fit. */
597 e
= extraindent
- strlen(pfx
.text
);
599 firstlinewidth
+= e
; /* this decreases it, since e < 0 */
600 if (firstlinewidth
< 0) {
601 e
= indent
+ extraindent
;
602 firstlinewidth
= width
;
609 e
= indent
+ extraindent
;
611 wrapping
= wrap_para(text
, firstlinewidth
, width
, text_width
);
612 for (p
= wrapping
; p
; p
= p
->next
) {
613 rdstringc t
= { 0, 0, NULL
};
614 text_rdaddwc(&t
, p
->begin
, p
->end
);
615 fprintf(fp
, "%*s%s\n", e
, "", t
.text
);
616 e
= indent
+ extraindent
;
623 static void text_codepara(FILE *fp
, word
*text
, int indent
, int width
) {
624 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
626 text_convert(text
->text
, &c
);
627 if (strlen(c
) > (size_t)width
) {
630 fprintf(fp
, "%*s%s\n", indent
, "", c
);
637 static void text_versionid(FILE *fp
, word
*text
) {
638 rdstringc t
= { 0, 0, NULL
};
640 rdaddc(&t
, '['); /* FIXME: configurability */
641 text_rdaddwc(&t
, text
, NULL
);
642 rdaddc(&t
, ']'); /* FIXME: configurability */
644 fprintf(fp
, "%s\n", t
.text
);