{-# LANGUAGE Trustworthy #-}
module MathiasSM.CleanURL (outputPath, pathRoute, cleanIndex, cleanIndexUrls, cleanIndexHtmls) where
import Data.List (isSuffixOf)
import Hakyll (Compiler, Item, Metadata, Routes, customRoute, replaceAll, toFilePath, withUrls)
import MathiasSM.Metadata (Key (Path), lookupKey)
import System.FilePath.Posix (takeExtension, (</>))
outputPath :: FilePath -> FilePath
outputPath :: String -> String
outputPath String
path
| String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (String -> String
takeExtension String
path) = String
withoutSlash String -> String -> String
</> String
"index.html"
| Bool
otherwise = String
withoutSlash
where
withoutSlash :: String
withoutSlash = (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/') String
path
pathRoute :: Metadata -> Routes
pathRoute :: Metadata -> Routes
pathRoute Metadata
metadata = case Key -> Metadata -> Maybe String
lookupKey Key
Path Metadata
metadata of
Maybe String
Nothing -> (Identifier -> String) -> Routes
customRoute ((Identifier -> String) -> Routes)
-> (Identifier -> String) -> Routes
forall a b. (a -> b) -> a -> b
$ \Identifier
ident -> String -> String
forall a. HasCallStack => String -> a
error (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
"Missing `path` in " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Identifier -> String
toFilePath Identifier
ident
Just String
path -> (Identifier -> String) -> Routes
customRoute ((Identifier -> String) -> Routes)
-> (Identifier -> String) -> Routes
forall a b. (a -> b) -> a -> b
$ String -> Identifier -> String
forall a b. a -> b -> a
const (String -> Identifier -> String) -> String -> Identifier -> String
forall a b. (a -> b) -> a -> b
$ String -> String
outputPath String
path
cleanIndexUrls :: Item String -> Compiler (Item String)
cleanIndexUrls :: Item String -> Compiler (Item String)
cleanIndexUrls = Item String -> Compiler (Item String)
forall a. a -> Compiler a
forall (m :: * -> *) a. Monad m => a -> m a
return (Item String -> Compiler (Item String))
-> (Item String -> Item String)
-> Item String
-> Compiler (Item String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> String) -> Item String -> Item String
forall a b. (a -> b) -> Item a -> Item b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((String -> String) -> String -> String
withUrls String -> String
cleanIndex)
cleanIndexHtmls :: Item String -> Compiler (Item String)
cleanIndexHtmls :: Item String -> Compiler (Item String)
cleanIndexHtmls = Item String -> Compiler (Item String)
forall a. a -> Compiler a
forall (m :: * -> *) a. Monad m => a -> m a
return (Item String -> Compiler (Item String))
-> (Item String -> Item String)
-> Item String
-> Compiler (Item String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> String) -> Item String -> Item String
forall a b. (a -> b) -> Item a -> Item b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String -> (String -> String) -> String -> String
replaceAll String
indexPattern String -> String
forall {b}. b -> String
replacement)
where
indexPattern :: String
indexPattern = String
"/index.html"
replacement :: b -> String
replacement = String -> b -> String
forall a b. a -> b -> a
const String
""
cleanIndex :: String -> String
cleanIndex :: String -> String
cleanIndex String
url
| String
idx String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
url = String
"/"
| String
idx String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isSuffixOf` String
url = Int -> String -> String
forall a. Int -> [a] -> [a]
take (String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
url Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
idx) String
url
| Bool
otherwise = String
url
where
idx :: String
idx = String
"/index.html"