Skip to content

Commit

Permalink
Fix a bug where autolink + github flavored markdown absorbs the ^C eoln
Browse files Browse the repository at this point in the history
character into a link at the end of a line.
  • Loading branch information
Orc committed Jun 27, 2014
1 parent 196a7ae commit d3d5afa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
29 changes: 21 additions & 8 deletions generate.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ push(char *bfr, int size, MMIOT *f)
}


/*
* push a character into the generator input buffer
*/
static void
pushc(char c, MMIOT *f)
{
EXPAND(f->in) = c;
}


/* look <i> characters ahead of the cursor.
*/
static inline int
Expand Down Expand Up @@ -207,7 +217,7 @@ ___mkd_reparse(char *bfr, int size, int flags, MMIOT *f, char *esc)
sub.esc = f->esc;

push(bfr, size, &sub);
EXPAND(sub.in) = 0;
pushc(0, &sub);
S(sub.in)--;

text(&sub);
Expand Down Expand Up @@ -262,7 +272,7 @@ puturl(char *s, int size, MMIOT *f, int display)
Qstring("%22", f);
else if ( isalnum(c) || ispunct(c) || (display && isspace(c)) )
Qchar(c, f);
else if ( c == 003 ) /* untokenize ^C */
else if ( c == MKD_EOLN ) /* untokenize hard return */
Qstring(" ", f);
else
Qprintf(f, "%%%02X", c);
Expand Down Expand Up @@ -837,7 +847,7 @@ code(MMIOT *f, char *s, int length)
int i,c;

for ( i=0; i < length; i++ )
if ( (c = s[i]) == 003) /* ^C: expand back to 2 spaces */
if ( (c = s[i]) == MKD_EOLN) /* ^C: expand back to 2 spaces */
Qstring(" ", f);
else if ( c == '\\' && (i < length-1) && escaped(f, s[i+1]) )
cputc(s[++i], f);
Expand Down Expand Up @@ -1044,13 +1054,14 @@ maybe_autolink(MMIOT *f)

/* greedily scan forward for the end of a legitimate link.
*/
for ( size=0; (c=peek(f, size+1)) != EOF; size++ )
for ( size=0; (c=peek(f, size+1)) != EOF; size++ ) {
if ( c == '\\' ) {
if ( peek(f, size+2) != EOF )
++size;
}
else if ( isspace(c) || strchr("'\"()[]{}<>`", c) )
else if ( isspace(c) || strchr("'\"()[]{}<>`", c) || c == MKD_EOLN )
break;
}

if ( (size > 1) && process_possible_link(f, size) ) {
shift(f, size);
Expand Down Expand Up @@ -1243,7 +1254,8 @@ text(MMIOT *f)
switch (c) {
case 0: break;

case 3: Qstring(tag_text(f) ? " " : "<br/>", f);
case MKD_EOLN:
Qstring(tag_text(f) ? " " : "<br/>", f);
break;

case '>': if ( tag_text(f) )
Expand Down Expand Up @@ -1567,13 +1579,14 @@ printblock(Paragraph *pp, MMIOT *f)
&& T(t->text)[S(t->text)-2] == ' '
&& T(t->text)[S(t->text)-1] == ' ' ) {
push(T(t->text), S(t->text)-2, f);
push("\003\n", 2, f);
pushc(MKD_EOLN, f);
pushc('\n', f);
}
else {
___mkd_tidy(&t->text);
push(T(t->text), S(t->text), f);
if ( t->next )
push("\n", 1, f);
pushc('\n', f);
}
}
t = t->next;
Expand Down
3 changes: 3 additions & 0 deletions markdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ typedef struct mmiot {
} MMIOT;


#define MKD_EOLN 3


/*
* the mkdio text input functions return a document structure,
* which contains a header (retrieved from the document if
Expand Down
6 changes: 6 additions & 0 deletions tests/defects.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ try 'masses of non-block html' \
<br>
<span>bar</span><br></p>'

try -fautolink -G 'autolink + github-flavoured markdown' \
'http://foo
bar' \
'<p><a href="http://foo">http://foo</a><br/>
bar</p>'

summary $0
exit $rc

0 comments on commit d3d5afa

Please sign in to comment.