module Hakyll.Web.Redirect
( Redirect (..)
, createRedirects
) where
import Control.Monad (forM_, when)
import Data.Binary (Binary (..))
import Data.List (sort, group)
import Hakyll.Core.Compiler
import Hakyll.Core.Identifier
import Hakyll.Core.Routes
import Hakyll.Core.Rules
import Hakyll.Core.Writable (Writable (..))
createRedirects :: [(Identifier, String)] -> Rules ()
createRedirects :: [(Identifier, FilePath)] -> Rules ()
createRedirects [(Identifier, FilePath)]
redirects =
do
let gkeys :: [[Identifier]]
gkeys = forall a. Eq a => [a] -> [[a]]
group forall a b. (a -> b) -> a -> b
$ forall a. Ord a => [a] -> [a]
sort forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(Identifier, FilePath)]
redirects
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [[Identifier]]
gkeys forall a b. (a -> b) -> a -> b
$ \[Identifier]
gkey -> case [Identifier]
gkey of
(Identifier
k : Identifier
_ : [Identifier]
_) -> forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail forall a b. (a -> b) -> a -> b
$
FilePath
"Duplicate 301 redirects; " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> FilePath
show Identifier
k forall a. [a] -> [a] -> [a]
++ FilePath
" is ambiguous."
[Identifier]
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Identifier, FilePath)]
redirects forall a b. (a -> b) -> a -> b
$ \(Identifier
r, FilePath
t) ->
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Identifier -> FilePath
toFilePath Identifier
r forall a. Eq a => a -> a -> Bool
== FilePath
t) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail forall a b. (a -> b) -> a -> b
$
FilePath
"Self-redirect detected: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> FilePath
show Identifier
r forall a. [a] -> [a] -> [a]
++ FilePath
" points to itself."
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Identifier, FilePath)]
redirects forall a b. (a -> b) -> a -> b
$ \(Identifier
ident, FilePath
to) ->
[Identifier] -> Rules () -> Rules ()
create [Identifier
ident] forall a b. (a -> b) -> a -> b
$ do
Routes -> Rules ()
route Routes
idRoute
forall a.
(Binary a, Typeable a, Writable a) =>
Compiler (Item a) -> Rules ()
compile forall a b. (a -> b) -> a -> b
$ forall a. a -> Compiler (Item a)
makeItem forall a b. (a -> b) -> a -> b
$! FilePath -> Redirect
Redirect FilePath
to
data Redirect = Redirect
{ Redirect -> FilePath
redirectTo :: String
} deriving (Redirect -> Redirect -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Redirect -> Redirect -> Bool
$c/= :: Redirect -> Redirect -> Bool
== :: Redirect -> Redirect -> Bool
$c== :: Redirect -> Redirect -> Bool
Eq, Eq Redirect
Redirect -> Redirect -> Bool
Redirect -> Redirect -> Ordering
Redirect -> Redirect -> Redirect
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Redirect -> Redirect -> Redirect
$cmin :: Redirect -> Redirect -> Redirect
max :: Redirect -> Redirect -> Redirect
$cmax :: Redirect -> Redirect -> Redirect
>= :: Redirect -> Redirect -> Bool
$c>= :: Redirect -> Redirect -> Bool
> :: Redirect -> Redirect -> Bool
$c> :: Redirect -> Redirect -> Bool
<= :: Redirect -> Redirect -> Bool
$c<= :: Redirect -> Redirect -> Bool
< :: Redirect -> Redirect -> Bool
$c< :: Redirect -> Redirect -> Bool
compare :: Redirect -> Redirect -> Ordering
$ccompare :: Redirect -> Redirect -> Ordering
Ord, Int -> Redirect -> ShowS
[Redirect] -> ShowS
Redirect -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [Redirect] -> ShowS
$cshowList :: [Redirect] -> ShowS
show :: Redirect -> FilePath
$cshow :: Redirect -> FilePath
showsPrec :: Int -> Redirect -> ShowS
$cshowsPrec :: Int -> Redirect -> ShowS
Show)
instance Binary Redirect where
put :: Redirect -> Put
put (Redirect FilePath
to) = forall t. Binary t => t -> Put
put FilePath
to
get :: Get Redirect
get = FilePath -> Redirect
Redirect forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall t. Binary t => Get t
get
instance Writable Redirect where
write :: FilePath -> Item Redirect -> IO ()
write FilePath
path = forall a. Writable a => FilePath -> Item a -> IO ()
write FilePath
path forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Redirect -> FilePath
redirectToHtml
redirectToHtml :: Redirect -> String
redirectToHtml :: Redirect -> FilePath
redirectToHtml (Redirect FilePath
working) =
FilePath
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"/><meta name=\"generator\" content=\"hakyll\"/>" forall a. [a] -> [a] -> [a]
++
FilePath
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" forall a. [a] -> [a] -> [a]
++
FilePath
"<meta http-equiv=\"refresh\" content=\"0; url=" forall a. [a] -> [a] -> [a]
++ FilePath
working forall a. [a] -> [a] -> [a]
++
FilePath
"\"><link rel=\"canonical\" href=\"" forall a. [a] -> [a] -> [a]
++ FilePath
working forall a. [a] -> [a] -> [a]
++
FilePath
"\"><title>Permanent Redirect</title></head><body><p>The page has moved to: <a href=\"" forall a. [a] -> [a] -> [a]
++ FilePath
working forall a. [a] -> [a] -> [a]
++
FilePath
"\">this page</a></p></body></html>"