git.iko.soy/typeable/pandoc
on master help sign in
Markdown citation parser improvements and test updates.

Now we handle a suffix after a bare locator, e.g. @item1 [p. 30, suffix] The suffix now includes any punctuation that introduces it. A few tests fail because of problems with citeproc (extra space before the suffix, missing space after comma separating multiple page ranges in the locator).

commitf3bb3c1ff1c85ea3bc9132b4c890905a9af20c3a authorJohn MacFarlane <jgm@berkeley.edu> dateThu 18 Nov 2010, 13:22 (16 years ago) parentaaf7de0
3 files changed+24−16
src/Text/Pandoc/Readers/Markdown.hs+15−13 tests/markdown-citations.plain+5−3 tests/markdown-citations.txt+4
src/Text/Pandoc/Readers/Markdown.hs+15−13
@@ -1348,13 +1348,12 @@ bareloc :: Citation -> GenParser Char ParserState [Citation]
13481348bareloc c = try $ do
13491349 spnl
13501350 char '['
1351− spnl
13521351 loc <- locator
1353− spnl
1352+ suff <- suffix
13541353 rest <- option [] $ try $ char ';' >> citeList
13551354 spnl
13561355 char ']'
1357− return $ c{ citationLocator = loc } : rest
1356+ return $ c{ citationLocator = loc, citationSuffix = suff } : rest
13581357
13591358normalCite :: GenParser Char ParserState [Citation]
13601359normalCite = try $ do
@@ -1376,28 +1375,31 @@ citeKey = try $ do
13761375locator :: GenParser Char st String
13771376locator = try $ do
13781377 spnl
1379− w <- many1 (noneOf " \t\n;]")
1380− spnl
1381− ws <- many locatorWord
1378+ w <- many1 (noneOf " \t\n;,]")
1379+ ws <- many (locatorWord <|> locatorComma)
13821380 return $ unwords $ w:ws
13831381
13841382locatorWord :: GenParser Char st String
13851383locatorWord = try $ do
1386− wd <- many1 $ (try $ char '\\' >> oneOf "]; \t\n") <|> noneOf "]; \t\n"
13871384 spnl
1388− if any isDigit wd
1389− then return wd
1390− else pzero
1385+ wd <- many1 $ (try $ char '\\' >> oneOf "];, \t\n") <|> noneOf "];, \t\n"
1386+ guard $ any isDigit wd
1387+ return wd
1388+
1389+locatorComma :: GenParser Char st String
1390+locatorComma = try $ do
1391+ char ','
1392+ lookAhead $ locatorWord
1393+ return ","
13911394
13921395suffix :: GenParser Char ParserState [Inline]
13931396suffix = try $ do
1394− char ','
13951397 spnl
13961398 liftM normalizeSpaces $ many $ notFollowedBy (oneOf ";]") >> inline
13971399
13981400prefix :: GenParser Char ParserState [Inline]
13991401prefix = liftM normalizeSpaces $
1400− manyTill inline (lookAhead citeKey)
1402+ manyTill inline (char ']' <|> liftM (const ']') (lookAhead citeKey))
14011403
14021404citeList :: GenParser Char ParserState [Citation]
14031405citeList = sepBy1 citation (try $ char ';' >> spnl)
@@ -1407,7 +1409,7 @@ citation = try $ do
14071409 pref <- prefix
14081410 (suppress_author, key) <- citeKey
14091411 loc <- option "" $ try $ blankSpace >> locator
1410− suff <- option [] suffix
1412+ suff <- suffix
14111413 return $ Citation{ citationId = key
14121414 , citationPrefix = pref
14131415 , citationSuffix = suff
tests/markdown-citations.plain+5−3
@@ -5,12 +5,14 @@ Pandoc with citeproc-hs
55
66@nonexistent
77
8−Doe (2005) says blah. Doe (2005, 30) says blah. Doe
9−(2005; 2006, 30; see also Doe and Roe 2007) says blah.
8+Doe (2005) says blah. Doe (2005, 30) says blah. Doe (2005, 30, suffix)
9+says blah. Doe (2005; 2006, 30; see also Doe and Roe 2007) says blah.
1010
1111In a note.[^1] A citation group
1212(see Doe 2005, 34-35; also Doe and Roe 2007, chap. 3). Another one
13−(see Doe 2005, 34-35). And another one in a note.[^2]
13+(see Doe 2005, 34-35). And another one in a note.[^2] Citation with
14+a suffix and locator (Doe 2005, 33, 35-37, and nowhere else).
15+Citation with suffix only (Doe 2005, and nowhere else).
1416
1517Now some modifiers.[^3]
1618
tests/markdown-citations.txt+4
@@ -6,11 +6,15 @@
66
77@item1 says blah.
88@item1 [p. 30] says blah.
9+@item1 [p. 30, with suffix] says blah.
910@item1 [-@item2 p. 30; see also @item3] says blah.
1011
1112In a note.[^1] A citation group [see
1213@item1 p. 34-35; also @item3 chap. 3]. Another one [see
1314@item1 p. 34-35]. And another one in a note.[^2]
15+Citation with a suffix and locator [@item1 pp. 33, 35-37,
16+and nowhere else]. Citation with suffix only
17+[@item1, and nowhere else].
1418
1519Now some modifiers.[^3]
1620