module MathiasSM.Context.Lists (socialMediaContext) where
import Data.List (stripPrefix)
import Data.Maybe (fromMaybe)
import Hakyll (
Compiler,
Context,
Identifier,
Item (Item, itemBody),
field,
fromFilePath,
listField,
load,
toFilePath,
)
import MathiasSM.Config (SiteConfig, mediaDir, socialsFile)
import MathiasSM.Content (requireSocials)
import MathiasSM.Yaml.Social (Social (..))
import System.FilePath.Posix ((</>))
socialMediaContext :: SiteConfig -> Context a
socialMediaContext :: forall a. SiteConfig -> Context a
socialMediaContext SiteConfig
cfg =
[Context a] -> Context a
forall a. Monoid a => [a] -> a
mconcat
[ String -> (Social -> Bool) -> Context a
socialsField String
"socials-all" (Bool -> Social -> Bool
forall a b. a -> b -> a
const Bool
True)
, String -> (Social -> Bool) -> Context a
socialsField String
"socials-contact" Social -> Bool
socialShowContact
, String -> (Social -> Bool) -> Context a
socialsField String
"socials-about" Social -> Bool
socialShowAbout
]
where
socialsField :: String -> (Social -> Bool) -> Context a
socialsField String
name Social -> Bool
keep =
String -> Context Social -> Compiler [Item Social] -> Context a
forall a b. String -> Context a -> Compiler [Item a] -> Context b
listField String
name (String -> Context Social
socialContext (String -> Context Social) -> String -> Context Social
forall a b. (a -> b) -> a -> b
$ SiteConfig -> String
mediaDir SiteConfig
cfg) (Compiler [Item Social] -> Context a)
-> Compiler [Item Social] -> Context a
forall a b. (a -> b) -> a -> b
$ Identifier
-> (Item String -> Compiler [Social])
-> (Social -> Bool)
-> Compiler [Item Social]
forall a.
Identifier
-> (Item String -> Compiler [a])
-> (a -> Bool)
-> Compiler [Item a]
listItems (SiteConfig -> Identifier
socialsFile SiteConfig
cfg) Item String -> Compiler [Social]
requireSocials Social -> Bool
keep
socialContext :: FilePath -> Context Social
socialContext :: String -> Context Social
socialContext String
mediaDir' =
String -> String -> (Social -> String) -> Context Social
forall a. String -> String -> (a -> String) -> Context a
iconContext String
mediaDir' String
"images/icons/social/" Social -> String
socialIcon
Context Social -> Context Social -> Context Social
forall a. Semigroup a => a -> a -> a
<> String -> (Item Social -> Compiler String) -> Context Social
forall a. String -> (Item a -> Compiler String) -> Context a
field String
"href" (String -> Compiler String
forall a. a -> Compiler a
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Compiler String)
-> (Item Social -> String) -> Item Social -> Compiler String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Social -> String
socialHref (Social -> String)
-> (Item Social -> Social) -> Item Social -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Item Social -> Social
forall a. Item a -> a
itemBody)
Context Social -> Context Social -> Context Social
forall a. Semigroup a => a -> a -> a
<> String -> (Item Social -> Compiler String) -> Context Social
forall a. String -> (Item a -> Compiler String) -> Context a
field String
"username" (String -> Compiler String
forall a. a -> Compiler a
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Compiler String)
-> (Item Social -> String) -> Item Social -> Compiler String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Social -> String
socialUsername (Social -> String)
-> (Item Social -> Social) -> Item Social -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Item Social -> Social
forall a. Item a -> a
itemBody)
iconContext :: FilePath -> String -> (a -> String) -> Context a
iconContext :: forall a. String -> String -> (a -> String) -> Context a
iconContext String
mediaDir' String
prefix a -> String
getIcon =
String -> (Item a -> Compiler String) -> Context a
forall a. String -> (Item a -> Compiler String) -> Context a
field String
"iconHref" (String -> Compiler String
forall a. a -> Compiler a
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Compiler String)
-> (Item a -> String) -> Item a -> Compiler String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
hrefOf (a -> String) -> (Item a -> a) -> Item a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Item a -> a
forall a. Item a -> a
itemBody)
Context a -> Context a -> Context a
forall a. Semigroup a => a -> a -> a
<> String -> (Item a -> Compiler String) -> Context a
forall a. String -> (Item a -> Compiler String) -> Context a
field String
"iconRaw" (a -> Compiler String
rawOf (a -> Compiler String)
-> (Item a -> a) -> Item a -> Compiler String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Item a -> a
forall a. Item a -> a
itemBody)
where
hrefOf :: a -> String
hrefOf a
item = String
prefix String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
getIcon a
item String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
".svg"
sourceOf :: a -> String
sourceOf a
item = String
mediaDir' String -> String -> String
</> String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe (a -> String
hrefOf a
item) (String -> String -> Maybe String
forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix String
"images/" (a -> String
hrefOf a
item))
rawOf :: a -> Compiler String
rawOf a
item = Item String -> String
forall a. Item a -> a
itemBody (Item String -> String)
-> Compiler (Item String) -> Compiler String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identifier -> Compiler (Item String)
forall a. (Binary a, Typeable a) => Identifier -> Compiler (Item a)
load (String -> Identifier
fromFilePath (String -> Identifier) -> String -> Identifier
forall a b. (a -> b) -> a -> b
$ a -> String
sourceOf a
item)
listItems :: Identifier -> (Item String -> Compiler [a]) -> (a -> Bool) -> Compiler [Item a]
listItems :: forall a.
Identifier
-> (Item String -> Compiler [a])
-> (a -> Bool)
-> Compiler [Item a]
listItems Identifier
path Item String -> Compiler [a]
require a -> Bool
keep = do
file <- Identifier -> Compiler (Item String)
forall a. (Binary a, Typeable a) => Identifier -> Compiler (Item a)
load Identifier
path
items <- require file
let kept = (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
filter a -> Bool
keep [a]
items
return [Item (fromFilePath $ toFilePath path ++ "#" ++ show n) item | (n, item) <- zip [0 :: Int ..] kept]