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
= dupstr(adv(source
->origkeyword
));
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 paragraph
*text_config_filename(char *filename
)
185 return cmdline_cfg_simple("text-filename", filename
, NULL
);
188 void text_backend(paragraph
*sourceform
, keywordlist
*keywords
,
189 indexdata
*idx
, void *unused
) {
192 word
*prefix
, *body
, *wp
;
196 int nesting
, nestindent
;
197 int indentb
, indenta
;
200 IGNORE(keywords
); /* we don't happen to need this */
201 IGNORE(idx
); /* or this */
203 conf
= text_configure(sourceform
);
206 * Open the output file.
208 fp
= fopen(conf
.filename
, "w");
210 error(err_cantopenw
, conf
.filename
);
215 for (p
= sourceform
; p
; p
= p
->next
)
216 if (p
->type
== para_Title
)
217 text_heading(fp
, NULL
, NULL
, p
->words
,
218 conf
.atitle
, conf
.indent
, conf
.width
);
220 nestindent
= conf
.listindentbefore
+ conf
.listindentafter
;
221 nesting
= (conf
.indent_preambles ?
0 : -conf
.indent
);
223 /* Do the main document */
224 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
231 assert(nesting
>= 0);
235 nesting
+= nestindent
;
238 nesting
-= nestindent
;
239 assert(nesting
>= 0);
243 * Things we ignore because we've already processed them or
244 * aren't going to touch them in this pass.
248 case para_Biblio
: /* only touch BiblioCited */
259 case para_UnnumberedChapter
:
260 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
261 conf
.achapter
, conf
.indent
, conf
.width
);
267 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
268 conf
.asect
[p
->aux
>=conf
.nasect ? conf
.nasect
-1 : p
->aux
],
269 conf
.indent
, conf
.width
);
273 text_rule(fp
, conf
.indent
+ nesting
, conf
.width
- nesting
);
278 case para_DescribedThing
:
279 case para_Description
:
280 case para_BiblioCited
:
282 case para_NumberedList
:
283 if (p
->type
== para_Bullet
) {
284 prefix
= &conf
.bullet
;
286 indentb
= conf
.listindentbefore
;
287 indenta
= conf
.listindentafter
;
288 } else if (p
->type
== para_NumberedList
) {
290 prefixextra
= "."; /* FIXME: configurability */
291 indentb
= conf
.listindentbefore
;
292 indenta
= conf
.listindentafter
;
293 } else if (p
->type
== para_Description
) {
296 indentb
= conf
.listindentbefore
;
297 indenta
= conf
.listindentafter
;
301 indentb
= indenta
= 0;
303 if (p
->type
== para_BiblioCited
) {
304 body
= dup_word_list(p
->kwtext
);
305 for (wp
= body
; wp
->next
; wp
= wp
->next
);
306 wp
->next
= &spaceword
;
307 spaceword
.next
= p
->words
;
308 spaceword
.alt
= NULL
;
309 spaceword
.type
= word_WhiteSpace
;
310 spaceword
.text
= NULL
;
315 text_para(fp
, prefix
, prefixextra
, body
,
316 conf
.indent
+ nesting
+ indentb
, indenta
,
317 conf
.width
- nesting
- indentb
- indenta
);
320 free_word_list(body
);
325 text_codepara(fp
, p
->words
,
326 conf
.indent
+ nesting
+ conf
.indent_code
,
327 conf
.width
- nesting
- 2 * conf
.indent_code
);
331 /* Do the version ID */
332 if (conf
.include_version_id
) {
333 for (p
= sourceform
; p
; p
= p
->next
)
334 if (p
->type
== para_VersionID
)
335 text_versionid(fp
, p
->words
);
343 sfree(conf
.filename
);
347 * Convert a wide string into a string of chars. If `result' is
348 * non-NULL, mallocs the resulting string and stores a pointer to
349 * it in `*result'. If `result' is NULL, merely checks whether all
350 * characters in the string are feasible for the output character
353 * Return is nonzero if all characters are OK. If not all
354 * characters are OK but `result' is non-NULL, a result _will_
355 * still be generated!
357 static int text_convert(wchar_t *s
, char **result
) {
359 * FIXME. Currently this is ISO8859-1 only.
361 int doing
= (result
!= 0);
364 int plen
= 0, psize
= 0;
370 if ((c
>= 32 && c
<= 126) ||
371 (c
>= 160 && c
<= 255)) {
375 /* Char is not OK. */
377 outc
= 0xBF; /* approximate the good old DEC `uh?' */
382 p
= resize(p
, psize
);
388 p
= resize(p
, plen
+1);
395 static void text_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
) {
398 for (; text
&& text
!= end
; text
= text
->next
) switch (text
->type
) {
411 case word_WhiteSpace
:
414 case word_WkCodeSpace
:
418 case word_WkCodeQuote
:
419 assert(text
->type
!= word_CodeQuote
&&
420 text
->type
!= word_WkCodeQuote
);
421 if (towordstyle(text
->type
) == word_Emph
&&
422 (attraux(text
->aux
) == attr_First
||
423 attraux(text
->aux
) == attr_Only
))
424 rdaddc(rs
, '_'); /* FIXME: configurability */
425 else if (towordstyle(text
->type
) == word_Code
&&
426 (attraux(text
->aux
) == attr_First
||
427 attraux(text
->aux
) == attr_Only
))
428 rdaddc(rs
, '`'); /* FIXME: configurability */
429 if (removeattr(text
->type
) == word_Normal
) {
430 if (text_convert(text
->text
, &c
) || !text
->alt
)
433 text_rdaddwc(rs
, text
->alt
, NULL
);
435 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
437 } else if (removeattr(text
->type
) == word_Quote
) {
438 rdaddc(rs
, quoteaux(text
->aux
) == quote_Open ?
'`' : '\'');
439 /* FIXME: configurability */
441 if (towordstyle(text
->type
) == word_Emph
&&
442 (attraux(text
->aux
) == attr_Last
||
443 attraux(text
->aux
) == attr_Only
))
444 rdaddc(rs
, '_'); /* FIXME: configurability */
445 else if (towordstyle(text
->type
) == word_Code
&&
446 (attraux(text
->aux
) == attr_Last
||
447 attraux(text
->aux
) == attr_Only
))
448 rdaddc(rs
, '\''); /* FIXME: configurability */
453 static int text_width(void *, word
*);
455 static int text_width_list(void *ctx
, word
*text
) {
458 w
+= text_width(ctx
, text
);
464 static int text_width(void *ctx
, 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
) || !text
->alt ?
486 ustrlen(text
->text
) :
487 text_width_list(ctx
, 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
, NULL
, 0);
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
, NULL
, 0);
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
);