-- | The context every page is rendered with, and the contexts that depend on it
module MathiasSM.Context (globalContext, postSocialTagsContext) where

import Data.List (isInfixOf)
import Hakyll (
    Context,
    Item (itemBody),
    boolField,
    constField,
    defaultContext,
    jsonldField,
    openGraphField,
    twitterCardField,
 )
import MathiasSM.Config (SiteConfig, twitterHandle)
import MathiasSM.Context.Data (dataContext)
import MathiasSM.Context.Feeds (feedsContext)
import MathiasSM.Context.Language (languageContext)
import MathiasSM.Context.Lists (socialMediaContext)
import MathiasSM.Context.Site (siteContext)

-- | Every field available to all pages and list items
globalContext :: SiteConfig -> Context String
globalContext :: SiteConfig -> Context [Char]
globalContext SiteConfig
cfg =
    SiteConfig -> Context [Char]
forall a. SiteConfig -> Context a
siteContext SiteConfig
cfg
        Context [Char] -> Context [Char] -> Context [Char]
forall a. Semigroup a => a -> a -> a
<> SiteConfig -> Context [Char]
forall a. SiteConfig -> Context a
socialMediaContext SiteConfig
cfg
        Context [Char] -> Context [Char] -> Context [Char]
forall a. Semigroup a => a -> a -> a
<> SiteConfig -> Context [Char]
forall a. SiteConfig -> Context a
dataContext SiteConfig
cfg
        Context [Char] -> Context [Char] -> Context [Char]
forall a. Semigroup a => a -> a -> a
<> Context [Char]
forall a. Context a
languageContext
        Context [Char] -> Context [Char] -> Context [Char]
forall a. Semigroup a => a -> a -> a
<> SiteConfig -> Context [Char]
forall a. SiteConfig -> Context a
feedsContext SiteConfig
cfg
        Context [Char] -> Context [Char] -> Context [Char]
forall a. Semigroup a => a -> a -> a
<> Context [Char]
mathContext
        Context [Char] -> Context [Char] -> Context [Char]
forall a. Semigroup a => a -> a -> a
<> Context [Char]
defaultContext

-- | True when the page's rendered body contains math, so templates can load MathJax
mathContext :: Context String
mathContext :: Context [Char]
mathContext =
    [Char] -> (Item [Char] -> Bool) -> Context [Char]
forall a. [Char] -> (Item a -> Bool) -> Context a
boolField [Char]
"math" ((Item [Char] -> Bool) -> Context [Char])
-> (Item [Char] -> Bool) -> Context [Char]
forall a b. (a -> b) -> a -> b
$ \Item [Char]
item ->
        [Char]
"class=\"math " [Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` Item [Char] -> [Char]
forall a. Item a -> a
itemBody Item [Char]
item

-- | Sets HTML (as context) for article metadata
postSocialTagsContext :: SiteConfig -> Context String
postSocialTagsContext :: SiteConfig -> Context [Char]
postSocialTagsContext SiteConfig
cfg =
    [Context [Char]] -> Context [Char]
forall a. Monoid a => [a] -> a
mconcat
        [ [Char] -> Context [Char] -> Context [Char]
twitterCardField [Char]
"twitter" Context [Char]
ctx
        , [Char] -> Context [Char] -> Context [Char]
openGraphField [Char]
"opengraph" Context [Char]
ctx
        , [Char] -> Context [Char] -> Context [Char]
jsonldField [Char]
"jsonld" Context [Char]
ctx
        ]
  where
    ctx :: Context [Char]
ctx =
        [Context [Char]] -> Context [Char]
forall a. Monoid a => [a] -> a
mconcat
            [ [Char] -> [Char] -> Context [Char]
forall a. [Char] -> [Char] -> Context a
constField [Char]
"twitter-creator" (SiteConfig -> [Char]
twitterHandle SiteConfig
cfg)
            , [Char] -> [Char] -> Context [Char]
forall a. [Char] -> [Char] -> Context a
constField [Char]
"twitter-site" (SiteConfig -> [Char]
twitterHandle SiteConfig
cfg)
            , SiteConfig -> Context [Char]
globalContext SiteConfig
cfg
            ]