From cbb3b45a8d16edff5b80cbbfdfe8377da926fa94 Mon Sep 17 00:00:00 2001 From: David Parsons Date: Tue, 16 Jun 2009 15:12:22 -0700 Subject: [PATCH] Update some tests, add some more tests from Mike Schiraldi, redo puturl and the url grabbers to do debackslashification in puturl --- VERSION | 2 +- generate.c | 37 ++++++++++++++++++++++++++----------- tests/backslash.t | 7 ++++--- tests/schiraldi.t | 16 ++++++++++++++++ 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/VERSION b/VERSION index 428b770..1c99cf0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.4.3 +1.4.4 diff --git a/generate.c b/generate.c index 3072459..28edf22 100644 --- a/generate.c +++ b/generate.c @@ -323,7 +323,7 @@ ___mkd_reparse(char *bfr, int size, int flags, MMIOT *f) * write out a url, escaping problematic characters */ static void -puturl(char *s, int size, MMIOT *f) +puturl(char *s, int size, MMIOT *f, int display) { unsigned char c; @@ -334,7 +334,13 @@ puturl(char *s, int size, MMIOT *f) Qstring("&", f); else if ( c == '<' ) Qstring("<", f); - else if ( isalnum(c) || ispunct(c) ) + else if ( c == '\\' ) { + if ( size && (ispunct(*s)||isspace(*s)) ) + /* silently discard the backslash */ ; + else + Qchar(c, f); + } + else if ( isalnum(c) || ispunct(c) || (display && isspace(c)) ) Qchar(c, f); else Qprintf(f, "%%%02X", c); @@ -463,7 +469,7 @@ linkysize(MMIOT *f, Footnote *ref) static int linkyurl(MMIOT *f, int image, Footnote *p) { - int c; + int c, size; int mayneedtotrim=0; if ( (c = eatspace(f)) == EOF ) @@ -474,16 +480,19 @@ linkyurl(MMIOT *f, int image, Footnote *p) mayneedtotrim=1; } - while ( (c = peek(f,1)) != ')' ) { + T(p->link) = cursor(f); + for ( S(p->link)=0; (c = peek(f,1)) != ')'; ++S(p->link) ) { if ( c == EOF ) return 0; else if ( (c == '"' || c == '\'') && linkytitle(f, c, p) ) break; else if ( image && (c == '=') && linkysize(f, p) ) break; - else if ( (c == '\\') && ispunct(peek(f,2)) ) + else if ( (c == '\\') && ispunct(peek(f,2)) ) { + ++S(p->link); pull(f); - EXPAND(p->link) = pull(f); + } + pull(f); } if ( peek(f, 1) == ')' ) pull(f); @@ -609,8 +618,8 @@ linkyformat(MMIOT *f, Cstring text, int image, Footnote *ref) if ( tag->kind & IS_URL ) { if ( f->base && T(ref->link) && (T(ref->link)[tag->szpat] == '/') ) - puturl(f->base, strlen(f->base), f); - puturl(T(ref->link) + tag->szpat, S(ref->link) - tag->szpat, f); + puturl(f->base, strlen(f->base), f, 0); + puturl(T(ref->link) + tag->szpat, S(ref->link) - tag->szpat, f, 0); } else ___mkd_reparse(T(ref->link) + tag->szpat, S(ref->link) - tag->szpat, INSIDE_TAG, f); @@ -811,9 +820,9 @@ process_possible_link(MMIOT *f, int size) } else if ( isautoprefix(text) ) { Qstring("", f); - puturl(text,size,f); + puturl(text,size,f, 1); Qstring("", f); return 1; } @@ -839,6 +848,10 @@ maybe_tag_or_link(MMIOT *f) for ( size=0; (c = peek(f, size+1)) != '>'; size++) { if ( c == EOF ) return 0; + else if ( c == '\\' ) { + maybetag=0; + size++; + } else if ( isspace(c) ) break; else if ( ! (c == '/' || isalnum(c) ) ) @@ -875,7 +888,9 @@ maybe_autolink(MMIOT *f) /* greedily scan forward for the end of a legitimate link. */ for ( size=0; (c=peek(f, size+1)) != EOF; size++ ) - if ( !(isalnum(c) || strchr("/:._%~@", c)) ) + if ( c == '\\' ) + ++size; + else if ( !(isalnum(c) || strchr("/:._%~@&?=", c) || (c & 0x80)) ) break; if ( (size > 1) && process_possible_link(f, size) ) { diff --git a/tests/backslash.t b/tests/backslash.t index fb4601e..663c121 100644 --- a/tests/backslash.t +++ b/tests/backslash.t @@ -9,17 +9,18 @@ try() { ./echo -n " $S " Q=`./echo "$2" | ./markdown` - count=`./echo "$Q" | grep "$3" | wc -l` - if [ $count -eq 1 ]; then + if [ "$3" = "$Q" ]; then ./echo "ok" else ./echo "FAILED" + ./echo "wanted: $3" + ./echo "got: $Q" rc=1 fi } try 'backslashes in []()' '[foo](http://\this\is\.a\test\(here\))' \ -'

foo

' +'

foo

' exit $rc diff --git a/tests/schiraldi.t b/tests/schiraldi.t index f277b6e..5ed3083 100644 --- a/tests/schiraldi.t +++ b/tests/schiraldi.t @@ -53,4 +53,20 @@ try -fnopants '[]() with a single quote mark' \ '[Poe'"'"'s law](http://rationalwiki.com/wiki/Poe'"'"'s_Law)' \ '

Poe'"'"'s law

' +try -fautolink 'autolink url with escaped spaces' \ + 'http://\(here\ I\ am\)' \ + '

http://(here I am)

' + +try -fautolink 'autolink café_racer' \ + 'http://en.wikipedia.org/wiki/café_racer' \ + '

http://en.wikipedia.org/wiki/caf%C3%A9_racer

' + +try -fautolink 'autolink url with arguments' \ + 'http://foo.bar?a&b=c' \ + '

http://foo.bar?a&b=c

' + +try -fautolink 'autolink url with escaped ()' \ + 'http://a.com/\(foo\)' \ + '

http://a.com/(foo)

' + exit $rc