Commit 7bb296d5 authored by Nicolas Lenz's avatar Nicolas Lenz ❄️
Browse files

Some refactoring

parent 76d2fa48
Loading
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -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.
+9 −6
Original line number Diff line number Diff line
@@ -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
+2 −1
Original line number Diff line number Diff line
{-# 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"
+11 −11
Original line number Diff line number Diff line
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.
}
+2 −1
Original line number Diff line number Diff line
@@ -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))
Loading