Skip to content

Commit

Permalink
refactor again
Browse files Browse the repository at this point in the history
  • Loading branch information
srijan-paul committed Mar 8, 2024
1 parent 3b5c260 commit 726c265
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
39 changes: 19 additions & 20 deletions app/Bark/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Bark.Core
readBarkProject,
printErrorMessage,
printInfoMessage,
urlFromMdPath
urlFromMdPath,
)
where

Expand Down Expand Up @@ -135,7 +135,7 @@ getPostFromMdfile project filePath = do
postFrontMatter = frontmatter,
postContent = T.decodeUtf8 content,
postUrl = url,
postOtherData = []
postData = HM.singleton "url" (Mustache.String $ T.pack url)
}

-- | Convert markdown content to HTML.
Expand Down Expand Up @@ -187,21 +187,21 @@ buildPost project post = do
postHtml <- liftEither $ postToHtml post
template <- loadTemplate project post
let metadata = fmMetaData (postFrontMatter post)
postData =
buildData =
Mustache.Object $
HM.fromList $
HM.fromList
[ ("content", Mustache.String postHtml),
("meta", Mustache.Object metadata)
]
++ postOtherData post
output = Mustache.substitute template postData
<> postData post
output = Mustache.substitute template buildData
return $ HTMLPage post output

writeHtmlPage :: HTMLPage -> ExceptT ErrorMessage IO ()
writeHtmlPage :: HTMLPage -> IO ()
writeHtmlPage (HTMLPage post content) = do
let outPath = postDstPath post
liftIO $ createDirectoryIfMissing True $ takeDirectory outPath
liftIO $ TIO.writeFile outPath content
createDirectoryIfMissing True $ takeDirectory outPath
TIO.writeFile outPath content

-- | Get a list of all posts in the project.
getPosts :: Project -> ExceptT ErrorMessage IO [Post]
Expand Down Expand Up @@ -233,13 +233,13 @@ copyCopyDir project = liftIO $ do
buildProjectImpl :: Project -> [Processor] -> [Post] -> ExceptT ErrorMessage IO ()
buildProjectImpl project processors postsInProject = do
-- apply all preprocessors to the posts
processedPosts <- preprocessPosts processors postsInProject >>= addPostListToMeta project
processedPosts <- preprocessPosts processors postsInProject >>= addPostListToPostData project
-- Convert markdown posts to HTML pages.
pages <- mapM (buildPost project) processedPosts
-- apply any post compilation processors (e.g. syntax highlighting, etc.)
processedPages <- mapM applyHtmlProcessors pages
-- write the processed pages to disk
mapM_ writeHtmlPage processedPages
mapM_ (liftIO . writeHtmlPage) processedPages
-- copy assets directory to the output directory
copyAssets project
-- copy the copy directory to the output directory
Expand All @@ -251,32 +251,31 @@ buildProjectImpl project processors postsInProject = do
preprocessPosts :: [Processor] -> [Post] -> ExceptT ErrorMessage IO [Post]
preprocessPosts [] posts = return posts
preprocessPosts _ [] = return []
preprocessPosts (f:fs) posts = do
preprocessPosts (f : fs) posts = do
posts' <- mapM (preprocess f posts) posts
preprocessPosts fs posts'

preprocess :: Processor -> [Post] -> Post -> ExceptT ErrorMessage IO Post
preprocess (OnPost f) allPosts post = f project allPosts post
preprocess (OnPost f) allPosts post = f project allPosts post
preprocess _ _ post = return post

applyHtmlProcessor :: HTMLPage -> Processor -> ExceptT ErrorMessage IO HTMLPage
applyHtmlProcessor page (OnHTML f) = f project page
applyHtmlProcessor page _ = return page

addPostListToMeta :: Project -> [Post] -> ExceptT ErrorMessage IO [Post]
addPostListToMeta (Project {projectOutDir = outDir}) posts' = do
-- Every post has an additional data field called `posts`.
-- | Add a list of posts to the build time data of each post.
addPostListToPostData :: Project -> [Post] -> ExceptT ErrorMessage IO [Post]
addPostListToPostData _ posts' = do
-- Every gets has an additional data field called `posts`.
-- It is an array of all posts in the project.
let allPosts = Mustache.Array $ Vector.fromList $ map getPostData posts'
posts = map (\p -> p {postOtherData = ("posts", allPosts) : postOtherData p}) posts'
posts = map (\p -> p {postData = HM.insert "posts" allPosts (postData p)}) posts'
return posts
where
getPostData :: Post -> Mustache.Value
getPostData post =
let meta = Mustache.Object $ fmMetaData $ postFrontMatter post
relativeUrl = makeRelative outDir (postDstPath post)
dstPath = Mustache.String $ T.pack relativeUrl
in Mustache.Object $ HM.fromList $ [("meta", meta), ("dstPath", dstPath)] ++ postOtherData post
in Mustache.Object $ HM.insert "meta" meta (postData post)

-- | Build a bark project using the given list of processors.
buildProject :: Project -> [Processor] -> ExceptT ErrorMessage IO ()
Expand Down
8 changes: 6 additions & 2 deletions app/Bark/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import Bark.FrontMatter (PostFrontMatter (..))
import Control.Monad.Except (ExceptT)
import Data.Aeson.Types (typeMismatch, (.:))
import qualified Data.Text as T
import qualified Data.Yaml as Yml
import qualified Data.Vector as Vector
import qualified Data.Yaml as Yml
import Text.Mustache.Types as Mustache

type ErrorMessage = String
Expand Down Expand Up @@ -94,7 +94,7 @@ data Post = Post
--
-- In addition to these, posts can be modified to have their own data fields.
-- For example, a **website_url** field that stores the URL where the site containing all pages is hosted.
postOtherData :: [(T.Text, Mustache.Value)]
postData :: Mustache.Object
}
deriving (Show)

Expand All @@ -114,13 +114,17 @@ data HTMLPage = HTMLPage
htmlPageContent :: T.Text
}

-- | A function that modifies a post before it is converted to HTML.
type Preprocessor = Project -> [Post] -> Post -> ExceptT ErrorMessage IO Post

-- | A function that modifies an HTML page before it is written to disk.
type Postprocessor = Project -> HTMLPage -> ExceptT ErrorMessage IO HTMLPage

-- | Modifies an asset file in the project before it is written to disk.
-- Useful for minifying, compressing, or otherwise modifying assets.
type AssetProcessor = Project -> AssetFile -> ExceptT ErrorMessage IO AssetFile

-- | Modifies a post, HTML page, or asset file before its written to disk.
data Processor
= OnPost Preprocessor
| OnHTML Postprocessor
Expand Down

0 comments on commit 726c265

Please sign in to comment.