git.iko.soy/typeable/pandoc
on master help sign in
Fixes: group biblatex citations even with prefix and suffix (#6058)

Closes #5849. Previously biblatex citations were only grouped if there was no prefix. This patch allows them to be grouped in subgroups split by prefixes and suffixes, which allows better citation sorting.

commitdaf770c1e9b8427611c10904bc4b80110556ea4d authorEthan Riley <riley787cmplex@gmail.com> dateFri 14 Feb 2020, 16:44 (7 years ago) parent652ed0b
2 files changed+74−25
src/Text/Pandoc/Writers/LaTeX.hs+42−25 test/command/5849-prefix.md+32
src/Text/Pandoc/Writers/LaTeX.hs+42−25
@@ -1421,24 +1421,35 @@ citeCommand c p s k = do
14211421 args <- citeArguments p s k
14221422 return $ literal ("\\" <> c) <> args
14231423
1424+type Prefix = [Inline]
1425+type Suffix = [Inline]
1426+type CiteId = Text
1427+data CiteGroup = CiteGroup Prefix Suffix [CiteId]
1428+
1429+citeArgumentsList :: PandocMonad m
1430+ => CiteGroup -> LW m (Doc Text)
1431+citeArgumentsList (CiteGroup _ _ []) = return empty
1432+citeArgumentsList (CiteGroup pfxs sfxs ids) = do
1433+ pdoc <- inlineListToLaTeX pfxs
1434+ sdoc <- inlineListToLaTeX sfxs'
1435+ return $ (optargs pdoc sdoc) <>
1436+ (braces (literal (T.intercalate "," (reverse ids))))
1437+ where sfxs' = stripLocatorBraces $ case sfxs of
1438+ (Str t : r) -> case T.uncons t of
1439+ Just (x, xs)
1440+ | T.null xs
1441+ , isPunctuation x -> dropWhile (== Space) r
1442+ | isPunctuation x -> Str xs : r
1443+ _ -> sfxs
1444+ _ -> sfxs
1445+ optargs pdoc sdoc = case (isEmpty pdoc, isEmpty sdoc) of
1446+ (True, True ) -> empty
1447+ (True, False) -> brackets sdoc
1448+ (_ , _ ) -> brackets pdoc <> brackets sdoc
1449+
14241450citeArguments :: PandocMonad m
14251451 => [Inline] -> [Inline] -> Text -> LW m (Doc Text)
1426−citeArguments p s k = do
1427− let s' = stripLocatorBraces $ case s of
1428− (Str t : r) -> case T.uncons t of
1429− Just (x, xs)
1430− | T.null xs
1431− , isPunctuation x -> dropWhile (== Space) r
1432− | isPunctuation x -> Str xs : r
1433− _ -> s
1434− _ -> s
1435− pdoc <- inlineListToLaTeX p
1436− sdoc <- inlineListToLaTeX s'
1437− let optargs = case (isEmpty pdoc, isEmpty sdoc) of
1438− (True, True ) -> empty
1439− (True, False) -> brackets sdoc
1440− (_ , _ ) -> brackets pdoc <> brackets sdoc
1441− return $ optargs <> braces (literal k)
1452+citeArguments p s k = citeArgumentsList (CiteGroup p s [k])
14421453
14431454-- strip off {} used to define locator in pandoc-citeproc; see #5722
14441455stripLocatorBraces :: [Inline] -> [Inline]
@@ -1470,18 +1481,24 @@ citationsToBiblatex (c:cs)
14701481 NormalCitation -> "\\autocite"
14711482 return $ text cmd <>
14721483 braces (literal (T.intercalate "," (map citationId (c:cs))))
1473− | otherwise = do
1474− let cmd = case citationMode c of
1484+ | otherwise
1485+ = do
1486+ let cmd = case citationMode c of
14751487 SuppressAuthor -> "\\autocites*"
14761488 AuthorInText -> "\\textcites"
14771489 NormalCitation -> "\\autocites"
1478− let convertOne Citation { citationId = k
1479− , citationPrefix = p
1480− , citationSuffix = s
1481− }
1482− = citeArguments p s k
1483− args <- mapM convertOne (c:cs)
1484− return $ text cmd <> foldl' (<>) empty args
1490+
1491+ groups <- mapM citeArgumentsList (reverse (foldl' grouper [] (c:cs)))
1492+
1493+ return $ text cmd <> (mconcat groups)
1494+
1495+ where grouper prev cit = case prev of
1496+ ((CiteGroup oPfx oSfx ids):rest)
1497+ | null oSfx && null pfx -> (CiteGroup oPfx sfx (cid:ids)):rest
1498+ _ -> (CiteGroup pfx sfx [cid]):prev
1499+ where pfx = citationPrefix cit
1500+ sfx = citationSuffix cit
1501+ cid = citationId cit
14851502
14861503citationsToBiblatex _ = return empty
14871504
test/command/5849-prefix.mdnew file+32
@@ -0,0 +1,32 @@
1+```
2+% pandoc -t latex --biblatex
3+[e.g. @a1;@a2;@a3; but also @b1;@b2;@b3]
4+^D
5+\autocites[e.g.~][]{a1,a2,a3}[but also][]{b1,b2,b3}
6+```
7+```
8+% pandoc -t latex --biblatex
9+[e.g. @a1; e.g. @a2;@a3; but also @b1;@b2;but also @b3]
10+^D
11+\autocites[e.g.~][]{a1}[e.g.~][]{a2,a3}[but also][]{b1,b2}[but
12+also][]{b3}
13+```
14+```
15+% pandoc -t latex --biblatex
16+[e.g. @a1, ch.3 and elsewhere;@a2;@a3; but also @a4;@a5]
17+^D
18+\autocites[e.g.~][ch.3 and elsewhere]{a1}{a2,a3}[but also][]{a4,a5}
19+```
20+```
21+% pandoc -t latex --biblatex
22+[e.g. @a1;@a2, ch.3 and elsewhere;@a3; but also @a4;@a5]
23+^D
24+\autocites[e.g.~][ch.3 and elsewhere]{a1,a2}{a3}[but also][]{a4,a5}
25+```
26+```
27+% pandoc -t latex --biblatex
28+[e.g. @a1, blah;@a2, ch.3 and elsewhere;@a3; but also @b4;@b5]
29+^D
30+\autocites[e.g.~][blah]{a1}[ch.3 and elsewhere]{a2}{a3}[but
31+also][]{b4,b5}
32+```