Commit 7bb296d5 authored by Nicolas Lenz's avatar Nicolas Lenz

Some refactoring

parent 76d2fa48
Pipeline #256 passed with stage
in 2 minutes and 41 seconds
......@@ -2,7 +2,7 @@
# Nebelhorn
A flexible static website generator using [Pandoc](https://pandoc.org), written in Haskell.
A flexible static website generator using [Pandoc](https://pandoc.org), written in Haskell. Still very much work in progress and changing fast, but it's already usable.
For an example of what it can do, take a look at [my blog](https://www.eisfunke.com).
......@@ -22,6 +22,7 @@ Alternatively download the latest binary [here](https://git.eisfunke.com/softwar
Official packages or container are not yet available, but planned.
## Contribution
If you discover any bugs, or if you have comments or feature ideas, please do open an issue or even submit a merge request.
......@@ -24,15 +24,17 @@ putNavbar nebelhorn@Nebelhorn{..}
nebelhornIndex = addMeta "navbar" navbar nebelhornIndex} where
navbar = generateNavbarMeta nebelhornNavItems
-- | Gets the previous and next neigbors for each article in the list. This only works if every pandocument has the link meta value set.
-- | Gets the previous and next neigbors for each article in the list.
-- This only works if every pandocument has the link meta value set.
getNeighbors :: [Pandoc] -> [(Maybe MetaValue, Maybe MetaValue)]
getNeighbors articles = f <$> [0..(length articles - 1)] where
f n = (getLinkMeta =<< articles `safeIndex` (n-1), getLinkMeta =<< articles `safeIndex` (n+1))
-- | Puts prev and next tags into all articles. Articles not for index are ignored, and also put in front of the list, which is okay as they're not put on the index page anyway.
-- | Puts prev and next tags into all articles.
-- Articles not for index are ignored, and also put in front of the list.
putNeighbors :: Nebelhorn -> Nebelhorn
putNeighbors nebelhorn@Nebelhorn{..}
= nebelhorn{nebelhornArticles = (<>) excludedArticles $ uncurry insertNeighbors <$> zip (getNeighbors articles) articles} where
putNeighbors nebelhorn@Nebelhorn{..} = nebelhorn{nebelhornArticles
= excludedArticles <> (uncurry insertNeighbors <$> zip (getNeighbors articles) articles)} where
articles = filter isForIndex nebelhornArticles
excludedArticles = filter (not . isForIndex) nebelhornArticles
insertNeighbors :: (Maybe MetaValue, Maybe MetaValue) -> Pandoc -> Pandoc
......@@ -61,7 +63,8 @@ isForIndex (Pandoc meta _) = case lookupMeta "noIndex" meta of
-- | Sorts a list of pandocuments by their date meta values.
sortArticles :: Nebelhorn -> Nebelhorn
sortArticles nebelhorn@Nebelhorn{..} = nebelhorn{nebelhornArticles = sortOn (Down . (\(Pandoc meta _) -> lookupMeta "date" meta)) nebelhornArticles}
sortArticles nebelhorn@Nebelhorn{..} = nebelhorn{nebelhornArticles
= sortOn (Down . (\(Pandoc meta _) -> lookupMeta "date" meta)) nebelhornArticles}
-- | Generates the Pandoc MetaMap for a document for the index page.
-- It contains the meta of the source, the link and the body.
......@@ -71,4 +74,4 @@ generateIndexMeta (Pandoc (Meta metaMap) body) = MetaMap $ metaMap
-- Builds a Nebelhorn page according to a config.
build :: (PandocMonad m, MonadIO m) => Config -> m ()
build config = (putIndex . putNavbar . putNeighbors . sortArticles <$> load config) >>= save
build config = save =<< putIndex . putNavbar . putNeighbors . sortArticles <$> load config
{-# LANGUAGE QuasiQuotes #-}
module Config where
module Config (Config(..), NavItem(..), loadConfig) where
import ClassyPrelude
import Data.Yaml
......@@ -39,5 +39,6 @@ instance FromJSON NavItem where
<$> v .: "name"
<*> v .: "link"
-- | Loads a config file from the nebelhorn.yaml in the current directory.
loadConfig :: IO (Either ParseException Config)
loadConfig = decodeFileEither "nebelhorn.yaml"
module Definition where
module Definition (Nebelhorn(..)) where
import ClassyPrelude
import Path
......@@ -7,14 +7,14 @@ import Text.Pandoc.Definition
-- | The Nebelhorn type.
data Nebelhorn = Nebelhorn {
nebelhornFolderOutput :: Path Rel Dir,
nebelhornFoldersToCopy :: [Path Rel Dir],
nebelhornNavItems :: [NavItem],
nebelhornArticles :: [Pandoc],
nebelhornPages :: [Pandoc],
nebelhornIndex :: Pandoc,
nebelhornTemplateArticle :: String,
nebelhornTemplatePage :: String,
nebelhornTemplateIndex :: String,
nebelhornStylesheet :: String
nebelhornFolderOutput :: Path Rel Dir, -- ^ The folder path to put the output into.
nebelhornFoldersToCopy :: [Path Rel Dir], -- ^ The list of folders to just copy over.
nebelhornNavItems :: [NavItem], -- ^ The list of navitems for the navbars.
nebelhornArticles :: [Pandoc], -- ^ The articles.
nebelhornPages :: [Pandoc], -- ^ The pages.
nebelhornIndex :: Pandoc, -- ^ The index page.
nebelhornTemplateArticle :: String, -- ^ The template for articles.
nebelhornTemplatePage :: String, -- ^ The template for pages.
nebelhornTemplateIndex :: String, -- ^ The template for the index pages.
nebelhornStylesheet :: FilePath -- ^ The stylesheet content.
}
......@@ -20,7 +20,8 @@ load Config{..} = Nebelhorn
<$> return configFolderOutput
<*> return configFoldersToCopy
<*> return configNavbar
<*> uncurry linkIntoMeta <$$> prependPath configFolderArticle <$$> readFolder configFolderArticle
<*> uncurry linkIntoMeta <$$> prependPath configFolderArticle
<$$> readFolder configFolderArticle
<*> uncurry linkIntoMeta <$$> readFolder configFolderPage
<*> return (Pandoc (Meta $ Map.singleton "link" $ MetaString "index.html") [])
<*> (unpack <$> readFileUtf8 (toFilePath configTemplateArticle))
......
{-# LANGUAGE QuasiQuotes #-}
module Main where
module Main (main) where
import ClassyPrelude hiding ((</>))
import Text.Pandoc.Class
import Config
import Build
-- | The main function. Loads the config and calls build.
main :: IO ()
main = loadConfig >>= \case
Left err -> putStrLn $ tshow err
......
{-# LANGUAGE QuasiQuotes #-}
module Save where
module Save (save, copyFolders) where
import ClassyPrelude hiding ((</>))
import Path
......@@ -32,7 +32,8 @@ writeDocument template basePath doc = do
ensureDir $ parent fileOutput
output <- writeHtml5String writerOptions doc
writeFileUtf8 (toFilePath fileOutput) output
where writerOptions = def{writerExtensions = pandocExtensions, writerTemplate = Just $ unpack template}
where writerOptions = def{writerExtensions
= pandocExtensions, writerTemplate = Just $ unpack template}
-- | Copies folders verbatim according to the target folder.
copyFolders :: [Path Rel Dir] -> Path a Dir -> IO ()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment