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
= 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
= L
" ";
75 ret
.asect
[0].underline
= L
'\0';
76 ret
.include_version_id
= TRUE
;
77 ret
.indent_preambles
= FALSE
;
78 ret
.bullet
.text
= 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];
160 ret
.asect
[n
].number_suffix
= p
;
161 } else if (!ustricmp(source
->keyword
, L
"text-title-align")) {
162 ret
.atitle
.align
= utoalign(uadv(source
->keyword
));
163 } else if (!ustricmp(source
->keyword
, L
"text-title-underline")) {
164 ret
.atitle
.underline
= *uadv(source
->keyword
);
165 } else if (!ustricmp(source
->keyword
, L
"text-versionid")) {
166 ret
.include_version_id
= utob(uadv(source
->keyword
));
167 } else if (!ustricmp(source
->keyword
, L
"text-indent-preamble")) {
168 ret
.indent_preambles
= utob(uadv(source
->keyword
));
169 } else if (!ustricmp(source
->keyword
, L
"text-bullet")) {
170 ret
.bullet
.text
= uadv(source
->keyword
);
178 void text_backend(paragraph
*sourceform
, keywordlist
*keywords
,
182 word
*prefix
, *body
, *wp
;
186 int nesting
, nestindent
;
187 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
);
215 /* Do the main document */
216 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
223 assert(nesting
>= 0);
227 nesting
+= nestindent
;
230 nesting
-= nestindent
;
231 assert(nesting
>= 0);
235 * Things we ignore because we've already processed them or
236 * aren't going to touch them in this pass.
240 case para_Biblio
: /* only touch BiblioCited */
251 case para_UnnumberedChapter
:
252 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
253 conf
.achapter
, conf
.indent
, conf
.width
);
259 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
260 conf
.asect
[p
->aux
>=conf
.nasect ? conf
.nasect
-1 : p
->aux
],
261 conf
.indent
, conf
.width
);
265 text_rule(fp
, conf
.indent
+ nesting
, conf
.width
- nesting
);
270 case para_DescribedThing
:
271 case para_Description
:
272 case para_BiblioCited
:
274 case para_NumberedList
:
275 if (p
->type
== para_Bullet
) {
276 prefix
= &conf
.bullet
;
278 indentb
= conf
.listindentbefore
;
279 indenta
= conf
.listindentafter
;
280 } else if (p
->type
== para_NumberedList
) {
282 prefixextra
= "."; /* FIXME: configurability */
283 indentb
= conf
.listindentbefore
;
284 indenta
= conf
.listindentafter
;
285 } else if (p
->type
== para_Description
) {
288 indentb
= conf
.listindentbefore
;
289 indenta
= conf
.listindentafter
;
293 indentb
= indenta
= 0;
295 if (p
->type
== para_BiblioCited
) {
296 body
= dup_word_list(p
->kwtext
);
297 for (wp
= body
; wp
->next
; wp
= wp
->next
);
298 wp
->next
= &spaceword
;
299 spaceword
.next
= p
->words
;
300 spaceword
.alt
= NULL
;
301 spaceword
.type
= word_WhiteSpace
;
302 spaceword
.text
= NULL
;
307 text_para(fp
, prefix
, prefixextra
, body
,
308 conf
.indent
+ nesting
+ indentb
, indenta
,
309 conf
.width
- nesting
- indentb
- indenta
);
312 free_word_list(body
);
317 text_codepara(fp
, p
->words
,
318 conf
.indent
+ nesting
+ conf
.indent_code
,
319 conf
.width
- nesting
- 2 * conf
.indent_code
);
323 /* Do the version ID */
324 if (conf
.include_version_id
) {
325 for (p
= sourceform
; p
; p
= p
->next
)
326 if (p
->type
== para_VersionID
)
327 text_versionid(fp
, p
->words
);
338 * Convert a wide string into a string of chars. If `result' is
339 * non-NULL, mallocs the resulting string and stores a pointer to
340 * it in `*result'. If `result' is NULL, merely checks whether all
341 * characters in the string are feasible for the output character
344 * Return is nonzero if all characters are OK. If not all
345 * characters are OK but `result' is non-NULL, a result _will_
346 * still be generated!
348 static int text_convert(wchar_t *s
, char **result
) {
350 * FIXME. Currently this is ISO8859-1 only.
352 int doing
= (result
!= 0);
355 int plen
= 0, psize
= 0;
361 if ((c
>= 32 && c
<= 126) ||
362 (c
>= 160 && c
<= 255)) {
366 /* Char is not OK. */
368 outc
= 0xBF; /* approximate the good old DEC `uh?' */
373 p
= resize(p
, psize
);
379 p
= resize(p
, plen
+1);
386 static void text_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
) {
389 for (; text
&& text
!= end
; text
= text
->next
) switch (text
->type
) {
402 case word_WhiteSpace
:
405 case word_WkCodeSpace
:
409 case word_WkCodeQuote
:
410 assert(text
->type
!= word_CodeQuote
&&
411 text
->type
!= word_WkCodeQuote
);
412 if (towordstyle(text
->type
) == word_Emph
&&
413 (attraux(text
->aux
) == attr_First
||
414 attraux(text
->aux
) == attr_Only
))
415 rdaddc(rs
, '_'); /* FIXME: configurability */
416 else if (towordstyle(text
->type
) == word_Code
&&
417 (attraux(text
->aux
) == attr_First
||
418 attraux(text
->aux
) == attr_Only
))
419 rdaddc(rs
, '`'); /* FIXME: configurability */
420 if (removeattr(text
->type
) == word_Normal
) {
421 if (text_convert(text
->text
, &c
))
424 text_rdaddwc(rs
, text
->alt
, NULL
);
426 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
428 } else if (removeattr(text
->type
) == word_Quote
) {
429 rdaddc(rs
, quoteaux(text
->aux
) == quote_Open ?
'`' : '\'');
430 /* FIXME: configurability */
432 if (towordstyle(text
->type
) == word_Emph
&&
433 (attraux(text
->aux
) == attr_Last
||
434 attraux(text
->aux
) == attr_Only
))
435 rdaddc(rs
, '_'); /* FIXME: configurability */
436 else if (towordstyle(text
->type
) == word_Code
&&
437 (attraux(text
->aux
) == attr_Last
||
438 attraux(text
->aux
) == attr_Only
))
439 rdaddc(rs
, '\''); /* FIXME: configurability */
444 static int text_width(word
*);
446 static int text_width_list(word
*text
) {
449 w
+= text_width(text
);
455 static int text_width(word
*text
) {
456 switch (text
->type
) {
469 return (((text
->type
== word_Emph
||
470 text
->type
== word_Code
)
471 ?
(attraux(text
->aux
) == attr_Only ?
2 :
472 attraux(text
->aux
) == attr_Always ?
0 : 1)
474 (text_convert(text
->text
, NULL
) ?
475 ustrlen(text
->text
) :
476 text_width_list(text
->alt
)));
478 case word_WhiteSpace
:
481 case word_WkCodeSpace
:
485 case word_WkCodeQuote
:
486 assert(text
->type
!= word_CodeQuote
&&
487 text
->type
!= word_WkCodeQuote
);
488 return (((towordstyle(text
->type
) == word_Emph
||
489 towordstyle(text
->type
) == word_Code
)
490 ?
(attraux(text
->aux
) == attr_Only ?
2 :
491 attraux(text
->aux
) == attr_Always ?
0 : 1)
494 return 0; /* should never happen */
497 static void text_heading(FILE *fp
, word
*tprefix
, word
*nprefix
, word
*text
,
498 alignstruct align
, int indent
, int width
) {
499 rdstringc t
= { 0, 0, NULL
};
501 int firstlinewidth
, wrapwidth
;
502 wrappedline
*wrapping
, *p
;
504 if (align
.just_numbers
&& nprefix
) {
506 text_rdaddwc(&t
, nprefix
, NULL
);
507 if (text_convert(align
.number_suffix
, &c
)) {
511 } else if (!align
.just_numbers
&& tprefix
) {
513 text_rdaddwc(&t
, tprefix
, NULL
);
514 if (text_convert(align
.number_suffix
, &c
)) {
519 margin
= length
= (t
.text ?
strlen(t
.text
) : 0);
521 if (align
.align
== LEFTPLUS
) {
522 margin
= indent
- margin
;
523 if (margin
< 0) margin
= 0;
524 firstlinewidth
= indent
+ width
- margin
- length
;
526 } else if (align
.align
== LEFT
|| align
.align
== CENTRE
) {
528 firstlinewidth
= indent
+ width
- length
;
529 wrapwidth
= indent
+ width
;
532 wrapping
= wrap_para(text
, firstlinewidth
, wrapwidth
, text_width
);
533 for (p
= wrapping
; p
; p
= p
->next
) {
534 text_rdaddwc(&t
, p
->begin
, p
->end
);
535 length
= (t
.text ?
strlen(t
.text
) : 0);
536 if (align
.align
== CENTRE
) {
537 margin
= (indent
+ width
- length
)/2;
538 if (margin
< 0) margin
= 0;
540 fprintf(fp
, "%*s%s\n", margin
, "", t
.text
);
541 if (align
.underline
!= L
'\0') {
544 uw
[0] = align
.underline
; uw
[1] = L
'\0';
545 text_convert(uw
, &u
);
548 fprintf(fp
, "%*s", margin
, "");
553 if (align
.align
== LEFTPLUS
)
566 static void text_rule(FILE *fp
, int indent
, int width
) {
567 while (indent
--) putc(' ', fp
);
568 while (width
--) putc('-', fp
); /* FIXME: configurability! */
573 static void text_para(FILE *fp
, word
*prefix
, char *prefixextra
, word
*text
,
574 int indent
, int extraindent
, int width
) {
575 wrappedline
*wrapping
, *p
;
576 rdstringc pfx
= { 0, 0, NULL
};
578 int firstlinewidth
= width
;
581 text_rdaddwc(&pfx
, prefix
, NULL
);
583 rdaddsc(&pfx
, prefixextra
);
584 fprintf(fp
, "%*s%s", indent
, "", pfx
.text
);
585 /* If the prefix is too long, shorten the first line to fit. */
586 e
= extraindent
- strlen(pfx
.text
);
588 firstlinewidth
+= e
; /* this decreases it, since e < 0 */
589 if (firstlinewidth
< 0) {
590 e
= indent
+ extraindent
;
591 firstlinewidth
= width
;
598 e
= indent
+ extraindent
;
600 wrapping
= wrap_para(text
, firstlinewidth
, width
, text_width
);
601 for (p
= wrapping
; p
; p
= p
->next
) {
602 rdstringc t
= { 0, 0, NULL
};
603 text_rdaddwc(&t
, p
->begin
, p
->end
);
604 fprintf(fp
, "%*s%s\n", e
, "", t
.text
);
605 e
= indent
+ extraindent
;
612 static void text_codepara(FILE *fp
, word
*text
, int indent
, int width
) {
613 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
615 text_convert(text
->text
, &c
);
616 if (strlen(c
) > (size_t)width
) {
619 fprintf(fp
, "%*s%s\n", indent
, "", c
);
626 static void text_versionid(FILE *fp
, word
*text
) {
627 rdstringc t
= { 0, 0, NULL
};
629 rdaddc(&t
, '['); /* FIXME: configurability */
630 text_rdaddwc(&t
, text
, NULL
);
631 rdaddc(&t
, ']'); /* FIXME: configurability */
633 fprintf(fp
, "%s\n", t
.text
);