From f7229b147314042f946dfded3b441ab0fae260a0 Mon Sep 17 00:00:00 2001
From: John MacFarlane <fiddlosopher@gmail.com>
Date: Sat, 9 Mar 2013 10:20:12 -0800
Subject: [PATCH] LaTeX reader: citation handling changes.

Previously, a LaTeX citation would always be parsed as a Citation
element, with the raw LaTeX in the [Inline] part.

Now, the LaTeX citation is parsed as a Citation element only if
`--biblio` was specified (i.e. only if there is a nonempty set
of references in readerReferences).  Otherwise it is parsed as
raw LaTeX.

This will make it possible to simplify some things in the markdown
writer.  It also makes the LaTeX reader behave more like the Markdown
reader.
---
 pandoc.cabal                        |   2 +
 src/Text/Pandoc/Readers/LaTeX.hs    |  11 ++-
 src/Text/Pandoc/Writers/Markdown.hs |   3 +-
 tests/Tests/Old.hs                  |   2 +-
 tests/Tests/Readers/LaTeX.hs        |  10 ++-
 tests/biblio.mods                   | 106 ++++++++++++++++++++++++++++
 tests/latex-reader.native           |   2 +-
 7 files changed, 128 insertions(+), 8 deletions(-)
 create mode 100644 tests/biblio.mods

diff --git a/pandoc.cabal b/pandoc.cabal
index b528a0339..402a4e581 100644
--- a/pandoc.cabal
+++ b/pandoc.cabal
@@ -109,6 +109,7 @@ Extra-Source-Files:
                  tests/lalune.jpg,
                  tests/movie.jpg,
                  tests/biblio.bib,
+                 tests/biblio.mods,
                  tests/chicago-author-date.csl,
                  tests/ieee.csl,
                  tests/mhra.csl,
@@ -383,6 +384,7 @@ Test-Suite test-pandoc
                   QuickCheck >= 2.4 && < 2.6,
                   HUnit >= 1.2 && < 1.3,
                   containers >= 0.1 && < 0.6,
+                   citeproc-hs >= 0.3 && < 0.4,
                   ansi-terminal >= 0.5 && < 0.7
   Other-Modules:  Tests.Old
                   Tests.Helpers
diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index 5a2ab599b..880997a6d 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -1021,7 +1021,10 @@ cites mode multi = try $ do
 citation :: String -> CitationMode -> Bool -> LP Inlines
 citation name mode multi = do
   (c,raw) <- withRaw $ cites mode multi
-  return $ cite c (rawInline "latex" $ "\\" ++ name ++ raw)
+  refs <- getOption readerReferences
+  return $ if null refs
+              then rawInline "latex" $ "\\" ++ name ++ raw
+              else cite c mempty
 
 complexNatbibCitation :: CitationMode -> LP Inlines
 complexNatbibCitation mode = try $ do
@@ -1040,8 +1043,10 @@ complexNatbibCitation mode = try $ do
                    optional $ char ';'
                    return $ addPrefix pref $ addSuffix suff $ cits'
   (c:cits, raw) <- withRaw $ grouped parseOne
-  return $ cite (c{ citationMode = mode }:cits)
-           (rawInline "latex" $ "\\citetext" ++ raw)
+  refs <- getOption readerReferences
+  return $ if null refs
+              then rawInline "latex" $ "\\citetext" ++ raw
+              else cite (c{ citationMode = mode}:cits) mempty
 
 -- tables
 
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index aa7764356..c19cad22b 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -644,7 +644,8 @@ inlineToMarkdown opts (LineBreak)
   | otherwise                              = return $ "  " <> cr
 inlineToMarkdown _ Space = return space
 inlineToMarkdown opts (Cite (c:cs) lst)
-  | not (isEnabled Ext_citations opts) = inlineListToMarkdown opts lst
+  | not (null lst) = inlineListToMarkdown opts lst
+  -- if lst is null, citeproc wasn't run; print a pandoc markdown citation
   | citationMode c == AuthorInText = do
     suffs <- inlineListToMarkdown opts $ citationSuffix c
     rest <- mapM convertOne cs
diff --git a/tests/Tests/Old.hs b/tests/Tests/Old.hs
index b6d95ca89..9e7493504 100644
--- a/tests/Tests/Old.hs
+++ b/tests/Tests/Old.hs
@@ -186,7 +186,7 @@ markdownCitationTests
      ++ [test "natbib" wopts "markdown-citations.txt"
          "markdown-citations.txt"]
   where
-    ropts             = ["-r", "markdown", "-w", "markdown-citations", "--bibliography",
+    ropts             = ["-r", "markdown", "-w", "markdown", "--bibliography",
                          "biblio.bib", "--no-wrap"]
     wopts             = ["-r", "markdown", "-w", "markdown", "--no-wrap", "--natbib"]
     styleToTest style = test style (ropts ++ ["--csl", style ++ ".csl"])
diff --git a/tests/Tests/Readers/LaTeX.hs b/tests/Tests/Readers/LaTeX.hs
index 271b32689..016281ccb 100644
--- a/tests/Tests/Readers/LaTeX.hs
+++ b/tests/Tests/Readers/LaTeX.hs
@@ -2,14 +2,20 @@
 module Tests.Readers.LaTeX (tests) where
 
 import Text.Pandoc.Definition
+import Data.Monoid (mempty)
+import Text.CSL (Reference, readBiblioFile)
 import Test.Framework
 import Tests.Helpers
 import Tests.Arbitrary()
 import Text.Pandoc.Builder
 import Text.Pandoc
+import System.IO.Unsafe (unsafePerformIO)
+
+refs :: [Reference]
+refs = unsafePerformIO $ readBiblioFile "biblio.mods"
 
 latex :: String -> Pandoc
-latex = readLaTeX def
+latex = readLaTeX def{ readerReferences = refs }
 
 infix 4 =:
 (=:) :: ToString c
@@ -71,7 +77,7 @@ baseCitation = Citation{ citationId      = "item1"
                        }
 
 rt :: String -> Inlines
-rt = rawInline "latex"
+rt = const mempty -- rawInline "latex"
 
 natbibCitations :: Test
 natbibCitations = testGroup "natbib"
diff --git a/tests/biblio.mods b/tests/biblio.mods
new file mode 100644
index 000000000..478439419
--- /dev/null
+++ b/tests/biblio.mods
@@ -0,0 +1,106 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<modsCollection xmlns="http://www.loc.gov/mods/v3">
+<mods ID="item1">
+    <titleInfo>
+        <title>First Book</title>
+    </titleInfo>
+    <name type="personal">
+        <namePart type="given">John</namePart>
+        <namePart type="family">Doe</namePart>
+        <role>
+            <roleTerm authority="marcrelator" type="text">author</roleTerm>
+        </role>
+    </name>
+    <originInfo>
+        <issuance>monographic</issuance>
+        <dateIssued>2005</dateIssued>
+        <publisher>Cambridge University Press</publisher>
+        <place>
+            <placeTerm type="text">Cambridge</placeTerm>
+        </place>
+    </originInfo>
+    <typeOfResource>text</typeOfResource>
+    <genre authority="marcgt">book</genre>
+    <identifier type="citekey">item1</identifier>
+</mods>
+<mods ID="item2">
+    <titleInfo>
+        <title>Article</title>
+    </titleInfo>
+    <name type="personal">
+        <namePart type="given">John</namePart>
+        <namePart type="family">Doe</namePart>
+        <role>
+            <roleTerm authority="marcrelator" type="text">author</roleTerm>
+        </role>
+    </name>
+    <originInfo>
+        <dateIssued>2006</dateIssued>
+    </originInfo>
+    <typeOfResource>text</typeOfResource>
+    <relatedItem type="host">
+        <titleInfo>
+            <title>Journal of Generic Studies</title>
+        </titleInfo>
+        <originInfo>
+            <issuance>continuing</issuance>
+        </originInfo>
+        <genre authority="marcgt">periodical</genre>
+        <genre>academic journal</genre>
+    </relatedItem>
+    <identifier type="citekey">item2</identifier>
+    <part>
+        <date>2006</date>
+        <detail type="volume"><number>6</number></detail>
+        <extent unit="page">
+            <start>33</start>
+            <end>34</end>
+        </extent>
+    </part>
+</mods>
+<mods ID="пункт3">
+    <titleInfo>
+        <title>Why Water Is Wet</title>
+    </titleInfo>
+    <name type="personal">
+        <namePart type="given">John</namePart>
+        <namePart type="family">Doe</namePart>
+        <role>
+            <roleTerm authority="marcrelator" type="text">author</roleTerm>
+        </role>
+    </name>
+    <name type="personal">
+        <namePart type="given">Jenny</namePart>
+        <namePart type="family">Roe</namePart>
+        <role>
+            <roleTerm authority="marcrelator" type="text">author</roleTerm>
+        </role>
+    </name>
+    <originInfo>
+        <issuance>monographic</issuance>
+        <dateIssued>2007</dateIssued>
+    </originInfo>
+    <typeOfResource>text</typeOfResource>
+    <relatedItem type="host">
+        <titleInfo>
+            <title>Third Book</title>
+        </titleInfo>
+        <name type="personal">
+            <namePart type="given">Sam</namePart>
+            <namePart type="family">Smith</namePart>
+            <role>
+                <roleTerm authority="marcrelator" type="text">editor</roleTerm>
+            </role>
+        </name>
+        <originInfo>
+            <dateIssued>2007</dateIssued>
+            <publisher>Oxford University Press</publisher>
+            <place>
+                <placeTerm type="text">Oxford</placeTerm>
+            </place>
+        </originInfo>
+        <genre>collection</genre>
+    </relatedItem>
+    <identifier type="citekey">пункт3</identifier>
+</mods>
+</modsCollection>
diff --git a/tests/latex-reader.native b/tests/latex-reader.native
index 28bbad89c..f225e0969 100644
--- a/tests/latex-reader.native
+++ b/tests/latex-reader.native
@@ -260,7 +260,7 @@ Pandoc (Meta {docTitle = [Str "Pandoc",Space,Str "Test",Space,Str "Suite"], docA
 ,HorizontalRule
 ,Header 1 ("",[],[]) [Str "LaTeX"]
 ,BulletList
- [[Para [Cite [Citation {citationId = "smith.1899", citationPrefix = [], citationSuffix = [Str ",",Space,Str "22-23"], citationMode = AuthorInText, citationNoteNum = 0, citationHash = 0}] [RawInline "latex" "\\cite[22-23]{smith.1899}"]]]
+ [[Para [RawInline "latex" "\\cite[22-23]{smith.1899}"]]
  ,[Para [RawInline "latex" "\\doublespacing"]]
  ,[Para [Math InlineMath "2+2=4"]]
  ,[Para [Math InlineMath "x \\in y"]]