Expand embedded latex to support $$ .. $$, \[ .. \], and \( .. \) escapes

This commit is contained in:
david parsons
2016-02-20 22:10:46 -08:00
parent 5d0d282906
commit a263259aae
3 changed files with 32 additions and 9 deletions
+21 -8
View File
@@ -1208,18 +1208,23 @@ smartypants(int c, int *flags, MMIOT *f)
#if WITH_LATEX
/* process latex with \(, \) delimiters
/* process latex with arbitrary 2-character ( $$ .. $$, \[ .. \], \( .. \)
* delimiters
*/
static int
mathhandler(MMIOT *f, int endtick) {
int i = 0, size;
mathhandler(MMIOT *f, int e1, e2)
{
int i = 0;
while(peek(f, ++i) != EOF)
if (peek(f, i) == '\\' && peek(f, i+1) == endtick) {
Qstring("\\(", f);
while(i-->-1) cputc(pull(f), f);
while(peek(f, ++i) != EOF) {
if (peek(f, i) == e1 && peek(f, i+1) == e2) {
cputc(peek(f,-1), f);
cputc(peek(f, 0), f);
while ( i-- > -1 )
cputc(pull(f), f);
return 1;
}
}
return 0;
}
#endif
@@ -1406,7 +1411,8 @@ text(MMIOT *f)
break;
#if WITH_LATEX
case '(': if ( mathhandler(f, ')') )
case '[':
case '(': if ( mathhandler(f, '\\', (c =='(')?')':']') )
break;
/* else fall through to default */
#endif
@@ -1436,6 +1442,13 @@ text(MMIOT *f)
Qchar(c, f);
break;
#if WITH_LATEX
case '$': if ( ((c = pull(f)) == '$') && mathhandler(f, '$', '$') )
break;
Qchar('$', f);
/* fall through to default */
#endif
default: Qchar(c, f);
break;
}
+3
View File
@@ -12,5 +12,8 @@ try 'latex passthrough' '\(\tex\)' '<p>\(\tex\)</p>'
try 'latex w/ special characters' 'Equation:\(a<+>b\).' \
'<p>Equation:\(a&lt;+&gt;b\).</p>'
try 'latex with $$ .. $$' '$$foo$$' '<p>$$foo$$</p>'
try 'latex with \[ .. \]' '\[foo\]' '<p>\[foo\]</p>'
summary $0
exit $rc
+8 -1
View File
@@ -10,7 +10,14 @@ try 'url contains +' '[hehehe](u+rl)' '<p><a href="u+rl">hehehe</a></p>'
try 'url contains "' '[hehehe](u"rl)' '<p><a href="u%22rl">hehehe</a></p>'
try 'url contains <' '[hehehe](u<rl)' '<p><a href="u&lt;rl">hehehe</a></p>'
try 'url contains whitespace' '[ha](r u)' '<p><a href="r%20u">ha</a></p>'
try 'label contains escaped []s' '[a\[b\]c](d)' '<p><a href="d">a[b]c</a></p>'
# latex collides with this test
if ./markdown -V | grep LATEX >/dev/null; then
RESULT='<p><a href="d">a\[b\]c</a></p>'
else
RESULT='<p><a href="d">a[b]c</a></p>'
fi
try 'label contains escaped []s' '[a\[b\]c](d)' "$RESULT"
try '<label> w/o title' '[hello](<sailor>)' '<p><a href="sailor">hello</a></p>'
try '<label> with title' '[hello](<sailor> "boy")' '<p><a href="sailor" title="boy">hello</a></p>'