git.iko.soy/typeable/pandoc
on master help sign in
Merge branch 'altcite'
commitb1d08a8aa824dee1ebf8f1db6b780516c22f0c4b authorJohn MacFarlane <fiddlosopher@gmail.com> dateTue 20 Aug 2013, 22:00 (13 years ago) parente8ddcfd7048c13
7 files changed+105−70
src/Text/Pandoc/Biblio.hs+4−1 src/Text/Pandoc/Readers/Markdown.hs+12−9 src/Text/Pandoc/Writers/Markdown.hs+21−14 tests/Tests/Old.hs+1−1 tests/markdown-citations.chicago-author-date.txt+8−2 tests/markdown-citations.ieee.txt+23−17 tests/markdown-citations.mhra.txt+36−26
src/Text/Pandoc/Biblio.hs+4−1
@@ -55,7 +55,10 @@ processBiblio (Just style) r p =
5555 cits_map = M.fromList $ zip grps (citations result)
5656 biblioList = map (renderPandoc' style) (bibliography result)
5757 Pandoc m b = bottomUp mvPunct . deNote . topDown (processCite style cits_map) $ p'
58− in Pandoc m $ b ++ biblioList
58+ (bs, lastb) = case reverse b of
59+ x@(Header _ _ _) : xs -> (reverse xs, [x])
60+ _ -> (b, [])
61+ in Pandoc m $ bs ++ [Div ("",["references"],[]) (lastb ++ biblioList)]
5962
6063-- | Substitute 'Cite' elements with formatted citations.
6164processCite :: Style -> M.Map [Citation] [FormattedOutput] -> Inline -> Inline
src/Text/Pandoc/Readers/Markdown.hs+12−9
@@ -55,7 +55,6 @@ import Text.Pandoc.Readers.LaTeX ( rawLaTeXInline, rawLaTeXBlock )
5555import Text.Pandoc.Readers.HTML ( htmlTag, htmlInBalanced, isInlineTag, isBlockTag,
5656 isTextTag, isCommentTag )
5757import Text.Pandoc.Biblio (processBiblio)
58−import qualified Text.CSL as CSL
5958import Data.Monoid (mconcat, mempty)
6059import Control.Applicative ((<$>), (<*), (*>), (<$))
6160import Control.Monad
@@ -1797,11 +1796,13 @@ rawHtmlInline = do
17971796cite :: MarkdownParser (F Inlines)
17981797cite = do
17991798 guardEnabled Ext_citations
1800− getOption readerReferences >>= guard . not . null
1801− citations <- textualCite <|> normalCite
1802− return $ flip B.cite mempty <$> citations
1799+ citations <- textualCite <|> (fmap (flip B.cite unknownC) <$> normalCite)
1800+ return citations
1801+
1802+unknownC :: Inlines
1803+unknownC = B.str "???"
18031804
1804−textualCite :: MarkdownParser (F [Citation])
1805+textualCite :: MarkdownParser (F Inlines)
18051806textualCite = try $ do
18061807 (_, key) <- citeKey
18071808 let first = Citation{ citationId = key
@@ -1813,8 +1814,12 @@ textualCite = try $ do
18131814 }
18141815 mbrest <- option Nothing $ try $ spnl >> Just <$> normalCite
18151816 case mbrest of
1816− Just rest -> return $ (first:) <$> rest
1817− Nothing -> option (return [first]) $ bareloc first
1817+ Just rest -> return $ (flip B.cite unknownC . (first:)) <$> rest
1818+ Nothing -> (fmap (flip B.cite unknownC) <$> bareloc first) <|>
1819+ return (do st <- askF
1820+ return $ case M.lookup key (stateExamples st) of
1821+ Just n -> B.str (show n)
1822+ _ -> B.cite [first] unknownC)
18181823
18191824bareloc :: Citation -> MarkdownParser (F [Citation])
18201825bareloc c = try $ do
@@ -1846,8 +1851,6 @@ citeKey = try $ do
18461851 let internal p = try $ p >>~ lookAhead (letter <|> digit)
18471852 rest <- many $ letter <|> digit <|> internal (oneOf ":.#$%&-_+?<>~/")
18481853 let key = first:rest
1849− citations' <- map CSL.refId <$> getOption readerReferences
1850− guard $ key `elem` citations'
18511854 return (suppress_author, key)
18521855
18531856suffix :: MarkdownParser (F Inlines)
src/Text/Pandoc/Writers/Markdown.hs+21−14
@@ -186,7 +186,12 @@ pandocToMarkdown opts (Pandoc meta blocks) = do
186186 let toc = if writerTableOfContents opts
187187 then tableOfContents opts headerBlocks
188188 else empty
189− body <- blockListToMarkdown opts blocks
189+ -- Strip off final 'references' header if markdown citations enabled
190+ let blocks' = case reverse blocks of
191+ (Div (_,["references"],_) _):xs
192+ | isEnabled Ext_citations opts -> reverse xs
193+ _ -> blocks
194+ body <- blockListToMarkdown opts blocks'
190195 st <- get
191196 notes' <- notesToMarkdown opts (reverse $ stNotes st)
192197 st' <- get -- note that the notes may contain refs
@@ -304,10 +309,10 @@ blockToMarkdown _ Null = return empty
304309blockToMarkdown opts (Div attrs ils) = do
305310 isPlain <- gets stPlain
306311 contents <- blockListToMarkdown opts ils
307− return $ if isPlain
312+ return $ if isPlain || not (isEnabled Ext_markdown_in_html_blocks opts)
308313 then contents <> blankline
309314 else tagWithAttrs "div" attrs <> blankline <>
310− contents <> blankline <> "</div>" <> blankline
315+ contents <> blankline <> "</div>" <> blankline
311316blockToMarkdown opts (Plain inlines) = do
312317 contents <- inlineListToMarkdown opts inlines
313318 return $ contents <> cr
@@ -711,17 +716,20 @@ inlineToMarkdown opts (LineBreak)
711716 | isEnabled Ext_escaped_line_breaks opts = return $ "\\" <> cr
712717 | otherwise = return $ " " <> cr
713718inlineToMarkdown _ Space = return space
714−inlineToMarkdown opts (Cite (c:cs) lst@[RawInline "latex" _])
719+inlineToMarkdown opts (Cite [] lst) = inlineListToMarkdown opts lst
720+inlineToMarkdown opts (Cite (c:cs) lst)
715721 | not (isEnabled Ext_citations opts) = inlineListToMarkdown opts lst
716− | citationMode c == AuthorInText = do
717− suffs <- inlineListToMarkdown opts $ citationSuffix c
718− rest <- mapM convertOne cs
719− let inbr = suffs <+> joincits rest
720− br = if isEmpty inbr then empty else char '[' <> inbr <> char ']'
721− return $ text ("@" ++ citationId c) <+> br
722− | otherwise = do
723− cits <- mapM convertOne (c:cs)
724− return $ text "[" <> joincits cits <> text "]"
722+ | otherwise =
723+ if citationMode c == AuthorInText
724+ then do
725+ suffs <- inlineListToMarkdown opts $ citationSuffix c
726+ rest <- mapM convertOne cs
727+ let inbr = suffs <+> joincits rest
728+ br = if isEmpty inbr then empty else char '[' <> inbr <> char ']'
729+ return $ text ("@" ++ citationId c) <+> br
730+ else do
731+ cits <- mapM convertOne (c:cs)
732+ return $ text "[" <> joincits cits <> text "]"
725733 where
726734 joincits = hcat . intersperse (text "; ") . filter (not . isEmpty)
727735 convertOne Citation { citationId = k
@@ -738,7 +746,6 @@ inlineToMarkdown opts (Cite (c:cs) lst@[RawInline "latex" _])
738746 return $ pdoc <+> r
739747 modekey SuppressAuthor = "-"
740748 modekey _ = ""
741−inlineToMarkdown opts (Cite _ lst) = inlineListToMarkdown opts lst
742749inlineToMarkdown opts (Link txt (src, tit)) = do
743750 linktext <- inlineListToMarkdown opts txt
744751 let linktitle = if null tit
tests/Tests/Old.hs+1−1
@@ -196,7 +196,7 @@ markdownCitationTests
196196 ++ [test "natbib" wopts "markdown-citations.txt"
197197 "markdown-citations.txt"]
198198 where
199− ropts = ["-r", "markdown", "-w", "markdown", "--bibliography",
199+ ropts = ["-r", "markdown", "-w", "markdown-citations", "--bibliography",
200200 "biblio.bib", "--no-wrap"]
201201 wopts = ["-r", "markdown", "-w", "markdown", "--no-wrap", "--natbib"]
202202 styleToTest style = test style (ropts ++ ["--csl", style ++ ".csl"])
tests/markdown-citations.chicago-author-date.txt+8−2
@@ -1,9 +1,9 @@
11Pandoc with citeproc-hs
22=======================
33
4−- [@nonexistent]
4+- ([CSL BIBLIOGRAPHIC DATA ERROR: reference "nonexistent" not found.])
55
6−- @nonexistent
6+- ([CSL BIBLIOGRAPHIC DATA ERROR: reference "nonexistent" not found.])
77
88- Doe (2005) says blah.
99
@@ -29,15 +29,21 @@ Pandoc with citeproc-hs
2929
3030- With some markup (*see* Doe 2005, 32).
3131
32+<div class="references">
33+
3234References
3335==========
3436
37+“Nonexistent Not Found!”
38+
3539Doe, John. 2005. *First Book*. Cambridge: Cambridge University Press.
3640
3741———. 2006. “Article.” *Journal of Generic Studies* 6: 33–34.
3842
3943Doe, John, and Jenny Roe. 2007. “Why Water Is Wet.” In *Third Book*, edited by Sam Smith. Oxford: Oxford University Press.
4044
45+</div>
46+
4147[^1]: Doe and Roe (2007, 12) and a citation without locators (Doe and Roe 2007).
4248
4349[^2]: Some citations (see Doe 2005, chap. 3; Doe and Roe 2007; Doe 2006).
tests/markdown-citations.ieee.txt+23−17
@@ -1,45 +1,51 @@
11Pandoc with citeproc-hs
22=======================
33
4−- [@nonexistent]
4+- []
55
6−- @nonexistent
6+-
77
8−- Reference 1 says blah.
8+- Reference 2 says blah.
99
10−- Reference 1 says blah.
10+- Reference 2 says blah.
1111
12−- Reference 1 says blah.
12+- Reference 2 says blah.
1313
14−- Reference 1 [3] says blah.
14+- Reference 2 [4] says blah.
1515
1616- In a note.[^1]
1717
18−- A citation group [1], [3].
18+- A citation group [2], [4].
1919
20−- Another one [1].
20+- Another one [2].
2121
2222- And another one in a note.[^2]
2323
24−- Citation with a suffix and locator [1].
24+- Citation with a suffix and locator [2].
2525
26−- Citation with suffix only [1].
26+- Citation with suffix only [2].
2727
2828- Now some modifiers.[^3]
2929
30−- With some markup [1].
30+- With some markup [2].
31+
32+<div class="references">
3133
3234References
3335==========
3436
35−[1] J. Doe, *First Book*. Cambridge: Cambridge University Press, 2005.
37+[1]“nonexistent not found!” .
38+
39+[2] J. Doe, *First Book*. Cambridge: Cambridge University Press, 2005.
40+
41+[3] J. Doe, “Article,” *Journal of Generic Studies*, vol. 6, pp. 33–34, 2006.
3642
37−[2] J. Doe, “Article,” *Journal of Generic Studies*, vol. 6, pp. 33–34, 2006.
43+[4] J. Doe and J. Roe, “Why Water Is Wet,” in *Third Book*, S. Smith, Ed. Oxford: Oxford University Press, 2007.
3844
39−[3] J. Doe and J. Roe, “Why Water Is Wet,” in *Third Book*, S. Smith, Ed. Oxford: Oxford University Press, 2007.
45+</div>
4046
41−[^1]: Reference 3 and a citation without locators [3].
47+[^1]: Reference 4 and a citation without locators [4].
4248
43−[^2]: Some citations [1–3].
49+[^2]: Some citations [2–4].
4450
45−[^3]: Like a citation without author: [1], and now Doe with a locator [2].
51+[^3]: Like a citation without author: [2], and now Doe with a locator [3].
tests/markdown-citations.mhra.txt+36−26
@@ -1,33 +1,35 @@
11Pandoc with citeproc-hs
22=======================
33
4−- [@nonexistent]
4+- [^1]
55
6−- @nonexistent
6+- [^2]
77
8−- John Doe[^1] says blah.
8+- John Doe[^3] says blah.
99
10−- Doe[^2] says blah.
10+- Doe[^4] says blah.
1111
12−- Doe[^3] says blah.
12+- Doe[^5] says blah.
1313
14−- Doe[^4] says blah.
14+- Doe[^6] says blah.
1515
16−- In a note.[^5]
16+- In a note.[^7]
1717
18−- A citation group.[^6]
18+- A citation group.[^8]
1919
20−- Another one.[^7]
20+- Another one.[^9]
2121
22−- And another one in a note.[^8]
22+- And another one in a note.[^10]
2323
24−- Citation with a suffix and locator.[^9]
24+- Citation with a suffix and locator.[^11]
2525
26−- Citation with suffix only.[^10]
26+- Citation with suffix only.[^12]
2727
28−- Now some modifiers.[^11]
28+- Now some modifiers.[^13]
2929
30−- With some markup.[^12]
30+- With some markup.[^14]
31+
32+<div class="references">
3133
3234References
3335==========
@@ -38,26 +40,34 @@ Doe, John, ‘Article’, *Journal of Generic Studies*, 6 (2006), 33–34.
3840
3941Doe, John, and Jenny Roe, ‘Why Water Is Wet’, in *Third Book*, ed. by Sam Smith (Oxford: Oxford University Press, 2007).
4042
41−[^1]: *First Book* (Cambridge: Cambridge University Press, 2005).
43+‘Nonexistent Not Found!’.
44+
45+</div>
46+
47+[^1]: [CSL BIBLIOGRAPHIC DATA ERROR: reference "nonexistent" not found.].
48+
49+[^2]: [CSL STYLE ERROR: reference with no printed form.].
50+
51+[^3]: *First Book* (Cambridge: Cambridge University Press, 2005).
4252
43−[^2]: *First Book*, p. 30.
53+[^4]: *First Book*, p. 30.
4454
45−[^3]: *First Book*, p. 30, with suffix.
55+[^5]: *First Book*, p. 30, with suffix.
4656
47−[^4]: *First Book*; ‘Article’, *Journal of Generic Studies*, 6 (2006), 33–34 (p. 30); see also John Doe and Jenny Roe, ‘Why Water Is Wet’, in *Third Book*, ed. by Sam Smith (Oxford: Oxford University Press, 2007).
57+[^6]: *First Book*; ‘Article’, *Journal of Generic Studies*, 6 (2006), 33–34 (p. 30); see also John Doe and Jenny Roe, ‘Why Water Is Wet’, in *Third Book*, ed. by Sam Smith (Oxford: Oxford University Press, 2007).
4858
49−[^5]: Doe and Roe, p. 12 and a citation without locators Doe and Roe.
59+[^7]: Doe and Roe, p. 12 and a citation without locators Doe and Roe.
5060
51−[^6]: See Doe, *First Book*, chap. 3; also Doe and Roe, pp. 34–35.
61+[^8]: See Doe, *First Book*, chap. 3; also Doe and Roe, pp. 34–35.
5262
53−[^7]: See Doe, *First Book*, pp. 34–35.
63+[^9]: See Doe, *First Book*, pp. 34–35.
5464
55−[^8]: Some citations see Doe, *First Book*, chap. 3; Doe and Roe; Doe, ‘Article’, 33–34.
65+[^10]: Some citations see Doe, *First Book*, chap. 3; Doe and Roe; Doe, ‘Article’, 33–34.
5666
57−[^9]: Doe, *First Book*, pp. 33, 35–37, and nowhere else.
67+[^11]: Doe, *First Book*, pp. 33, 35–37, and nowhere else.
5868
59−[^10]: Doe, *First Book* and nowhere else.
69+[^12]: Doe, *First Book* and nowhere else.
6070
61−[^11]: Like a citation without author: *First Book*, and now Doe with a locator ‘Article’, 33–34 (p. 44).
71+[^13]: Like a citation without author: *First Book*, and now Doe with a locator ‘Article’, 33–34 (p. 44).
6272
63−[^12]: *See* Doe, *First Book*, p. 32.
73+[^14]: *See* Doe, *First Book*, p. 32.