2019-01-27 21:41:21 +01:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2019-02-03 22:56:21 +01:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2019-01-27 21:41:21 +01:00
|
|
|
module Blog (
|
2019-02-02 23:23:05 +01:00
|
|
|
Blog(..)
|
2019-02-15 15:11:31 +01:00
|
|
|
, Path(..)
|
2019-02-07 17:51:06 +01:00
|
|
|
, Skin(..)
|
2019-02-03 22:56:21 +01:00
|
|
|
, build
|
2019-01-27 21:41:21 +01:00
|
|
|
, get
|
|
|
|
) where
|
|
|
|
|
2019-02-15 15:11:31 +01:00
|
|
|
import Arguments (Arguments)
|
2019-02-07 17:51:06 +01:00
|
|
|
import qualified Arguments (name)
|
2019-02-02 23:23:05 +01:00
|
|
|
import Article (Article)
|
2019-02-15 18:07:59 +01:00
|
|
|
import qualified Article (at, getKey)
|
2019-02-15 15:11:31 +01:00
|
|
|
import Blog.Path (Path(..))
|
|
|
|
import qualified Blog.Path as Path (build)
|
2019-02-07 17:51:06 +01:00
|
|
|
import Blog.Skin (Skin(..))
|
|
|
|
import qualified Blog.Skin as Skin (build)
|
2019-01-27 21:41:21 +01:00
|
|
|
import Control.Monad ((>=>), filterM, forM)
|
2019-02-03 22:56:21 +01:00
|
|
|
import Control.Monad.Reader (MonadReader, ask)
|
2019-02-15 14:13:43 +01:00
|
|
|
import Data.Either (rights)
|
2019-01-27 21:41:21 +01:00
|
|
|
import Data.Map (Map)
|
|
|
|
import qualified Data.Map as Map (fromList, member)
|
|
|
|
import Data.Set (Set)
|
|
|
|
import qualified Data.Set as Set (empty, null, singleton, union)
|
2019-02-06 17:16:52 +01:00
|
|
|
import qualified Files (find)
|
|
|
|
import System.Directory (doesFileExist, withCurrentDirectory)
|
2019-02-15 15:11:31 +01:00
|
|
|
import System.FilePath ((</>), dropTrailingPathSeparator, takeExtension, takeFileName)
|
2019-01-27 21:41:21 +01:00
|
|
|
|
2019-02-15 14:16:21 +01:00
|
|
|
type Collection = Map String Article
|
2019-01-27 21:41:21 +01:00
|
|
|
|
|
|
|
data Blog = Blog {
|
|
|
|
articles :: Collection
|
|
|
|
, name :: String
|
2019-02-15 15:11:31 +01:00
|
|
|
, path :: Path
|
2019-02-07 17:51:06 +01:00
|
|
|
, skin :: Skin
|
2019-02-15 14:16:21 +01:00
|
|
|
, tags :: Map String (Set String)
|
2019-01-27 21:41:21 +01:00
|
|
|
}
|
|
|
|
|
2019-02-03 22:56:21 +01:00
|
|
|
get :: MonadReader Blog m => (Blog -> a) -> m a
|
|
|
|
get = (<$> ask)
|
|
|
|
|
2019-02-15 14:16:21 +01:00
|
|
|
findArticles :: FilePath -> IO (Map String Article)
|
2019-02-15 14:13:43 +01:00
|
|
|
findArticles =
|
2019-02-06 17:16:52 +01:00
|
|
|
Files.find
|
2019-02-02 23:23:05 +01:00
|
|
|
>=> filterM isMarkDownFile
|
2019-02-15 14:13:43 +01:00
|
|
|
>=> mapM Article.at
|
|
|
|
>=> return . Map.fromList . rights
|
2019-01-27 21:41:21 +01:00
|
|
|
where
|
|
|
|
isMarkDownFile path = do
|
|
|
|
let correctExtension = takeExtension path == ".md"
|
2019-02-02 23:23:05 +01:00
|
|
|
(correctExtension &&) <$> doesFileExist path
|
2019-01-27 21:41:21 +01:00
|
|
|
|
2019-02-15 14:16:21 +01:00
|
|
|
tagged :: Collection -> FilePath -> IO (String, Set String)
|
2019-01-27 21:41:21 +01:00
|
|
|
tagged collection path = do
|
2019-02-06 17:16:52 +01:00
|
|
|
links <- Files.find path
|
2019-02-15 14:16:21 +01:00
|
|
|
keys <- forM links $ \link -> do
|
2019-02-02 23:23:05 +01:00
|
|
|
fileExists <- doesFileExist link
|
2019-02-15 14:16:21 +01:00
|
|
|
return $ if fileExists
|
2019-02-15 18:07:59 +01:00
|
|
|
then let articleKey = Article.getKey link in
|
2019-02-15 14:16:21 +01:00
|
|
|
if Map.member articleKey collection then Set.singleton articleKey else Set.empty
|
|
|
|
else Set.empty
|
|
|
|
return (takeFileName path, foldl Set.union Set.empty keys)
|
2019-01-27 21:41:21 +01:00
|
|
|
|
2019-02-03 22:56:21 +01:00
|
|
|
build :: Arguments -> IO Blog
|
2019-02-15 15:11:31 +01:00
|
|
|
build arguments = withCurrentDirectory (root path) $ do
|
2019-02-07 17:51:06 +01:00
|
|
|
skin <- Skin.build name arguments
|
2019-02-15 15:11:31 +01:00
|
|
|
articles <- findArticles $ articlesPath path
|
2019-01-27 21:41:21 +01:00
|
|
|
tags <- Map.fromList . filter (not . Set.null . snd)
|
2019-02-15 15:11:31 +01:00
|
|
|
<$> (Files.find (articlesPath path </> "tags") >>= mapM (articles `tagged`))
|
|
|
|
return $ Blog {articles, name, path, skin, tags}
|
2019-02-01 13:46:55 +01:00
|
|
|
where
|
2019-02-15 15:11:31 +01:00
|
|
|
path = Path.build arguments
|
|
|
|
name = maybe (takeFileName . dropTrailingPathSeparator $ root path) id
|
|
|
|
$ Arguments.name arguments
|