2019-12-21 12:50:38 +01:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module DOM.Card (
|
2020-12-13 20:09:23 +01:00
|
|
|
HasCard(..)
|
2019-12-21 12:50:38 +01:00
|
|
|
, make
|
|
|
|
) where
|
|
|
|
|
2020-12-13 20:09:23 +01:00
|
|
|
import Article (Article(..))
|
2020-03-25 19:47:28 +01:00
|
|
|
import ArticlesList (ArticlesList(..))
|
2020-05-08 15:51:25 +02:00
|
|
|
import qualified ArticlesList (description)
|
2020-12-13 20:09:23 +01:00
|
|
|
import Blog (Blog(..), Renderer, Skin(..), template)
|
2020-05-08 15:51:25 +02:00
|
|
|
import Collection (Collection(..))
|
|
|
|
import qualified Collection (title)
|
2019-12-21 12:50:38 +01:00
|
|
|
import Control.Applicative ((<|>))
|
2020-05-08 15:51:25 +02:00
|
|
|
import Control.Monad.Reader (asks)
|
2019-12-21 12:50:38 +01:00
|
|
|
import qualified Data.Map as Map (lookup)
|
|
|
|
import Data.Text (Text, pack)
|
|
|
|
import Lucid (HtmlT, content_, meta_)
|
|
|
|
import Lucid.Base (makeAttribute)
|
2020-12-13 20:09:23 +01:00
|
|
|
import Markdown (MarkdownContent(..), metadata)
|
|
|
|
import qualified Markdown (Markdown(..))
|
|
|
|
import Page (Page(..))
|
2019-12-21 12:50:38 +01:00
|
|
|
import Pretty ((.$))
|
2020-12-13 20:09:23 +01:00
|
|
|
import System.FilePath.Posix ((</>), (<.>))
|
2019-12-21 12:50:38 +01:00
|
|
|
|
|
|
|
class HasCard a where
|
2020-12-13 20:09:23 +01:00
|
|
|
cardType :: Renderer m => a -> m Text
|
|
|
|
description :: Renderer m => a -> m Text
|
|
|
|
image :: Renderer m => a -> m (Maybe String)
|
|
|
|
title :: Renderer m => a -> m String
|
|
|
|
urlPath :: Renderer m => a -> m String
|
2019-12-21 12:50:38 +01:00
|
|
|
|
|
|
|
og :: Applicative m => Text -> Text -> HtmlT m ()
|
|
|
|
og attribute value =
|
|
|
|
meta_ [
|
|
|
|
makeAttribute "property" $ "og:" <> attribute
|
|
|
|
, content_ value
|
|
|
|
]
|
|
|
|
|
2020-05-08 15:51:25 +02:00
|
|
|
make :: (HasCard a, Renderer m) => a -> String -> HtmlT m ()
|
2019-12-21 12:50:38 +01:00
|
|
|
make element siteURL = do
|
2020-12-13 20:09:23 +01:00
|
|
|
og "url" . sitePrefix =<< urlPath element
|
|
|
|
og "type" =<< cardType element
|
|
|
|
og "title" . pack =<< title element
|
|
|
|
og "description" =<< description element
|
|
|
|
maybeImage =<< ((<|>) <$> image element <*> (asks $skin.$cardImage))
|
2020-05-08 15:51:25 +02:00
|
|
|
og "site_name" =<< (asks $name.$pack)
|
2019-12-21 12:50:38 +01:00
|
|
|
where
|
2020-12-13 20:09:23 +01:00
|
|
|
maybeImage = maybe (return ()) (og "image" . sitePrefix)
|
|
|
|
sitePrefix = pack . (siteURL </>)
|
2019-12-21 12:50:38 +01:00
|
|
|
|
2020-12-13 20:09:23 +01:00
|
|
|
mDImage :: (Renderer m, MarkdownContent a ) => a -> m (Maybe String)
|
|
|
|
mDImage = return . Map.lookup "featuredImage" . metadata . getMarkdown
|
|
|
|
|
|
|
|
mDTitle :: (Renderer m, MarkdownContent a) => a -> m String
|
|
|
|
mDTitle = return . Markdown.title . getMarkdown
|
|
|
|
|
|
|
|
mDUrlPath :: (Renderer m, MarkdownContent a) => a -> m String
|
|
|
|
mDUrlPath a = return $ Markdown.path (getMarkdown a) <.> "html"
|
|
|
|
|
|
|
|
mDDescription :: (Renderer m, MarkdownContent a) => String -> a -> m Text
|
|
|
|
mDDescription key =
|
|
|
|
getDescription . Map.lookup "summary" . metadata . getMarkdown
|
|
|
|
where
|
|
|
|
getDescription = maybe defaultDescription (return . pack)
|
|
|
|
defaultDescription = asks name >>= template key . \v -> [("name", pack v)]
|
|
|
|
|
|
|
|
instance HasCard Article where
|
|
|
|
cardType _ = return "article"
|
|
|
|
description = mDDescription "articleDescription"
|
|
|
|
image = mDImage
|
|
|
|
title = mDTitle
|
|
|
|
urlPath = mDUrlPath
|
|
|
|
|
|
|
|
instance HasCard Page where
|
|
|
|
cardType _ = return "website"
|
|
|
|
description = mDDescription "pageDescription"
|
|
|
|
image = mDImage
|
|
|
|
title = mDTitle
|
|
|
|
urlPath = mDUrlPath
|
2019-12-21 12:50:38 +01:00
|
|
|
|
|
|
|
instance HasCard ArticlesList where
|
2020-12-13 20:09:23 +01:00
|
|
|
cardType _ = return "website"
|
|
|
|
description = ArticlesList.description
|
|
|
|
image _ = return Nothing
|
|
|
|
title (ArticlesList {collection}) = Collection.title collection
|
|
|
|
urlPath al@(ArticlesList {collection}) =
|
|
|
|
return $ maybe "" id (tag collection) </> file
|
2019-12-21 12:50:38 +01:00
|
|
|
where
|
2020-12-13 20:09:23 +01:00
|
|
|
file = (if full al then "all" else "index") <.> ".html"
|