From 96919a6ac5c21a8b46fbc347a4d815f0c9c89b98 Mon Sep 17 00:00:00 2001
From: fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>
Date: Wed, 24 Jan 2007 08:14:43 +0000
Subject: [PATCH] More smart quote bug fixes: + LaTeX writer now handles
 consecutive quotes properly:   for example, ``\,`hello'\,'' + LaTeX reader
 now parses '\,' as empty Str + normalizeSpaces function in Shared now removes
 empty Str elements + Modified tests accordingly

git-svn-id: https://pandoc.googlecode.com/svn/trunk@506 788f1e2b-df1e-0410-8736-df70ead52e1b
---
 src/Text/Pandoc/Readers/LaTeX.hs |  7 ++++++-
 src/Text/Pandoc/Shared.hs        |  4 +++-
 src/Text/Pandoc/Writers/LaTeX.hs | 13 +++++++++++--
 tests/writer.latex               |  4 ++--
 4 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index 38777b003..c152cc336 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -381,7 +381,8 @@ comment = try (do
 -- inline
 --
 
-inline =  choice [ strong, emph, ref, lab, code, linebreak, math, ellipses,
+inline =  choice [ strong, emph, ref, lab, code, linebreak, spacer,
+                   math, ellipses,
                    emDash, enDash, hyphen, quoted, apostrophe,
                    accentedChar, specialChar, specialInline, escapedChar,
                    unescapedChar, str, endline, whitespace ] <?> "inline"
@@ -579,6 +580,10 @@ linebreak = try (do
   string "\\\\"
   return LineBreak)
 
+spacer = try $ do
+  string "\\," 
+  return (Str "")
+
 str = do 
   result <- many1 (noneOf specialChars)
   return (Str result)
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index 1df2b4d38..2ba19f8dd 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -306,12 +306,14 @@ splitByIndices (x:xs) lst =
     first:(splitByIndices (map (\y -> y - x)  xs) rest)
 
 -- | Normalize a list of inline elements: remove leading and trailing
--- @Space@ elements, and collapse double @Space@s into singles.
+-- @Space@ elements, collapse double @Space@s into singles, and
+-- remove empty Str elements.
 normalizeSpaces :: [Inline] -> [Inline]
 normalizeSpaces [] = []
 normalizeSpaces list = 
     let removeDoubles [] = []
         removeDoubles (Space:Space:rest) = removeDoubles (Space:rest)
+        removeDoubles ((Str ""):rest) = removeDoubles rest 
         removeDoubles (x:rest) = x:(removeDoubles rest) in
     let removeLeading [] = []
         removeLeading lst = if ((head lst) == Space)
diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs
index 2badf2f24..d72c915d5 100644
--- a/src/Text/Pandoc/Writers/LaTeX.hs
+++ b/src/Text/Pandoc/Writers/LaTeX.hs
@@ -166,6 +166,11 @@ inlineListToLaTeX :: [Block]   -- ^ List of note blocks to use in resolving note
 inlineListToLaTeX notes lst = 
   concatMap (inlineToLaTeX notes) lst
 
+isQuoted :: Inline -> Bool
+isQuoted (Quoted _ _) = True
+isQuoted Apostrophe = True
+isQuoted _ = False
+
 -- | Convert inline element to LaTeX
 inlineToLaTeX :: [Block]   -- ^ List of note blocks to use in resolving note refs
               -> Inline    -- ^ Inline to convert
@@ -178,9 +183,13 @@ inlineToLaTeX notes (Code str) = "\\verb" ++ [chr] ++ stuffing ++ [chr]
                      where stuffing = str 
                            chr      = ((enumFromTo '!' '~') \\ stuffing) !! 0
 inlineToLaTeX notes (Quoted SingleQuote lst) =
-  "`" ++ inlineListToLaTeX notes lst ++ "'"
+  let s1 = if (not (null lst)) && (isQuoted (head lst)) then "\\," else ""
+      s2 = if (not (null lst)) && (isQuoted (last lst)) then "\\," else "" in
+  "`" ++ s1 ++ inlineListToLaTeX notes lst ++ s2 ++ "'"
 inlineToLaTeX notes (Quoted DoubleQuote lst) =
-  "``" ++ inlineListToLaTeX notes lst ++ "''"
+  let s1 = if (not (null lst)) && (isQuoted (head lst)) then "\\," else ""
+      s2 = if (not (null lst)) && (isQuoted (last lst)) then "\\," else "" in
+  "``" ++ s1 ++ inlineListToLaTeX notes lst ++ s2 ++ "''"
 inlineToLaTeX notes Apostrophe = "'"
 inlineToLaTeX notes EmDash = "---"
 inlineToLaTeX notes EnDash = "--"
diff --git a/tests/writer.latex b/tests/writer.latex
index f3ffeed62..e7b4d08e4 100644
--- a/tests/writer.latex
+++ b/tests/writer.latex
@@ -377,13 +377,13 @@ This is code: \verb!>!, \verb!$!, \verb!\!, \verb!\$!, \verb!<html>!.
 
 \section{Smart quotes, ellipses, dashes}
 
-``Hello,'' said the spider. ```Shelob' is my name.''
+``Hello,'' said the spider. ``\,`Shelob' is my name.''
 
 `A', `B', and `C' are letters.
 
 `Oak,' `elm,' and `beech' are names of trees. So is `pine.'
 
-`He said, ``I want to go.''' Were you alive in the 70's?
+`He said, ``I want to go.''\,' Were you alive in the 70's?
 
 Here is some quoted `\verb!code!' and a ``\href{http://example.com/?foo=1&bar=2}{quoted link}''.