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 paragraph
*text_config_filename(char *filename
)
186 wchar_t *ufilename
, *up
;
189 p
= mknew(paragraph
);
190 memset(p
, 0, sizeof(*p
));
191 p
->type
= para_Config
;
193 p
->fpos
.filename
= "<command line>";
194 p
->fpos
.line
= p
->fpos
.col
= -1;
196 ufilename
= ufroma_dup(filename
);
197 len
= ustrlen(ufilename
) + 2 + lenof(L
"text-filename");
198 p
->keyword
= mknewa(wchar_t, len
);
200 ustrcpy(up
, L
"text-filename");
202 ustrcpy(up
, ufilename
);
205 assert(up
- p
->keyword
< len
);
211 void text_backend(paragraph
*sourceform
, keywordlist
*keywords
,
215 word
*prefix
, *body
, *wp
;
219 int nesting
, nestindent
;
220 int indentb
, indenta
;
222 IGNORE(keywords
); /* we don't happen to need this */
223 IGNORE(idx
); /* or this */
225 conf
= text_configure(sourceform
);
228 * Open the output file.
230 fp
= fopen(conf
.filename
, "w");
232 error(err_cantopenw
, conf
.filename
);
237 for (p
= sourceform
; p
; p
= p
->next
)
238 if (p
->type
== para_Title
)
239 text_heading(fp
, NULL
, NULL
, p
->words
,
240 conf
.atitle
, conf
.indent
, conf
.width
);
242 nestindent
= conf
.listindentbefore
+ conf
.listindentafter
;
243 nesting
= (conf
.indent_preambles ?
0 : -conf
.indent
);
245 /* Do the main document */
246 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
253 assert(nesting
>= 0);
257 nesting
+= nestindent
;
260 nesting
-= nestindent
;
261 assert(nesting
>= 0);
265 * Things we ignore because we've already processed them or
266 * aren't going to touch them in this pass.
270 case para_Biblio
: /* only touch BiblioCited */
281 case para_UnnumberedChapter
:
282 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
283 conf
.achapter
, conf
.indent
, conf
.width
);
289 text_heading(fp
, p
->kwtext
, p
->kwtext2
, p
->words
,
290 conf
.asect
[p
->aux
>=conf
.nasect ? conf
.nasect
-1 : p
->aux
],
291 conf
.indent
, conf
.width
);
295 text_rule(fp
, conf
.indent
+ nesting
, conf
.width
- nesting
);
300 case para_DescribedThing
:
301 case para_Description
:
302 case para_BiblioCited
:
304 case para_NumberedList
:
305 if (p
->type
== para_Bullet
) {
306 prefix
= &conf
.bullet
;
308 indentb
= conf
.listindentbefore
;
309 indenta
= conf
.listindentafter
;
310 } else if (p
->type
== para_NumberedList
) {
312 prefixextra
= "."; /* FIXME: configurability */
313 indentb
= conf
.listindentbefore
;
314 indenta
= conf
.listindentafter
;
315 } else if (p
->type
== para_Description
) {
318 indentb
= conf
.listindentbefore
;
319 indenta
= conf
.listindentafter
;
323 indentb
= indenta
= 0;
325 if (p
->type
== para_BiblioCited
) {
326 body
= dup_word_list(p
->kwtext
);
327 for (wp
= body
; wp
->next
; wp
= wp
->next
);
328 wp
->next
= &spaceword
;
329 spaceword
.next
= p
->words
;
330 spaceword
.alt
= NULL
;
331 spaceword
.type
= word_WhiteSpace
;
332 spaceword
.text
= NULL
;
337 text_para(fp
, prefix
, prefixextra
, body
,
338 conf
.indent
+ nesting
+ indentb
, indenta
,
339 conf
.width
- nesting
- indentb
- indenta
);
342 free_word_list(body
);
347 text_codepara(fp
, p
->words
,
348 conf
.indent
+ nesting
+ conf
.indent_code
,
349 conf
.width
- nesting
- 2 * conf
.indent_code
);
353 /* Do the version ID */
354 if (conf
.include_version_id
) {
355 for (p
= sourceform
; p
; p
= p
->next
)
356 if (p
->type
== para_VersionID
)
357 text_versionid(fp
, p
->words
);
365 sfree(conf
.filename
);
369 * Convert a wide string into a string of chars. If `result' is
370 * non-NULL, mallocs the resulting string and stores a pointer to
371 * it in `*result'. If `result' is NULL, merely checks whether all
372 * characters in the string are feasible for the output character
375 * Return is nonzero if all characters are OK. If not all
376 * characters are OK but `result' is non-NULL, a result _will_
377 * still be generated!
379 static int text_convert(wchar_t *s
, char **result
) {
381 * FIXME. Currently this is ISO8859-1 only.
383 int doing
= (result
!= 0);
386 int plen
= 0, psize
= 0;
392 if ((c
>= 32 && c
<= 126) ||
393 (c
>= 160 && c
<= 255)) {
397 /* Char is not OK. */
399 outc
= 0xBF; /* approximate the good old DEC `uh?' */
404 p
= resize(p
, psize
);
410 p
= resize(p
, plen
+1);
417 static void text_rdaddwc(rdstringc
*rs
, word
*text
, word
*end
) {
420 for (; text
&& text
!= end
; text
= text
->next
) switch (text
->type
) {
433 case word_WhiteSpace
:
436 case word_WkCodeSpace
:
440 case word_WkCodeQuote
:
441 assert(text
->type
!= word_CodeQuote
&&
442 text
->type
!= word_WkCodeQuote
);
443 if (towordstyle(text
->type
) == word_Emph
&&
444 (attraux(text
->aux
) == attr_First
||
445 attraux(text
->aux
) == attr_Only
))
446 rdaddc(rs
, '_'); /* FIXME: configurability */
447 else if (towordstyle(text
->type
) == word_Code
&&
448 (attraux(text
->aux
) == attr_First
||
449 attraux(text
->aux
) == attr_Only
))
450 rdaddc(rs
, '`'); /* FIXME: configurability */
451 if (removeattr(text
->type
) == word_Normal
) {
452 if (text_convert(text
->text
, &c
))
455 text_rdaddwc(rs
, text
->alt
, NULL
);
457 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
459 } else if (removeattr(text
->type
) == word_Quote
) {
460 rdaddc(rs
, quoteaux(text
->aux
) == quote_Open ?
'`' : '\'');
461 /* FIXME: configurability */
463 if (towordstyle(text
->type
) == word_Emph
&&
464 (attraux(text
->aux
) == attr_Last
||
465 attraux(text
->aux
) == attr_Only
))
466 rdaddc(rs
, '_'); /* FIXME: configurability */
467 else if (towordstyle(text
->type
) == word_Code
&&
468 (attraux(text
->aux
) == attr_Last
||
469 attraux(text
->aux
) == attr_Only
))
470 rdaddc(rs
, '\''); /* FIXME: configurability */
475 static int text_width(word
*);
477 static int text_width_list(word
*text
) {
480 w
+= text_width(text
);
486 static int text_width(word
*text
) {
487 switch (text
->type
) {
500 return (((text
->type
== word_Emph
||
501 text
->type
== word_Code
)
502 ?
(attraux(text
->aux
) == attr_Only ?
2 :
503 attraux(text
->aux
) == attr_Always ?
0 : 1)
505 (text_convert(text
->text
, NULL
) ?
506 ustrlen(text
->text
) :
507 text_width_list(text
->alt
)));
509 case word_WhiteSpace
:
512 case word_WkCodeSpace
:
516 case word_WkCodeQuote
:
517 assert(text
->type
!= word_CodeQuote
&&
518 text
->type
!= word_WkCodeQuote
);
519 return (((towordstyle(text
->type
) == word_Emph
||
520 towordstyle(text
->type
) == word_Code
)
521 ?
(attraux(text
->aux
) == attr_Only ?
2 :
522 attraux(text
->aux
) == attr_Always ?
0 : 1)
525 return 0; /* should never happen */
528 static void text_heading(FILE *fp
, word
*tprefix
, word
*nprefix
, word
*text
,
529 alignstruct align
, int indent
, int width
) {
530 rdstringc t
= { 0, 0, NULL
};
532 int firstlinewidth
, wrapwidth
;
533 wrappedline
*wrapping
, *p
;
535 if (align
.just_numbers
&& nprefix
) {
537 text_rdaddwc(&t
, nprefix
, NULL
);
538 if (text_convert(align
.number_suffix
, &c
)) {
542 } else if (!align
.just_numbers
&& tprefix
) {
544 text_rdaddwc(&t
, tprefix
, NULL
);
545 if (text_convert(align
.number_suffix
, &c
)) {
550 margin
= length
= (t
.text ?
strlen(t
.text
) : 0);
552 if (align
.align
== LEFTPLUS
) {
553 margin
= indent
- margin
;
554 if (margin
< 0) margin
= 0;
555 firstlinewidth
= indent
+ width
- margin
- length
;
557 } else if (align
.align
== LEFT
|| align
.align
== CENTRE
) {
559 firstlinewidth
= indent
+ width
- length
;
560 wrapwidth
= indent
+ width
;
563 wrapping
= wrap_para(text
, firstlinewidth
, wrapwidth
, text_width
);
564 for (p
= wrapping
; p
; p
= p
->next
) {
565 text_rdaddwc(&t
, p
->begin
, p
->end
);
566 length
= (t
.text ?
strlen(t
.text
) : 0);
567 if (align
.align
== CENTRE
) {
568 margin
= (indent
+ width
- length
)/2;
569 if (margin
< 0) margin
= 0;
571 fprintf(fp
, "%*s%s\n", margin
, "", t
.text
);
572 if (align
.underline
!= L
'\0') {
575 uw
[0] = align
.underline
; uw
[1] = L
'\0';
576 text_convert(uw
, &u
);
579 fprintf(fp
, "%*s", margin
, "");
584 if (align
.align
== LEFTPLUS
)
597 static void text_rule(FILE *fp
, int indent
, int width
) {
598 while (indent
--) putc(' ', fp
);
599 while (width
--) putc('-', fp
); /* FIXME: configurability! */
604 static void text_para(FILE *fp
, word
*prefix
, char *prefixextra
, word
*text
,
605 int indent
, int extraindent
, int width
) {
606 wrappedline
*wrapping
, *p
;
607 rdstringc pfx
= { 0, 0, NULL
};
609 int firstlinewidth
= width
;
612 text_rdaddwc(&pfx
, prefix
, NULL
);
614 rdaddsc(&pfx
, prefixextra
);
615 fprintf(fp
, "%*s%s", indent
, "", pfx
.text
);
616 /* If the prefix is too long, shorten the first line to fit. */
617 e
= extraindent
- strlen(pfx
.text
);
619 firstlinewidth
+= e
; /* this decreases it, since e < 0 */
620 if (firstlinewidth
< 0) {
621 e
= indent
+ extraindent
;
622 firstlinewidth
= width
;
629 e
= indent
+ extraindent
;
631 wrapping
= wrap_para(text
, firstlinewidth
, width
, text_width
);
632 for (p
= wrapping
; p
; p
= p
->next
) {
633 rdstringc t
= { 0, 0, NULL
};
634 text_rdaddwc(&t
, p
->begin
, p
->end
);
635 fprintf(fp
, "%*s%s\n", e
, "", t
.text
);
636 e
= indent
+ extraindent
;
643 static void text_codepara(FILE *fp
, word
*text
, int indent
, int width
) {
644 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
646 text_convert(text
->text
, &c
);
647 if (strlen(c
) > (size_t)width
) {
650 fprintf(fp
, "%*s%s\n", indent
, "", c
);
657 static void text_versionid(FILE *fp
, word
*text
) {
658 rdstringc t
= { 0, 0, NULL
};
660 rdaddc(&t
, '['); /* FIXME: configurability */
661 text_rdaddwc(&t
, text
, NULL
);
662 rdaddc(&t
, ']'); /* FIXME: configurability */
664 fprintf(fp
, "%s\n", t
.text
);