{-# language OverloadedStrings #-}
module SitePipe.Utilities
( addPrefix
, setExt
, getTags
) where
import System.FilePath.Posix
import Data.Aeson
import Data.Aeson.Lens
import qualified Data.Map as M
import qualified Data.Text as T
import Control.Lens hiding ((.=))
setExt :: String -> FilePath -> FilePath
setExt = flip replaceExtension
addPrefix :: String -> FilePath -> FilePath
addPrefix = (++)
getTags :: (String -> String)
-> [Value]
-> [Value]
getTags makeUrl postList = uncurry (makeTag makeUrl) <$> M.toList tagMap
where
tagMap = M.unionsWith mappend (toMap <$> postList)
toMap post = M.fromList (zip (post ^.. key "tags" . values . _String . to T.unpack) $ repeat [post])
makeTag :: (String -> String) -> String -> [Value] -> Value
makeTag makeUrl tagname posts = object
[ "tag" .= tagname
, "url" .= makeUrl tagname
, "posts" .= posts
]