From 8f60176511d006cc1c9fb8146f4e38cb9b760dc6 Mon Sep 17 00:00:00 2001
From: Nathan Gass <gass@search.ch>
Date: Wed, 15 Dec 2010 12:06:14 +0100
Subject: [PATCH] Remove punctuation at start of suffix for natbib and biblatex
 output.

This is necessary as the latex citation commands include there own
punctuation, which resulted in doubled commas for markdown documents
where citeproc output works correctly.
---
 src/Text/Pandoc/Writers/LaTeX.hs | 8 ++++++--
 tests/biblatex-citations.latex   | 6 +++---
 tests/markdown-citations.txt     | 6 +++---
 tests/natbib-citations.latex     | 6 +++---
 4 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs
index 9ddfc8b84..ca7446994 100644
--- a/src/Text/Pandoc/Writers/LaTeX.hs
+++ b/src/Text/Pandoc/Writers/LaTeX.hs
@@ -33,7 +33,7 @@ import Text.Pandoc.Shared
 import Text.Pandoc.Templates
 import Text.Printf ( printf )
 import Data.List ( (\\), isSuffixOf, isPrefixOf, intersperse, intercalate )
-import Data.Char ( toLower )
+import Data.Char ( toLower, isPunctuation )
 import Control.Monad.State
 import Text.PrettyPrint.HughesPJ hiding ( Str )
 import System.FilePath (dropExtension)
@@ -428,8 +428,12 @@ citeCommand c p s k = do
 
 citeArguments :: [Inline] -> [Inline] -> String -> State WriterState Doc
 citeArguments p s k = do
+  let s' = case s of
+        (Str (x:[]) : r) | isPunctuation x -> dropWhile (== Space) r
+        (Str (x:xs) : r) | isPunctuation x -> Str xs : r
+        _                                  -> s
   pdoc <- inlineListToLaTeX p
-  sdoc <- inlineListToLaTeX s
+  sdoc <- inlineListToLaTeX s'
   let optargs = case (isEmpty pdoc, isEmpty sdoc) of
                      (True, True ) -> empty
                      (True, False) -> brackets sdoc
diff --git a/tests/biblatex-citations.latex b/tests/biblatex-citations.latex
index 88c441234..05fe00f48 100644
--- a/tests/biblatex-citations.latex
+++ b/tests/biblatex-citations.latex
@@ -39,7 +39,7 @@
 
 \item
   A citation group
-  \autocites[see][, p.~34--35]{item1}[also][chap. 3]{item3}.
+  \autocites[see][p.~34--35]{item1}[also][chap. 3]{item3}.
 
 \item
   Another one \autocite[see][p.~34--35]{item1}.
@@ -50,10 +50,10 @@
 
 \item
   Citation with a suffix and locator
-  \autocite[, pp.~33, 35--37, and nowhere else]{item1}.
+  \autocite[pp.~33, 35--37, and nowhere else]{item1}.
 
 \item
-  Citation with suffix only \autocite[, and nowhere else]{item1}.
+  Citation with suffix only \autocite[and nowhere else]{item1}.
 
 \item
   Now some modifiers.%
diff --git a/tests/markdown-citations.txt b/tests/markdown-citations.txt
index 43154d2d4..0944b1101 100644
--- a/tests/markdown-citations.txt
+++ b/tests/markdown-citations.txt
@@ -14,16 +14,16 @@
 
 -   In a note.[^1]
 
--   A citation group [see @item1, p. 34-35; also @item3 chap. 3].
+-   A citation group [see @item1 p. 34-35; also @item3 chap. 3].
 
 -   Another one [see @item1 p. 34-35].
 
 -   And another one in a note.[^2]
 
 -   Citation with a suffix and locator
-    [@item1, pp. 33, 35-37, and nowhere else].
+    [@item1 pp. 33, 35-37, and nowhere else].
 
--   Citation with suffix only [@item1, and nowhere else].
+-   Citation with suffix only [@item1 and nowhere else].
 
 -   Now some modifiers.[^3]
 
diff --git a/tests/natbib-citations.latex b/tests/natbib-citations.latex
index 503d9cd9b..00787584e 100644
--- a/tests/natbib-citations.latex
+++ b/tests/natbib-citations.latex
@@ -40,7 +40,7 @@
 
 \item
   A citation group
-  \citetext{\citealp[see][, p.~34--35]{item1}; \citealp[also][chap. 3]{item3}}.
+  \citetext{\citealp[see][p.~34--35]{item1}; \citealp[also][chap. 3]{item3}}.
 
 \item
   Another one \citep[see][p.~34--35]{item1}.
@@ -52,10 +52,10 @@
 
 \item
   Citation with a suffix and locator
-  \citep[, pp.~33, 35--37, and nowhere else]{item1}.
+  \citep[pp.~33, 35--37, and nowhere else]{item1}.
 
 \item
-  Citation with suffix only \citep[, and nowhere else]{item1}.
+  Citation with suffix only \citep[and nowhere else]{item1}.
 
 \item
   Now some modifiers.%