aboutsummaryrefslogtreecommitdiff
path: root/Utils.hs
blob: 482833522811989d0abb70002c71d233a73f1479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Utils where

import Control.Monad.IO.Class
import Data.List.Extra (stripSuffix)
import Data.Maybe (isJust)
import qualified Data.Text as T
import Text.Pandoc.Definition
import qualified Text.Pandoc.Walk

import Types

-- | A shortcut for `liftIO`.
io :: MonadIO m => IO a -> m a
io = liftIO

-- | A helper for throwing an error if something is `Nothing`
just :: String -> Maybe a -> a
just _ (Just val) = val
just err Nothing = error ("Missing: " ++ err)

-- | Test for whether something listy has a suffix
hasSuffix :: Eq a => [a] -> [a] -> Bool
hasSuffix s = isJust . stripSuffix s

-- | The same second as from arrows et al.
second :: (a -> b) -> (c, a) -> (c, b)
second f (a, b) = (a, f b)

-- | A pandoc walker for modifying the URLs.
walkURLs ::
     (FilePath -> Site FilePath)
  -> Text.Pandoc.Definition.Pandoc
  -> Site Text.Pandoc.Definition.Pandoc
walkURLs f = Text.Pandoc.Walk.walkM go
  where
    go (Link a i (u, t)) = do
      u' <- T.pack <$> f (T.unpack u)
      pure $ Link a i (u', t)
    go (Image a i (u, t)) = do
      u' <- T.pack <$> f (T.unpack u)
      pure $ Image a i (u', t)
    go x = pure x

hasUriScheme :: String -> String -> Bool
hasUriScheme x = all id . zipWith (==) x . (++ ":")

unAbsolute :: String -> String
unAbsolute = dropWhile (== '/')