Skip to content

Commit

Permalink
Use slick instead of sitepipe
Browse files Browse the repository at this point in the history
  • Loading branch information
isovector committed Feb 6, 2022
1 parent 7483e8c commit c5c2a9f
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 55 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.stack-work/
*~
*~
.shake
114 changes: 98 additions & 16 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,22 +1,104 @@
{-# language OverloadedStrings #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

module Main where

import SitePipe
import Prelude hiding (length)
import qualified Prelude as P
import Control.Lens
import Control.Monad
import Data.Aeson as A
import Data.Aeson.Lens
import Development.Shake
import Development.Shake.Classes
import Development.Shake.Forward
import Development.Shake.FilePath
import GHC.Generics (Generic)
import Slick
import qualified Data.Text as T

outputFolder :: FilePath
outputFolder = "docs/"

-- | Data for the index page
data IndexInfo =
IndexInfo
{ episodes :: [Episode]
} deriving (Generic, Show, FromJSON, ToJSON)

data Episode = Episode
{ guest :: String
, date :: String
, length :: String
, url :: String
, topics :: [String]
, episodeId :: String
} deriving (Generic, Eq, Ord, Show, FromJSON, ToJSON, Binary)


buildIndex :: [Episode] -> Action ()
buildIndex posts' = do
indexT <- compileTemplate' "site/templates/index.html"
let indexInfo = IndexInfo {episodes = reverse posts'}
indexHTML = T.unpack $ substitute indexT (toJSON indexInfo)
writeFile' (outputFolder </> "index.html") indexHTML

buildEpisodes :: Action [Episode]
buildEpisodes = do
pPaths <- getDirectoryFiles "." ["site/episodes//*.md"]
forP pPaths buildEpisode


getEpisodeId :: FilePath -> Int
getEpisodeId = read . take 3 . drop (P.length @[] "site/episodes/")


buildEpisode :: FilePath -> Action Episode
buildEpisode srcPath = cacheAction ("build" :: T.Text, srcPath) $ do
liftIO . putStrLn $ "Rebuilding episode: " <> srcPath
postContent <- readFile' srcPath
postData <- markdownToHTML . T.pack $ postContent

let epId = getEpisodeId srcPath
postUrl = T.pack . dropDirectory1 $ srcPath -<.> "html"
withPostUrl = _Object . at "url" ?~ String postUrl

fullPostData
= withPostUrl
$ postData
& _Object . at "episodeId" ?~ String (T.pack $ show epId)

template <- compileTemplate' "site/templates/episode.html"
writeFile' (outputFolder </> T.unpack postUrl)
. T.unpack
$ substitute template fullPostData
convert fullPostData

-- | Copy all static files from the listed folders to their destination
copyStaticFiles :: Action ()
copyStaticFiles = do
filepaths <- getDirectoryFiles "./site/" ["images//*", "css//*", "js//*"]
void $ forP filepaths $ \filepath ->
copyFileChanged ("site" </> filepath) (outputFolder </> filepath)

-- | Specific build rules for the Shake system
-- defines workflow to build the website
buildRules :: Action ()
buildRules = do
allPosts <- buildEpisodes
buildIndex allPosts
copyStaticFiles

-- | Kick it all off
main :: IO ()
main = site $ do
-- Load all the posts from site/posts/
posts <- resourceLoader markdownReader ["episodes/*.md"]

-- Build up a context for our index page
let indexContext :: Value
indexContext = object [ "posts" .= posts
-- The url is where the index page will be written to
, "url" .= ("/index.html" :: String)
]

-- write out index page and posts via templates
writeTemplate "templates/index.html" [indexContext]
writeTemplate "templates/episode.html" posts
main = do
let shOpts =
forwardOptions $ shakeOptions
{ shakeVerbosity = Chatty
, shakeLintInside = ["./site/"]
, shakeVersion = "3"
}
shakeArgsForward shOpts buildRules

22 changes: 5 additions & 17 deletions cofree-coffee-cast.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,10 @@ executable cofree-coffee-cast
build-depends:
aeson
, base >=4.7 && <5
, lens
, lens-aeson
, pandoc
, sitepipe
default-language: Haskell2010

test-suite cofree-coffee-cast-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_cofree_coffee_cast
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
aeson
, base >=4.7 && <5
, cofree-coffee-cast
, pandoc
, sitepipe
, shake
, slick
, text
default-language: Haskell2010
175 changes: 175 additions & 0 deletions docs/episodes/001-jonathan-lorimer.html

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions docs/episodes/002-solomon-bothwell.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Solomon Bothwell -- Cofree Coffee Cast</title>
</head>
<body>
<article>
<h1>Solomon Bothwell</h1>

</article>
</body>
</html>
15 changes: 15 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Cofree Coffee Cast</title>
</head>
<body>
<h1>Episodes</h1>
<ul>
<li><a href="episodes/002-solomon-bothwell.html">2 - Solomon Bothwell (2022-01-30)</a></li>
<li><a href="episodes/001-jonathan-lorimer.html">1 - Jonathan Lorimer (2022-01-15)</a></li>
</ul>
</body>
</html>
17 changes: 5 additions & 12 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ description: Please see the README on GitHub at <https://github.com/isov

dependencies:
- base >= 4.7 && < 5
- sitepipe
- slick
- aeson
- pandoc
- shake
- text
- lens-aeson
- lens

executables:
cofree-coffee-cast:
Expand All @@ -33,14 +37,3 @@ executables:
- -threaded
- -rtsopts
- -with-rtsopts=-N

tests:
cofree-coffee-cast-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- cofree-coffee-cast
5 changes: 2 additions & 3 deletions site/templates/episode.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>{{title}}</title>
<title>{{guest}} -- Cofree Coffee Cast</title>
</head>
<body>
<article>
<h1>{{title}}</h1>
<h2>By <i>{{author}}</i></h2>
<h1>{{guest}}</h1>
{{{content}}}
</article>
</body>
Expand Down
10 changes: 5 additions & 5 deletions site/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>My Blog</title>
<title>Cofree Coffee Cast</title>
</head>
<body>
<h1>Posts</h1>
<h1>Episodes</h1>
<ul>
{{#posts}}
<li><a href="{{url}}">{{title}}</a></li>
{{/posts}}
{{#episodes}}
<li><a href="{{url}}">{{episodeId}} - {{guest}} ({{date}})</a></li>
{{/episodes}}
</ul>
</body>
</html>
1 change: 0 additions & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ extra-deps:
- sitepipe-0.4.0.1
- MissingH-1.4.3.0


0 comments on commit c5c2a9f

Please sign in to comment.