-
Notifications
You must be signed in to change notification settings - Fork 35
/
gemini.c
1050 lines (954 loc) · 24.4 KB
/
gemini.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 <kristaps@bsd.lv>
*
* 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"
/*
* A standalone link is one that lives in its own paragraph.
*/
#define IS_STANDALONE_LINK(_n, _prev) \
((_n)->parent != NULL && \
(_n)->parent->type == LOWDOWN_PARAGRAPH && \
(_n)->parent->parent != NULL && \
(_n)->parent->parent->type == LOWDOWN_ROOT && \
(_prev) == NULL && \
TAILQ_NEXT((_n), entries) == NULL)
/*
* A link queued for display.
* This only happens when using footnote or endnote links.
*/
struct link {
const struct lowdown_node *n; /* node needing link */
size_t id; /* link-%zu */
TAILQ_ENTRY(link) entries;
};
TAILQ_HEAD(linkq, link);
struct gemini {
unsigned int flags; /* output flags */
ssize_t last_blank; /* line breaks or -1 (start) */
struct lowdown_buf *tmp; /* for temporary allocations */
size_t nolinkqsz; /* if >0, don't record links */
int nolinkflush; /* if TRUE, don't flush links */
struct linkq linkq; /* link queue */
size_t linkqsz; /* position in link queue */
wchar_t *buf; /* buffer for counting wchar */
size_t bufsz; /* size of buf */
ssize_t headers_offs; /* header offset */
struct lowdown_buf **foots; /* footnotes */
size_t footsz; /* footnotes size */
const char *templ; /* output template */
};
/*
* Forward declaration.
*/
static int
rndr(struct lowdown_buf *, struct lowdown_metaq *,
struct gemini *, const struct lowdown_node *);
static void
link_freeq(struct linkq *q)
{
struct link *l;
while ((l = TAILQ_FIRST(q)) != NULL) {
TAILQ_REMOVE(q, l, entries);
free(l);
}
}
static int
rndr_link_ref(const struct gemini *st,
struct lowdown_buf *out, size_t ref, int nl)
{
char buf[32], c;
size_t sz = 0, i;
assert(ref);
if (st->flags & LOWDOWN_GEMINI_LINK_NOREF)
return hbuf_printf(out, "%s", nl ? "\n" : "");
buf[0] = '\0';
if (st->flags & LOWDOWN_GEMINI_LINK_ROMAN) {
while(ref)
if (ref >= 1000) {
strlcat(buf, "m", sizeof(buf));
ref -= 1000;
} else if (ref >= 900) {
strlcat(buf, "cm", sizeof(buf));
ref -= 900;
} else if (ref >= 500) {
strlcat(buf, "d", sizeof(buf));
ref -= 500;
} else if (ref >= 400) {
strlcat(buf, "cd", sizeof(buf));
ref -= 400;
} else if (ref >= 100) {
strlcat(buf, "c", sizeof(buf));
ref -= 100;
} else if (ref >= 90) {
strlcat(buf, "xc", sizeof(buf));
ref -= 90;
} else if (ref >= 50) {
strlcat(buf, "l", sizeof(buf));
ref -= 50;
} else if (ref >= 40) {
strlcat(buf, "xl", sizeof(buf));
ref -= 40;
} else if (ref >= 10) {
strlcat(buf, "x", sizeof(buf));
ref -= 10;
} else if (ref >= 9) {
strlcat(buf, "ix", sizeof(buf));
ref -= 9;
} else if (ref >= 5) {
strlcat(buf, "v", sizeof(buf));
ref -= 5;
} else if (ref >= 4) {
strlcat(buf, "iv", sizeof(buf));
ref -= 4;
} else if (ref >= 1) {
strlcat(buf, "i", sizeof(buf));
ref -= 1;
}
} else {
while (ref && sz < sizeof(buf) - 1) {
buf[sz++] = 'a' + (ref - 1) % 26;
ref = (ref - 1) / 26;
}
buf[sz] = '\0';
for (i = 0; i < sz; i++, sz--) {
c = buf[i];
buf[i] = buf[sz - 1];
buf[sz - 1] = c;
}
}
return hbuf_printf(out, "%s[%s]%s",
nl ? " " : "", buf, nl ? "\n" : "");
}
/*
* Output optional number of newlines before or after content.
* Return zero on failure (memory), non-zero on success.
*/
static int
rndr_buf_vspace(struct gemini *st, struct lowdown_buf *out, size_t sz)
{
if (st->last_blank >= 0)
while ((size_t)st->last_blank < sz) {
if (!HBUF_PUTSL(out, "\n"))
return 0;
st->last_blank++;
}
return 1;
}
/*
* Emit text in "in" the current line with output "out".
* Return zero on failure (memory), non-zero on success.
*/
static int
rndr_buf(struct gemini *st, struct lowdown_buf *out,
const struct lowdown_node *n, const struct lowdown_buf *in)
{
const struct lowdown_node *nn;
size_t i = 0;
for (nn = n; nn != NULL; nn = nn->parent)
if (nn->type == LOWDOWN_BLOCKCODE ||
nn->type == LOWDOWN_BLOCKHTML) {
st->last_blank = 1;
return hbuf_putb(out, in);
}
/*
* If we last printed some space and we're not in literal mode,
* suppress any leading blanks.
* This is only likely to happen around links.
*/
assert(in != NULL);
if (st->last_blank != 0)
for ( ; i < in->size; i++)
if (!isspace((unsigned char)in->data[i]))
break;
if (!lowdown_gemini_esc(out, in->data + i, in->size - i, 1))
return 0;
if (in->size && st->last_blank != 0)
st->last_blank = 0;
return 1;
}
/*
* Output the unicode entry "val", which must be strictly greater than
* zero, as a UTF-8 sequence.
* This does no error checking.
* Return zero on failure (memory), non-zero on success.
*/
static int
rndr_entity(struct lowdown_buf *buf, int32_t val)
{
assert(val > 0);
if (val < 0x80)
return hbuf_putc(buf, val);
if (val < 0x800)
return hbuf_putc(buf, 192 + val / 64) &&
hbuf_putc(buf, 128 + val % 64);
if (val - 0xd800u < 0x800)
return 1;
if (val < 0x10000)
return hbuf_putc(buf, 224 + val / 4096) &&
hbuf_putc(buf, 128 + val / 64 % 64) &&
hbuf_putc(buf, 128 + val % 64);
if (val < 0x110000)
return hbuf_putc(buf, 240 + val / 262144) &&
hbuf_putc(buf, 128 + val / 4096 % 64) &&
hbuf_putc(buf, 128 + val / 64 % 64) &&
hbuf_putc(buf, 128 + val % 64);
return 1;
}
static int
rndr_doc_header(struct gemini *st, struct lowdown_buf *out,
const struct lowdown_metaq *mq)
{
const struct lowdown_meta *m;
if (!(st->flags & LOWDOWN_GEMINI_METADATA))
return 1;
TAILQ_FOREACH(m, mq, entries) {
if (!lowdown_gemini_esc(out, m->key, strlen(m->key), 1))
return 0;
if (!HBUF_PUTSL(out, ": "))
return 0;
if (!lowdown_gemini_esc(out, m->value, strlen(m->value), 1))
return 0;
st->last_blank = 0;
if (!rndr_buf_vspace(st, out, 1))
return 0;
}
return 1;
}
/*
* Render the key and value, then store the results in our "mq"
* conditional to it existing.
* Return zero on failure (memory), non-zero on success.
*/
static int
rndr_meta(struct gemini *st,
const struct lowdown_node *n, struct lowdown_metaq *mq)
{
ssize_t last_blank;
struct lowdown_meta *m;
ssize_t val;
const char *ep;
/*
* Manually render the children of the meta into a
* buffer and use that as our value. Start by zeroing
* our terminal position and using another output buffer
* (st->tmp would be clobbered by children).
*/
last_blank = st->last_blank;
st->last_blank = -1;
if ((m = lowdown_get_meta(n, mq)) == NULL)
return 0;
if (strcmp(m->key, "shiftheadinglevelby") == 0) {
val = (ssize_t)strtonum(m->value, -100, 100, &ep);
if (ep == NULL)
st->headers_offs = val + 1;
} else if (strcmp(m->key, "baseheaderlevel") == 0) {
val = (ssize_t)strtonum(m->value, 1, 100, &ep);
if (ep == NULL)
st->headers_offs = val;
}
st->last_blank = last_blank;
return 1;
}
/*
* Return zero on failure (memory), non-zero on success.
*/
static int
rndr_flush_linkq(struct gemini *st, struct lowdown_buf *out)
{
struct link *l;
int rc;
assert(st->nolinkqsz == 0);
while ((l = TAILQ_FIRST(&st->linkq)) != NULL) {
TAILQ_REMOVE(&st->linkq, l, entries);
if (!HBUF_PUTSL(out, "=> "))
return 0;
if (l->n->type == LOWDOWN_LINK)
rc = hbuf_putb(out, &l->n->rndr_link.link);
else if (l->n->type == LOWDOWN_LINK_AUTO)
rc = hbuf_putb(out, &l->n->rndr_autolink.link);
else if (l->n->type == LOWDOWN_IMAGE)
rc = hbuf_putb(out, &l->n->rndr_image.link);
else
rc = 1;
if (!rc)
return 0;
if (!rndr_link_ref(st, out, l->id, 1))
return 0;
st->last_blank = 1;
free(l);
}
st->linkqsz = 0;
return 1;
}
/*
* Get the column width of a multi-byte sequence.
* If the sequence is bad, return the number of raw bytes to print.
* Return <0 on failure (memory), >=0 otherwise.
*/
static ssize_t
rndr_mbswidth(struct gemini *st, const struct lowdown_buf *in)
{
size_t wsz, csz;
const char *cp;
void *pp;
mbstate_t mbs;
memset(&mbs, 0, sizeof(mbstate_t));
cp = in->data;
wsz = mbsnrtowcs(NULL, &cp, in->size, 0, &mbs);
if (wsz == (size_t)-1)
return in->size;
if (st->bufsz < wsz) {
st->bufsz = wsz;
pp = reallocarray(st->buf, wsz, sizeof(wchar_t));
if (pp == NULL)
return -1;
st->buf = pp;
}
memset(&mbs, 0, sizeof(mbstate_t));
cp = in->data;
mbsnrtowcs(st->buf, &cp, in->size, wsz, &mbs);
csz = wcswidth(st->buf, wsz);
return csz == (size_t)-1 ? in->size : csz;
}
/*
* Return zero on failure (memory), non-zero on success.
*/
static int
rndr_table(struct lowdown_buf *ob, struct gemini *st,
const struct lowdown_node *n)
{
size_t *widths = NULL;
const struct lowdown_node *row, *top, *cell;
struct lowdown_buf *celltmp = NULL,
*rowtmp = NULL;
size_t i, j, sz;
ssize_t last_blank, ssz;
unsigned int flags, oflags;
int rc = 0;
assert(n->type == LOWDOWN_TABLE_BLOCK);
/*
* Temporarily make us not use in-line links.
* This is obviously because tables and inline links don't work
* well together.
*/
oflags = st->flags;
if (st->flags & LOWDOWN_GEMINI_LINK_IN)
st->flags &= ~LOWDOWN_GEMINI_LINK_IN;
widths = calloc(n->rndr_table.columns, sizeof(size_t));
if (widths == NULL)
goto out;
if ((rowtmp = hbuf_new(128)) == NULL ||
(celltmp = hbuf_new(128)) == NULL)
goto out;
/*
* Begin by counting the number of printable columns in each
* column in each row. Don't let us accumulate any links, as
* we're going to re-run this after.
*/
st->nolinkqsz = st->linkqsz + 1;
TAILQ_FOREACH(top, &n->children, entries) {
assert(top->type == LOWDOWN_TABLE_HEADER ||
top->type == LOWDOWN_TABLE_BODY);
TAILQ_FOREACH(row, &top->children, entries)
TAILQ_FOREACH(cell, &row->children, entries) {
i = cell->rndr_table_cell.col;
assert(i < n->rndr_table.columns);
hbuf_truncate(celltmp);
last_blank = st->last_blank;
st->last_blank = 0;
if (!rndr(celltmp, NULL, st, cell))
goto out;
ssz = rndr_mbswidth(st, celltmp);
if (ssz < 0)
goto out;
if (widths[i] < (size_t)ssz)
widths[i] = (size_t)ssz;
st->last_blank = last_blank;
}
}
st->nolinkqsz = 0;
/* Now actually print, row-by-row into the output. */
TAILQ_FOREACH(top, &n->children, entries) {
assert(top->type == LOWDOWN_TABLE_HEADER ||
top->type == LOWDOWN_TABLE_BODY);
TAILQ_FOREACH(row, &top->children, entries) {
hbuf_truncate(rowtmp);
TAILQ_FOREACH(cell, &row->children, entries) {
i = cell->rndr_table_cell.col;
hbuf_truncate(celltmp);
last_blank = st->last_blank;
st->last_blank = 0;
if (!rndr(celltmp, NULL, st, cell))
goto out;
ssz = rndr_mbswidth(st, celltmp);
if (ssz < 0)
goto out;
assert(widths[i] >= (size_t)ssz);
sz = widths[i] - (size_t)ssz;
/*
* Alignment is either beginning,
* ending, or splitting the remaining
* spaces around the word.
* Be careful about uneven splitting in
* the case of centre.
*/
flags = cell->rndr_table_cell.flags &
HTBL_FL_ALIGNMASK;
if (flags == HTBL_FL_ALIGN_RIGHT)
for (j = 0; j < sz; j++)
if (!HBUF_PUTSL(rowtmp, " "))
goto out;
if (flags == HTBL_FL_ALIGN_CENTER)
for (j = 0; j < sz / 2; j++)
if (!HBUF_PUTSL(rowtmp, " "))
goto out;
if (!hbuf_putb(rowtmp, celltmp))
goto out;
if (flags == 0 ||
flags == HTBL_FL_ALIGN_LEFT)
for (j = 0; j < sz; j++)
if (!HBUF_PUTSL(rowtmp, " "))
goto out;
if (flags == HTBL_FL_ALIGN_CENTER) {
sz = (sz % 2) ?
(sz / 2) + 1 : (sz / 2);
for (j = 0; j < sz; j++)
if (!HBUF_PUTSL(rowtmp, " "))
goto out;
}
st->last_blank = last_blank;
if (TAILQ_NEXT(cell, entries) != NULL &&
!HBUF_PUTSL(rowtmp, " | "))
goto out;
}
/*
* Some magic here.
* First, emulate rndr() by setting the
* stackpos to the table, which is required for
* checking the line start.
* Then directly print, as we've already escaped
* all characters, and have embedded escapes of
* our own. Then end the line.
*/
if (!hbuf_putb(ob, rowtmp))
goto out;
st->last_blank = 0;
if (!rndr_buf_vspace(st, ob, 1))
goto out;
}
if (top->type == LOWDOWN_TABLE_HEADER) {
for (i = 0; i < n->rndr_table.columns; i++) {
for (j = 0; j <= widths[i]; j++)
if (!HBUF_PUTSL(ob, "-"))
goto out;
if (i < n->rndr_table.columns - 1 &&
!HBUF_PUTSL(ob, "|-"))
goto out;
}
st->last_blank = 0;
if (!rndr_buf_vspace(st, ob, 1))
goto out;
}
}
rc = 1;
out:
hbuf_free(celltmp);
hbuf_free(rowtmp);
free(widths);
st->flags = oflags;
return rc;
}
/*
* Return zero on failure (memory), non-zero on success.
*/
static int
rndr(struct lowdown_buf *ob, struct lowdown_metaq *mq,
struct gemini *st, const struct lowdown_node *n)
{
const struct lowdown_node *child, *prev, *nn;
struct link *l;
void *pp;
struct lowdown_buf *tmpbuf;
size_t i;
ssize_t level;
int32_t entity;
int rc;
prev = n->parent == NULL ? NULL :
TAILQ_PREV(n, lowdown_nodeq, entries);
/* Vertical space before content. */
switch (n->type) {
case LOWDOWN_ROOT:
st->last_blank = -1;
break;
case LOWDOWN_BLOCKCODE:
case LOWDOWN_BLOCKHTML:
case LOWDOWN_BLOCKQUOTE:
case LOWDOWN_DEFINITION:
case LOWDOWN_HEADER:
case LOWDOWN_LIST:
case LOWDOWN_PARAGRAPH:
case LOWDOWN_TABLE_BLOCK:
/*
* Blocks in a definition list get special treatment
* because we only put one newline between the title and
* the data regardless of its contents.
*/
if (n->parent != NULL &&
n->parent->type == LOWDOWN_LISTITEM &&
n->parent->parent != NULL &&
n->parent->parent->type ==
LOWDOWN_DEFINITION_DATA &&
prev == NULL) {
if (!rndr_buf_vspace(st, ob, 1))
return 0;
} else {
if (!rndr_buf_vspace(st, ob, 2))
return 0;
}
break;
case LOWDOWN_MATH_BLOCK:
if (n->rndr_math.blockmode &&
!rndr_buf_vspace(st, ob, 1))
return 0;
break;
case LOWDOWN_DEFINITION_DATA:
/*
* Vertical space if previous block-mode data.
*/
if (n->parent != NULL &&
n->parent->type == LOWDOWN_DEFINITION &&
(n->parent->rndr_definition.flags &
HLIST_FL_BLOCK) &&
prev != NULL &&
prev->type == LOWDOWN_DEFINITION_DATA) {
if (!rndr_buf_vspace(st, ob, 2))
return 0;
} else {
if (!rndr_buf_vspace(st, ob, 1))
return 0;
}
break;
case LOWDOWN_DEFINITION_TITLE:
case LOWDOWN_HRULE:
case LOWDOWN_LISTITEM:
case LOWDOWN_META:
case LOWDOWN_TABLE_ROW:
if (!rndr_buf_vspace(st, ob, 1))
return 0;
break;
case LOWDOWN_LINEBREAK:
if (!rndr_buf_vspace(st, ob, 1))
return 0;
/*
* A linebreak is special if invoked within a
* blockquote, because it means we want to output a
* newline but also continue outputting the blockquote
* marker.
*/
for (nn = n->parent; nn != NULL; nn = nn->parent)
if (nn->type == LOWDOWN_BLOCKQUOTE) {
if (!HBUF_PUTSL(ob, "> "))
return 0;
st->last_blank = 0;
break;
}
break;
case LOWDOWN_IMAGE:
case LOWDOWN_LINK:
case LOWDOWN_LINK_AUTO:
if ((st->flags & LOWDOWN_GEMINI_LINK_IN) &&
!rndr_buf_vspace(st, ob, 1))
return 0;
break;
default:
break;
}
/* Output leading content. */
hbuf_truncate(st->tmp);
switch (n->type) {
case LOWDOWN_TABLE_BLOCK:
case LOWDOWN_BLOCKCODE:
case LOWDOWN_BLOCKHTML:
if (!HBUF_PUTSL(st->tmp, "```\n"))
return 0;
if (!rndr_buf(st, ob, n, st->tmp))
return 0;
if (!rndr_buf_vspace(st, ob, 1))
return 0;
break;
case LOWDOWN_BLOCKQUOTE:
if (!HBUF_PUTSL(st->tmp, "> "))
return 0;
if (!rndr_buf(st, ob, n, st->tmp))
return 0;
st->last_blank = -1;
break;
case LOWDOWN_HEADER:
level = (ssize_t)n->rndr_header.level +
st->headers_offs;
if (level < 1)
level = 1;
for (i = 0; i < (size_t)level; i++)
if (!HBUF_PUTSL(st->tmp, "#"))
return 0;
if (!HBUF_PUTSL(st->tmp, " "))
return 0;
if (!rndr_buf(st, ob, n, st->tmp))
return 0;
st->last_blank = -1;
break;
case LOWDOWN_IMAGE:
case LOWDOWN_LINK:
case LOWDOWN_LINK_AUTO:
if (!(IS_STANDALONE_LINK(n, prev) ||
(st->flags & LOWDOWN_GEMINI_LINK_IN)))
break;
if (!HBUF_PUTSL(st->tmp, "=> "))
return 0;
rc = 1;
if (n->type == LOWDOWN_LINK_AUTO)
rc = hbuf_putb(st->tmp, &n->rndr_autolink.link);
else if (n->type == LOWDOWN_LINK)
rc = hbuf_putb(st->tmp, &n->rndr_link.link);
else if (n->type == LOWDOWN_IMAGE)
rc = hbuf_putb(st->tmp, &n->rndr_image.link);
if (!rc)
return 0;
if (!HBUF_PUTSL(st->tmp, " "))
return 0;
if (!rndr_buf(st, ob, n, st->tmp))
return 0;
st->last_blank = -1;
break;
case LOWDOWN_LISTITEM:
rc = 1;
if (n->rndr_listitem.flags & HLIST_FL_DEF)
rc = HBUF_PUTSL(st->tmp, ": ");
else if (n->rndr_listitem.flags & HLIST_FL_CHECKED)
rc = HBUF_PUTSL(st->tmp, "☑ ");
else if (n->rndr_listitem.flags & HLIST_FL_UNCHECKED)
rc = HBUF_PUTSL(st->tmp, "☐ ");
else if (n->rndr_listitem.flags & HLIST_FL_UNORDERED)
rc = HBUF_PUTSL(st->tmp, "* ");
else
rc = hbuf_printf(st->tmp, "%zu. ",
n->rndr_listitem.num);
if (!rc)
return 0;
if (!rndr_buf(st, ob, n, st->tmp))
return 0;
st->last_blank = -1;
break;
case LOWDOWN_SUBSCRIPT:
if (!HBUF_PUTSL(st->tmp, "~") ||
!rndr_buf(st, ob, n, st->tmp))
return 0;
break;
case LOWDOWN_SUPERSCRIPT:
if (!HBUF_PUTSL(st->tmp, "^") ||
!rndr_buf(st, ob, n, st->tmp))
return 0;
break;
default:
break;
}
/* Descend into children. */
switch (n->type) {
case LOWDOWN_TABLE_BLOCK:
if (!rndr_table(ob, st, n))
return 0;
break;
case LOWDOWN_META:
if (n->chng != LOWDOWN_CHNG_DELETE &&
!rndr_meta(st, n, mq))
return 0;
break;
case LOWDOWN_FOOTNOTE:
if ((tmpbuf = hbuf_new(32)) == NULL)
return 0;
if (!hbuf_printf(tmpbuf, "[%zu] ", st->footsz + 1))
return 0;
st->last_blank = -1;
st->nolinkflush = 1;
TAILQ_FOREACH(child, &n->children, entries)
if (!rndr(tmpbuf, mq, st, child))
return 0;
st->nolinkflush = 0;
pp = reallocarray(st->foots,
st->footsz + 1,
sizeof(struct lowdown_buf *));
if (pp == NULL)
return 0;
st->foots = pp;
st->foots[st->footsz++] = tmpbuf;
break;
default:
TAILQ_FOREACH(child, &n->children, entries)
if (!rndr(ob, mq, st, child))
return 0;
break;
}
/* Output non-child or trailing content. */
hbuf_truncate(st->tmp);
switch (n->type) {
case LOWDOWN_HRULE:
if (!HBUF_PUTSL(st->tmp, "~~~~~~~~"))
return 0;
if (!rndr_buf(st, ob, n, st->tmp))
return 0;
break;
case LOWDOWN_FOOTNOTE:
if (!hbuf_printf(st->tmp, "[%zu]", st->footsz))
return 0;
if (!rndr_buf(st, ob, n, st->tmp))
return 0;
break;
case LOWDOWN_RAW_HTML:
if (!rndr_buf(st, ob, n, &n->rndr_raw_html.text))
return 0;
break;
case LOWDOWN_MATH_BLOCK:
if (!rndr_buf(st, ob, n, &n->rndr_math.text))
return 0;
break;
case LOWDOWN_ENTITY:
entity = entity_find_iso(&n->rndr_entity.text);
if (entity > 0) {
if (!rndr_entity(st->tmp, entity))
return 0;
if (!rndr_buf(st, ob, n, st->tmp))
return 0;
} else {
if (!rndr_buf(st, ob, n, &n->rndr_entity.text))
return 0;
}
break;
case LOWDOWN_BLOCKCODE:
if (!rndr_buf(st, ob, n, &n->rndr_blockcode.text))
return 0;
break;
case LOWDOWN_BLOCKHTML:
if (!rndr_buf(st, ob, n, &n->rndr_blockhtml.text))
return 0;
break;
case LOWDOWN_CODESPAN:
if (!rndr_buf(st, ob, n, &n->rndr_codespan.text))
return 0;
break;
case LOWDOWN_IMAGE:
if (!rndr_buf(st, ob, n, &n->rndr_image.alt))
return 0;
/* FALLTHROUGH */
case LOWDOWN_LINK:
case LOWDOWN_LINK_AUTO:
if (IS_STANDALONE_LINK(n, prev) ||
(st->flags & LOWDOWN_GEMINI_LINK_IN))
break;
if (st->nolinkqsz == 0) {
if ((l = calloc(1, sizeof(struct link))) == NULL)
return 0;
l->n = n;
l->id = ++st->linkqsz;
TAILQ_INSERT_TAIL(&st->linkq, l, entries);
i = l->id;
} else
i = st->nolinkqsz++;
if (!rndr_link_ref(st, st->tmp, i, 0))
return 0;
if (!rndr_buf(st, ob, n, st->tmp))
return 0;
break;
case LOWDOWN_NORMAL_TEXT:
if (!rndr_buf(st, ob, n, &n->rndr_normal_text.text))
return 0;
break;
case LOWDOWN_ROOT:
if (!TAILQ_EMPTY(&st->linkq) &&
(st->flags & LOWDOWN_GEMINI_LINK_END)) {
if (!rndr_buf_vspace(st, ob, 2))
return 0;
if (!rndr_flush_linkq(st, ob))
return 0;
}
if (st->footsz == 0)
break;
if (!HBUF_PUTSL(ob, "~~~~~~~~\n\n"))
return 0;
for (i = 0; i < st->footsz; i++) {
if (!hbuf_putb(ob, st->foots[i]))
return 0;
if (!HBUF_PUTSL(ob, "\n"))
return 0;
}
break;
case LOWDOWN_DOC_HEADER:
if (!rndr_doc_header(st, ob, mq))
return 0;
break;
default:
break;
}
/* Trailing block spaces. */
hbuf_truncate(st->tmp);
switch (n->type) {
case LOWDOWN_TABLE_BLOCK:
case LOWDOWN_BLOCKCODE:
case LOWDOWN_BLOCKHTML:
if (!HBUF_PUTSL(st->tmp, "```"))
return 0;
if (!rndr_buf(st, ob, n, st->tmp))
return 0;
st->last_blank = 0;
if (!rndr_buf_vspace(st, ob, 2))
return 0;
break;
case LOWDOWN_DOC_HEADER:
if ((st->flags & LOWDOWN_STANDALONE) &&
!rndr_buf_vspace(st, ob, 2))
return 0;
break;
case LOWDOWN_BLOCKQUOTE:
case LOWDOWN_DEFINITION:
case LOWDOWN_HEADER:
case LOWDOWN_LIST:
case LOWDOWN_PARAGRAPH:
if (!rndr_buf_vspace(st, ob, 2))
return 0;
break;
case LOWDOWN_MATH_BLOCK:
if (n->rndr_math.blockmode &&
!rndr_buf_vspace(st, ob, 1))
return 0;
break;
case LOWDOWN_DEFINITION_DATA:
case LOWDOWN_DEFINITION_TITLE:
case LOWDOWN_HRULE:
case LOWDOWN_LISTITEM:
case LOWDOWN_META:
case LOWDOWN_TABLE_ROW:
if (!rndr_buf_vspace(st, ob, 1))
return 0;
break;
case LOWDOWN_IMAGE:
case LOWDOWN_LINK:
case LOWDOWN_LINK_AUTO:
if (IS_STANDALONE_LINK(n, prev) ||
(st->flags & LOWDOWN_GEMINI_LINK_IN))
if (!rndr_buf_vspace(st, ob, 1))
return 0;
break;
case LOWDOWN_ROOT:
/*
* Special case: snip any trailing newlines that may
* have been printed as trailing vertical space.
* This tidies up the output.
*/
if (!rndr_buf_vspace(st, ob, 1))
return 0;
while (ob->size && ob->data[ob->size - 1] == '\n')
ob->size--;
if (!HBUF_PUTSL(ob, "\n"))
return 0;
break;
default:
break;
}
if (!st->nolinkflush &&
st->nolinkqsz == 0 && st->last_blank > 1 &&
!TAILQ_EMPTY(&st->linkq) &&
!(st->flags & LOWDOWN_GEMINI_LINK_END)) {
if (!rndr_flush_linkq(st, ob))
return 0;
if (!HBUF_PUTSL(ob, "\n"))
return 0;
st->last_blank = 2;
}
return 1;
}
int
lowdown_gemini_rndr(struct lowdown_buf *ob, void *arg,
const struct lowdown_node *n)
{
struct gemini *st = arg;
int rc = 0;
size_t i;
struct lowdown_metaq metaq;
struct lowdown_buf *tmp = NULL;
TAILQ_INIT(&metaq);
st->last_blank = 0;
st->headers_offs = 1;
if (st->templ != NULL) {
if ((tmp = hbuf_new(64)) == NULL)
goto out;
if (!rndr(tmp, &metaq, st, n))
goto out;
rc = lowdown_template(st->templ, tmp, ob, &metaq, 0);
} else
rc = rndr(ob, &metaq, st, n);
out:
link_freeq(&st->linkq);
st->linkqsz = 0;
st->nolinkqsz = 0;