From d74e8d14a565a359126b6b1633c074f4591dc7c3 Mon Sep 17 00:00:00 2001
From: John MacFarlane <fiddlosopher@gmail.com>
Date: Wed, 9 Nov 2011 13:18:01 -0800
Subject: [PATCH] Markdown citations: don't strip off initial space in locator.

Previously `[@item1 and nowhere else]` yielded the locator ", and nowhere
else", or, with the new citeproc-hs, "and nowhere else".
Now it yields " and nowhere else".
---
 src/Text/Pandoc/Readers/Markdown.hs              | 6 +++++-
 tests/markdown-citations.chicago-author-date.txt | 2 +-
 tests/markdown-citations.mhra.txt                | 2 +-
 3 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs
index f04ea88c4..ab146bdd0 100644
--- a/src/Text/Pandoc/Readers/Markdown.hs
+++ b/src/Text/Pandoc/Readers/Markdown.hs
@@ -1321,8 +1321,12 @@ citeKey = try $ do
 
 suffix :: GenParser Char ParserState [Inline]
 suffix = try $ do
+  hasSpace <- option False (notFollowedBy nonspaceChar >> return True)
   spnl
-  liftM normalizeSpaces $ many $ notFollowedBy (oneOf ";]") >> inline
+  rest <- liftM normalizeSpaces $ many $ notFollowedBy (oneOf ";]") >> inline
+  return $ if hasSpace
+              then Space : rest
+              else rest
 
 prefix :: GenParser Char ParserState [Inline]
 prefix = liftM normalizeSpaces $
diff --git a/tests/markdown-citations.chicago-author-date.txt b/tests/markdown-citations.chicago-author-date.txt
index 11d84dadc..07539faf4 100644
--- a/tests/markdown-citations.chicago-author-date.txt
+++ b/tests/markdown-citations.chicago-author-date.txt
@@ -22,7 +22,7 @@
 
 -   Citation with a suffix and locator (Doe 2005, 33, 35–37, and nowhere else).
 
--   Citation with suffix only (Doe 2005, and nowhere else).
+-   Citation with suffix only (Doe 2005 and nowhere else).
 
 -   Now some modifiers.[^3]
 
diff --git a/tests/markdown-citations.mhra.txt b/tests/markdown-citations.mhra.txt
index 56eb147a3..ed3f1c850 100644
--- a/tests/markdown-citations.mhra.txt
+++ b/tests/markdown-citations.mhra.txt
@@ -54,7 +54,7 @@ Doe, John, and Jenny Roe, ‘Why Water Is Wet’, in *Third Book*, ed by Sam Smi
 
 [^9]: Doe, First Book, pp. 33, 35-37, and nowhere else.
 
-[^10]: Doe, First Book, and nowhere else.
+[^10]: Doe, First Book and nowhere else.
 
 [^11]: Like a citation without author: First Book, and now Doe with a locator Article, 33-34 (p. 44).