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
;
28 wchar_t *lquote
, *rquote
, *rule
;
30 wchar_t *listsuffix
, *startemph
, *endemph
;
39 static void text_heading(textfile
*, word
*, word
*, word
*, alignstruct
,
40 int, int, textconfig
*);
41 static void text_rule(textfile
*, int, int, textconfig
*);
42 static void text_para(textfile
*, word
*, wchar_t *, word
*, int, int, int,
44 static void text_codepara(textfile
*, word
*, int, int);
45 static void text_versionid(textfile
*, word
*, textconfig
*);
47 static void text_output(textfile
*, const wchar_t *);
48 static void text_output_many(textfile
*, int, wchar_t);
50 static alignment
utoalign(wchar_t *p
) {
51 if (!ustricmp(p
, L
"centre") || !ustricmp(p
, L
"center"))
53 if (!ustricmp(p
, L
"leftplus"))
58 static textconfig
text_configure(paragraph
*source
) {
66 ret
.bullet
.next
= NULL
;
67 ret
.bullet
.alt
= NULL
;
68 ret
.bullet
.type
= word_Normal
;
69 ret
.atitle
.just_numbers
= FALSE
; /* ignored */
76 ret
.listindentbefore
= 1;
77 ret
.listindentafter
= 3;
79 ret
.atitle
.align
= CENTRE
;
80 ret
.atitle
.underline
= L
"\x2550\0=\0\0";
81 ret
.achapter
.align
= LEFT
;
82 ret
.achapter
.just_numbers
= FALSE
;
83 ret
.achapter
.number_suffix
= L
": ";
84 ret
.achapter
.underline
= L
"\x203E\0-\0\0";
86 ret
.asect
= snewn(ret
.nasect
, alignstruct
);
87 ret
.asect
[0].align
= LEFTPLUS
;
88 ret
.asect
[0].just_numbers
= TRUE
;
89 ret
.asect
[0].number_suffix
= L
" ";
90 ret
.asect
[0].underline
= L
"\0";
91 ret
.include_version_id
= TRUE
;
92 ret
.indent_preambles
= FALSE
;
93 ret
.bullet
.text
= L
"\x2022\0-\0\0";
94 ret
.rule
= L
"\x2500\0-\0\0";
95 ret
.filename
= dupstr("output.txt");
96 ret
.startemph
= L
"_\0_\0\0";
97 ret
.endemph
= uadv(ret
.startemph
);
98 ret
.listsuffix
= L
".";
99 ret
.charset
= CS_ASCII
;
101 * Default quote characters are Unicode matched single quotes,
102 * falling back to the TeXlike `'.
104 ret
.lquote
= L
"\x2018\0\x2019\0`\0'\0\0";
105 ret
.rquote
= uadv(ret
.lquote
);
108 * Two-pass configuration so that we can pick up global config
109 * (e.g. `quotes') before having it overridden by specific
110 * config (`text-quotes'), irrespective of the order in which
113 for (p
= source
; p
; p
= p
->next
) {
114 if (p
->type
== para_Config
) {
115 if (!ustricmp(p
->keyword
, L
"quotes")) {
116 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
117 ret
.lquote
= uadv(p
->keyword
);
118 ret
.rquote
= uadv(ret
.lquote
);
124 for (p
= source
; p
; p
= p
->next
) {
125 if (p
->type
== para_Config
) {
126 if (!ustricmp(p
->keyword
, L
"text-indent")) {
127 ret
.indent
= utoi(uadv(p
->keyword
));
128 } else if (!ustricmp(p
->keyword
, L
"text-charset")) {
129 char *csname
= utoa_dup(uadv(p
->keyword
), CS_ASCII
);
130 ret
.charset
= charset_from_localenc(csname
);
132 } else if (!ustricmp(p
->keyword
, L
"text-filename")) {
134 ret
.filename
= dupstr(adv(p
->origkeyword
));
135 } else if (!ustricmp(p
->keyword
, L
"text-indent-code")) {
136 ret
.indent_code
= utoi(uadv(p
->keyword
));
137 } else if (!ustricmp(p
->keyword
, L
"text-width")) {
138 ret
.width
= utoi(uadv(p
->keyword
));
139 } else if (!ustricmp(p
->keyword
, L
"text-list-indent")) {
140 ret
.listindentbefore
= utoi(uadv(p
->keyword
));
141 } else if (!ustricmp(p
->keyword
, L
"text-listitem-indent")) {
142 ret
.listindentafter
= utoi(uadv(p
->keyword
));
143 } else if (!ustricmp(p
->keyword
, L
"text-chapter-align")) {
144 ret
.achapter
.align
= utoalign(uadv(p
->keyword
));
145 } else if (!ustricmp(p
->keyword
, L
"text-chapter-underline")) {
146 ret
.achapter
.underline
= uadv(p
->keyword
);
147 } else if (!ustricmp(p
->keyword
, L
"text-chapter-numeric")) {
148 ret
.achapter
.just_numbers
= utob(uadv(p
->keyword
));
149 } else if (!ustricmp(p
->keyword
, L
"text-chapter-suffix")) {
150 ret
.achapter
.number_suffix
= uadv(p
->keyword
);
151 } else if (!ustricmp(p
->keyword
, L
"text-section-align")) {
152 wchar_t *q
= uadv(p
->keyword
);
158 if (n
>= ret
.nasect
) {
160 ret
.asect
= sresize(ret
.asect
, n
+1, alignstruct
);
161 for (i
= ret
.nasect
; i
<= n
; i
++)
162 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
165 ret
.asect
[n
].align
= utoalign(q
);
166 } else if (!ustricmp(p
->keyword
, L
"text-section-underline")) {
167 wchar_t *q
= uadv(p
->keyword
);
173 if (n
>= ret
.nasect
) {
175 ret
.asect
= sresize(ret
.asect
, n
+1, alignstruct
);
176 for (i
= ret
.nasect
; i
<= n
; i
++)
177 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
180 ret
.asect
[n
].underline
= q
;
181 } else if (!ustricmp(p
->keyword
, L
"text-section-numeric")) {
182 wchar_t *q
= uadv(p
->keyword
);
188 if (n
>= ret
.nasect
) {
190 ret
.asect
= sresize(ret
.asect
, n
+1, alignstruct
);
191 for (i
= ret
.nasect
; i
<= n
; i
++)
192 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
195 ret
.asect
[n
].just_numbers
= utob(q
);
196 } else if (!ustricmp(p
->keyword
, L
"text-section-suffix")) {
197 wchar_t *q
= uadv(p
->keyword
);
203 if (n
>= ret
.nasect
) {
205 ret
.asect
= sresize(ret
.asect
, n
+1, alignstruct
);
206 for (i
= ret
.nasect
; i
<= n
; i
++) {
207 ret
.asect
[i
] = ret
.asect
[ret
.nasect
-1];
211 ret
.asect
[n
].number_suffix
= q
;
212 } else if (!ustricmp(p
->keyword
, L
"text-title-align")) {
213 ret
.atitle
.align
= utoalign(uadv(p
->keyword
));
214 } else if (!ustricmp(p
->keyword
, L
"text-title-underline")) {
215 ret
.atitle
.underline
= uadv(p
->keyword
);
216 } else if (!ustricmp(p
->keyword
, L
"text-versionid")) {
217 ret
.include_version_id
= utob(uadv(p
->keyword
));
218 } else if (!ustricmp(p
->keyword
, L
"text-indent-preamble")) {
219 ret
.indent_preambles
= utob(uadv(p
->keyword
));
220 } else if (!ustricmp(p
->keyword
, L
"text-bullet")) {
221 ret
.bullet
.text
= uadv(p
->keyword
);
222 } else if (!ustricmp(p
->keyword
, L
"text-rule")) {
223 ret
.rule
= uadv(p
->keyword
);
224 } else if (!ustricmp(p
->keyword
, L
"text-list-suffix")) {
225 ret
.listsuffix
= uadv(p
->keyword
);
226 } else if (!ustricmp(p
->keyword
, L
"text-emphasis")) {
227 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
228 ret
.startemph
= uadv(p
->keyword
);
229 ret
.endemph
= uadv(ret
.startemph
);
231 } else if (!ustricmp(p
->keyword
, L
"text-quotes")) {
232 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
233 ret
.lquote
= uadv(p
->keyword
);
234 ret
.rquote
= uadv(ret
.lquote
);
241 * Now process fallbacks on quote characters, underlines, the
242 * rule character, the emphasis characters, and bullets.
244 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
)) &&
245 (!cvt_ok(ret
.charset
, ret
.lquote
) ||
246 !cvt_ok(ret
.charset
, ret
.rquote
))) {
247 ret
.lquote
= uadv(ret
.rquote
);
248 ret
.rquote
= uadv(ret
.lquote
);
251 while (*uadv(ret
.endemph
) && *uadv(uadv(ret
.endemph
)) &&
252 (!cvt_ok(ret
.charset
, ret
.startemph
) ||
253 !cvt_ok(ret
.charset
, ret
.endemph
))) {
254 ret
.startemph
= uadv(ret
.endemph
);
255 ret
.endemph
= uadv(ret
.startemph
);
258 while (*ret
.atitle
.underline
&& *uadv(ret
.atitle
.underline
) &&
259 !cvt_ok(ret
.charset
, ret
.atitle
.underline
))
260 ret
.atitle
.underline
= uadv(ret
.atitle
.underline
);
262 while (*ret
.achapter
.underline
&& *uadv(ret
.achapter
.underline
) &&
263 !cvt_ok(ret
.charset
, ret
.achapter
.underline
))
264 ret
.achapter
.underline
= uadv(ret
.achapter
.underline
);
266 for (n
= 0; n
< ret
.nasect
; n
++) {
267 while (*ret
.asect
[n
].underline
&& *uadv(ret
.asect
[n
].underline
) &&
268 !cvt_ok(ret
.charset
, ret
.asect
[n
].underline
))
269 ret
.asect
[n
].underline
= uadv(ret
.asect
[n
].underline
);
272 while (*ret
.bullet
.text
&& *uadv(ret
.bullet
.text
) &&
273 !cvt_ok(ret
.charset
, ret
.bullet
.text
))
274 ret
.bullet
.text
= uadv(ret
.bullet
.text
);
276 while (*ret
.rule
&& *uadv(ret
.rule
) &&
277 !cvt_ok(ret
.charset
, ret
.rule
))
278 ret
.rule
= uadv(ret
.rule
);
283 paragraph
*text_config_filename(char *filename
)
285 return cmdline_cfg_simple("text-filename", filename
, NULL
);
288 void text_backend(paragraph
*sourceform
, keywordlist
*keywords
,
289 indexdata
*idx
, void *unused
) {
292 word
*prefix
, *body
, *wp
;
295 wchar_t *prefixextra
;
296 int nesting
, nestindent
;
297 int indentb
, indenta
;
300 IGNORE(keywords
); /* we don't happen to need this */
301 IGNORE(idx
); /* or this */
303 conf
= text_configure(sourceform
);
306 * Open the output file.
308 tf
.fp
= fopen(conf
.filename
, "w");
310 error(err_cantopenw
, conf
.filename
);
313 tf
.charset
= conf
.charset
;
314 tf
.state
= charset_init_state
;
317 for (p
= sourceform
; p
; p
= p
->next
)
318 if (p
->type
== para_Title
)
319 text_heading(&tf
, NULL
, NULL
, p
->words
,
320 conf
.atitle
, conf
.indent
, conf
.width
, &conf
);
322 nestindent
= conf
.listindentbefore
+ conf
.listindentafter
;
323 nesting
= (conf
.indent_preambles ?
0 : -conf
.indent
);
325 /* Do the main document */
326 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
333 assert(nesting
>= 0);
337 nesting
+= nestindent
;
340 nesting
-= nestindent
;
341 assert(nesting
>= 0);
345 * Things we ignore because we've already processed them or
346 * aren't going to touch them in this pass.
350 case para_Biblio
: /* only touch BiblioCited */
361 case para_UnnumberedChapter
:
362 text_heading(&tf
, p
->kwtext
, p
->kwtext2
, p
->words
,
363 conf
.achapter
, conf
.indent
, conf
.width
, &conf
);
369 text_heading(&tf
, p
->kwtext
, p
->kwtext2
, p
->words
,
370 conf
.asect
[p
->aux
>=conf
.nasect ? conf
.nasect
-1 : p
->aux
],
371 conf
.indent
, conf
.width
, &conf
);
375 text_rule(&tf
, conf
.indent
+ nesting
, conf
.width
- nesting
, &conf
);
380 case para_DescribedThing
:
381 case para_Description
:
382 case para_BiblioCited
:
384 case para_NumberedList
:
385 if (p
->type
== para_Bullet
) {
386 prefix
= &conf
.bullet
;
388 indentb
= conf
.listindentbefore
;
389 indenta
= conf
.listindentafter
;
390 } else if (p
->type
== para_NumberedList
) {
392 prefixextra
= conf
.listsuffix
;
393 indentb
= conf
.listindentbefore
;
394 indenta
= conf
.listindentafter
;
395 } else if (p
->type
== para_Description
) {
398 indentb
= conf
.listindentbefore
;
399 indenta
= conf
.listindentafter
;
403 indentb
= indenta
= 0;
405 if (p
->type
== para_BiblioCited
) {
406 body
= dup_word_list(p
->kwtext
);
407 for (wp
= body
; wp
->next
; wp
= wp
->next
);
408 wp
->next
= &spaceword
;
409 spaceword
.next
= p
->words
;
410 spaceword
.alt
= NULL
;
411 spaceword
.type
= word_WhiteSpace
;
412 spaceword
.text
= NULL
;
417 text_para(&tf
, prefix
, prefixextra
, body
,
418 conf
.indent
+ nesting
+ indentb
, indenta
,
419 conf
.width
- nesting
- indentb
- indenta
, &conf
);
422 free_word_list(body
);
427 text_codepara(&tf
, p
->words
,
428 conf
.indent
+ nesting
+ conf
.indent_code
,
429 conf
.width
- nesting
- 2 * conf
.indent_code
);
433 /* Do the version ID */
434 if (conf
.include_version_id
) {
435 for (p
= sourceform
; p
; p
= p
->next
)
436 if (p
->type
== para_VersionID
)
437 text_versionid(&tf
, p
->words
, &conf
);
443 text_output(&tf
, NULL
); /* end charset conversion */
446 sfree(conf
.filename
);
449 static void text_output(textfile
*tf
, const wchar_t *s
)
464 ret
= charset_from_unicode(sp
, &len
, buf
, lenof(buf
),
465 tf
->charset
, &tf
->state
, NULL
);
468 fwrite(buf
, 1, ret
, tf
->fp
);
472 static void text_output_many(textfile
*tf
, int n
, wchar_t c
)
481 static void text_rdaddw(rdstring
*rs
, word
*text
, word
*end
, textconfig
*cfg
) {
482 for (; text
&& text
!= end
; text
= text
->next
) switch (text
->type
) {
495 case word_WhiteSpace
:
498 case word_WkCodeSpace
:
502 case word_WkCodeQuote
:
503 assert(text
->type
!= word_CodeQuote
&&
504 text
->type
!= word_WkCodeQuote
);
505 if (towordstyle(text
->type
) == word_Emph
&&
506 (attraux(text
->aux
) == attr_First
||
507 attraux(text
->aux
) == attr_Only
))
508 rdadds(rs
, cfg
->startemph
);
509 else if (towordstyle(text
->type
) == word_Code
&&
510 (attraux(text
->aux
) == attr_First
||
511 attraux(text
->aux
) == attr_Only
))
512 rdadds(rs
, cfg
->lquote
);
513 if (removeattr(text
->type
) == word_Normal
) {
514 if (cvt_ok(cfg
->charset
, text
->text
) || !text
->alt
)
515 rdadds(rs
, text
->text
);
517 text_rdaddw(rs
, text
->alt
, NULL
, cfg
);
518 } else if (removeattr(text
->type
) == word_WhiteSpace
) {
520 } else if (removeattr(text
->type
) == word_Quote
) {
521 rdadds(rs
, quoteaux(text
->aux
) == quote_Open ?
522 cfg
->lquote
: cfg
->rquote
);
524 if (towordstyle(text
->type
) == word_Emph
&&
525 (attraux(text
->aux
) == attr_Last
||
526 attraux(text
->aux
) == attr_Only
))
527 rdadds(rs
, cfg
->endemph
);
528 else if (towordstyle(text
->type
) == word_Code
&&
529 (attraux(text
->aux
) == attr_Last
||
530 attraux(text
->aux
) == attr_Only
))
531 rdadds(rs
, cfg
->rquote
);
536 static int text_width(void *, word
*);
538 static int text_width_list(void *ctx
, word
*text
) {
541 w
+= text_width(ctx
, text
);
547 static int text_width(void *ctx
, word
*text
) {
548 textconfig
*cfg
= (textconfig
*)ctx
;
552 switch (text
->type
) {
562 assert(text
->type
< word_internal_endattrs
);
565 attr
= towordstyle(text
->type
);
566 if (attr
== word_Emph
|| attr
== word_Code
) {
567 if (attraux(text
->aux
) == attr_Only
||
568 attraux(text
->aux
) == attr_First
)
569 wid
+= ustrwid(attr
== word_Emph ? cfg
->startemph
: cfg
->lquote
,
572 if (attr
== word_Emph
|| attr
== word_Code
) {
573 if (attraux(text
->aux
) == attr_Only
||
574 attraux(text
->aux
) == attr_Last
)
575 wid
+= ustrwid(attr
== word_Emph ? cfg
->startemph
: cfg
->lquote
,
579 switch (text
->type
) {
584 if (cvt_ok(cfg
->charset
, text
->text
) || !text
->alt
)
585 wid
+= ustrwid(text
->text
, cfg
->charset
);
587 wid
+= text_width_list(ctx
, text
->alt
);
590 case word_WhiteSpace
:
593 case word_WkCodeSpace
:
597 case word_WkCodeQuote
:
598 assert(text
->type
!= word_CodeQuote
&&
599 text
->type
!= word_WkCodeQuote
);
600 if (removeattr(text
->type
) == word_Quote
) {
601 if (quoteaux(text
->aux
) == quote_Open
)
602 wid
+= ustrwid(cfg
->lquote
, cfg
->charset
);
604 wid
+= ustrwid(cfg
->rquote
, cfg
->charset
);
612 static void text_heading(textfile
*tf
, word
*tprefix
, word
*nprefix
,
613 word
*text
, alignstruct align
,
614 int indent
, int width
, textconfig
*cfg
) {
615 rdstring t
= { 0, 0, NULL
};
617 int firstlinewidth
, wrapwidth
;
618 wrappedline
*wrapping
, *p
;
620 if (align
.just_numbers
&& nprefix
) {
621 text_rdaddw(&t
, nprefix
, NULL
, cfg
);
622 rdadds(&t
, align
.number_suffix
);
623 } else if (!align
.just_numbers
&& tprefix
) {
624 text_rdaddw(&t
, tprefix
, NULL
, cfg
);
625 rdadds(&t
, align
.number_suffix
);
627 margin
= length
= ustrwid(t
.text ? t
.text
: L
"", cfg
->charset
);
629 if (align
.align
== LEFTPLUS
) {
630 margin
= indent
- margin
;
631 if (margin
< 0) margin
= 0;
632 firstlinewidth
= indent
+ width
- margin
- length
;
634 } else if (align
.align
== LEFT
|| align
.align
== CENTRE
) {
636 firstlinewidth
= indent
+ width
- length
;
637 wrapwidth
= indent
+ width
;
640 wrapping
= wrap_para(text
, firstlinewidth
, wrapwidth
,
642 for (p
= wrapping
; p
; p
= p
->next
) {
643 text_rdaddw(&t
, p
->begin
, p
->end
, cfg
);
644 length
= ustrwid(t
.text ? t
.text
: L
"", cfg
->charset
);
645 if (align
.align
== CENTRE
) {
646 margin
= (indent
+ width
- length
)/2;
647 if (margin
< 0) margin
= 0;
649 text_output_many(tf
, margin
, L
' ');
650 text_output(tf
, t
.text
);
651 text_output(tf
, L
"\n");
652 if (*align
.underline
) {
653 text_output_many(tf
, margin
, L
' ');
655 text_output(tf
, align
.underline
);
656 length
-= ustrwid(align
.underline
, cfg
->charset
);
658 text_output(tf
, L
"\n");
660 if (align
.align
== LEFTPLUS
)
668 text_output(tf
, L
"\n");
673 static void text_rule(textfile
*tf
, int indent
, int width
, textconfig
*cfg
) {
674 text_output_many(tf
, indent
, L
' ');
676 text_output(tf
, cfg
->rule
);
677 width
-= ustrwid(cfg
->rule
, cfg
->charset
);
679 text_output_many(tf
, 2, L
'\n');
682 static void text_para(textfile
*tf
, word
*prefix
, wchar_t *prefixextra
,
683 word
*text
, int indent
, int extraindent
, int width
,
685 wrappedline
*wrapping
, *p
;
686 rdstring pfx
= { 0, 0, NULL
};
688 int firstlinewidth
= width
;
691 text_rdaddw(&pfx
, prefix
, NULL
, cfg
);
693 rdadds(&pfx
, prefixextra
);
694 text_output_many(tf
, indent
, L
' ');
695 text_output(tf
, pfx
.text
);
696 /* If the prefix is too long, shorten the first line to fit. */
697 e
= extraindent
- ustrwid(pfx
.text ? pfx
.text
: L
"", cfg
->charset
);
699 firstlinewidth
+= e
; /* this decreases it, since e < 0 */
700 if (firstlinewidth
< 0) {
701 e
= indent
+ extraindent
;
702 firstlinewidth
= width
;
703 text_output(tf
, L
"\n");
709 e
= indent
+ extraindent
;
711 wrapping
= wrap_para(text
, firstlinewidth
, width
,
713 for (p
= wrapping
; p
; p
= p
->next
) {
714 rdstring t
= { 0, 0, NULL
};
715 text_rdaddw(&t
, p
->begin
, p
->end
, cfg
);
716 text_output_many(tf
, e
, L
' ');
717 text_output(tf
, t
.text
);
718 text_output(tf
, L
"\n");
719 e
= indent
+ extraindent
;
723 text_output(tf
, L
"\n");
726 static void text_codepara(textfile
*tf
, word
*text
, int indent
, int width
) {
727 for (; text
; text
= text
->next
) if (text
->type
== word_WeakCode
) {
728 int wid
= ustrwid(text
->text
, tf
->charset
);
730 error(err_text_codeline
, &text
->fpos
, wid
, width
);
731 text_output_many(tf
, indent
, L
' ');
732 text_output(tf
, text
->text
);
733 text_output(tf
, L
"\n");
736 text_output(tf
, L
"\n");
739 static void text_versionid(textfile
*tf
, word
*text
, textconfig
*cfg
) {
740 rdstring t
= { 0, 0, NULL
};
743 text_rdaddw(&t
, text
, NULL
, cfg
);
747 text_output(tf
, t
.text
);