Loading src/Build.hs +29 −10 Original line number Diff line number Diff line Loading @@ -30,11 +30,11 @@ generateNavbarMeta navItems = MetaList $ map f navItems where -- | Reads in a single document to a Pandoc value using an extra Meta. read :: (PandocMonad m, MonadIO m) => Meta -> (Path Rel File, Text) -> m (Path Rel File, Pandoc) read extraMeta (path, content) = do read (Meta extraMetaMap) (path, content) = do contentR <- read' content -- TODO: This should really be done without IO... newPath <- liftIO $ setFileExtension ".html" path return (newPath, addMeta extraMeta contentR) return (newPath, addMeta (Meta $ Map.insert "link" (MetaString $ unpack $ toFilePath newPath) extraMetaMap) contentR) where read' = readMarkdown $ def {readerExtensions = pandocExtensions} -- | Writes a single Pandoc document to HTML5 output using a template. Loading Loading @@ -62,8 +62,12 @@ buildArticles navbar inputs = do templateArticle <- readFileUtf8 "template/article.html" templateIndex <- readFileUtf8 "template/index.html" articlesInput <- read extraMeta `mapM` inputs let indexInput = generateIndex extraMeta articlesInput articlesInput' <- read extraMeta `mapM` inputs let neighbors = getNeighbors $ snd <$> articlesInput' -- TODO Refactor! let articlesInput = map (\((prevM, nextM), (path, art)) -> (path, addMeta (Meta $ Map.fromList [("prev", fromMaybe (MetaString "") prevM), ("next", fromMaybe (MetaString "") nextM)]) art)) (zip neighbors articlesInput') let indexInput = generateIndex extraMeta (snd <$> articlesInput) articlesOutput <- write templateArticle `mapMT` articlesInput indexOutput <- write templateIndex indexInput Loading @@ -72,20 +76,35 @@ buildArticles navbar inputs = do where extraMeta = Meta $ Map.fromList [("navbar", navbar)] getNeighbors :: [Pandoc] -> [(Maybe MetaValue, Maybe MetaValue)] getNeighbors articles = f <$> [0..(length articles - 1)] where f n = (getLink =<< articles `safeIndex` (n-1), getLink =<< articles `safeIndex` (n+1)) getLink :: Pandoc -> Maybe MetaValue getLink (Pandoc meta _) = lookupMeta "link" meta safeIndex :: [a] -> Int -> Maybe a safeIndex [] _ = Nothing safeIndex _ n | n < 0 = Nothing safeIndex (x:_) 0 = Just x safeIndex (_:xs) n = safeIndex xs (n-1) -- | Generate an index page from a list of Articles. generateIndex :: Meta -> [(Path Rel File, Pandoc)] -> Pandoc -- Expects each article to contain a meta value "link" with its link path. generateIndex :: Meta -> [Pandoc] -> Pandoc generateIndex extraMeta = generateIndex' . sort where sort = sortOn (Down . (\(_, Pandoc meta _) -> lookupMeta "date" meta)) sort = sortOn (Down . (\(Pandoc meta _) -> lookupMeta "date" meta)) generateIndex' articles = Pandoc metaOutput [] where metaOutput = extraMeta <> Meta (Map.fromList [("pagetitle", MetaString "Home"), ("articles", MetaList articlesOutput)]) articlesOutput = uncurry generateIndexMeta <$> articles articlesOutput = generateIndexMeta <$> articles -- | Generates the Pandoc MetaMap for a document for the index page. -- It contains the meta of the source, the link and the body. generateIndexMeta :: Path Rel File -> Pandoc -> MetaValue generateIndexMeta path (Pandoc (Meta metaMap) body) = MetaMap $ metaMap <> Map.fromList [("link", MetaString $ toFilePath path), ("body", MetaBlocks body)] generateIndexMeta :: Pandoc -> MetaValue generateIndexMeta (Pandoc (Meta metaMap) body) = MetaMap $ metaMap <> Map.fromList [("body", MetaBlocks body)] -- | Adds a Meta block to a pandocument. Already known keys are overwritten. addMeta :: Meta -> Pandoc -> Pandoc Loading Loading
src/Build.hs +29 −10 Original line number Diff line number Diff line Loading @@ -30,11 +30,11 @@ generateNavbarMeta navItems = MetaList $ map f navItems where -- | Reads in a single document to a Pandoc value using an extra Meta. read :: (PandocMonad m, MonadIO m) => Meta -> (Path Rel File, Text) -> m (Path Rel File, Pandoc) read extraMeta (path, content) = do read (Meta extraMetaMap) (path, content) = do contentR <- read' content -- TODO: This should really be done without IO... newPath <- liftIO $ setFileExtension ".html" path return (newPath, addMeta extraMeta contentR) return (newPath, addMeta (Meta $ Map.insert "link" (MetaString $ unpack $ toFilePath newPath) extraMetaMap) contentR) where read' = readMarkdown $ def {readerExtensions = pandocExtensions} -- | Writes a single Pandoc document to HTML5 output using a template. Loading Loading @@ -62,8 +62,12 @@ buildArticles navbar inputs = do templateArticle <- readFileUtf8 "template/article.html" templateIndex <- readFileUtf8 "template/index.html" articlesInput <- read extraMeta `mapM` inputs let indexInput = generateIndex extraMeta articlesInput articlesInput' <- read extraMeta `mapM` inputs let neighbors = getNeighbors $ snd <$> articlesInput' -- TODO Refactor! let articlesInput = map (\((prevM, nextM), (path, art)) -> (path, addMeta (Meta $ Map.fromList [("prev", fromMaybe (MetaString "") prevM), ("next", fromMaybe (MetaString "") nextM)]) art)) (zip neighbors articlesInput') let indexInput = generateIndex extraMeta (snd <$> articlesInput) articlesOutput <- write templateArticle `mapMT` articlesInput indexOutput <- write templateIndex indexInput Loading @@ -72,20 +76,35 @@ buildArticles navbar inputs = do where extraMeta = Meta $ Map.fromList [("navbar", navbar)] getNeighbors :: [Pandoc] -> [(Maybe MetaValue, Maybe MetaValue)] getNeighbors articles = f <$> [0..(length articles - 1)] where f n = (getLink =<< articles `safeIndex` (n-1), getLink =<< articles `safeIndex` (n+1)) getLink :: Pandoc -> Maybe MetaValue getLink (Pandoc meta _) = lookupMeta "link" meta safeIndex :: [a] -> Int -> Maybe a safeIndex [] _ = Nothing safeIndex _ n | n < 0 = Nothing safeIndex (x:_) 0 = Just x safeIndex (_:xs) n = safeIndex xs (n-1) -- | Generate an index page from a list of Articles. generateIndex :: Meta -> [(Path Rel File, Pandoc)] -> Pandoc -- Expects each article to contain a meta value "link" with its link path. generateIndex :: Meta -> [Pandoc] -> Pandoc generateIndex extraMeta = generateIndex' . sort where sort = sortOn (Down . (\(_, Pandoc meta _) -> lookupMeta "date" meta)) sort = sortOn (Down . (\(Pandoc meta _) -> lookupMeta "date" meta)) generateIndex' articles = Pandoc metaOutput [] where metaOutput = extraMeta <> Meta (Map.fromList [("pagetitle", MetaString "Home"), ("articles", MetaList articlesOutput)]) articlesOutput = uncurry generateIndexMeta <$> articles articlesOutput = generateIndexMeta <$> articles -- | Generates the Pandoc MetaMap for a document for the index page. -- It contains the meta of the source, the link and the body. generateIndexMeta :: Path Rel File -> Pandoc -> MetaValue generateIndexMeta path (Pandoc (Meta metaMap) body) = MetaMap $ metaMap <> Map.fromList [("link", MetaString $ toFilePath path), ("body", MetaBlocks body)] generateIndexMeta :: Pandoc -> MetaValue generateIndexMeta (Pandoc (Meta metaMap) body) = MetaMap $ metaMap <> Map.fromList [("body", MetaBlocks body)] -- | Adds a Meta block to a pandocument. Already known keys are overwritten. addMeta :: Meta -> Pandoc -> Pandoc Loading