@@ -11,10 +11,12 @@ module HsBlog.Directory
1111import qualified HsBlog.Markup as Markup
1212import qualified HsBlog.Html as Html
1313import HsBlog.Convert (convert , convertStructure )
14+ import HsBlog.Env (Env (.. ))
1415
1516import Data.List (partition )
1617import Data.Traversable (for )
1718import Control.Monad (void , when )
19+ import Control.Monad.Reader (Reader , runReader , ask )
1820
1921import System.IO (hPutStrLn , stderr )
2022import Control.Exception (catch , displayException , SomeException (.. ))
@@ -38,12 +40,12 @@ import System.Directory
3840-- '.html' files in the process. Recording unsuccessful reads and writes to stderr.
3941--
4042-- May throw an exception on output directory creation.
41- convertDirectory :: FilePath -> FilePath -> IO ()
42- convertDirectory inputDir outputDir = do
43+ convertDirectory :: Env -> FilePath -> FilePath -> IO ()
44+ convertDirectory env inputDir outputDir = do
4345 DirContents filesToProcess filesToCopy <- getDirFilesAndContent inputDir
4446 createOutputDirectoryOrExit outputDir
4547 let
46- outputHtmls = txtsToRenderedHtml filesToProcess
48+ outputHtmls = runReader ( txtsToRenderedHtml filesToProcess) env
4749 copyFiles outputDir filesToCopy
4850 writeFiles outputDir outputHtmls
4951 putStrLn " Done."
@@ -77,8 +79,9 @@ data DirContents
7779------------------------------------
7880-- * Build index page
7981
80- buildIndex :: [(FilePath , Markup. Document )] -> Html. Html
81- buildIndex files =
82+ buildIndex :: [(FilePath , Markup. Document )] -> Reader Env Html. Html
83+ buildIndex files = do
84+ env <- ask
8285 let
8386 previews =
8487 map
@@ -92,9 +95,10 @@ buildIndex files =
9295 Html. h_ 3 (Html. link_ file (Html. txt_ file))
9396 )
9497 files
95- in
96- Html. html_
97- " Blog"
98+ pure $ Html. html_
99+ ( Html. title_ (eBlogName env)
100+ <> Html. stylesheet_ (eStylesheetPath env)
101+ )
98102 ( Html. h_ 1 (Html. link_ " index.html" (Html. txt_ " Blog" ))
99103 <> Html. h_ 2 (Html. txt_ " Posts" )
100104 <> mconcat previews
@@ -104,20 +108,22 @@ buildIndex files =
104108-- * Conversion
105109
106110-- | Convert text files to Markup, build an index, and render as html.
107- txtsToRenderedHtml :: [(FilePath , String )] -> [(FilePath , String )]
108- txtsToRenderedHtml txtFiles =
111+ txtsToRenderedHtml :: [(FilePath , String )] -> Reader Env [(FilePath , String )]
112+ txtsToRenderedHtml txtFiles = do
109113 let
110114 txtOutputFiles = map toOutputMarkupFile txtFiles
111- index = ( " index.html" , buildIndex txtOutputFiles)
112- in
113- map (fmap Html. render) (index : map convertFile txtOutputFiles )
115+ index <- (,) " index.html" <$> buildIndex txtOutputFiles
116+ htmlPages <- traverse convertFile txtOutputFiles
117+ pure $ map (fmap Html. render) (index : htmlPages )
114118
115119toOutputMarkupFile :: (FilePath , String ) -> (FilePath , Markup. Document )
116120toOutputMarkupFile (file, content) =
117121 (takeBaseName file <.> " html" , Markup. parse content)
118122
119- convertFile :: (FilePath , Markup. Document ) -> (FilePath , Html. Html )
120- convertFile (file, doc) = (file, convert file doc)
123+ convertFile :: (FilePath , Markup. Document ) -> Reader Env (FilePath , Html. Html )
124+ convertFile (file, doc) = do
125+ env <- ask
126+ pure (file, convert env (takeBaseName file) doc)
121127
122128------------------------------------
123129-- * Output to directory
0 commit comments