First attempt at exporting to JSON
This commit is contained in:
parent
fb66c578fa
commit
f7442f5b37
1 changed files with 41 additions and 6 deletions
47
src/JSON.hs
47
src/JSON.hs
|
@ -1,16 +1,51 @@
|
||||||
|
{-# LANGUAGE DeriveGeneric #-}
|
||||||
module JSON (
|
module JSON (
|
||||||
generate
|
generate
|
||||||
) where
|
) 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.IO.Class (MonadIO(..))
|
||||||
import Control.Monad.Reader (MonadReader(..), ReaderT)
|
import Control.Monad.Reader (ReaderT)
|
||||||
import Data.Aeson
|
import Data.Aeson (ToJSON(..), genericToEncoding, defaultOptions, encode)
|
||||||
import Data.ByteString.Lazy (writeFile)
|
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 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 :: ReaderT Blog IO ()
|
||||||
generate = do
|
generate = do
|
||||||
path <- root <$> ask
|
path <- Blog.get Blog.root
|
||||||
liftIO $ writeFile (path </> "articles.json") (encode $ object [])
|
jsonContent <- exportBlog
|
||||||
|
liftIO $ writeFile (path </> "articles.json") (encode jsonContent)
|
||||||
|
|
Loading…
Reference in a new issue