Handle nested parens in links
Example input: [With parens in the URL](http://en.wikipedia.org/wiki/WIMP_(computing))
This commit is contained in:
parent
67be1bfcb8
commit
c21412ce48
1 changed files with 16 additions and 5 deletions
21
smu.c
21
smu.c
|
@ -233,7 +233,7 @@ dolineprefix(const char *begin, const char *end, int newblock) {
|
||||||
|
|
||||||
int
|
int
|
||||||
dolink(const char *begin, const char *end, int newblock) {
|
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 *desc, *link, *p, *q, *descend, *linkend;
|
||||||
const char *title = NULL, *titleend = NULL;
|
const char *title = NULL, *titleend = NULL;
|
||||||
|
|
||||||
|
@ -251,8 +251,20 @@ dolink(const char *begin, const char *end, int newblock) {
|
||||||
return 0;
|
return 0;
|
||||||
descend = p;
|
descend = p;
|
||||||
link = p + 2;
|
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) {
|
if((p = strpbrk(link, "\"'")) && p < end && q > p) {
|
||||||
sep = p[0]; /* separator: can be " or ' */
|
sep = p[0]; /* separator: can be " or ' */
|
||||||
title = p + 1;
|
title = p + 1;
|
||||||
|
@ -261,12 +273,11 @@ dolink(const char *begin, const char *end, int newblock) {
|
||||||
if(!(p = strchr(title, sep)) || q > end || p > q)
|
if(!(p = strchr(title, sep)) || q > end || p > q)
|
||||||
return 0;
|
return 0;
|
||||||
titleend = p;
|
titleend = p;
|
||||||
len = p + 2 - begin;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
linkend = q;
|
linkend = q;
|
||||||
len = q + 1 - begin;
|
|
||||||
}
|
}
|
||||||
|
len = q + 1 - begin;
|
||||||
if(img) {
|
if(img) {
|
||||||
fputs("<img src=\"", stdout);
|
fputs("<img src=\"", stdout);
|
||||||
hprint(link, linkend);
|
hprint(link, linkend);
|
||||||
|
|
Loading…
Add table
Reference in a new issue