Skip to content

Commit

Permalink
Add basic post list
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanbruegge committed Mar 22, 2024
1 parent ac46a65 commit 47f0501
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 23 deletions.
42 changes: 33 additions & 9 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ import Data.Text qualified as T
import Slick (markdownToHTML, compileTemplate', substitute)
import Slick.Utils (convert)
import qualified Data.Aeson.KeyMap as KM
import Development.Shake.FilePath (dropDirectory1, (-<.>), (</>))
import Development.Shake.FilePath (dropDirectory1, (</>), dropExtension)
import Data.Functor (void)
import Data.Maybe (fromMaybe)
import Control.Monad (foldM)
import Data.Aeson.Key (fromText)

outputFolder :: FilePath
outputFolder = "build/"

data Post = MkPost
{ title :: String
, author :: String
, description :: String
, content :: String
, url :: String
, date :: String
, readableDate :: String
, tags :: [String]
}
deriving stock (Generic, Eq, Ord, Show)
Expand Down Expand Up @@ -55,27 +59,47 @@ formatDate (A.String t) |
_ -> Nothing
formatDate _ = Nothing

renderTemplates :: A.Value -> [FilePath] -> Action A.Value
renderTemplates = foldM go
where
go o@(A.Object m) fp = do
template <- compileTemplate' ("templates" </> fp)
pure . A.Object $ KM.insert "content" (A.String $ substitute template o) m
go x _ = pure x

getRendered :: A.Value -> T.Text
getRendered (A.Object m) | Just (A.String x) <- KM.lookup "content" m = x
getRendered _ = error "Could not find content key in object"

buildRules :: Action ()
buildRules = do
_allPosts <- mapP buildPost =<< getDirectoryFiles "." ["articles//*.md"]
allPosts <- mapP buildPost =<< getDirectoryFiles "." ["articles//*.md"]
buildPostList allPosts
copyStaticFiles

buildPost :: FilePath -> Action Post
buildPost srcPath = cacheAction ("build" :: T.Text, srcPath) $ do
content <- readFile' srcPath
A.Object postData <- markdownToHTML . T.pack $ content
let postUrl = T.pack . dropDirectory1 $ srcPath -<.> "html"
let postUrl = T.pack . dropDirectory1 $ dropExtension srcPath
dateOpt = KM.lookup "date" postData >>= formatDate
postData' =
KM.insert "url" (A.String postUrl) $
KM.insert "prefix" (A.String "..") $
KM.insert "readableDate" (A.String $ fromMaybe "" dateOpt) postData
template <- compileTemplate' "templates/post.html"
shell <- compileTemplate' "templates/shell.html"
let postData'' = KM.insert "content" (A.String $ substitute template (A.Object postData')) postData'
writeFile' (outputFolder </> T.unpack postUrl) . T.unpack $ substitute shell (A.Object postData'')
KM.insert "prefix" (A.String "../..") $ -- posts are placed in <year>/<slug>/index.html
KM.insert "readableDate" (A.String $ fromMaybe "unknown date" dateOpt) postData
rendered <- getRendered <$> renderTemplates (A.Object postData') ["post.html", "shell.html"]
writeFile' (outputFolder </> T.unpack postUrl </> "index.html") . T.unpack $ rendered
convert $ A.Object postData'

buildPostList :: [Post] -> Action ()
buildPostList posts = do
let postData = A.Object $ KM.fromList
[ (fromText "posts", A.toJSON posts)
, (fromText "prefix", A.String ".")
]
rendered <- getRendered <$> renderTemplates postData ["postList.html", "shell.html"]
writeFile' (outputFolder </> "index.html") . T.unpack $ rendered

copyStaticFiles :: Action ()
copyStaticFiles = do
filepaths <- getDirectoryFiles "./static" ["*"]
Expand Down
11 changes: 9 additions & 2 deletions static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
margin: 0;
padding: 0;
box-sizing: border-box;
color: var(--primary-text);
}

html {
background: rgb(24, 29, 36);
color: var(--primary-text);
font-family: sans-serif;
display: flex;
justify-content: center;
Expand Down Expand Up @@ -36,7 +36,14 @@ nav {
align-items: center;
}

nav > span {
nav a {
text-decoration: none;
}
nav > ul a:hover {
text-decoration: underline;
}

nav > a {
color: #9861c1;
}

Expand Down
10 changes: 1 addition & 9 deletions templates/post.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
<article>
<header>
<h1>{{title}}</h1>
<time datetime="{{date}}">{{readableDate}}</time>
<ul>
{{#tags}}
<li><a href="{{prefix}}/tags/{{.}}">{{.}}</a></li>
{{/tags}}
</ul>
</header>
{{> templates/postHeader.html}}
{{{content}}}
</article>
10 changes: 10 additions & 0 deletions templates/postHeader.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<header>
<h1>{{title}}</h1>
<time datetime="{{date}}">{{readableDate}}</time>
<ul>
{{#tags}}
<li><a href="{{prefix}}/tags/{{.}}">{{.}}</a></li>
{{/tags}}
</ul>
</header>

7 changes: 7 additions & 0 deletions templates/postList.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{#posts}}
<article>
{{> templates/postHeader.html}}
{{{description}}}
<a href="{{prefix}}/{{url}}">Read more</a>
</article>
{{/posts}}
6 changes: 3 additions & 3 deletions templates/shell.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<body>
<header>
<nav id="main-menu">
<span>Programming & Proving</span>
<a href="{{prefix}}">Programming & Proving</a>
<ul>
<li><a>Blog</a></li>
<li><a>RSS</a></li>
<li><a href="{{prefix}}">Blog</a></li>
<li><a href="{{prefix}}/feed.xml">RSS</a></li>
</ul>
</nav>
</header>
Expand Down

0 comments on commit 47f0501

Please sign in to comment.