First attempt at exporting to JSON

This commit is contained in:
Tissevert 2019-02-04 18:30:23 +01:00
parent fb66c578fa
commit f7442f5b37

View file

@ -1,16 +1,51 @@
{-# LANGUAGE DeriveGeneric #-}
module JSON (
generate
) where
import Blog (Blog(..))
import Article (Article)
import qualified Article (Article(..))
import Blog (Blog)
import qualified Blog (Blog(..), get)
import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad.Reader (MonadReader(..), ReaderT)
import Data.Aeson
import Control.Monad.Reader (ReaderT)
import Data.Aeson (ToJSON(..), genericToEncoding, defaultOptions, encode)
import Data.ByteString.Lazy (writeFile)
import System.FilePath.Posix ((</>))
import qualified Data.Map as Map (elems)
import System.FilePath.Posix ((</>), (<.>))
import Prelude hiding (writeFile)
import GHC.Generics
data ArticleExport = ArticleExport {
source :: String
, title :: String
} deriving (Generic)
instance ToJSON ArticleExport where
toEncoding = genericToEncoding defaultOptions
data BlogDB = BlogDB {
articles :: [ArticleExport]
} deriving (Generic)
instance ToJSON BlogDB where
toEncoding = genericToEncoding defaultOptions
export :: Article -> ArticleExport
export article = ArticleExport {
source = "/" </> Article.urlPath article <.> "md"
, title = Article.title article
}
exportBlog :: ReaderT Blog IO BlogDB
exportBlog = do
allArticles <- Blog.get Blog.articles
return $ BlogDB {
articles = export <$> Map.elems allArticles
}
generate :: ReaderT Blog IO ()
generate = do
path <- root <$> ask
liftIO $ writeFile (path </> "articles.json") (encode $ object [])
path <- Blog.get Blog.root
jsonContent <- exportBlog
liftIO $ writeFile (path </> "articles.json") (encode jsonContent)