apex-ruby 1.0.48 → 1.0.49
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 +22 -4
- data/ext/apex_ext/apex_src/src/extensions/emoji.c +167 -0
- data/ext/apex_ext/apex_src/src/extensions/relaxed_tables.c +115 -3
- data/ext/apex_ext/apex_src/src/extensions/sup_sub.c +128 -1
- 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: 9ef8ac654a4a4157b887908ebc382df6e02922a487e23a35e429ae4495c012e5
|
|
4
|
+
data.tar.gz: a6474e4891cc24c92ab53fa62907d210d5b2bedbc4c05b16ac43b374affbac73
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 311a4819f5ccc1ad4a65c841f3305aae2ede81e689c07ad8dea36f725b14e6ba06253ec41cd2e4b5040aebdd7afb21244ed89855fb26d2eab91e393286f97ff9
|
|
7
|
+
data.tar.gz: 68a0b352b0e1d73526b23a9f8de32152235987450e8a938677f632ff863f2532583cad4a2788dc2c591cafbe710bcc13325825772fb8fb52d9b2c547de039557
|
|
@@ -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 57
|
|
25
|
+
#define APEX_VERSION_STRING "1.1.57"
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Processor compatibility modes
|
|
@@ -954,7 +954,13 @@ static char *apex_preprocess_autolinks(const char *text, const apex_options *opt
|
|
|
954
954
|
if ((url_len > 6 && strncmp(start, "http://", 7) == 0) ||
|
|
955
955
|
(url_len > 7 && strncmp(start, "https://", 8) == 0) ||
|
|
956
956
|
(url_len > 7 && strncmp(start, "mailto:", 7) == 0)) {
|
|
957
|
-
|
|
957
|
+
/* Escape | in the label so a later table parse does not
|
|
958
|
+
* split the address. The href keeps the raw pipe. */
|
|
959
|
+
size_t label_len = url_len;
|
|
960
|
+
for (size_t i = 0; i < url_len; i++) {
|
|
961
|
+
if (start[i] == '|') label_len++;
|
|
962
|
+
}
|
|
963
|
+
size_t needed = 2 + label_len + 3 + url_len + 2; /* [url](url) */
|
|
958
964
|
if ((size_t)(w - out) + needed + 1 > cap) {
|
|
959
965
|
size_t used = (size_t)(w - out);
|
|
960
966
|
cap = (used + needed + 1) * 2;
|
|
@@ -964,7 +970,10 @@ static char *apex_preprocess_autolinks(const char *text, const apex_options *opt
|
|
|
964
970
|
w = out + used;
|
|
965
971
|
}
|
|
966
972
|
*w++ = '[';
|
|
967
|
-
|
|
973
|
+
for (size_t i = 0; i < url_len; i++) {
|
|
974
|
+
if (start[i] == '|') *w++ = '\\';
|
|
975
|
+
*w++ = start[i];
|
|
976
|
+
}
|
|
968
977
|
*w++ = ']'; *w++ = '(';
|
|
969
978
|
memcpy(w, start, url_len); w += url_len;
|
|
970
979
|
*w++ = ')';
|
|
@@ -1120,7 +1129,13 @@ skip_email_candidate:
|
|
|
1120
1129
|
if (url_len > 0 &&
|
|
1121
1130
|
!(r > text && (r[-1] == '(' || r[-1] == '[')) &&
|
|
1122
1131
|
!(r == text && *r == '#' && (r[1] == ' ' || r[1] == '\t' || r[1] == '\n'))) {
|
|
1123
|
-
|
|
1132
|
+
/* Escape | in the label so a later table parse does not
|
|
1133
|
+
* split the address. The href keeps the raw pipe. */
|
|
1134
|
+
size_t label_len = link_text_len;
|
|
1135
|
+
for (size_t i = 0; i < link_text_len; i++) {
|
|
1136
|
+
if (link_text[i] == '|') label_len++;
|
|
1137
|
+
}
|
|
1138
|
+
size_t needed = 2 + label_len + 3 + href_len + 2; /* [text](href) */
|
|
1124
1139
|
if ((size_t)(w - out) + needed + 1 > cap) {
|
|
1125
1140
|
size_t used = (size_t)(w - out);
|
|
1126
1141
|
cap = (used + needed + 1) * 2;
|
|
@@ -1130,7 +1145,10 @@ skip_email_candidate:
|
|
|
1130
1145
|
w = out + used;
|
|
1131
1146
|
}
|
|
1132
1147
|
*w++ = '[';
|
|
1133
|
-
|
|
1148
|
+
for (size_t i = 0; i < link_text_len; i++) {
|
|
1149
|
+
if (link_text[i] == '|') *w++ = '\\';
|
|
1150
|
+
*w++ = link_text[i];
|
|
1151
|
+
}
|
|
1134
1152
|
*w++ = ']'; *w++ = '(';
|
|
1135
1153
|
memcpy(w, href_text, href_len); w += href_len;
|
|
1136
1154
|
*w++ = ')';
|
|
@@ -44,6 +44,148 @@ static int line_is_indented_code_block(const char *read) {
|
|
|
44
44
|
return *content && !looks_like_list_marker(content);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
/* End of the URL inside a markdown link or image destination. The title
|
|
48
|
+
* after the URL is left for normal scanning. */
|
|
49
|
+
static const char *emoji_destination_url_end(const char *after_open_paren) {
|
|
50
|
+
const char *p = after_open_paren;
|
|
51
|
+
int depth = 0;
|
|
52
|
+
|
|
53
|
+
if (!p) return NULL;
|
|
54
|
+
while (*p == ' ' || *p == '\t') p++;
|
|
55
|
+
if (*p == '<') {
|
|
56
|
+
const char *end = p + 1;
|
|
57
|
+
while (*end && *end != '>' && *end != '\n' && *end != '\r') end++;
|
|
58
|
+
if (*end == '>') return end + 1;
|
|
59
|
+
return NULL;
|
|
60
|
+
}
|
|
61
|
+
if (!*p || *p == '\n' || *p == '\r') return NULL;
|
|
62
|
+
while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') {
|
|
63
|
+
if (*p == '(') depth++;
|
|
64
|
+
else if (*p == ')') {
|
|
65
|
+
if (depth == 0) return p;
|
|
66
|
+
depth--;
|
|
67
|
+
}
|
|
68
|
+
p++;
|
|
69
|
+
}
|
|
70
|
+
return p;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/* Past the closing '>' of an HTML tag or autolink. A stray '<' in prose
|
|
74
|
+
* returns NULL so the rest of the line is still scanned. */
|
|
75
|
+
static const char *emoji_html_tag_end(const char *text) {
|
|
76
|
+
if (text[0] != '<') return NULL;
|
|
77
|
+
const char *p = text + 1;
|
|
78
|
+
if (*p == '/') p++;
|
|
79
|
+
if (!isalpha((unsigned char)*p)) return NULL;
|
|
80
|
+
|
|
81
|
+
char quote = 0;
|
|
82
|
+
for (; *p; p++) {
|
|
83
|
+
if (quote) {
|
|
84
|
+
if (*p == quote) quote = 0;
|
|
85
|
+
} else if (*p == '"' || *p == '\'') {
|
|
86
|
+
quote = *p;
|
|
87
|
+
} else if (*p == '>') {
|
|
88
|
+
return p + 1;
|
|
89
|
+
} else if (*p == '\n' && (p[1] == '\n' || (p[1] == '\r' && p[2] == '\n'))) {
|
|
90
|
+
return NULL;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return NULL;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/* End of a reference-definition destination, including the [label]: prefix.
|
|
97
|
+
* The title after the URL is not included. Footnote definitions return NULL. */
|
|
98
|
+
static const char *emoji_reference_url_end(const char *read) {
|
|
99
|
+
const char *p = read;
|
|
100
|
+
int spaces = 0;
|
|
101
|
+
while (spaces < 3 && *p == ' ') {
|
|
102
|
+
spaces++;
|
|
103
|
+
p++;
|
|
104
|
+
}
|
|
105
|
+
if (*p != '[' || p[1] == '^' || p[1] == ']') return NULL;
|
|
106
|
+
const char *q = p + 1;
|
|
107
|
+
while (*q && *q != ']' && *q != '\n' && *q != '\r') q++;
|
|
108
|
+
if (*q != ']' || q[1] != ':') return NULL;
|
|
109
|
+
q += 2;
|
|
110
|
+
while (*q == ' ' || *q == '\t') q++;
|
|
111
|
+
if (*q == '<') {
|
|
112
|
+
const char *end = q + 1;
|
|
113
|
+
while (*end && *end != '>' && *end != '\n' && *end != '\r') end++;
|
|
114
|
+
if (*end == '>') return end + 1;
|
|
115
|
+
return NULL;
|
|
116
|
+
}
|
|
117
|
+
if (!*q || *q == '\n' || *q == '\r') return NULL;
|
|
118
|
+
int depth = 0;
|
|
119
|
+
const char *end = q;
|
|
120
|
+
while (*end && *end != ' ' && *end != '\t' && *end != '\n' && *end != '\r') {
|
|
121
|
+
if (*end == '(') depth++;
|
|
122
|
+
else if (*end == ')') {
|
|
123
|
+
if (depth == 0) break;
|
|
124
|
+
depth--;
|
|
125
|
+
}
|
|
126
|
+
end++;
|
|
127
|
+
}
|
|
128
|
+
if (end == q) return NULL;
|
|
129
|
+
return end;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/* End of a bare URL at read, or NULL. Stops at whitespace or '<'. */
|
|
133
|
+
static const char *emoji_bare_url_end(const char *read) {
|
|
134
|
+
static const char *const schemes[] = {
|
|
135
|
+
"https://", "http://", "ftp://", "file://", "mailto:", NULL
|
|
136
|
+
};
|
|
137
|
+
for (int i = 0; schemes[i]; i++) {
|
|
138
|
+
size_t n = strlen(schemes[i]);
|
|
139
|
+
if (strncmp(read, schemes[i], n) == 0) {
|
|
140
|
+
const char *p = read + n;
|
|
141
|
+
while (*p && !isspace((unsigned char)*p) && *p != '<') p++;
|
|
142
|
+
return p;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return NULL;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/* End of a source span whose contents are a URL and must not be rewritten. */
|
|
149
|
+
static const char *emoji_source_protected_end(const char *text, const char *read) {
|
|
150
|
+
if (read == text || read[-1] == '\n') {
|
|
151
|
+
const char *url_end = emoji_reference_url_end(read);
|
|
152
|
+
if (url_end) return url_end;
|
|
153
|
+
}
|
|
154
|
+
if (read == text || !isalnum((unsigned char)read[-1])) {
|
|
155
|
+
const char *url_end = emoji_bare_url_end(read);
|
|
156
|
+
if (url_end && url_end > read) return url_end;
|
|
157
|
+
}
|
|
158
|
+
if (read[0] == ']' && read[1] == '(') {
|
|
159
|
+
const char *dest = emoji_destination_url_end(read + 2);
|
|
160
|
+
if (dest && dest > read) return dest;
|
|
161
|
+
}
|
|
162
|
+
if (read[0] == '<') {
|
|
163
|
+
const char *tag = emoji_html_tag_end(read);
|
|
164
|
+
if (tag) return tag;
|
|
165
|
+
}
|
|
166
|
+
return NULL;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
static int emoji_copy_span(char **output, char **write, size_t *remaining, size_t *capacity,
|
|
170
|
+
const char **read, const char *end) {
|
|
171
|
+
size_t n = (size_t)(end - *read);
|
|
172
|
+
if (n > *remaining) {
|
|
173
|
+
size_t written = (size_t)(*write - *output);
|
|
174
|
+
size_t grown = (*capacity * 2) + n + 1;
|
|
175
|
+
char *buf = realloc(*output, grown);
|
|
176
|
+
if (!buf) return 0;
|
|
177
|
+
*output = buf;
|
|
178
|
+
*write = buf + written;
|
|
179
|
+
*capacity = grown;
|
|
180
|
+
*remaining = grown - written;
|
|
181
|
+
}
|
|
182
|
+
memcpy(*write, *read, n);
|
|
183
|
+
*write += n;
|
|
184
|
+
*remaining -= n;
|
|
185
|
+
*read = end;
|
|
186
|
+
return 1;
|
|
187
|
+
}
|
|
188
|
+
|
|
47
189
|
/**
|
|
48
190
|
* True if :name: is a bounded shortcode (not inside a word or digit run).
|
|
49
191
|
* Left of the opening colon and right of the closing colon must not be alphanumeric.
|
|
@@ -325,6 +467,15 @@ char *apex_replace_emoji(const char *html) {
|
|
|
325
467
|
continue;
|
|
326
468
|
}
|
|
327
469
|
|
|
470
|
+
/* A URL in rendered text, including an autolink's visible address,
|
|
471
|
+
* keeps :shortcodes: as typed. */
|
|
472
|
+
if ((read == html || !isalnum((unsigned char)read[-1]))) {
|
|
473
|
+
const char *url_end = emoji_bare_url_end(read);
|
|
474
|
+
if (url_end && url_end > read &&
|
|
475
|
+
emoji_copy_span(&output, &write, &remaining, &capacity, &read, url_end))
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
|
|
328
479
|
/* Skip emoji inside HTML comments (citation placeholders are
|
|
329
480
|
* <!--CITE:key-->; a key like "Wayne:" contains ":Wayne:" and would
|
|
330
481
|
* otherwise fuzzy-match to :wave: → <!--CITE👋-->). */
|
|
@@ -593,6 +744,14 @@ char *apex_replace_emoji_text(const char *text) {
|
|
|
593
744
|
continue;
|
|
594
745
|
}
|
|
595
746
|
|
|
747
|
+
/* Leave :shortcodes: inside destinations, tags, and URLs as typed. */
|
|
748
|
+
{
|
|
749
|
+
const char *protected_end = emoji_source_protected_end(text, read);
|
|
750
|
+
if (protected_end &&
|
|
751
|
+
emoji_copy_span(&output, &write, &remaining, &capacity, &read, protected_end))
|
|
752
|
+
continue;
|
|
753
|
+
}
|
|
754
|
+
|
|
596
755
|
if (*read == ':') {
|
|
597
756
|
/* Look for closing : */
|
|
598
757
|
const char *end = strchr(read + 1, ':');
|
|
@@ -857,6 +1016,14 @@ char *apex_autocorrect_emoji_names(const char *text) {
|
|
|
857
1016
|
continue;
|
|
858
1017
|
}
|
|
859
1018
|
|
|
1019
|
+
/* Leave :shortcodes: inside destinations, tags, and URLs as typed. */
|
|
1020
|
+
{
|
|
1021
|
+
const char *protected_end = emoji_source_protected_end(text, read);
|
|
1022
|
+
if (protected_end &&
|
|
1023
|
+
emoji_copy_span(&output, &write, &remaining, &capacity, &read, protected_end))
|
|
1024
|
+
continue;
|
|
1025
|
+
}
|
|
1026
|
+
|
|
860
1027
|
if (*read == ':') {
|
|
861
1028
|
/* Look for closing : */
|
|
862
1029
|
const char *end = strchr(read + 1, ':');
|
|
@@ -87,6 +87,114 @@ static size_t skip_wiki_link(const char *line, size_t len, size_t i) {
|
|
|
87
87
|
return i;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
/* If an inline link [label](dest) or [label][id] opens at i and closes on
|
|
91
|
+
* this line, return the index of its last character. Pipes in the label or
|
|
92
|
+
* destination are not cell separators. An autolinked URL is this form. */
|
|
93
|
+
static size_t skip_inline_link(const char *line, size_t len, size_t i) {
|
|
94
|
+
if (i >= len || line[i] != '[') return i;
|
|
95
|
+
if (i + 1 < len && line[i + 1] == '[') return i;
|
|
96
|
+
|
|
97
|
+
size_t j = i + 1;
|
|
98
|
+
int depth = 1;
|
|
99
|
+
while (j < len && depth > 0) {
|
|
100
|
+
if (line[j] == '\\' && j + 1 < len) {
|
|
101
|
+
j += 2;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (line[j] == '[') depth++;
|
|
105
|
+
else if (line[j] == ']') depth--;
|
|
106
|
+
if (depth == 0) break;
|
|
107
|
+
j++;
|
|
108
|
+
}
|
|
109
|
+
if (j >= len || line[j] != ']') return i;
|
|
110
|
+
|
|
111
|
+
if (j + 1 < len && line[j + 1] == '(') {
|
|
112
|
+
size_t p = j + 2;
|
|
113
|
+
int paren = 1;
|
|
114
|
+
char quote = 0;
|
|
115
|
+
while (p < len && paren > 0) {
|
|
116
|
+
char c = line[p];
|
|
117
|
+
if (quote) {
|
|
118
|
+
if (c == '\\' && p + 1 < len) {
|
|
119
|
+
p += 2;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (c == quote) quote = 0;
|
|
123
|
+
p++;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
if (c == '"' || c == '\'') {
|
|
127
|
+
quote = c;
|
|
128
|
+
p++;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (c == '(') paren++;
|
|
132
|
+
else if (c == ')') {
|
|
133
|
+
paren--;
|
|
134
|
+
if (paren == 0) return p;
|
|
135
|
+
}
|
|
136
|
+
p++;
|
|
137
|
+
}
|
|
138
|
+
return i;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (j + 1 < len && line[j + 1] == '[') {
|
|
142
|
+
size_t p = j + 2;
|
|
143
|
+
while (p < len && line[p] != ']') p++;
|
|
144
|
+
if (p < len && line[p] == ']') return p;
|
|
145
|
+
return i;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return j;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/* Past a raw HTML tag on this line, so a pipe in an attribute is not a cell. */
|
|
152
|
+
static size_t skip_html_tag(const char *line, size_t len, size_t i) {
|
|
153
|
+
if (i >= len || line[i] != '<') return i;
|
|
154
|
+
size_t p = i + 1;
|
|
155
|
+
if (p < len && line[p] == '/') p++;
|
|
156
|
+
if (p >= len || !isalpha((unsigned char)line[p])) return i;
|
|
157
|
+
|
|
158
|
+
char quote = 0;
|
|
159
|
+
for (; p < len; p++) {
|
|
160
|
+
if (quote) {
|
|
161
|
+
if (line[p] == quote) quote = 0;
|
|
162
|
+
} else if (line[p] == '"' || line[p] == '\'') {
|
|
163
|
+
quote = line[p];
|
|
164
|
+
} else if (line[p] == '>') {
|
|
165
|
+
return p;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return i;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/* Last character of a bare URL at i, or i if read is not one. */
|
|
172
|
+
static size_t skip_bare_url(const char *line, size_t len, size_t i) {
|
|
173
|
+
if (i > 0 && isalnum((unsigned char)line[i - 1])) return i;
|
|
174
|
+
static const char *const schemes[] = {
|
|
175
|
+
"https://", "http://", "ftp://", "file://", "mailto:", NULL
|
|
176
|
+
};
|
|
177
|
+
for (int s = 0; schemes[s]; s++) {
|
|
178
|
+
size_t n = strlen(schemes[s]);
|
|
179
|
+
if (i + n <= len && strncmp(line + i, schemes[s], n) == 0) {
|
|
180
|
+
size_t j = i + n;
|
|
181
|
+
while (j < len && line[j] != ' ' && line[j] != '\t' && line[j] != '<') j++;
|
|
182
|
+
if (j > i) return j - 1;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return i;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
static size_t skip_non_cell(const char *line, size_t len, size_t i) {
|
|
189
|
+
size_t end = skip_wiki_link(line, len, i);
|
|
190
|
+
if (end != i) return end;
|
|
191
|
+
end = skip_inline_link(line, len, i);
|
|
192
|
+
if (end != i) return end;
|
|
193
|
+
end = skip_html_tag(line, len, i);
|
|
194
|
+
if (end != i) return end;
|
|
195
|
+
return skip_bare_url(line, len, i);
|
|
196
|
+
}
|
|
197
|
+
|
|
90
198
|
static int count_columns(const char *line, size_t len) {
|
|
91
199
|
if (!line || len == 0) return -1;
|
|
92
200
|
|
|
@@ -101,9 +209,13 @@ static int count_columns(const char *line, size_t len) {
|
|
|
101
209
|
|
|
102
210
|
bool starts_with_pipe = (start < len && line[start] == '|');
|
|
103
211
|
|
|
104
|
-
/* Count pipes */
|
|
212
|
+
/* Count pipes that separate cells. Pipes inside links and URLs do not. */
|
|
105
213
|
for (size_t i = start; i < len; i++) {
|
|
106
|
-
|
|
214
|
+
size_t skipped = skip_non_cell(line, len, i);
|
|
215
|
+
if (skipped != i) {
|
|
216
|
+
i = skipped;
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
107
219
|
if (line[i] == '|') {
|
|
108
220
|
has_pipe = true;
|
|
109
221
|
pipe_count++;
|
|
@@ -337,7 +449,7 @@ static bool is_table_row(const char *line, size_t len) {
|
|
|
337
449
|
bool has_content = false;
|
|
338
450
|
|
|
339
451
|
for (size_t i = 0; i < len; i++) {
|
|
340
|
-
size_t link_end =
|
|
452
|
+
size_t link_end = skip_non_cell(line, len, i);
|
|
341
453
|
if (link_end != i) {
|
|
342
454
|
has_content = true;
|
|
343
455
|
i = link_end;
|
|
@@ -21,6 +21,109 @@ static bool looks_like_list_marker(const char *p) {
|
|
|
21
21
|
return false;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/* Just past the closing ')' of a markdown link or image destination.
|
|
25
|
+
* Quotes hide parentheses. A newline means the destination is unclosed. */
|
|
26
|
+
static const char *markdown_destination_end(const char *after_open_paren) {
|
|
27
|
+
int depth = 1;
|
|
28
|
+
bool in_quote = false;
|
|
29
|
+
char quote = 0;
|
|
30
|
+
const char *p = after_open_paren;
|
|
31
|
+
|
|
32
|
+
if (!p) return NULL;
|
|
33
|
+
while (*p && *p != '\n' && *p != '\r') {
|
|
34
|
+
if (in_quote) {
|
|
35
|
+
if (*p == '\\' && p[1]) {
|
|
36
|
+
p += 2;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (*p == quote) in_quote = false;
|
|
40
|
+
p++;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (*p == '"' || *p == '\'') {
|
|
44
|
+
in_quote = true;
|
|
45
|
+
quote = *p++;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (*p == '(') {
|
|
49
|
+
depth++;
|
|
50
|
+
} else if (*p == ')') {
|
|
51
|
+
depth--;
|
|
52
|
+
if (depth == 0) return p + 1;
|
|
53
|
+
}
|
|
54
|
+
p++;
|
|
55
|
+
}
|
|
56
|
+
return NULL;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* If text starts an HTML tag (<name or </name) or an autolink, return a
|
|
60
|
+
* pointer just past its closing '>'. Gives up at a blank line so a stray
|
|
61
|
+
* '<' in prose does not swallow the rest of the document. */
|
|
62
|
+
static const char *html_tag_end(const char *text) {
|
|
63
|
+
if (text[0] != '<') return NULL;
|
|
64
|
+
const char *p = text + 1;
|
|
65
|
+
if (*p == '/') p++;
|
|
66
|
+
if (!isalpha((unsigned char)*p)) return NULL;
|
|
67
|
+
|
|
68
|
+
char quote = 0;
|
|
69
|
+
for (; *p; p++) {
|
|
70
|
+
if (quote) {
|
|
71
|
+
if (*p == quote) quote = 0;
|
|
72
|
+
} else if (*p == '"' || *p == '\'') {
|
|
73
|
+
quote = *p;
|
|
74
|
+
} else if (*p == '>') {
|
|
75
|
+
return p + 1;
|
|
76
|
+
} else if (*p == '\n' && (p[1] == '\n' || (p[1] == '\r' && p[2] == '\n'))) {
|
|
77
|
+
return NULL;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return NULL;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* End of a link reference definition line, not including the line break.
|
|
84
|
+
* Footnote definitions ([^id]:) are prose and return NULL. */
|
|
85
|
+
static const char *reference_definition_line_end(const char *read) {
|
|
86
|
+
const char *p = read;
|
|
87
|
+
int spaces = 0;
|
|
88
|
+
while (spaces < 3 && *p == ' ') {
|
|
89
|
+
spaces++;
|
|
90
|
+
p++;
|
|
91
|
+
}
|
|
92
|
+
if (*p != '[' || p[1] == '^' || p[1] == ']') return NULL;
|
|
93
|
+
const char *q = p + 1;
|
|
94
|
+
while (*q && *q != ']' && *q != '\n' && *q != '\r') q++;
|
|
95
|
+
if (*q != ']' || q[1] != ':') return NULL;
|
|
96
|
+
while (*q && *q != '\n' && *q != '\r') q++;
|
|
97
|
+
return q;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/* End of a bare URL starting at read, or NULL. Stops at whitespace or '<'. */
|
|
101
|
+
static const char *bare_url_end(const char *read) {
|
|
102
|
+
static const char *const schemes[] = {
|
|
103
|
+
"https://", "http://", "ftp://", "file://", "mailto:", NULL
|
|
104
|
+
};
|
|
105
|
+
for (int i = 0; schemes[i]; i++) {
|
|
106
|
+
size_t n = strlen(schemes[i]);
|
|
107
|
+
if (strncmp(read, schemes[i], n) == 0) {
|
|
108
|
+
const char *p = read + n;
|
|
109
|
+
while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r' && *p != '<')
|
|
110
|
+
p++;
|
|
111
|
+
return p;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return NULL;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
static bool copy_span(char **write, size_t *remaining, const char **read, const char *end) {
|
|
118
|
+
size_t n = (size_t)(end - *read);
|
|
119
|
+
if (n > *remaining) return false;
|
|
120
|
+
memcpy(*write, *read, n);
|
|
121
|
+
*write += n;
|
|
122
|
+
*remaining -= n;
|
|
123
|
+
*read = end;
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
|
|
24
127
|
/** True if we're at the start of a line that is an indented code block (4+ spaces or tab)
|
|
25
128
|
* and not a list line. List lines (nested or continuation) should still get sup/sub. */
|
|
26
129
|
static bool line_is_indented_code_block(const char *read) {
|
|
@@ -164,6 +267,29 @@ char *apex_process_sup_sub(const char *text) {
|
|
|
164
267
|
continue;
|
|
165
268
|
}
|
|
166
269
|
|
|
270
|
+
/* Copy link destinations, HTML tags, autolinks, reference definitions,
|
|
271
|
+
* and bare URLs verbatim. ~ and ^ inside them are not sup/sub. */
|
|
272
|
+
if (read == text || read[-1] == '\n') {
|
|
273
|
+
const char *line_end = reference_definition_line_end(read);
|
|
274
|
+
if (line_end && copy_span(&write, &remaining, &read, line_end))
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
if (read == text || !isalnum((unsigned char)read[-1])) {
|
|
278
|
+
const char *url_end = bare_url_end(read);
|
|
279
|
+
if (url_end && url_end > read && copy_span(&write, &remaining, &read, url_end))
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (*read == ']' && read[1] == '(') {
|
|
283
|
+
const char *dest_end = markdown_destination_end(read + 2);
|
|
284
|
+
if (dest_end && copy_span(&write, &remaining, &read, dest_end))
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (*read == '<') {
|
|
288
|
+
const char *tag_end = html_tag_end(read);
|
|
289
|
+
if (tag_end && copy_span(&write, &remaining, &read, tag_end))
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
|
|
167
293
|
/* Check for superscript: ^word (only first word, stops at space, ^, or end) */
|
|
168
294
|
/* Skip if it's part of a footnote reference pattern [^ */
|
|
169
295
|
if (*read == '^' && read[1] != '\0' && read[1] != ' ' && read[1] != '\t' && read[1] != '\n' && read[1] != '^') {
|
|
@@ -285,7 +411,8 @@ char *apex_process_sup_sub(const char *text) {
|
|
|
285
411
|
}
|
|
286
412
|
|
|
287
413
|
/* Check for tilde-based syntax: ~text~ (underline), ~word (subscript), or ~~text~~ (strikethrough, already skipped) */
|
|
288
|
-
|
|
414
|
+
/* ~/ is a home path, not subscript or underline. */
|
|
415
|
+
if (*read == '~' && read[1] != '\0' && read[1] != ' ' && read[1] != '\t' && read[1] != '\n' && read[1] != '~' && read[1] != '/') {
|
|
289
416
|
const char *content_start = read + 1;
|
|
290
417
|
const char *content_end = content_start;
|
|
291
418
|
const char *closing_tilde = NULL;
|
data/lib/apex/version.rb
CHANGED