aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Readers/Org/Inlines.hs
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2016-06-21 21:11:28 +0200
committerAlbert Krewinkel <albert@zeitkraut.de>2016-06-21 23:51:15 +0200
commit7df656089f960b5c0f329776d6ebc0094b0fef30 (patch)
treeec234cfdd12fabdd1221d2f1a847c29a49b227c8 /src/Text/Pandoc/Readers/Org/Inlines.hs
parent175cc2d44dab936e9245fbeb9cc30478359a97c7 (diff)
downloadpandoc-7df656089f960b5c0f329776d6ebc0094b0fef30.tar.gz
Org reader: remove partial functions
Partial functions like `head` lead to avoidable errors and should be avoided. They are replaced with total functions. This fixes #2991.
Diffstat (limited to 'src/Text/Pandoc/Readers/Org/Inlines.hs')
-rw-r--r--src/Text/Pandoc/Readers/Org/Inlines.hs34
1 files changed, 17 insertions, 17 deletions
diff --git a/src/Text/Pandoc/Readers/Org/Inlines.hs b/src/Text/Pandoc/Readers/Org/Inlines.hs
index d0e007312..6a8773c3c 100644
--- a/src/Text/Pandoc/Readers/Org/Inlines.hs
+++ b/src/Text/Pandoc/Readers/Org/Inlines.hs
@@ -236,11 +236,15 @@ berkeleyCite = try $ do
prefix <- berkeleyCiteCommonPrefix <$> bcl
suffix <- berkeleyCiteCommonSuffix <$> bcl
citationList <- berkeleyCiteCitations <$> bcl
- if parens
- then return . toCite . addToFirstAndLast prefix suffix $ citationList
- else return $ maybe mempty (<> " ") prefix
- <> (toListOfCites $ map toInTextMode citationList)
- <> maybe mempty (", " <>) suffix
+ return $
+ if parens
+ then toCite
+ . maybe id (\p -> alterFirst (prependPrefix p)) prefix
+ . maybe id (\s -> alterLast (appendSuffix s)) suffix
+ $ citationList
+ else maybe mempty (<> " ") prefix
+ <> (toListOfCites $ map toInTextMode citationList)
+ <> maybe mempty (", " <>) suffix
where
toCite :: [Citation] -> Inlines
toCite cs = B.cite cs mempty
@@ -251,18 +255,14 @@ berkeleyCite = try $ do
toInTextMode :: Citation -> Citation
toInTextMode c = c { citationMode = AuthorInText }
- addToFirstAndLast :: Maybe Inlines -> Maybe Inlines -> [Citation] -> [Citation]
- addToFirstAndLast pre suf (c:cs) =
- let firstCite = maybe c
- (\p -> c { citationPrefix = B.toList p <> citationPrefix c })
- pre
- cites = firstCite:cs
- lc = last cites
- lastCite = maybe lc
- (\s -> lc { citationSuffix = B.toList s <> citationSuffix lc })
- suf
- in init cites ++ [lastCite]
- addToFirstAndLast _ _ _ = []
+ alterFirst, alterLast :: (a -> a) -> [a] -> [a]
+ alterFirst _ [] = []
+ alterFirst f (c:cs) = (f c):cs
+ alterLast f = reverse . alterFirst f . reverse
+
+ prependPrefix, appendSuffix :: Inlines -> Citation -> Citation
+ prependPrefix pre c = c { citationPrefix = B.toList pre <> citationPrefix c }
+ appendSuffix suf c = c { citationSuffix = citationSuffix c <> B.toList suf }
data BerkeleyCitationList = BerkeleyCitationList
{ berkeleyCiteParens :: Bool