-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathterm.c
1837 lines (1619 loc) · 43.4 KB
/
term.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) Kristaps Dzonsons <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#if HAVE_SYS_QUEUE
# include <sys/queue.h>
#endif
#include <assert.h>
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
#include "lowdown.h"
#include "extern.h"
struct tstack {
const struct lowdown_node *n; /* node in question */
size_t lines; /* times emitted */
};
struct term {
unsigned int opts; /* oflags from lowdown_cfg */
size_t col; /* output column from zero */
ssize_t last_blank; /* line breaks or -1 (start) */
struct tstack *stack; /* stack of nodes */
size_t stackmax; /* size of stack */
size_t stackpos; /* position in stack */
size_t width; /* soft width of content */
size_t hmargin; /* left of content */
size_t hpadding; /* left of content */
size_t vmargin; /* before/after content */
struct lowdown_buf *tmp; /* for temporary allocations */
wchar_t *buf; /* buffer for counting wchar */
size_t bufsz; /* size of buf */
struct lowdown_buf **foots; /* footnotes */
size_t footsz; /* footnotes size */
int footoff; /* don't collect (tables) */
struct lowdown_metaq metaq; /* metadata */
const struct lowdown_node*in_link; /* in an OSC8 hyperlink */
};
/*
* How to style the output on the screen.
*/
struct sty {
int italic; /* italic */
int strike; /* strikethrough */
int bold; /* bold */
int under; /* underline */
size_t bcolour; /* not inherited */
size_t colour; /* not inherited */
int override; /* don't inherit... */
#define OSTY_UNDER 0x01 /* underlining */
#define OSTY_BOLD 0x02 /* bold */
};
/*
* Prefixes to put before each line. This only applies to very specific
* circumstances.
*/
struct pfx {
const char *text;
size_t cols;
};
#include "term.h"
static const struct sty *stys[LOWDOWN__MAX] = {
NULL, /* LOWDOWN_ROOT */
&sty_blockcode, /* LOWDOWN_BLOCKCODE */
NULL, /* LOWDOWN_BLOCKQUOTE */
NULL, /* LOWDOWN_DEFINITION */
NULL, /* LOWDOWN_DEFINITION_TITLE */
NULL, /* LOWDOWN_DEFINITION_DATA */
&sty_header, /* LOWDOWN_HEADER */
&sty_hrule, /* LOWDOWN_HRULE */
NULL, /* LOWDOWN_LIST */
NULL, /* LOWDOWN_LISTITEM */
NULL, /* LOWDOWN_PARAGRAPH */
NULL, /* LOWDOWN_TABLE_BLOCK */
NULL, /* LOWDOWN_TABLE_HEADER */
NULL, /* LOWDOWN_TABLE_BODY */
NULL, /* LOWDOWN_TABLE_ROW */
NULL, /* LOWDOWN_TABLE_CELL */
&sty_blockhtml, /* LOWDOWN_BLOCKHTML */
&sty_autolink, /* LOWDOWN_LINK_AUTO */
&sty_codespan, /* LOWDOWN_CODESPAN */
&sty_d_emph, /* LOWDOWN_DOUBLE_EMPHASIS */
&sty_emph, /* LOWDOWN_EMPHASIS */
&sty_highlight, /* LOWDOWN_HIGHLIGHT */
&sty_img, /* LOWDOWN_IMAGE */
NULL, /* LOWDOWN_LINEBREAK */
&sty_link, /* LOWDOWN_LINK */
&sty_t_emph, /* LOWDOWN_TRIPLE_EMPHASIS */
&sty_strike, /* LOWDOWN_STRIKETHROUGH */
NULL, /* LOWDOWN_SUBSCRIPT */
NULL, /* LOWDOWN_SUPERSCRIPT */
NULL, /* LOWDOWN_FOOTNOTE */
NULL, /* LOWDOWN_MATH_BLOCK */
&sty_rawhtml, /* LOWDOWN_RAW_HTML */
NULL, /* LOWDOWN_ENTITY */
NULL, /* LOWDOWN_NORMAL_TEXT */
NULL, /* LOWDOWN_DOC_HEADER */
NULL, /* LOWDOWN_META */
};
/*
* Whether the style is not empty (i.e., has style attributes).
*/
#define STY_NONEMPTY(_s) \
((_s)->colour || (_s)->bold || (_s)->italic || \
(_s)->under || (_s)->strike || (_s)->bcolour || \
(_s)->override)
/* Forward declaration. */
static int
rndr(struct lowdown_buf *, struct term *, const struct lowdown_node *);
/*
* Get the column width of a multi-byte sequence. The sequence should
* be self-contained, i.e., not straddle multi-byte borders, because the
* calculation for UTF-8 columns is local to this function: a split
* multi-byte sequence will fail to return the correct number of
* printable columns. If the sequence is bad, return the number of raw
* bytes to print. Return <0 on failure (memory), >=0 otherwise with
* the number of printable columns.
*/
static ssize_t
rndr_mbswidth(struct term *term, const char *buf, size_t sz)
{
size_t wsz, csz;
const char *cp;
void *pp;
mbstate_t mbs;
memset(&mbs, 0, sizeof(mbstate_t));
cp = buf;
wsz = mbsnrtowcs(NULL, &cp, sz, 0, &mbs);
if (wsz == (size_t)-1)
return sz;
if (term->bufsz < wsz) {
term->bufsz = wsz;
pp = reallocarray(term->buf, wsz, sizeof(wchar_t));
if (pp == NULL)
return -1;
term->buf = pp;
}
memset(&mbs, 0, sizeof(mbstate_t));
cp = buf;
mbsnrtowcs(term->buf, &cp, sz, wsz, &mbs);
csz = wcswidth(term->buf, wsz);
return csz == (size_t)-1 ? sz : csz;
}
/*
* Determine whether a link URL is relative. Use a simple heuristic to
* accomplish this: a relative URL is one without a schema. Returns
* zero if not a relative link, non-zero if it is.
*/
static int
link_isrelative(const struct lowdown_buf *link)
{
const char *colon;
size_t rem;
/* If there's no colon, it's a relative link (no schema) */
if ((colon = memchr(link->data, ':', link->size)) == NULL)
return 1;
/* If there's a slash before the colon, it's a (rel) path. */
assert(colon > link->data);
rem = colon - link->data;
return memchr(link->data, '/', rem) != NULL;
}
/*
* Copy the buffer into "out", escaping along the width.
* Returns the number of actual printed columns, which in the case of
* multi-byte glyphs, may be less than the given bytes.
* Return <0 on failure (memory), >= 0 otherwise.
*/
static ssize_t
rndr_escape(struct term *term, struct lowdown_buf *out,
const char *buf, size_t sz)
{
size_t i, start = 0, cols = 0;
ssize_t ret;
unsigned char ch;
/* Don't allow control characters through. */
for (i = 0; i < sz; i++) {
ch = (unsigned char)buf[i];
if (ch < 0x80 && iscntrl(ch)) {
ret = rndr_mbswidth (term, buf + start,
i - start);
if (ret < 0)
return -1;
cols += ret;
if (!hbuf_put(out, buf + start, i - start))
return -1;
start = i + 1;
}
}
/* Remaining bytes. */
if (start < sz) {
ret = rndr_mbswidth(term, buf + start, sz - start);
if (ret < 0)
return -1;
cols += ret;
if (!hbuf_put(out, buf + start, sz - start))
return -1;
}
return cols;
}
static void
rndr_free_footnotes(struct term *st)
{
size_t i;
for (i = 0; i < st->footsz; i++)
hbuf_free(st->foots[i]);
free(st->foots);
st->foots = NULL;
st->footsz = 0;
st->footoff = 0;
}
/*
* If there's an active style in "s" or s is NULL), then emit an
* unstyling escape sequence. Return zero on failure (memory), non-zero
* on success.
*/
static int
rndr_buf_unstyle(const struct term *term,
struct lowdown_buf *out, const struct sty *s)
{
if (term->opts & LOWDOWN_TERM_NOANSI)
return 1;
if (s != NULL && !STY_NONEMPTY(s))
return 1;
return HBUF_PUTSL(out, "\033[0m");
}
/*
* Start sequence for a terminal link (only in ANSI mode).
*/
static int
rndr_buf_osc8_open(const struct term *term, struct lowdown_buf *out,
const struct lowdown_node *n)
{
const struct lowdown_buf *uri = NULL;
if (term->opts & LOWDOWN_TERM_NOANSI)
return 1;
if (n->type == LOWDOWN_LINK_AUTO)
uri = &n->rndr_autolink.link;
else if (n->type == LOWDOWN_LINK)
uri = &n->rndr_link.link;
else if (n->type == LOWDOWN_IMAGE)
uri = &n->rndr_image.link;
/*
* Don't output an id for the link. It's trivial to have a
* random per-page value for this identifier (e.g., to
* initialise a random number then append the node identifier),
* but let the terminal handle this.
*/
assert(uri != NULL);
return HBUF_PUTSL(out, "\033]8;;") &&
hbuf_putb(out, uri) &&
HBUF_PUTSL(out, "\033\\");
}
/*
* Close a currently-open link.
*/
static int
rndr_buf_osc8_close(const struct term *term, struct lowdown_buf *out)
{
if (term->opts & LOWDOWN_TERM_NOANSI)
return 1;
/*
* It would be trivial to crawl up our parent chain and either
* switch from the current link or close out the link context
* entirely, but lowdown(5) stipulates that nested links are not
* possible.
*/
return HBUF_PUTSL(out, "\033]8;;\033\\");
}
/*
* Output style "s" into "out" as an ANSI escape. If "s" does not have
* any style information or is NULL, output nothing. Return zero on
* failure (memory), non-zero on success.
*/
static int
rndr_buf_style(const struct term *term,
struct lowdown_buf *out, const struct sty *s)
{
int has = 0;
if (term->opts & LOWDOWN_TERM_NOANSI)
return 1;
if (s == NULL || !STY_NONEMPTY(s))
return 1;
if (!HBUF_PUTSL(out, "\033["))
return 0;
if (s->bold) {
if (!HBUF_PUTSL(out, "1"))
return 0;
has++;
}
if (s->under) {
if (has++ && !HBUF_PUTSL(out, ";"))
return 0;
if (!HBUF_PUTSL(out, "4"))
return 0;
}
if (s->italic) {
if (has++ && !HBUF_PUTSL(out, ";"))
return 0;
if (!HBUF_PUTSL(out, "3"))
return 0;
}
if (s->strike) {
if (has++ && !HBUF_PUTSL(out, ";"))
return 0;
if (!HBUF_PUTSL(out, "9"))
return 0;
}
if (s->bcolour && !(term->opts & LOWDOWN_TERM_NOCOLOUR) &&
((s->bcolour >= 40 && s->bcolour <= 47) ||
(s->bcolour >= 100 && s->bcolour <= 107))) {
if (has++ && !HBUF_PUTSL(out, ";"))
return 0;
if (!hbuf_printf(out, "%zu", s->bcolour))
return 0;
}
if (s->colour && !(term->opts & LOWDOWN_TERM_NOCOLOUR) &&
((s->colour >= 30 && s->colour <= 37) ||
(s->colour >= 90 && s->colour <= 97))) {
if (has++ && !HBUF_PUTSL(out, ";"))
return 0;
if (!hbuf_printf(out, "%zu", s->colour))
return 0;
}
return HBUF_PUTSL(out, "m");
}
/*
* Take the given style "from" and apply it to "to".
* This accumulates styles: unless an override has been set, it adds to
* the existing style in "to" instead of overriding it.
* The one exception is TODO colours, which override each other.
*/
static void
rndr_node_style_apply(struct sty *to, const struct sty *from)
{
if (from->italic)
to->italic = 1;
if (from->strike)
to->strike = 1;
if (from->bold)
to->bold = 1;
else if ((from->override & OSTY_BOLD))
to->bold = 0;
if (from->under)
to->under = 1;
else if ((from->override & OSTY_UNDER))
to->under = 0;
if (from->bcolour)
to->bcolour = from->bcolour;
if (from->colour)
to->colour = from->colour;
}
/*
* Apply the style for only the given node to the current style.
* This *augments* the current style: see rndr_node_style_apply().
* (This does not ascend to the parent node.)
*/
static void
rndr_node_style(struct sty *s, const struct lowdown_node *n)
{
/* The basic node itself. */
if (stys[n->type] != NULL)
rndr_node_style_apply(s, stys[n->type]);
/* Any special node situation that overrides. */
switch (n->type) {
case LOWDOWN_HEADER:
if (n->rndr_header.level > 0)
rndr_node_style_apply(s, &sty_header_n);
else
rndr_node_style_apply(s, &sty_header_1);
break;
default:
/* FIXME: crawl up nested? */
if (n->parent != NULL &&
n->parent->type == LOWDOWN_LINK)
rndr_node_style_apply(s, &sty_linkalt);
break;
}
if (n->chng == LOWDOWN_CHNG_INSERT)
rndr_node_style_apply(s, &sty_chng_ins);
if (n->chng == LOWDOWN_CHNG_DELETE)
rndr_node_style_apply(s, &sty_chng_del);
}
/*
* Bookkeep that we've put "len" characters into the current line.
*/
static void
rndr_buf_advance(struct term *term, size_t len)
{
term->col += len;
if (term->col && term->last_blank != 0)
term->last_blank = 0;
}
/*
* Return non-zero if "n" or any of its ancestors require resetting the
* output line mode, otherwise return zero.
* This applies to both block and inline styles.
*/
static int
rndr_buf_endstyle(const struct lowdown_node *n)
{
struct sty s;
if (n->parent != NULL)
if (rndr_buf_endstyle(n->parent))
return 1;
memset(&s, 0, sizeof(struct sty));
rndr_node_style(&s, n);
return STY_NONEMPTY(&s);
}
/*
* Unsets the current style context given "n" and an optional terminal
* style "osty", if applies. Return zero on failure (memory), non-zero
* on success.
*/
static int
rndr_buf_endwords(struct term *term, struct lowdown_buf *out,
const struct lowdown_node *n, const struct sty *osty)
{
/*
* If an OSC8 hyperlink should be closed, do it now (it doesn't
* matter where this appears in relation to other styling).
*/
if (term->in_link && !rndr_buf_osc8_close(term, out))
return 0;
if (rndr_buf_endstyle(n))
return rndr_buf_unstyle(term, out, NULL);
if (osty != NULL)
return rndr_buf_unstyle(term, out, osty);
return 1;
}
/*
* Like rndr_buf_endwords(), but also terminating the line itself.
* Return zero on failure (memory), non-zero on success.
*/
static int
rndr_buf_endline(struct term *term, struct lowdown_buf *out,
const struct lowdown_node *n, const struct sty *osty)
{
if (!rndr_buf_endwords(term, out, n, osty))
return 0;
/*
* We can legit be at col == 0 if, for example, we're in a
* literal context with a blank line.
* assert(term->col > 0);
* assert(term->last_blank == 0);
*/
term->col = 0;
term->last_blank = 1;
return HBUF_PUTSL(out, "\n");
}
/*
* Return the printed width of the number up to six digits (we're
* probably not going to have more list items than that).
*/
static size_t
rndr_numlen(size_t sz)
{
if (sz > 100000)
return 6;
if (sz > 10000)
return 5;
if (sz > 1000)
return 4;
if (sz > 100)
return 3;
if (sz > 10)
return 2;
return 1;
}
/*
* Output prefixes of the given node in the style further accumulated
* from the parent nodes. "Depth" is set to how deep we are, starting
* at -1 (the root).
* Return zero on failure (memory), non-zero on success.
*/
static int
rndr_buf_startline_prefixes(struct term *term,
struct sty *s, const struct lowdown_node *n,
struct lowdown_buf *out, size_t *depth)
{
struct sty sinner;
const struct pfx *pfx;
size_t i, emit, len;
int pstyle = 0;
enum hlist_fl fl;
if (n->parent != NULL &&
!rndr_buf_startline_prefixes(term, s, n->parent, out, depth))
return 0;
if (n->parent == NULL) {
assert(n->type == LOWDOWN_ROOT);
*depth = -1;
}
/*
* The "sinner" value is temporary for only this function.
* This allows us to set a temporary style mask that only
* applies to the prefix data.
* Otherwise "s" propagates to the subsequent line.
*/
rndr_node_style(s, n);
sinner = *s;
/*
* Look up the current node in the list of node's we're
* servicing so we can get how many times we've output the
* prefix. This is used for (e.g.) lists, where we only output
* the list prefix once. XXX: read backwards for faster perf?
*/
for (i = 0; i <= term->stackpos; i++)
if (term->stack[i].n == n)
break;
/*
* If we can't find the node, then we're in a "faked" context
* like footnotes within a table. Ignore this. XXX: is there a
* non-hacky way for this?
*/
if (i > term->stackpos)
return 1;
emit = term->stack[i].lines++;
/*
* Output any prefixes.
* Any output must have rndr_buf_style() and set pstyle so that
* we close out the style afterward.
*/
switch (n->type) {
case LOWDOWN_BLOCKCODE:
rndr_node_style_apply(&sinner, &sty_bkcd_pfx);
if (!rndr_buf_style(term, out, &sinner))
return 0;
pstyle = 1;
if (!hbuf_puts(out, pfx_bkcd.text))
return 0;
rndr_buf_advance(term, pfx_bkcd.cols);
break;
case LOWDOWN_ROOT:
if (!rndr_buf_style(term, out, &sinner))
return 0;
pstyle = 1;
for (i = 0; i < term->hmargin; i++)
if (!HBUF_PUTSL(out, " "))
return 0;
for (i = 0; i < term->hpadding; i++)
if (!HBUF_PUTSL(out, " "))
return 0;
break;
case LOWDOWN_BLOCKQUOTE:
rndr_node_style_apply(&sinner, &sty_bkqt_pfx);
if (!rndr_buf_style(term, out, &sinner))
return 0;
pstyle = 1;
if (!hbuf_puts(out, pfx_bkqt.text))
return 0;
rndr_buf_advance(term, pfx_bkqt.cols);
break;
case LOWDOWN_DEFINITION_DATA:
rndr_node_style_apply(&sinner, &sty_dli_pfx);
if (!rndr_buf_style(term, out, &sinner))
return 0;
pstyle = 1;
if (emit == 0) {
if (!hbuf_puts(out, pfx_dli_1.text))
return 0;
rndr_buf_advance(term, pfx_dli_1.cols);
} else {
if (!hbuf_puts(out, pfx_dli_n.text))
return 0;
rndr_buf_advance(term, pfx_dli_n.cols);
}
break;
case LOWDOWN_FOOTNOTE:
rndr_node_style_apply(&sinner, &sty_fdef_pfx);
if (!rndr_buf_style(term, out, &sinner))
return 0;
pstyle = 1;
if (emit == 0) {
if (!hbuf_printf(out, "%2zu. ",
term->footsz + 1))
return 0;
len = rndr_numlen(term->footsz + 1);
if (len + 2 > pfx_fdef_1.cols)
len += 2;
else
len = pfx_fdef_1.cols;
rndr_buf_advance(term, len);
} else {
if (!hbuf_puts(out, pfx_fdef_n.text))
return 0;
rndr_buf_advance(term, pfx_fdef_n.cols);
}
break;
case LOWDOWN_HEADER:
if (n->rndr_header.level == 0)
pfx = &pfx_header_1;
else
pfx = &pfx_header_n;
if (!rndr_buf_style(term, out, &sinner))
return 0;
pstyle = 1;
for (i = 0; i < n->rndr_header.level + 1; i++) {
if (!hbuf_puts(out, pfx->text))
return 0;
rndr_buf_advance(term, pfx->cols);
}
if (pfx->cols) {
if (!HBUF_PUTSL(out, " "))
return 0;
rndr_buf_advance(term, 1);
}
break;
case LOWDOWN_LISTITEM:
if (n->parent == NULL ||
n->parent->type == LOWDOWN_DEFINITION_DATA)
break;
/* Don't print list item prefix after first. */
if (emit) {
if (!hbuf_puts(out, pfx_li_n.text))
return 0;
rndr_buf_advance(term, pfx_li_n.cols);
break;
}
/* List item prefix depends upon type. */
fl = n->rndr_list.flags;
rndr_node_style_apply(&sinner, &sty_li_pfx);
if (!rndr_buf_style(term, out, &sinner))
return 0;
pstyle = 1;
if (fl & HLIST_FL_CHECKED)
pfx = &pfx_uli_c1;
else if (fl & HLIST_FL_UNCHECKED)
pfx = &pfx_uli_nc1;
else if (fl & HLIST_FL_UNORDERED)
pfx = &pfx_uli_1;
else
pfx = &pfx_oli_1;
if (pfx == &pfx_oli_1) {
if (!hbuf_printf(out, "%2zu. ",
n->rndr_listitem.num))
return 0;
len = rndr_numlen(n->rndr_listitem.num);
if (len + 2 > pfx->cols)
len += 2;
else
len = pfx->cols;
} else {
if (pfx->text != NULL &&
!hbuf_puts(out, pfx->text))
return 0;
len = pfx->cols;
}
rndr_buf_advance(term, len);
break;
default:
break;
}
if (pstyle && !rndr_buf_unstyle(term, out, &sinner))
return 0;
(*depth)++;
return 1;
}
/*
* Like rndr_buf_startwords(), but at the start of a line. (Unlike
* rndr_buf_endline(), which calls rndr_buf_endwords(), this does not
* call rndr_buf_startwords().) This also outputs all line prefixes of
* the block context. Return zero on failure (memory), non-zero on
* success.
*/
static int
rndr_buf_startline(struct term *term, struct lowdown_buf *out,
const struct lowdown_node *n, const struct sty *osty)
{
struct sty s;
size_t depth = 0;
assert(term->last_blank);
assert(term->col == 0);
memset(&s, 0, sizeof(struct sty));
if (!rndr_buf_startline_prefixes(term, &s, n, out, &depth))
return 0;
/*
* If an OSC8 hyperlink should be printed, do it now (it doesn't
* matter where this appears in relation to other styling).
*/
if (term->in_link != NULL &&
!rndr_buf_osc8_open(term, out, term->in_link))
return 0;
if (osty != NULL)
rndr_node_style_apply(&s, osty);
return rndr_buf_style(term, out, &s);
}
/*
* Output optional number of newlines before or after content.
* Return zero on failure, non-zero on success.
*/
static int
rndr_buf_vspace(struct term *term, struct lowdown_buf *out,
const struct lowdown_node *n, size_t sz)
{
const struct lowdown_node *prev;
if (term->last_blank == -1)
return 1;
prev = n->parent == NULL ? NULL :
TAILQ_PREV(n, lowdown_nodeq, entries);
assert(sz > 0);
while ((size_t)term->last_blank < sz) {
if (term->col || prev == NULL) {
if (!HBUF_PUTSL(out, "\n"))
return 0;
} else {
if (!rndr_buf_startline
(term, out, n->parent, NULL))
return 0;
if (!rndr_buf_endline
(term, out, n->parent, NULL))
return 0;
}
term->last_blank++;
term->col = 0;
}
return 1;
}
/*
* Ascend to the root of the parse tree from rndr_buf_startwords(),
* accumulating styles as we do so.
*/
static void
rndr_buf_startwords_style(const struct lowdown_node *n, struct sty *s)
{
if (n->parent != NULL)
rndr_buf_startwords_style(n->parent, s);
rndr_node_style(s, n);
}
/*
* Accumulate and output the style at the start of one or more words.
* Should *not* be called on the start of a new line, which calls for
* rndr_buf_startline().
* Return zero on failure, non-zero on success.
*/
static int
rndr_buf_startwords(struct term *term, struct lowdown_buf *out,
const struct lowdown_node *n, const struct sty *osty)
{
struct sty s;
/*
* If an OSC8 hyperlink should be printed, do it now (it doesn't
* matter where this appears in relation to other styling).
*/
if (term->in_link != NULL &&
!rndr_buf_osc8_open(term, out, term->in_link))
return 0;
assert(!term->last_blank);
assert(term->col > 0);
memset(&s, 0, sizeof(struct sty));
rndr_buf_startwords_style(n, &s);
if (osty != NULL)
rndr_node_style_apply(&s, osty);
return rndr_buf_style(term, out, &s);
}
/*
* Return zero on failure, non-zero on success.
*/
static int
rndr_buf_literal(struct term *term, struct lowdown_buf *out,
const struct lowdown_node *n, const struct lowdown_buf *in,
const struct sty *osty)
{
size_t i = 0, len;
const char *start;
while (i < in->size) {
start = &in->data[i];
while (i < in->size && in->data[i] != '\n')
i++;
len = &in->data[i] - start;
i++;
if (!rndr_buf_startline(term, out, n, osty))
return 0;
/*
* No need to record the column width here because we're
* going to reset to zero anyway.
*/
if (rndr_escape(term, out, start, len) < 0)
return 0;
rndr_buf_advance(term, len);
if (!rndr_buf_endline(term, out, n, osty))
return 0;
}
return 1;
}
/*
* Emit text in "in" the current line with output "out".
* Use "n" and its ancestry to determine our context.
* Return zero on failure, non-zero on success.
*/
static int
rndr_buf(struct term *term, struct lowdown_buf *out,
const struct lowdown_node *n, const struct lowdown_buf *in,
const struct sty *osty)
{
size_t i = 0, len, cols, nlen;
ssize_t ret;
int needspace, hasspace,
begin = 1, end = 0;
const char *start;
const struct lowdown_node *nn;
for (nn = n; nn != NULL; nn = nn->parent)
if (nn->type == LOWDOWN_BLOCKCODE ||
nn->type == LOWDOWN_BLOCKHTML)
return rndr_buf_literal(term, out, n, in, osty);
while (i < in->size) {
/*
* Whether we need a space (word begins with space) and
* have a space (current printed content ends with one).
*/
needspace = isspace((unsigned char)in->data[i]);
hasspace = out->size > 0 &&
isspace((unsigned char)out->data[out->size - 1]);
/* Skip to next word, then see how long the word is. */
while (i < in->size &&
isspace((unsigned char)in->data[i]))
i++;
start = &in->data[i];
while (i < in->size &&
!isspace((unsigned char)in->data[i]))
i++;
/* Get length and adjusted length (includes space). */
len = &in->data[i] - start;
nlen = len + (needspace ? 1 : 0);
/*
* If we cross our maximum width and are preceded by a
* space, then break.
* (Leaving out the check for a space will cause
* adjacent text or punctuation to have a preceding
* newline.)
* This will also unset the current style.
*/
if ((needspace || hasspace) &&
term->col > 0 &&
term->col + nlen >= term->width) {
if (!rndr_buf_endline(term, out, n, osty))
return 0;
end = 0;
}
/*
* Either emit our new line prefix (only if we have a
* word that will follow!) or, if we need space, emit
* the spacing. In the first case, or if we have
* following text and are starting this node, emit our
* current style.
*/
if (term->last_blank && len) {
if (!rndr_buf_startline(term, out, n, osty))
return 0;
begin = 0;
end = 1;
} else if (!term->last_blank) {
if (begin && len) {
if (!rndr_buf_startwords
(term, out, n, osty))
return 0;
begin = 0;
end = 1;
}
if (needspace) {
if (!HBUF_PUTSL(out, " "))
return 0;
rndr_buf_advance(term, 1);
}
}
/* Emit the word itself. */