@@ -236,11 +236,15 @@ berkeleyCite = try $ do
236 prefix <- berkeleyCiteCommonPrefix <$> bcl
236 prefix <- berkeleyCiteCommonPrefix <$> bcl
237 suffix <- berkeleyCiteCommonSuffix <$> bcl
237 suffix <- berkeleyCiteCommonSuffix <$> bcl
238 citationList <- berkeleyCiteCitations <$> bcl
238 citationList <- berkeleyCiteCitations <$> bcl
239− if parens
240+ if parens
240− then return . toCite . addToFirstAndLast prefix suffix $ citationList
241+ then toCite
242+ . maybe id (\p -> alterFirst (prependPrefix p)) prefix
243+ . maybe id (\s -> alterLast (appendSuffix s)) suffix
241− else return $ maybe mempty (<> " ") prefix
245+ else maybe mempty (<> " ") prefix
242− <> (toListOfCites $ map toInTextMode citationList)
246+ <> (toListOfCites $ map toInTextMode citationList)
243− <> maybe mempty (", " <>) suffix
247+ <> maybe mempty (", " <>) suffix
245 toCite :: [Citation] -> Inlines
249 toCite :: [Citation] -> Inlines
246 toCite cs = B.cite cs mempty
250 toCite cs = B.cite cs mempty
@@ -251,18 +255,14 @@ berkeleyCite = try $ do
251 toInTextMode :: Citation -> Citation
255 toInTextMode :: Citation -> Citation
252 toInTextMode c = c { citationMode = AuthorInText }
256 toInTextMode c = c { citationMode = AuthorInText }
254− addToFirstAndLast :: Maybe Inlines -> Maybe Inlines -> [Citation] -> [Citation]
258+ alterFirst, alterLast :: (a -> a) -> [a] -> [a]
255− addToFirstAndLast pre suf (c:cs) =
259+ alterFirst _ [] = []
256− let firstCite = maybe c
260+ alterFirst f (c:cs) = (f c):cs
257− (\p -> c { citationPrefix = B.toList p <> citationPrefix c })
261+ alterLast f = reverse . alterFirst f . reverse
259− cites = firstCite:cs
263+ prependPrefix, appendSuffix :: Inlines -> Citation -> Citation
260− lc = last cites
264+ prependPrefix pre c = c { citationPrefix = B.toList pre <> citationPrefix c }
261− lastCite = maybe lc
265+ appendSuffix suf c = c { citationSuffix = citationSuffix c <> B.toList suf }
262− (\s -> lc { citationSuffix = B.toList s <> citationSuffix lc })
264− in init cites ++ [lastCite]
265− addToFirstAndLast _ _ _ = []
267data BerkeleyCitationList = BerkeleyCitationList
267data BerkeleyCitationList = BerkeleyCitationList
268 { berkeleyCiteParens :: Bool
268 { berkeleyCiteParens :: Bool