From 94e4a5ec44ea3ac53a5cd69479ba716862f86c07 Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Sat, 18 Apr 2015 00:53:20 +0300
Subject: [PATCH 1/2] MD Reader: Test for smart `'` after inline math

---
 tests/Tests/Readers/Markdown.hs | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tests/Tests/Readers/Markdown.hs b/tests/Tests/Readers/Markdown.hs
index 03884a8e5..6c8e9f306 100644
--- a/tests/Tests/Readers/Markdown.hs
+++ b/tests/Tests/Readers/Markdown.hs
@@ -208,6 +208,9 @@ tests = [ testGroup "inline code"
           , test markdownSmart "apostrophe in French"
             ("À l'arrivée de la guerre, le thème de l'«impossibilité du socialisme»"
             =?> para "À l’arrivée de la guerre, le thème de l’«impossibilité du socialisme»")
+          , test markdownSmart "apostrophe after math" $ -- issue #1909
+              "The value of the $x$'s and the systems' condition." =?>
+              para (text "The value of the " <> math "x" <> text "\8217s and the systems\8217 condition.")
           ]
         , testGroup "footnotes"
           [ "indent followed by newline and flush-left text" =:

From 4229cf2d92faf5774fe1a3a9c89a5de885cf75cd Mon Sep 17 00:00:00 2001
From: Nikolay Yakimov <root@livid.pp.ru>
Date: Sat, 18 Apr 2015 01:18:06 +0300
Subject: [PATCH 2/2] MD Reader: Smart `'` after inline math

Closes #1909.

Adds new parser combinator to Parsing.hs

`a <+?> b`

:   if a succeeds, applies b and mappends
    output (if any) to result of a. If b fails,
    it's just a, if a fails, whole expression fails.
---
 src/Text/Pandoc/Parsing.hs          | 7 ++++++-
 src/Text/Pandoc/Readers/Markdown.hs | 3 ++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/Text/Pandoc/Parsing.hs b/src/Text/Pandoc/Parsing.hs
index c18aa331f..d30c74230 100644
--- a/src/Text/Pandoc/Parsing.hs
+++ b/src/Text/Pandoc/Parsing.hs
@@ -161,7 +161,8 @@ module Text.Pandoc.Parsing ( anyLine,
                              setSourceColumn,
                              setSourceLine,
                              newPos,
-                             addWarning
+                             addWarning,
+                             (<+?>)
                              )
 where
 
@@ -1245,3 +1246,7 @@ addWarning mbpos msg =
 
 generalize :: (Monad m) => Parser s st a -> ParserT s st m a
 generalize m = mkPT (\ s -> (return $ (return . runIdentity) <$> runIdentity (runParsecT m s)))
+
+infixr 5 <+?>
+(<+?>) :: (Monoid a, Monad m) => ParserT s st m a -> ParserT s st m a -> ParserT s st m a
+a <+?> b = a >>= flip fmap (try b <|> return mempty) . (<>)
diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs
index ccda83576..5e0cef4f8 100644
--- a/src/Text/Pandoc/Readers/Markdown.hs
+++ b/src/Text/Pandoc/Readers/Markdown.hs
@@ -1487,7 +1487,8 @@ code = try $ do
 
 math :: MarkdownParser Inlines
 math =  (B.displayMath <$> (mathDisplay >>= applyMacros'))
-     <|> (B.math <$> (mathInline >>= applyMacros'))
+     <|> ((B.math <$> (mathInline >>= applyMacros')) <+?>
+                            ((getOption readerSmart >>= guard) *> apostrophe <* notFollowedBy space))
 
 -- Parses material enclosed in *s, **s, _s, or __s.
 -- Designed to avoid backtracking.