Skip to content

Commit

Permalink
Handle nested parens in links
Browse files Browse the repository at this point in the history
Example input:

[With parens in the URL](http://en.wikipedia.org/wiki/WIMP_(computing))
  • Loading branch information
karlb authored and Gottox committed Jul 1, 2020
1 parent 67be1bf commit c21412c
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions smu.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ dolineprefix(const char *begin, const char *end, int newblock) {

int
dolink(const char *begin, const char *end, int newblock) {
int img, len, sep;
int img, len, sep, parens_depth = 1;
const char *desc, *link, *p, *q, *descend, *linkend;
const char *title = NULL, *titleend = NULL;

Expand All @@ -251,8 +251,20 @@ dolink(const char *begin, const char *end, int newblock) {
return 0;
descend = p;
link = p + 2;
if(!(q = strchr(link, ')')) || q > end)
return 0;

/* find end of link while handling nested parens */
q = link;
while(parens_depth) {
if(!(q = strpbrk(q, "()")) || q > end)
return 0;
if(*q == '(')
parens_depth++;
else
parens_depth--;
if(parens_depth && q < end)
q++;
}

if((p = strpbrk(link, "\"'")) && p < end && q > p) {
sep = p[0]; /* separator: can be " or ' */
title = p + 1;
Expand All @@ -261,12 +273,11 @@ dolink(const char *begin, const char *end, int newblock) {
if(!(p = strchr(title, sep)) || q > end || p > q)
return 0;
titleend = p;
len = p + 2 - begin;
}
else {
linkend = q;
len = q + 1 - begin;
}
len = q + 1 - begin;
if(img) {
fputs("<img src=\"", stdout);
hprint(link, linkend);
Expand Down

0 comments on commit c21412c

Please sign in to comment.