aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Citeproc.hs
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2025-06-22 16:33:42 -0700
committerJohn MacFarlane <[email protected]>2025-07-31 22:08:51 -0700
commitcfcd442b2fcdac4d5f49b0850f73de0153fa287f (patch)
tree1ccc5d77748394845d95086f5283c9aea71f44bb /src/Text/Pandoc/Citeproc.hs
parentc77476b597ef89e9ab7baf3452f84cd4f5ff1a5a (diff)
Extract citationSuffix, citationPrefix.cite-prefix
In transforming pandoc Cite to citeproc Citation, extract a `citationSuffix` and `citationPrefix` from the last item's suffix and first item's prefix, respectively, if they contain a `|` character which separates the item's suffix or prefix from the whole Citation's. for example: [for example, see |@C1; @A3; @B4|, and others] Here "for example, see" acts as a prefix for the whole group and will remain at the beginning even if the citation items are reordered by citeproc. Similarly, ", and others" will be a suffix for the whole group. Closes #10894. Notes: 1. The org reader now adds global prefixes and suffixes the same way as the Markdown reader: as affixes to the first item's prefix or the last item's suffix, separated by a pipe (`|`). 2. The org writer, however, has not been modified to convert the `|` to a `;`, as required by org-cite syntax. 3. This change doesn't currently do what one would expect, because of changes that were made to citeproc to prevent citation items with prefixes and suffixes from being sorted. Hence in `test/command/10894.md`, we have test output ``` (Doe, 2020; Smith, 2021) ``` without affixes, but ``` (see Smith, 2021; Doe, 2020, and others) ``` with affixes. To make this work well, we'd need to remove the citeproc code that prevented bad results before we had proper global prefixes and suffixes. However, removing this code would mean that existing documents would render differently, unless the new pipe syntax for citation affixes were used. That may be something we want to avoid. 4. The use of pipes to separate out global affixes from item-level affixes is a kludge that could be avoided if we added additional fields to Cite in the pandoc AST. However, AST changes are disruptive, so perhaps it's not worth doing that.
Diffstat (limited to 'src/Text/Pandoc/Citeproc.hs')
-rw-r--r--src/Text/Pandoc/Citeproc.hs40
1 files changed, 36 insertions, 4 deletions
diff --git a/src/Text/Pandoc/Citeproc.hs b/src/Text/Pandoc/Citeproc.hs
index 5ab245e16..954953fdb 100644
--- a/src/Text/Pandoc/Citeproc.hs
+++ b/src/Text/Pandoc/Citeproc.hs
@@ -299,17 +299,49 @@ getCitations locale otherIdsMap = Foldable.toList . query getCitation
where
getCitation (Cite cs _fallback) = Seq.singleton $
Citeproc.Citation { Citeproc.citationId = Nothing
- , Citeproc.citationPrefix = Nothing
- , Citeproc.citationSuffix = Nothing
+ , Citeproc.citationPrefix = pref
+ , Citeproc.citationSuffix = suff
, Citeproc.citationNoteNumber =
case cs of
[] -> Nothing
(Pandoc.Citation{ Pandoc.citationNoteNum = n }:
_) | n > 0 -> Just n
| otherwise -> Nothing
- , Citeproc.citationItems =
- fromPandocCitations locale otherIdsMap cs
+ , Citeproc.citationItems = items
}
+ where
+ (pref, suff, items) =
+ case fromPandocCitations locale otherIdsMap cs of
+ [] -> (Nothing, Nothing, [])
+ (i:is) ->
+ let (pref', i') = case citationItemPrefix i of
+ Nothing -> (Nothing, i)
+ Just p ->
+ case splitInlinesOnPipe (B.toList p) of
+ (_,[]) -> (Nothing, i)
+ (as,bs) -> (Just (B.fromList as),
+ i{ citationItemPrefix = Just (B.fromList bs) })
+ (suff', is') = case reverse is of
+ [] -> (Nothing, [])
+ (i'':is'') ->
+ case Citeproc.citationItemSuffix i'' of
+ Nothing -> (Nothing, is)
+ Just s ->
+ case splitInlinesOnPipe (B.toList s) of
+ (_,[]) -> (Nothing, is)
+ (as,bs) -> (Just (B.fromList bs), reverse
+ (i''{ citationItemSuffix = Just (B.fromList as) }:is''))
+ in (pref', suff', i':is')
+ splitInlinesOnPipe ils =
+ case break isStrWithPipe ils of
+ (xs,Str s : ys) ->
+ let (as,bs) = T.break (=='|') s
+ bs' = T.drop 1 bs
+ in (xs ++ [Str as | not (T.null as)],
+ [Str bs' | not (T.null bs')] ++ ys)
+ _ -> (ils,[])
+ isStrWithPipe (Str s) = T.any (=='|') s
+ isStrWithPipe _ = False
getCitation _ = mempty
fromPandocCitations :: Locale