git.iko.soy/typeable/pandoc
on master help sign in
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.

commitf7229b147314042f946dfded3b441ab0fae260a0 authorJohn MacFarlane <fiddlosopher@gmail.com> dateSat 9 Mar 2013, 10:20 (14 years ago) parentbe165a2
7 files changed+128−8
pandoc.cabal+2 src/Text/Pandoc/Readers/LaTeX.hs+8−3 src/Text/Pandoc/Writers/Markdown.hs+2−1 tests/Tests/Old.hs+1−1 tests/Tests/Readers/LaTeX.hs+8−2 tests/biblio.mods+106 tests/latex-reader.native+1−1
pandoc.cabal+2
@@ -109,6 +109,7 @@ Extra-Source-Files:
109109 tests/lalune.jpg,
110110 tests/movie.jpg,
111111 tests/biblio.bib,
112+ tests/biblio.mods,
112113 tests/chicago-author-date.csl,
113114 tests/ieee.csl,
114115 tests/mhra.csl,
@@ -383,6 +384,7 @@ Test-Suite test-pandoc
383384 QuickCheck >= 2.4 && < 2.6,
384385 HUnit >= 1.2 && < 1.3,
385386 containers >= 0.1 && < 0.6,
387+ citeproc-hs >= 0.3 && < 0.4,
386388 ansi-terminal >= 0.5 && < 0.7
387389 Other-Modules: Tests.Old
388390 Tests.Helpers
src/Text/Pandoc/Readers/LaTeX.hs+8−3
@@ -1021,7 +1021,10 @@ cites mode multi = try $ do
10211021citation :: String -> CitationMode -> Bool -> LP Inlines
10221022citation name mode multi = do
10231023 (c,raw) <- withRaw $ cites mode multi
1024− return $ cite c (rawInline "latex" $ "\\" ++ name ++ raw)
1024+ refs <- getOption readerReferences
1025+ return $ if null refs
1026+ then rawInline "latex" $ "\\" ++ name ++ raw
1027+ else cite c mempty
10251028
10261029complexNatbibCitation :: CitationMode -> LP Inlines
10271030complexNatbibCitation mode = try $ do
@@ -1040,8 +1043,10 @@ complexNatbibCitation mode = try $ do
10401043 optional $ char ';'
10411044 return $ addPrefix pref $ addSuffix suff $ cits'
10421045 (c:cits, raw) <- withRaw $ grouped parseOne
1043− return $ cite (c{ citationMode = mode }:cits)
1044− (rawInline "latex" $ "\\citetext" ++ raw)
1046+ refs <- getOption readerReferences
1047+ return $ if null refs
1048+ then rawInline "latex" $ "\\citetext" ++ raw
1049+ else cite (c{ citationMode = mode}:cits) mempty
10451050
10461051-- tables
10471052
src/Text/Pandoc/Writers/Markdown.hs+2−1
@@ -644,7 +644,8 @@ inlineToMarkdown opts (LineBreak)
644644 | otherwise = return $ " " <> cr
645645inlineToMarkdown _ Space = return space
646646inlineToMarkdown opts (Cite (c:cs) lst)
647− | not (isEnabled Ext_citations opts) = inlineListToMarkdown opts lst
647+ | not (null lst) = inlineListToMarkdown opts lst
648+ -- if lst is null, citeproc wasn't run; print a pandoc markdown citation
648649 | citationMode c == AuthorInText = do
649650 suffs <- inlineListToMarkdown opts $ citationSuffix c
650651 rest <- mapM convertOne cs
tests/Tests/Old.hs+1−1
@@ -186,7 +186,7 @@ markdownCitationTests
186186 ++ [test "natbib" wopts "markdown-citations.txt"
187187 "markdown-citations.txt"]
188188 where
189− ropts = ["-r", "markdown", "-w", "markdown-citations", "--bibliography",
189+ ropts = ["-r", "markdown", "-w", "markdown", "--bibliography",
190190 "biblio.bib", "--no-wrap"]
191191 wopts = ["-r", "markdown", "-w", "markdown", "--no-wrap", "--natbib"]
192192 styleToTest style = test style (ropts ++ ["--csl", style ++ ".csl"])
tests/Tests/Readers/LaTeX.hs+8−2
@@ -2,14 +2,20 @@
22module Tests.Readers.LaTeX (tests) where
33
44import Text.Pandoc.Definition
5+import Data.Monoid (mempty)
6+import Text.CSL (Reference, readBiblioFile)
57import Test.Framework
68import Tests.Helpers
79import Tests.Arbitrary()
810import Text.Pandoc.Builder
911import Text.Pandoc
12+import System.IO.Unsafe (unsafePerformIO)
13+
14+refs :: [Reference]
15+refs = unsafePerformIO $ readBiblioFile "biblio.mods"
1016
1117latex :: String -> Pandoc
12−latex = readLaTeX def
18+latex = readLaTeX def{ readerReferences = refs }
1319
1420infix 4 =:
1521(=:) :: ToString c
@@ -71,7 +77,7 @@ baseCitation = Citation{ citationId = "item1"
7177 }
7278
7379rt :: String -> Inlines
74−rt = rawInline "latex"
80+rt = const mempty -- rawInline "latex"
7581
7682natbibCitations :: Test
7783natbibCitations = testGroup "natbib"
tests/biblio.modsnew file+106
@@ -0,0 +1,106 @@
1+<?xml version="1.0" encoding="UTF-8"?>
2+<modsCollection xmlns="http://www.loc.gov/mods/v3">
3+<mods ID="item1">
4+ <titleInfo>
5+ <title>First Book</title>
6+ </titleInfo>
7+ <name type="personal">
8+ <namePart type="given">John</namePart>
9+ <namePart type="family">Doe</namePart>
10+ <role>
11+ <roleTerm authority="marcrelator" type="text">author</roleTerm>
12+ </role>
13+ </name>
14+ <originInfo>
15+ <issuance>monographic</issuance>
16+ <dateIssued>2005</dateIssued>
17+ <publisher>Cambridge University Press</publisher>
18+ <place>
19+ <placeTerm type="text">Cambridge</placeTerm>
20+ </place>
21+ </originInfo>
22+ <typeOfResource>text</typeOfResource>
23+ <genre authority="marcgt">book</genre>
24+ <identifier type="citekey">item1</identifier>
25+</mods>
26+<mods ID="item2">
27+ <titleInfo>
28+ <title>Article</title>
29+ </titleInfo>
30+ <name type="personal">
31+ <namePart type="given">John</namePart>
32+ <namePart type="family">Doe</namePart>
33+ <role>
34+ <roleTerm authority="marcrelator" type="text">author</roleTerm>
35+ </role>
36+ </name>
37+ <originInfo>
38+ <dateIssued>2006</dateIssued>
39+ </originInfo>
40+ <typeOfResource>text</typeOfResource>
41+ <relatedItem type="host">
42+ <titleInfo>
43+ <title>Journal of Generic Studies</title>
44+ </titleInfo>
45+ <originInfo>
46+ <issuance>continuing</issuance>
47+ </originInfo>
48+ <genre authority="marcgt">periodical</genre>
49+ <genre>academic journal</genre>
50+ </relatedItem>
51+ <identifier type="citekey">item2</identifier>
52+ <part>
53+ <date>2006</date>
54+ <detail type="volume"><number>6</number></detail>
55+ <extent unit="page">
56+ <start>33</start>
57+ <end>34</end>
58+ </extent>
59+ </part>
60+</mods>
61+<mods ID="пункт3">
62+ <titleInfo>
63+ <title>Why Water Is Wet</title>
64+ </titleInfo>
65+ <name type="personal">
66+ <namePart type="given">John</namePart>
67+ <namePart type="family">Doe</namePart>
68+ <role>
69+ <roleTerm authority="marcrelator" type="text">author</roleTerm>
70+ </role>
71+ </name>
72+ <name type="personal">
73+ <namePart type="given">Jenny</namePart>
74+ <namePart type="family">Roe</namePart>
75+ <role>
76+ <roleTerm authority="marcrelator" type="text">author</roleTerm>
77+ </role>
78+ </name>
79+ <originInfo>
80+ <issuance>monographic</issuance>
81+ <dateIssued>2007</dateIssued>
82+ </originInfo>
83+ <typeOfResource>text</typeOfResource>
84+ <relatedItem type="host">
85+ <titleInfo>
86+ <title>Third Book</title>
87+ </titleInfo>
88+ <name type="personal">
89+ <namePart type="given">Sam</namePart>
90+ <namePart type="family">Smith</namePart>
91+ <role>
92+ <roleTerm authority="marcrelator" type="text">editor</roleTerm>
93+ </role>
94+ </name>
95+ <originInfo>
96+ <dateIssued>2007</dateIssued>
97+ <publisher>Oxford University Press</publisher>
98+ <place>
99+ <placeTerm type="text">Oxford</placeTerm>
100+ </place>
101+ </originInfo>
102+ <genre>collection</genre>
103+ </relatedItem>
104+ <identifier type="citekey">пункт3</identifier>
105+</mods>
106+</modsCollection>
tests/latex-reader.native+1−1
@@ -260,7 +260,7 @@ Pandoc (Meta {docTitle = [Str "Pandoc",Space,Str "Test",Space,Str "Suite"], docA
260260,HorizontalRule
261261,Header 1 ("",[],[]) [Str "LaTeX"]
262262,BulletList
263− [[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}"]]]
263+ [[Para [RawInline "latex" "\\cite[22-23]{smith.1899}"]]
264264 ,[Para [RawInline "latex" "\\doublespacing"]]
265265 ,[Para [Math InlineMath "2+2=4"]]
266266 ,[Para [Math InlineMath "x \\in y"]]