Generate cards for pages

This commit is contained in:
Tissevert 2019-02-06 12:57:57 +01:00
parent 773689c4ff
commit f537cde283
2 changed files with 31 additions and 19 deletions

View File

@ -7,7 +7,6 @@ module ArticlesList (
) where ) where
import Article (Article) import Article (Article)
import Data.Text (Text, pack)
import System.FilePath.Posix ((</>)) import System.FilePath.Posix ((</>))
data ArticlesList = ArticlesList { data ArticlesList = ArticlesList {
@ -16,29 +15,20 @@ data ArticlesList = ArticlesList {
, featured :: [Article] , featured :: [Article]
} }
otherUrl :: ArticlesList -> Text otherUrl :: ArticlesList -> String
otherUrl (ArticlesList {full, tagged}) = otherUrl (ArticlesList {full, tagged}) =
if full if full
then pack $ url tagged then url tagged
else pack $ url tagged </> "all.html" else url tagged </> "all.html"
where where
url = maybe "/" ("/" </>) url = maybe "/" ("/" </>)
otherLink :: ArticlesList -> Text otherLink :: ArticlesList -> String
otherLink (ArticlesList {full}) = pack $ otherLink (ArticlesList {full}) =
if full if full
then "See only latest" then "See only latest"
else "See all" else "See all"
pageTitle :: ArticlesList -> Text pageTitle :: ArticlesList -> String
pageTitle (ArticlesList {full, tagged}) = pack $ pageTitle (ArticlesList {full, tagged}) =
(if full then "All" else "Latest") ++ " articles" ++ maybe "" (" tagged " ++) tagged (if full then "All" else "Latest") ++ " articles" ++ maybe "" (" tagged " ++) tagged
{-
pageTitle =
(if full then "All" else "Latest") ++ " articles" ++ maybe "" (" tagged " ++) category
p_ $ if full
then a_ [href_ . pack $ url category] "See only latest"
else a_ [href_ . pack $ url category </> "all.html"] "See all"
-}

View File

@ -10,16 +10,23 @@ import Blog (Blog(..))
import qualified Blog (get) import qualified Blog (get)
import Control.Monad.Reader (ReaderT) import Control.Monad.Reader (ReaderT)
import qualified Data.Map as Map (keys) import qualified Data.Map as Map (keys)
import Data.Monoid ((<>))
import Data.Text (pack, empty) import Data.Text (pack, empty)
import Lucid import Lucid
import Lucid.Base (makeAttribute)
import System.FilePath.Posix ((</>), (<.>)) import System.FilePath.Posix ((</>), (<.>))
type HtmlGenerator = HtmlT (ReaderT Blog IO) type HtmlGenerator = HtmlT (ReaderT Blog IO)
class Page a where class Page a where
card :: a -> HtmlGenerator ()
content :: a -> HtmlGenerator () content :: a -> HtmlGenerator ()
instance Page Article where instance Page Article where
card (Article {title}) =
("A new article on " <>) <$> Blog.get name
>>= makeCard title
content (Article {fullContents, urlPath}) = content (Article {fullContents, urlPath}) =
article_ (do article_ (do
a_ [href_ . pack $ "/" </> urlPath <.> "md"] "Raw" a_ [href_ . pack $ "/" </> urlPath <.> "md"] "Raw"
@ -27,13 +34,27 @@ instance Page Article where
) )
instance Page ArticlesList where instance Page ArticlesList where
card al = do
blogName <- Blog.get name
makeCard (maybe blogName ((blogName ++ " - ") ++) $ tagged al) (pageTitle al)
content al@(ArticlesList {featured}) = content al@(ArticlesList {featured}) =
div_ [id_ "contents"] (do div_ [id_ "contents"] (do
h2_ . toHtml $ pageTitle al h2_ . toHtml . pack $ pageTitle al
p_ . a_ [href_ $ otherUrl al] . toHtml $ otherLink al p_ . a_ [href_ . pack $ otherUrl al] . toHtml . pack $ otherLink al
div_ [class_ "articles"] (mapM_ previewArticle featured) div_ [class_ "articles"] (mapM_ previewArticle featured)
) )
makeCard :: String -> String -> HtmlGenerator ()
makeCard title description = do
blogName <- Blog.get name
og "title" title
og "description" description
og "image" $ "/image" </> blogName <.> "png"
og "site_name" blogName
where
og attribute value = meta_ [makeAttribute "property" $ "og:" <> attribute , content_ $ pack value]
previewArticle :: Article -> HtmlGenerator () previewArticle :: Article -> HtmlGenerator ()
previewArticle (Article {urlPath, title, preview}) = previewArticle (Article {urlPath, title, preview}) =
article_ (do article_ (do
@ -65,6 +86,7 @@ page aPage =
script_ [src_ "/UnitJS/async.js"] empty script_ [src_ "/UnitJS/async.js"] empty
script_ [src_ "/UnitJS/dom.js"] empty script_ [src_ "/UnitJS/dom.js"] empty
script_ [src_ "/js/main.js"] empty script_ [src_ "/js/main.js"] empty
card aPage
maybe (toHtml empty) toHtmlRaw =<< Blog.get customHead maybe (toHtml empty) toHtmlRaw =<< Blog.get customHead
) )
body_ (do body_ (do