From 71a195a0e4f603669657dd4534dbdcccafcd016c Mon Sep 17 00:00:00 2001 From: wrmr Date: Sat, 9 Nov 2024 03:36:03 -0500 Subject: fix plaintext url parsing --- parse.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'parse.c') diff --git a/parse.c b/parse.c index 7af7e52..b2ac019 100644 --- a/parse.c +++ b/parse.c @@ -9,18 +9,15 @@ int isurlch(char c) { return isalpha(c) || isdigit(c) || c == '-' || c == '.' || c == '_' || c == '~' || c == '!' || c == '$' || c == '\'' || c == '(' || c == ')' || c == '*' || c == '+' || c == ',' || c == ';' || c == '=' || c == '%' || c == '@' || c == ':' || c == '/'; } -void parse_plain_url(struct doc *d, struct doc_line *l, size_t i) { +void parse_plain_url(struct doc *d, strv_t ln, size_t i) { size_t start = i - 1; - while (start > 0 && isalpha(l->txt[start])) start--; - if (!isalpha(l->txt[start])) start++; + while (start > 0 && isalpha(ln.s[start])) start--; + if (!isalpha(ln.s[start])) start++; size_t end = i + 3; - while (end < l->len && isurlch(l->txt[end])) end++; + while (end < ln.n && isurlch(ln.s[end])) end++; if (end == i + 3) return; - strv_t url = { - &l->txt[start], - end - start - }; - l->link = doc_add_link(d, url); + strv_t url = { &ln.s[start], end - start }; + doc_line_at(d, d->latest)->link = doc_add_link(d, url); } int parse_plain(struct doc *d, const str_t *b) { @@ -28,11 +25,9 @@ int parse_plain(struct doc *d, const str_t *b) { strv_t ln, buf = (strv_t) { b->buf, b->sz }; while (strv_split(&buf, '\n', &ln)) { if (ln.n > 0 && ln.s[ln.n - 1] == '\r') ln.n--; - struct doc_line *l = doc_line_at(d, d->latest); for (size_t j = 1; j + 2 < ln.n; j++) { if (ln.s[j] == ':' && ln.s[j + 1] == '/' && ln.s[j + 2] == '/') { - parse_plain_url(d, l, j); - break; + parse_plain_url(d, ln, j); } } doc_add_line(d, ln); -- cgit 1.4.1-2-gfad0