apex-ruby 1.0.46 → 1.0.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/apex_ext/apex_src/include/apex/apex.h +2 -2
- data/ext/apex_ext/apex_src/src/apex.c +3 -0
- data/ext/apex_ext/apex_src/src/extensions/definition_list.c +556 -5
- data/ext/apex_ext/apex_src/src/extensions/definition_list.h +14 -0
- data/ext/apex_ext/apex_src/src/extensions/html_markdown.c +182 -0
- data/lib/apex/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4bd56294a5d4efab3a7dcbe88da7fbe6a51df8a88cfaa3dfd6802a7f1d1971f1
|
|
4
|
+
data.tar.gz: e3b751eb49ba9a49876f69c4eea8df8f16ec909dbf07b824f024644523fe3595
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b314a0984fc6fb02217466cc60acb2d2b99323634a2f09e4c00606938181eaa7657c1116ff4e0830e0e34a4bbe7366a036a30d1b84709e486db0dbf1e35bfa99
|
|
7
|
+
data.tar.gz: 3536e3db8a3183198f8f69f385bc085af35f60b3009267b5da56bb137495b028f5233394faee1834a2c6b751044b3f65395cbb4f621a57e1168f2b80f0912778
|
|
@@ -21,8 +21,8 @@ typedef struct cmark_node cmark_node;
|
|
|
21
21
|
|
|
22
22
|
#define APEX_VERSION_MAJOR 1
|
|
23
23
|
#define APEX_VERSION_MINOR 1
|
|
24
|
-
#define APEX_VERSION_PATCH
|
|
25
|
-
#define APEX_VERSION_STRING "1.1.
|
|
24
|
+
#define APEX_VERSION_PATCH 54
|
|
25
|
+
#define APEX_VERSION_STRING "1.1.54"
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Processor compatibility modes
|
|
@@ -6098,6 +6098,9 @@ char *apex_markdown_to_html(const char *markdown, size_t len, const apex_options
|
|
|
6098
6098
|
return NULL;
|
|
6099
6099
|
}
|
|
6100
6100
|
|
|
6101
|
+
/* Footnote markers shielded inside definition lists are real refs now. */
|
|
6102
|
+
apex_splice_definition_footnotes(document);
|
|
6103
|
+
|
|
6101
6104
|
if (options->enable_task_lists) {
|
|
6102
6105
|
apex_fix_blockquote_tasklists(document, cmark_find_syntax_extension("tasklist"), 0);
|
|
6103
6106
|
}
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
#include "../html_renderer.h"
|
|
32
32
|
#include <string.h>
|
|
33
33
|
#include <stdlib.h>
|
|
34
|
+
#include <stdio.h>
|
|
34
35
|
#include <stdbool.h>
|
|
35
36
|
|
|
36
37
|
/**
|
|
@@ -221,6 +222,59 @@ static bool is_code_fence_line(const char *line, size_t len) {
|
|
|
221
222
|
return false;
|
|
222
223
|
}
|
|
223
224
|
|
|
225
|
+
/* CommonMark HTML block type 1. Contents are not Markdown, so `::before`
|
|
226
|
+
* in CSS must not become a one-line definition. */
|
|
227
|
+
static bool raw_tag_name_at(const char *p, const char *end, const char *name) {
|
|
228
|
+
size_t n = strlen(name);
|
|
229
|
+
if ((size_t)(end - p) < n) return false;
|
|
230
|
+
for (size_t i = 0; i < n; i++) {
|
|
231
|
+
char c = p[i];
|
|
232
|
+
if (c >= 'A' && c <= 'Z') c = (char)(c - 'A' + 'a');
|
|
233
|
+
if (c != name[i]) return false;
|
|
234
|
+
}
|
|
235
|
+
if ((size_t)(end - p) == n) return true;
|
|
236
|
+
char next = p[n];
|
|
237
|
+
return next == '>' || next == ' ' || next == '\t' || next == '\r' ||
|
|
238
|
+
next == '\n' || next == '/';
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/* Closing tag for a style/script/pre/textarea block that starts this line,
|
|
242
|
+
* or NULL. Up to three leading spaces, matching CommonMark. */
|
|
243
|
+
static const char *raw_html_block_close(const char *line, size_t len) {
|
|
244
|
+
const char *p = line;
|
|
245
|
+
const char *end = line + len;
|
|
246
|
+
int spaces = 0;
|
|
247
|
+
while (p < end && *p == ' ') {
|
|
248
|
+
spaces++;
|
|
249
|
+
if (spaces > 3) return NULL;
|
|
250
|
+
p++;
|
|
251
|
+
}
|
|
252
|
+
if (p >= end || *p != '<') return NULL;
|
|
253
|
+
p++;
|
|
254
|
+
if (raw_tag_name_at(p, end, "script")) return "</script>";
|
|
255
|
+
if (raw_tag_name_at(p, end, "pre")) return "</pre>";
|
|
256
|
+
if (raw_tag_name_at(p, end, "style")) return "</style>";
|
|
257
|
+
if (raw_tag_name_at(p, end, "textarea")) return "</textarea>";
|
|
258
|
+
return NULL;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
static bool line_contains_ci(const char *line, size_t len, const char *token) {
|
|
262
|
+
size_t tlen = strlen(token);
|
|
263
|
+
if (tlen == 0 || len < tlen) return false;
|
|
264
|
+
for (size_t i = 0; i + tlen <= len; i++) {
|
|
265
|
+
size_t j = 0;
|
|
266
|
+
for (; j < tlen; j++) {
|
|
267
|
+
char a = line[i + j];
|
|
268
|
+
char b = token[j];
|
|
269
|
+
if (a >= 'A' && a <= 'Z') a = (char)(a - 'A' + 'a');
|
|
270
|
+
if (b >= 'A' && b <= 'Z') b = (char)(b - 'A' + 'a');
|
|
271
|
+
if (a != b) break;
|
|
272
|
+
}
|
|
273
|
+
if (j == tlen) return true;
|
|
274
|
+
}
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
|
|
224
278
|
/**
|
|
225
279
|
* Lines that clearly start a new Markdown block should not be buffered as
|
|
226
280
|
* potential definition-list terms (avoids eating blanks / mis-ordering fences).
|
|
@@ -310,6 +364,417 @@ static char *render_inline_with_doc(const char *content, size_t content_len,
|
|
|
310
364
|
return result;
|
|
311
365
|
}
|
|
312
366
|
|
|
367
|
+
/* Footnote markers inside a definition are rendered before the main parse,
|
|
368
|
+
* so [^id] would stay literal and the note would be dropped. Shield the
|
|
369
|
+
* marker, leave a real reference for the main parser, then splice it back. */
|
|
370
|
+
static bool parse_fn_token(const char *s, const char *prefix,
|
|
371
|
+
const char **id_out, size_t *id_len, size_t *token_len) {
|
|
372
|
+
size_t prefix_len = strlen(prefix);
|
|
373
|
+
if (strncmp(s, prefix, prefix_len) != 0) return false;
|
|
374
|
+
const char *p = s + prefix_len;
|
|
375
|
+
char *end = NULL;
|
|
376
|
+
long n = strtol(p, &end, 10);
|
|
377
|
+
if (end == p || *end != ':' || n < 0) return false;
|
|
378
|
+
const char *id = end + 1;
|
|
379
|
+
if ((size_t)n > strlen(id)) return false;
|
|
380
|
+
if (id[n] != '%' || id[n + 1] != '%') return false;
|
|
381
|
+
*id_out = id;
|
|
382
|
+
*id_len = (size_t)n;
|
|
383
|
+
*token_len = (size_t)((id + n + 2) - s);
|
|
384
|
+
return true;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
static void fn_buf_append(char **buf, size_t *len, size_t *cap, const char *data, size_t n) {
|
|
388
|
+
if (*len + n + 1 > *cap) {
|
|
389
|
+
size_t next = (*cap < 64) ? 64 : *cap * 2;
|
|
390
|
+
while (*len + n + 1 > next) next *= 2;
|
|
391
|
+
char *grown = realloc(*buf, next);
|
|
392
|
+
if (!grown) return;
|
|
393
|
+
*buf = grown;
|
|
394
|
+
*cap = next;
|
|
395
|
+
}
|
|
396
|
+
memcpy(*buf + *len, data, n);
|
|
397
|
+
*len += n;
|
|
398
|
+
(*buf)[*len] = '\0';
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
static void fn_stash_add(char **stash, size_t *len, size_t *cap, const char *id, size_t id_len) {
|
|
402
|
+
char prefix[64];
|
|
403
|
+
int plen = snprintf(prefix, sizeof(prefix), "%%%%APEXFNSTASH:%zu:", id_len);
|
|
404
|
+
if (plen < 0) return;
|
|
405
|
+
fn_buf_append(stash, len, cap, prefix, (size_t)plen);
|
|
406
|
+
fn_buf_append(stash, len, cap, id, id_len);
|
|
407
|
+
fn_buf_append(stash, len, cap, "%%[^", 4);
|
|
408
|
+
fn_buf_append(stash, len, cap, id, id_len);
|
|
409
|
+
fn_buf_append(stash, len, cap, "]", 1);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
static bool at_line_start(const char *s, size_t i) {
|
|
413
|
+
return i == 0 || s[i - 1] == '\n';
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/* A fence line. Closing fences may contain only trailing space. */
|
|
417
|
+
static bool fence_marker_at(const char *s, size_t len, size_t i, bool closing,
|
|
418
|
+
char *ch_out, int *n_out) {
|
|
419
|
+
size_t j = i;
|
|
420
|
+
int spaces = 0;
|
|
421
|
+
while (j < len && s[j] == ' ' && spaces < 4) {
|
|
422
|
+
j++;
|
|
423
|
+
spaces++;
|
|
424
|
+
}
|
|
425
|
+
if (spaces > 3 || j >= len) return false;
|
|
426
|
+
char ch = s[j];
|
|
427
|
+
if (ch != '`' && ch != '~') return false;
|
|
428
|
+
int n = 0;
|
|
429
|
+
while (j + (size_t)n < len && s[j + (size_t)n] == ch) n++;
|
|
430
|
+
if (n < 3) return false;
|
|
431
|
+
size_t k = j + (size_t)n;
|
|
432
|
+
if (closing) {
|
|
433
|
+
while (k < len && s[k] != '\n') {
|
|
434
|
+
if (s[k] != ' ' && s[k] != '\t' && s[k] != '\r') return false;
|
|
435
|
+
k++;
|
|
436
|
+
}
|
|
437
|
+
} else if (ch == '`') {
|
|
438
|
+
while (k < len && s[k] != '\n') {
|
|
439
|
+
if (s[k] == '`') return false;
|
|
440
|
+
k++;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
if (ch_out) *ch_out = ch;
|
|
444
|
+
if (n_out) *n_out = n;
|
|
445
|
+
return true;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
static bool footnote_definition_line_at(const char *s, size_t len, size_t bracket, size_t bracket_end) {
|
|
449
|
+
if (bracket_end >= len || s[bracket_end] != ':') return false;
|
|
450
|
+
size_t line = bracket;
|
|
451
|
+
while (line > 0 && s[line - 1] != '\n') line--;
|
|
452
|
+
size_t p = line;
|
|
453
|
+
int spaces = 0;
|
|
454
|
+
while (p < bracket && s[p] == ' ' && spaces < 4) {
|
|
455
|
+
p++;
|
|
456
|
+
spaces++;
|
|
457
|
+
}
|
|
458
|
+
return p == bracket;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/* Replace [^id] outside code with a placeholder. Append a real
|
|
462
|
+
* reference to stash so the main parser still builds the note. */
|
|
463
|
+
char *apex_shield_footnote_refs(const char *content, size_t len, bool in_code,
|
|
464
|
+
char **stash, size_t *stash_len, size_t *stash_cap) {
|
|
465
|
+
if (!content || len == 0 || !memchr(content, '[', len)) return NULL;
|
|
466
|
+
|
|
467
|
+
bool found = false;
|
|
468
|
+
char *out = NULL;
|
|
469
|
+
size_t out_len = 0;
|
|
470
|
+
size_t out_cap = 0;
|
|
471
|
+
bool code = in_code;
|
|
472
|
+
bool in_fence = false;
|
|
473
|
+
char fence_char = 0;
|
|
474
|
+
int fence_len = 0;
|
|
475
|
+
|
|
476
|
+
for (size_t i = 0; i < len; i++) {
|
|
477
|
+
if (!code && at_line_start(content, i)) {
|
|
478
|
+
char ch = 0;
|
|
479
|
+
int n = 0;
|
|
480
|
+
if (!in_fence && fence_marker_at(content, len, i, false, &ch, &n)) {
|
|
481
|
+
in_fence = true;
|
|
482
|
+
fence_char = ch;
|
|
483
|
+
fence_len = n;
|
|
484
|
+
} else if (in_fence && fence_marker_at(content, len, i, true, &ch, &n) &&
|
|
485
|
+
ch == fence_char && n >= fence_len) {
|
|
486
|
+
in_fence = false;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
if (in_fence) {
|
|
490
|
+
fn_buf_append(&out, &out_len, &out_cap, content + i, 1);
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
if (content[i] == '`') {
|
|
494
|
+
int count = 1;
|
|
495
|
+
while (i + (size_t)count < len && content[i + (size_t)count] == '`') count++;
|
|
496
|
+
if (count == 1) code = !code;
|
|
497
|
+
fn_buf_append(&out, &out_len, &out_cap, content + i, (size_t)count);
|
|
498
|
+
i += (size_t)(count - 1);
|
|
499
|
+
continue;
|
|
500
|
+
}
|
|
501
|
+
if (!code && content[i] == '[' && i + 2 < len && content[i + 1] == '^') {
|
|
502
|
+
size_t id_start = i + 2;
|
|
503
|
+
size_t id_end = id_start;
|
|
504
|
+
while (id_end < len && content[id_end] != ']' && content[id_end] != '\n') id_end++;
|
|
505
|
+
if (id_end < len && content[id_end] == ']' && id_end > id_start &&
|
|
506
|
+
(id_end + 1 >= len || (content[id_end + 1] != '(' && content[id_end + 1] != '[')) &&
|
|
507
|
+
!footnote_definition_line_at(content, len, i, id_end + 1)) {
|
|
508
|
+
size_t id_len = id_end - id_start;
|
|
509
|
+
char prefix[64];
|
|
510
|
+
int plen = snprintf(prefix, sizeof(prefix), "%%%%APEXFN:%zu:", id_len);
|
|
511
|
+
if (plen > 0) {
|
|
512
|
+
fn_buf_append(&out, &out_len, &out_cap, prefix, (size_t)plen);
|
|
513
|
+
fn_buf_append(&out, &out_len, &out_cap, content + id_start, id_len);
|
|
514
|
+
fn_buf_append(&out, &out_len, &out_cap, "%%", 2);
|
|
515
|
+
fn_stash_add(stash, stash_len, stash_cap, content + id_start, id_len);
|
|
516
|
+
found = true;
|
|
517
|
+
i = id_end;
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
fn_buf_append(&out, &out_len, &out_cap, content + i, 1);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
if (!found) {
|
|
526
|
+
free(out);
|
|
527
|
+
return NULL;
|
|
528
|
+
}
|
|
529
|
+
return out;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
static char *render_deflist_text(const char *content, size_t content_len, bool in_code,
|
|
533
|
+
const char *full_doc, size_t full_doc_len, bool unsafe,
|
|
534
|
+
char **stash, size_t *stash_len, size_t *stash_cap) {
|
|
535
|
+
char *shielded = apex_shield_footnote_refs(content, content_len, in_code, stash, stash_len, stash_cap);
|
|
536
|
+
const char *use = shielded ? shielded : content;
|
|
537
|
+
size_t use_len = shielded ? strlen(shielded) : content_len;
|
|
538
|
+
char *html = render_inline_with_doc(use, use_len, full_doc, full_doc_len, unsafe);
|
|
539
|
+
free(shielded);
|
|
540
|
+
return html;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
static char *href_escape(const char *s) {
|
|
544
|
+
size_t len = strlen(s);
|
|
545
|
+
char *out = malloc(len * 3 + 1);
|
|
546
|
+
if (!out) return NULL;
|
|
547
|
+
char *w = out;
|
|
548
|
+
for (const unsigned char *p = (const unsigned char *)s; *p; p++) {
|
|
549
|
+
if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') ||
|
|
550
|
+
(*p >= '0' && *p <= '9') || *p == '-' || *p == '_' || *p == '.') {
|
|
551
|
+
*w++ = (char)*p;
|
|
552
|
+
} else {
|
|
553
|
+
sprintf(w, "%%%02X", *p);
|
|
554
|
+
w += 3;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
*w = '\0';
|
|
558
|
+
return out;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
static char *render_footnote_ref_html(cmark_node *ref) {
|
|
562
|
+
if (!ref || !ref->parent_footnote_def) return NULL;
|
|
563
|
+
const char *id = cmark_node_get_literal(ref->parent_footnote_def);
|
|
564
|
+
const char *num = cmark_node_get_literal(ref);
|
|
565
|
+
if (!id || !id[0] || !num) return NULL;
|
|
566
|
+
char *id_esc = href_escape(id);
|
|
567
|
+
char *num_esc = href_escape(num);
|
|
568
|
+
if (!id_esc || !num_esc) {
|
|
569
|
+
free(id_esc);
|
|
570
|
+
free(num_esc);
|
|
571
|
+
return NULL;
|
|
572
|
+
}
|
|
573
|
+
char suffix[32] = "";
|
|
574
|
+
if (ref->footnote.ref_ix > 1)
|
|
575
|
+
snprintf(suffix, sizeof(suffix), "-%d", ref->footnote.ref_ix);
|
|
576
|
+
size_t n = strlen(id_esc) * 2 + strlen(num_esc) + strlen(suffix) + 96;
|
|
577
|
+
char *out = malloc(n);
|
|
578
|
+
if (out) {
|
|
579
|
+
snprintf(out, n,
|
|
580
|
+
"<sup class=\"footnote-ref\"><a href=\"#fn-%s\" id=\"fnref-%s%s\" data-footnote-ref>%s</a></sup>",
|
|
581
|
+
id_esc, id_esc, suffix, num_esc);
|
|
582
|
+
}
|
|
583
|
+
free(id_esc);
|
|
584
|
+
free(num_esc);
|
|
585
|
+
return out;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
static char *fallback_footnote_marker(const char *id, size_t id_len) {
|
|
589
|
+
char *out = malloc(id_len + 4);
|
|
590
|
+
if (!out) return NULL;
|
|
591
|
+
out[0] = '[';
|
|
592
|
+
out[1] = '^';
|
|
593
|
+
memcpy(out + 2, id, id_len);
|
|
594
|
+
out[2 + id_len] = ']';
|
|
595
|
+
out[3 + id_len] = '\0';
|
|
596
|
+
return out;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
static bool text_has_stash(cmark_node *para) {
|
|
600
|
+
for (cmark_node *child = cmark_node_first_child(para); child; child = cmark_node_next(child)) {
|
|
601
|
+
if (cmark_node_get_type(child) != CMARK_NODE_TEXT) continue;
|
|
602
|
+
const char *lit = cmark_node_get_literal(child);
|
|
603
|
+
if (lit && strstr(lit, "%%APEXFNSTASH:")) return true;
|
|
604
|
+
}
|
|
605
|
+
return false;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
static void collect_stash_replacements(cmark_node *para, char ***reps, size_t *count, size_t *cap) {
|
|
609
|
+
size_t unfilled = *count;
|
|
610
|
+
for (cmark_node *child = cmark_node_first_child(para); child; child = cmark_node_next(child)) {
|
|
611
|
+
if (cmark_node_get_type(child) == CMARK_NODE_TEXT) {
|
|
612
|
+
const char *lit = cmark_node_get_literal(child);
|
|
613
|
+
if (!lit) continue;
|
|
614
|
+
const char *p = lit;
|
|
615
|
+
while ((p = strstr(p, "%%APEXFNSTASH:")) != NULL) {
|
|
616
|
+
const char *id = NULL;
|
|
617
|
+
size_t id_len = 0;
|
|
618
|
+
size_t token_len = 0;
|
|
619
|
+
if (!parse_fn_token(p, "%%APEXFNSTASH:", &id, &id_len, &token_len)) {
|
|
620
|
+
p += 14;
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
if (*count + 1 > *cap) {
|
|
624
|
+
size_t next = (*cap < 8) ? 8 : *cap * 2;
|
|
625
|
+
char **grown = realloc(*reps, next * sizeof(char *));
|
|
626
|
+
if (!grown) return;
|
|
627
|
+
*reps = grown;
|
|
628
|
+
*cap = next;
|
|
629
|
+
}
|
|
630
|
+
(*reps)[(*count)++] = fallback_footnote_marker(id, id_len);
|
|
631
|
+
p += token_len;
|
|
632
|
+
}
|
|
633
|
+
} else if (cmark_node_get_type(child) == CMARK_NODE_FOOTNOTE_REFERENCE && unfilled < *count) {
|
|
634
|
+
char *html = render_footnote_ref_html(child);
|
|
635
|
+
if (html) {
|
|
636
|
+
free((*reps)[unfilled]);
|
|
637
|
+
(*reps)[unfilled] = html;
|
|
638
|
+
unfilled++;
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
static char *replace_fn_placeholders(const char *literal, char **reps, size_t rep_count, size_t *used) {
|
|
645
|
+
size_t cap = strlen(literal) + 64;
|
|
646
|
+
char *out = malloc(cap);
|
|
647
|
+
if (!out) return NULL;
|
|
648
|
+
size_t len = 0;
|
|
649
|
+
const char *p = literal;
|
|
650
|
+
while (*p) {
|
|
651
|
+
const char *token = strstr(p, "%%APEXFN:");
|
|
652
|
+
if (!token) {
|
|
653
|
+
size_t n = strlen(p);
|
|
654
|
+
if (len + n + 1 > cap) {
|
|
655
|
+
cap = len + n + 1;
|
|
656
|
+
char *grown = realloc(out, cap);
|
|
657
|
+
if (!grown) { free(out); return NULL; }
|
|
658
|
+
out = grown;
|
|
659
|
+
}
|
|
660
|
+
memcpy(out + len, p, n);
|
|
661
|
+
len += n;
|
|
662
|
+
break;
|
|
663
|
+
}
|
|
664
|
+
size_t prefix_n = (size_t)(token - p);
|
|
665
|
+
const char *id = NULL;
|
|
666
|
+
size_t id_len = 0;
|
|
667
|
+
size_t token_len = 0;
|
|
668
|
+
if (!parse_fn_token(token, "%%APEXFN:", &id, &id_len, &token_len)) {
|
|
669
|
+
if (len + prefix_n + 1 + 1 > cap) {
|
|
670
|
+
cap = (len + prefix_n + 16) * 2;
|
|
671
|
+
char *grown = realloc(out, cap);
|
|
672
|
+
if (!grown) { free(out); return NULL; }
|
|
673
|
+
out = grown;
|
|
674
|
+
}
|
|
675
|
+
memcpy(out + len, p, prefix_n + 1);
|
|
676
|
+
len += prefix_n + 1;
|
|
677
|
+
p = token + 1;
|
|
678
|
+
continue;
|
|
679
|
+
}
|
|
680
|
+
const char *repl = (*used < rep_count) ? reps[*used] : NULL;
|
|
681
|
+
char *fallback = NULL;
|
|
682
|
+
if (!repl) {
|
|
683
|
+
fallback = fallback_footnote_marker(id, id_len);
|
|
684
|
+
repl = fallback ? fallback : "";
|
|
685
|
+
} else {
|
|
686
|
+
(*used)++;
|
|
687
|
+
}
|
|
688
|
+
size_t repl_len = strlen(repl);
|
|
689
|
+
if (len + prefix_n + repl_len + 1 > cap) {
|
|
690
|
+
cap = (len + prefix_n + repl_len + 64) * 2;
|
|
691
|
+
char *grown = realloc(out, cap);
|
|
692
|
+
if (!grown) { free(fallback); free(out); return NULL; }
|
|
693
|
+
out = grown;
|
|
694
|
+
}
|
|
695
|
+
memcpy(out + len, p, prefix_n);
|
|
696
|
+
len += prefix_n;
|
|
697
|
+
memcpy(out + len, repl, repl_len);
|
|
698
|
+
len += repl_len;
|
|
699
|
+
free(fallback);
|
|
700
|
+
p = token + token_len;
|
|
701
|
+
}
|
|
702
|
+
out[len] = '\0';
|
|
703
|
+
return out;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
static void collect_fn_nodes(cmark_node *node, cmark_node ***blocks, size_t *block_count, size_t *block_cap,
|
|
707
|
+
cmark_node ***paras, size_t *para_count, size_t *para_cap,
|
|
708
|
+
char ***reps, size_t *rep_count, size_t *rep_cap) {
|
|
709
|
+
for (cmark_node *child = cmark_node_first_child(node); child; child = cmark_node_next(child)) {
|
|
710
|
+
cmark_node_type type = cmark_node_get_type(child);
|
|
711
|
+
if (type == CMARK_NODE_HTML_BLOCK) {
|
|
712
|
+
const char *lit = cmark_node_get_literal(child);
|
|
713
|
+
if (lit && strstr(lit, "%%APEXFN:")) {
|
|
714
|
+
if (*block_count + 1 > *block_cap) {
|
|
715
|
+
size_t next = (*block_cap < 8) ? 8 : *block_cap * 2;
|
|
716
|
+
cmark_node **grown = realloc(*blocks, next * sizeof(cmark_node *));
|
|
717
|
+
if (!grown) return;
|
|
718
|
+
*blocks = grown;
|
|
719
|
+
*block_cap = next;
|
|
720
|
+
}
|
|
721
|
+
(*blocks)[(*block_count)++] = child;
|
|
722
|
+
}
|
|
723
|
+
} else if (type == CMARK_NODE_PARAGRAPH && text_has_stash(child)) {
|
|
724
|
+
if (*para_count + 1 > *para_cap) {
|
|
725
|
+
size_t next = (*para_cap < 8) ? 8 : *para_cap * 2;
|
|
726
|
+
cmark_node **grown = realloc(*paras, next * sizeof(cmark_node *));
|
|
727
|
+
if (!grown) return;
|
|
728
|
+
*paras = grown;
|
|
729
|
+
*para_cap = next;
|
|
730
|
+
}
|
|
731
|
+
(*paras)[(*para_count)++] = child;
|
|
732
|
+
collect_stash_replacements(child, reps, rep_count, rep_cap);
|
|
733
|
+
} else {
|
|
734
|
+
collect_fn_nodes(child, blocks, block_count, block_cap, paras, para_count, para_cap,
|
|
735
|
+
reps, rep_count, rep_cap);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
void apex_splice_definition_footnotes(cmark_node *root) {
|
|
741
|
+
if (!root) return;
|
|
742
|
+
|
|
743
|
+
cmark_node **blocks = NULL;
|
|
744
|
+
size_t block_count = 0, block_cap = 0;
|
|
745
|
+
cmark_node **paras = NULL;
|
|
746
|
+
size_t para_count = 0, para_cap = 0;
|
|
747
|
+
char **reps = NULL;
|
|
748
|
+
size_t rep_count = 0, rep_cap = 0;
|
|
749
|
+
|
|
750
|
+
collect_fn_nodes(root, &blocks, &block_count, &block_cap, ¶s, ¶_count, ¶_cap,
|
|
751
|
+
&reps, &rep_count, &rep_cap);
|
|
752
|
+
if (block_count == 0 && para_count == 0) {
|
|
753
|
+
free(blocks);
|
|
754
|
+
free(paras);
|
|
755
|
+
free(reps);
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
size_t used = 0;
|
|
760
|
+
for (size_t i = 0; i < block_count; i++) {
|
|
761
|
+
const char *lit = cmark_node_get_literal(blocks[i]);
|
|
762
|
+
if (!lit) continue;
|
|
763
|
+
char *replaced = replace_fn_placeholders(lit, reps, rep_count, &used);
|
|
764
|
+
if (replaced) {
|
|
765
|
+
cmark_node_set_literal(blocks[i], replaced);
|
|
766
|
+
free(replaced);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
for (size_t i = 0; i < para_count; i++)
|
|
770
|
+
cmark_node_free(paras[i]);
|
|
771
|
+
for (size_t i = 0; i < rep_count; i++)
|
|
772
|
+
free(reps[i]);
|
|
773
|
+
free(reps);
|
|
774
|
+
free(blocks);
|
|
775
|
+
free(paras);
|
|
776
|
+
}
|
|
777
|
+
|
|
313
778
|
/**
|
|
314
779
|
* Process one-line definition lists: Term :: Definition -> <dl><dt>Term</dt><dd>Definition</dd></dl>
|
|
315
780
|
* Returns newly allocated string with HTML, or NULL if no changes (caller uses original).
|
|
@@ -353,6 +818,9 @@ char *apex_process_definition_lists(const char *text, bool unsafe) {
|
|
|
353
818
|
const char *read = text;
|
|
354
819
|
char *write = output;
|
|
355
820
|
size_t remaining = output_capacity;
|
|
821
|
+
char *fn_stash = NULL;
|
|
822
|
+
size_t fn_stash_len = 0;
|
|
823
|
+
size_t fn_stash_cap = 0;
|
|
356
824
|
|
|
357
825
|
#define ENSURE_SPACE(needed) do { \
|
|
358
826
|
if (remaining <= (needed)) { \
|
|
@@ -360,17 +828,31 @@ char *apex_process_definition_lists(const char *text, bool unsafe) {
|
|
|
360
828
|
size_t min_capacity = used + (needed) + 1; \
|
|
361
829
|
output_capacity = (min_capacity < 1024) ? 2048 : min_capacity * 2; \
|
|
362
830
|
char *new_output = realloc(output, output_capacity + 1); \
|
|
363
|
-
if (!new_output) { free(output); return NULL; } \
|
|
831
|
+
if (!new_output) { free(output); free(fn_stash); return NULL; } \
|
|
364
832
|
output = new_output; \
|
|
365
833
|
write = output + used; \
|
|
366
834
|
remaining = output_capacity - used; \
|
|
367
835
|
} \
|
|
368
836
|
} while(0)
|
|
369
837
|
|
|
838
|
+
#define FLUSH_FN_STASH() do { \
|
|
839
|
+
if (fn_stash_len > 0) { \
|
|
840
|
+
ENSURE_SPACE(fn_stash_len + 2); \
|
|
841
|
+
memcpy(write, fn_stash, fn_stash_len); \
|
|
842
|
+
write += fn_stash_len; \
|
|
843
|
+
remaining -= fn_stash_len; \
|
|
844
|
+
*write++ = '\n'; \
|
|
845
|
+
*write++ = '\n'; \
|
|
846
|
+
remaining -= 2; \
|
|
847
|
+
fn_stash_len = 0; \
|
|
848
|
+
} \
|
|
849
|
+
} while(0)
|
|
850
|
+
|
|
370
851
|
bool in_def_list = false;
|
|
371
852
|
bool in_code_block = false;
|
|
372
853
|
bool in_indented_code_block = false;
|
|
373
854
|
bool in_inline_code_span = false;
|
|
855
|
+
const char *raw_close = NULL; /* inside <style>, <script>, <pre>, or <textarea> */
|
|
374
856
|
char term_buffer[4096];
|
|
375
857
|
int term_len = 0;
|
|
376
858
|
int blanks_after_term = 0; /* blanks skipped while a potential term is buffered */
|
|
@@ -385,6 +867,20 @@ char *apex_process_definition_lists(const char *text, bool unsafe) {
|
|
|
385
867
|
size_t line_length = (size_t)(line_end - line_start);
|
|
386
868
|
int sep = -1; /* One-line def separator pos; -1 = none (used for inline code state update) */
|
|
387
869
|
|
|
870
|
+
/* Leave raw HTML blocks alone. `::before` in CSS is not a definition. */
|
|
871
|
+
if (raw_close) {
|
|
872
|
+
ENSURE_SPACE(line_length + 2);
|
|
873
|
+
memcpy(write, line_start, line_length);
|
|
874
|
+
write += line_length;
|
|
875
|
+
remaining -= line_length;
|
|
876
|
+
*write++ = '\n';
|
|
877
|
+
remaining--;
|
|
878
|
+
if (line_contains_ci(line_start, line_length, raw_close))
|
|
879
|
+
raw_close = NULL;
|
|
880
|
+
read = line_end + (line_end < text + text_len && *line_end == '\n' ? 1 : 0);
|
|
881
|
+
continue;
|
|
882
|
+
}
|
|
883
|
+
|
|
388
884
|
/* Track indented code blocks (4+ spaces or tab, not list continuation) */
|
|
389
885
|
if (read == text || read[-1] == '\n') {
|
|
390
886
|
bool this_line_indented = line_is_indented_code_block(line_start, line_length);
|
|
@@ -417,6 +913,7 @@ char *apex_process_definition_lists(const char *text, bool unsafe) {
|
|
|
417
913
|
write += 7;
|
|
418
914
|
remaining -= 7;
|
|
419
915
|
in_def_list = false;
|
|
916
|
+
FLUSH_FN_STASH();
|
|
420
917
|
}
|
|
421
918
|
if (term_len > 0) {
|
|
422
919
|
ENSURE_SPACE((size_t)term_len + 2 + (size_t)blanks_after_term);
|
|
@@ -454,6 +951,51 @@ char *apex_process_definition_lists(const char *text, bool unsafe) {
|
|
|
454
951
|
continue;
|
|
455
952
|
}
|
|
456
953
|
|
|
954
|
+
/* <style>, <script>, <pre>, and <textarea> are raw until their closer.
|
|
955
|
+
* Close any open list first so the block is not nested in a <dd>. */
|
|
956
|
+
const char *raw_open = raw_html_block_close(line_start, line_length);
|
|
957
|
+
if (raw_open) {
|
|
958
|
+
if (dd_open) {
|
|
959
|
+
ENSURE_SPACE(10);
|
|
960
|
+
memcpy(write, "</dd>\n", 6);
|
|
961
|
+
write += 6;
|
|
962
|
+
remaining -= 6;
|
|
963
|
+
dd_open = false;
|
|
964
|
+
}
|
|
965
|
+
if (in_def_list) {
|
|
966
|
+
ENSURE_SPACE(10);
|
|
967
|
+
memcpy(write, "</dl>\n\n", 7);
|
|
968
|
+
write += 7;
|
|
969
|
+
remaining -= 7;
|
|
970
|
+
in_def_list = false;
|
|
971
|
+
FLUSH_FN_STASH();
|
|
972
|
+
}
|
|
973
|
+
if (term_len > 0) {
|
|
974
|
+
ENSURE_SPACE((size_t)term_len + 2 + (size_t)blanks_after_term);
|
|
975
|
+
memcpy(write, term_buffer, (size_t)term_len);
|
|
976
|
+
write += term_len;
|
|
977
|
+
remaining -= (size_t)term_len;
|
|
978
|
+
*write++ = '\n';
|
|
979
|
+
remaining--;
|
|
980
|
+
for (int bi = 0; bi < blanks_after_term; bi++) {
|
|
981
|
+
*write++ = '\n';
|
|
982
|
+
remaining--;
|
|
983
|
+
}
|
|
984
|
+
term_len = 0;
|
|
985
|
+
blanks_after_term = 0;
|
|
986
|
+
}
|
|
987
|
+
ENSURE_SPACE(line_length + 2);
|
|
988
|
+
memcpy(write, line_start, line_length);
|
|
989
|
+
write += line_length;
|
|
990
|
+
remaining -= line_length;
|
|
991
|
+
*write++ = '\n';
|
|
992
|
+
remaining--;
|
|
993
|
+
if (!line_contains_ci(line_start, line_length, raw_open))
|
|
994
|
+
raw_close = raw_open;
|
|
995
|
+
read = line_end + (line_end < text + text_len && *line_end == '\n' ? 1 : 0);
|
|
996
|
+
continue;
|
|
997
|
+
}
|
|
998
|
+
|
|
457
999
|
/* Skip definition processing inside indented code blocks */
|
|
458
1000
|
if (in_indented_code_block) {
|
|
459
1001
|
if (term_len > 0) {
|
|
@@ -534,7 +1076,7 @@ char *apex_process_definition_lists(const char *text, bool unsafe) {
|
|
|
534
1076
|
memcpy(write, "<dt>", 4);
|
|
535
1077
|
write += 4;
|
|
536
1078
|
remaining -= 4;
|
|
537
|
-
char *term_html =
|
|
1079
|
+
char *term_html = render_deflist_text(term_buffer, (size_t)term_len, false, text, text_len, unsafe, &fn_stash, &fn_stash_len, &fn_stash_cap);
|
|
538
1080
|
if (term_html) {
|
|
539
1081
|
size_t html_len = strlen(term_html);
|
|
540
1082
|
ENSURE_SPACE(html_len + 20);
|
|
@@ -565,7 +1107,7 @@ char *apex_process_definition_lists(const char *text, bool unsafe) {
|
|
|
565
1107
|
dd_open = true;
|
|
566
1108
|
|
|
567
1109
|
if (def_len > 0) {
|
|
568
|
-
char *def_html =
|
|
1110
|
+
char *def_html = render_deflist_text(def_start, def_len, in_inline_code_span, text, text_len, unsafe, &fn_stash, &fn_stash_len, &fn_stash_cap);
|
|
569
1111
|
if (def_html) {
|
|
570
1112
|
size_t html_len = strlen(def_html);
|
|
571
1113
|
ENSURE_SPACE(html_len + 20);
|
|
@@ -594,10 +1136,12 @@ char *apex_process_definition_lists(const char *text, bool unsafe) {
|
|
|
594
1136
|
}
|
|
595
1137
|
if (in_def_list && term_len > 0) {
|
|
596
1138
|
/* Buffered term wasn't used - output as regular line, close list */
|
|
1139
|
+
ENSURE_SPACE(10);
|
|
597
1140
|
memcpy(write, "</dl>\n\n", 7);
|
|
598
1141
|
write += 7;
|
|
599
1142
|
remaining -= 7;
|
|
600
1143
|
in_def_list = false;
|
|
1144
|
+
FLUSH_FN_STASH();
|
|
601
1145
|
}
|
|
602
1146
|
if (term_len > 0) {
|
|
603
1147
|
ENSURE_SPACE((size_t)term_len + 2 + (size_t)blanks_after_term);
|
|
@@ -645,7 +1189,7 @@ char *apex_process_definition_lists(const char *text, bool unsafe) {
|
|
|
645
1189
|
|
|
646
1190
|
/* Parse term as inline markdown */
|
|
647
1191
|
if (term_len > 0) {
|
|
648
|
-
char *term_html =
|
|
1192
|
+
char *term_html = render_deflist_text(term_start, term_len, in_inline_code_span, text, text_len, unsafe, &fn_stash, &fn_stash_len, &fn_stash_cap);
|
|
649
1193
|
if (term_html) {
|
|
650
1194
|
size_t html_len = strlen(term_html);
|
|
651
1195
|
ENSURE_SPACE(html_len + 20);
|
|
@@ -667,7 +1211,7 @@ char *apex_process_definition_lists(const char *text, bool unsafe) {
|
|
|
667
1211
|
remaining -= 4;
|
|
668
1212
|
|
|
669
1213
|
if (def_len > 0) {
|
|
670
|
-
char *def_html =
|
|
1214
|
+
char *def_html = render_deflist_text(def_start, def_len, in_inline_code_span, text, text_len, unsafe, &fn_stash, &fn_stash_len, &fn_stash_cap);
|
|
671
1215
|
if (def_html) {
|
|
672
1216
|
size_t html_len = strlen(def_html);
|
|
673
1217
|
ENSURE_SPACE(html_len + 20);
|
|
@@ -703,10 +1247,12 @@ char *apex_process_definition_lists(const char *text, bool unsafe) {
|
|
|
703
1247
|
/* else: in_def_list, skip blank, list stays open */
|
|
704
1248
|
} else {
|
|
705
1249
|
if (in_def_list) {
|
|
1250
|
+
ENSURE_SPACE(10);
|
|
706
1251
|
memcpy(write, "</dl>\n\n", 7);
|
|
707
1252
|
write += 7;
|
|
708
1253
|
remaining -= 7;
|
|
709
1254
|
in_def_list = false;
|
|
1255
|
+
FLUSH_FN_STASH();
|
|
710
1256
|
}
|
|
711
1257
|
if (term_len > 0) {
|
|
712
1258
|
ENSURE_SPACE((size_t)term_len + 2 + (size_t)blanks_after_term);
|
|
@@ -761,8 +1307,11 @@ char *apex_process_definition_lists(const char *text, bool unsafe) {
|
|
|
761
1307
|
write += 6;
|
|
762
1308
|
}
|
|
763
1309
|
if (in_def_list) {
|
|
1310
|
+
ENSURE_SPACE(10);
|
|
764
1311
|
memcpy(write, "</dl>\n\n", 7);
|
|
765
1312
|
write += 7;
|
|
1313
|
+
remaining -= 7;
|
|
1314
|
+
FLUSH_FN_STASH();
|
|
766
1315
|
}
|
|
767
1316
|
if (term_len > 0) {
|
|
768
1317
|
ENSURE_SPACE((size_t)term_len + 2 + (size_t)blanks_after_term);
|
|
@@ -775,8 +1324,10 @@ char *apex_process_definition_lists(const char *text, bool unsafe) {
|
|
|
775
1324
|
}
|
|
776
1325
|
|
|
777
1326
|
*write = '\0';
|
|
1327
|
+
#undef FLUSH_FN_STASH
|
|
778
1328
|
#undef ENSURE_SPACE
|
|
779
1329
|
|
|
1330
|
+
free(fn_stash);
|
|
780
1331
|
return output;
|
|
781
1332
|
}
|
|
782
1333
|
|
|
@@ -23,6 +23,20 @@ extern "C" {
|
|
|
23
23
|
*/
|
|
24
24
|
char *apex_process_definition_lists(const char *text, bool unsafe);
|
|
25
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Move footnote references that were shielded inside definition lists
|
|
28
|
+
* back into the rendered HTML, and drop the temporary reference paragraphs.
|
|
29
|
+
*/
|
|
30
|
+
void apex_splice_definition_footnotes(cmark_node *root);
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Replace [^id] outside code with a placeholder and append a real reference
|
|
34
|
+
* to stash. Returns a new string, or NULL when content has no footnote marker.
|
|
35
|
+
* Caller frees the result.
|
|
36
|
+
*/
|
|
37
|
+
char *apex_shield_footnote_refs(const char *content, size_t len, bool in_code,
|
|
38
|
+
char **stash, size_t *stash_len, size_t *stash_cap);
|
|
39
|
+
|
|
26
40
|
/**
|
|
27
41
|
* Debug touch - no-op for one-line format
|
|
28
42
|
*/
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
#include "html_markdown.h"
|
|
7
|
+
#include "definition_list.h"
|
|
7
8
|
#include "ial.h"
|
|
8
9
|
#include "../html_renderer.h"
|
|
9
10
|
#include "cmark-gfm.h"
|
|
@@ -308,6 +309,135 @@ static bool is_footnote_definition_line(const char *line_start, const char *line
|
|
|
308
309
|
return reference_id_matches(footnote_id, content_start + 1, (size_t)(id_end - (content_start + 1)));
|
|
309
310
|
}
|
|
310
311
|
|
|
312
|
+
static bool line_is_any_footnote_definition(const char *line, const char *line_end) {
|
|
313
|
+
const char *p = line;
|
|
314
|
+
int spaces = 0;
|
|
315
|
+
while (p < line_end && *p == ' ' && spaces < 4) {
|
|
316
|
+
p++;
|
|
317
|
+
spaces++;
|
|
318
|
+
}
|
|
319
|
+
if (spaces > 3 || p + 3 >= line_end || p[0] != '[' || p[1] != '^') return false;
|
|
320
|
+
const char *id_end = memchr(p + 2, ']', (size_t)(line_end - (p + 2)));
|
|
321
|
+
return id_end && id_end < line_end && id_end[1] == ':' && id_end > p + 2;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
static bool line_is_fence_marker(const char *line, const char *line_end, bool closing,
|
|
325
|
+
char expect, int expect_len, char *ch_out, int *n_out) {
|
|
326
|
+
const char *p = line;
|
|
327
|
+
int spaces = 0;
|
|
328
|
+
while (p < line_end && *p == ' ' && spaces < 4) {
|
|
329
|
+
p++;
|
|
330
|
+
spaces++;
|
|
331
|
+
}
|
|
332
|
+
if (spaces > 3 || p >= line_end) return false;
|
|
333
|
+
char ch = *p;
|
|
334
|
+
if (ch != '`' && ch != '~') return false;
|
|
335
|
+
int n = 0;
|
|
336
|
+
while (p + n < line_end && p[n] == ch) n++;
|
|
337
|
+
if (n < 3) return false;
|
|
338
|
+
const char *rest = p + n;
|
|
339
|
+
if (closing) {
|
|
340
|
+
if (ch != expect || n < expect_len) return false;
|
|
341
|
+
while (rest < line_end) {
|
|
342
|
+
if (*rest != ' ' && *rest != '\t' && *rest != '\r') return false;
|
|
343
|
+
rest++;
|
|
344
|
+
}
|
|
345
|
+
} else if (ch == '`') {
|
|
346
|
+
while (rest < line_end) {
|
|
347
|
+
if (*rest == '`') return false;
|
|
348
|
+
rest++;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (ch_out) *ch_out = ch;
|
|
352
|
+
if (n_out) *n_out = n;
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
static void append_bytes(char **buf, size_t *len, size_t *cap, const char *data, size_t n) {
|
|
357
|
+
if (*len + n + 1 > *cap) {
|
|
358
|
+
size_t next = (*cap < 64) ? 64 : *cap * 2;
|
|
359
|
+
while (*len + n + 1 > next) next *= 2;
|
|
360
|
+
char *grown = realloc(*buf, next);
|
|
361
|
+
if (!grown) return;
|
|
362
|
+
*buf = grown;
|
|
363
|
+
*cap = next;
|
|
364
|
+
}
|
|
365
|
+
memcpy(*buf + *len, data, n);
|
|
366
|
+
*len += n;
|
|
367
|
+
(*buf)[*len] = '\0';
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/* Pull [^id]: blocks out of a markdown region so the document parser owns them. */
|
|
371
|
+
static char *hoist_footnote_definitions(const char *content, char **hoisted_out) {
|
|
372
|
+
if (!content || !hoisted_out || !strstr(content, "[^")) return NULL;
|
|
373
|
+
*hoisted_out = NULL;
|
|
374
|
+
|
|
375
|
+
size_t len = strlen(content);
|
|
376
|
+
char *kept = malloc(len + 1);
|
|
377
|
+
if (!kept) return NULL;
|
|
378
|
+
size_t klen = 0;
|
|
379
|
+
char *hoisted = NULL;
|
|
380
|
+
size_t hlen = 0;
|
|
381
|
+
size_t hcap = 0;
|
|
382
|
+
bool moved = false;
|
|
383
|
+
bool in_fence = false;
|
|
384
|
+
char fence_char = 0;
|
|
385
|
+
int fence_len = 0;
|
|
386
|
+
const char *p = content;
|
|
387
|
+
|
|
388
|
+
while (*p) {
|
|
389
|
+
const char *line_end = strchr(p, '\n');
|
|
390
|
+
bool has_nl = line_end != NULL;
|
|
391
|
+
if (!line_end) line_end = p + strlen(p);
|
|
392
|
+
size_t line_len = (size_t)(line_end - p);
|
|
393
|
+
char ch = 0;
|
|
394
|
+
int n = 0;
|
|
395
|
+
bool fence = line_is_fence_marker(p, line_end, in_fence, fence_char, fence_len, &ch, &n);
|
|
396
|
+
if (fence && !in_fence) {
|
|
397
|
+
in_fence = true;
|
|
398
|
+
fence_char = ch;
|
|
399
|
+
fence_len = n;
|
|
400
|
+
} else if (fence && in_fence) {
|
|
401
|
+
in_fence = false;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if (!in_fence && !fence && line_is_any_footnote_definition(p, line_end)) {
|
|
405
|
+
moved = true;
|
|
406
|
+
append_bytes(&hoisted, &hlen, &hcap, p, line_len);
|
|
407
|
+
if (has_nl) append_bytes(&hoisted, &hlen, &hcap, "\n", 1);
|
|
408
|
+
p = has_nl ? line_end + 1 : line_end;
|
|
409
|
+
while (*p) {
|
|
410
|
+
const char *cont_end = strchr(p, '\n');
|
|
411
|
+
bool cont_nl = cont_end != NULL;
|
|
412
|
+
if (!cont_end) cont_end = p + strlen(p);
|
|
413
|
+
size_t cont_len = (size_t)(cont_end - p);
|
|
414
|
+
bool indented = cont_len > 0 &&
|
|
415
|
+
(*p == '\t' || (cont_len >= 4 && p[0] == ' ' && p[1] == ' ' && p[2] == ' ' && p[3] == ' '));
|
|
416
|
+
if (!indented) break;
|
|
417
|
+
append_bytes(&hoisted, &hlen, &hcap, p, cont_len);
|
|
418
|
+
if (cont_nl) append_bytes(&hoisted, &hlen, &hcap, "\n", 1);
|
|
419
|
+
p = cont_nl ? cont_end + 1 : cont_end;
|
|
420
|
+
if (!cont_nl) break;
|
|
421
|
+
}
|
|
422
|
+
continue;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
memcpy(kept + klen, p, line_len);
|
|
426
|
+
klen += line_len;
|
|
427
|
+
if (has_nl) kept[klen++] = '\n';
|
|
428
|
+
p = has_nl ? line_end + 1 : line_end;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
if (!moved) {
|
|
432
|
+
free(kept);
|
|
433
|
+
free(hoisted);
|
|
434
|
+
return NULL;
|
|
435
|
+
}
|
|
436
|
+
kept[klen] = '\0';
|
|
437
|
+
*hoisted_out = hoisted;
|
|
438
|
+
return kept;
|
|
439
|
+
}
|
|
440
|
+
|
|
311
441
|
static bool is_footnote_definition_continuation(const char *line_start, size_t line_len) {
|
|
312
442
|
if (line_len == 0) {
|
|
313
443
|
return true;
|
|
@@ -794,12 +924,35 @@ static char *apex_process_html_markdown_impl(const char *text, void *img_attrs,
|
|
|
794
924
|
}
|
|
795
925
|
|
|
796
926
|
if (parse_markdown && content_len > 0) {
|
|
927
|
+
/* Footnote refs in this region are resolved by the document parser.
|
|
928
|
+
* The region parser only sees placeholders, and the real references
|
|
929
|
+
* are emitted after the element so numbering stays in reading order. */
|
|
930
|
+
char *fn_stash = NULL;
|
|
931
|
+
size_t fn_stash_len = 0;
|
|
932
|
+
size_t fn_stash_cap = 0;
|
|
933
|
+
char *fn_hoisted = NULL;
|
|
934
|
+
bool wrote_region = false;
|
|
935
|
+
|
|
797
936
|
/* Extract content */
|
|
798
937
|
char *content = malloc(content_len + 1);
|
|
799
938
|
if (content) {
|
|
800
939
|
memcpy(content, content_start, content_len);
|
|
801
940
|
content[content_len] = '\0';
|
|
802
941
|
|
|
942
|
+
char *without_defs = hoist_footnote_definitions(content, &fn_hoisted);
|
|
943
|
+
if (without_defs) {
|
|
944
|
+
free(content);
|
|
945
|
+
content = without_defs;
|
|
946
|
+
content_len = strlen(content);
|
|
947
|
+
}
|
|
948
|
+
char *shielded = apex_shield_footnote_refs(content, content_len, false,
|
|
949
|
+
&fn_stash, &fn_stash_len, &fn_stash_cap);
|
|
950
|
+
if (shielded) {
|
|
951
|
+
free(content);
|
|
952
|
+
content = shielded;
|
|
953
|
+
content_len = strlen(content);
|
|
954
|
+
}
|
|
955
|
+
|
|
803
956
|
/* Recursively process nested divs with markdown="1" BEFORE parsing */
|
|
804
957
|
/* This ensures nested divs are processed before cmark-gfm sees them */
|
|
805
958
|
char *processed_content = apex_process_html_markdown_impl(content, img_attrs, full_doc, options);
|
|
@@ -927,6 +1080,7 @@ static char *apex_process_html_markdown_impl(const char *text, void *img_attrs,
|
|
|
927
1080
|
memcpy(write_pos, html_content, html_len);
|
|
928
1081
|
write_pos += html_len;
|
|
929
1082
|
remaining -= html_len;
|
|
1083
|
+
wrote_region = true;
|
|
930
1084
|
|
|
931
1085
|
free(html);
|
|
932
1086
|
}
|
|
@@ -958,6 +1112,34 @@ static char *apex_process_html_markdown_impl(const char *text, void *img_attrs,
|
|
|
958
1112
|
*write_pos++ = '\n';
|
|
959
1113
|
remaining--;
|
|
960
1114
|
}
|
|
1115
|
+
|
|
1116
|
+
if (wrote_region && (fn_stash_len > 0 || (fn_hoisted && fn_hoisted[0]))) {
|
|
1117
|
+
size_t hlen = fn_hoisted ? strlen(fn_hoisted) : 0;
|
|
1118
|
+
size_t extra = 1 + fn_stash_len + 2 + hlen + 1;
|
|
1119
|
+
char *extra_buf = malloc(extra + 1);
|
|
1120
|
+
if (extra_buf) {
|
|
1121
|
+
size_t n = 0;
|
|
1122
|
+
extra_buf[n++] = '\n';
|
|
1123
|
+
if (fn_stash_len > 0) {
|
|
1124
|
+
memcpy(extra_buf + n, fn_stash, fn_stash_len);
|
|
1125
|
+
n += fn_stash_len;
|
|
1126
|
+
extra_buf[n++] = '\n';
|
|
1127
|
+
}
|
|
1128
|
+
if (hlen > 0) {
|
|
1129
|
+
if (fn_stash_len > 0) extra_buf[n++] = '\n';
|
|
1130
|
+
memcpy(extra_buf + n, fn_hoisted, hlen);
|
|
1131
|
+
n += hlen;
|
|
1132
|
+
if (fn_hoisted[hlen - 1] != '\n') extra_buf[n++] = '\n';
|
|
1133
|
+
}
|
|
1134
|
+
ENSURE_OUTPUT_SPACE(n);
|
|
1135
|
+
memcpy(write_pos, extra_buf, n);
|
|
1136
|
+
write_pos += n;
|
|
1137
|
+
remaining -= n;
|
|
1138
|
+
free(extra_buf);
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
free(fn_stash);
|
|
1142
|
+
free(fn_hoisted);
|
|
961
1143
|
} else {
|
|
962
1144
|
/* markdown="0" or no parsing - copy everything as-is */
|
|
963
1145
|
size_t total_len = closing_tag - tag_start;
|
data/lib/apex/version.rb
CHANGED