Commit 2c45cd93 authored by Nicolas Lenz's avatar Nicolas Lenz ❄️
Browse files

Add activeOnArticles option for navItems

Refactor putNavbar
parent dcf8e94b
Loading
Loading
Loading
Loading
Loading
+17 −24
Original line number Diff line number Diff line
@@ -10,32 +10,25 @@ import Config
import Load
import Save

-- | Generate the Pandoc MetaValue for a navbar from a list of NavItem from a Nebelhorn config.
generateNavbarMeta :: [NavItem] -> Maybe MetaValue -> MetaValue
generateNavbarMeta navItems (Just (MetaString link)) = MetaList $ map f navItems where
    f NavItem{..} = MetaMap $ Map.fromList
        [ ("name", MetaString $ unpack navItemName)
        , ("link", MetaString $ unpack navItemLink)
        , ("active", if unpack navItemLink == ("/" <> link) then MetaBool True else MetaBool False)
        ]
generateNavbarMeta navItems _ = MetaList $ map f navItems where
    f NavItem{..} = MetaMap $ Map.fromList
-- | Puts the navbar into all documents of a Nebelhorn.
putNavbar :: Nebelhorn -> Nebelhorn
putNavbar nebelhorn@Nebelhorn{..} = nebelhorn
    { nebelhornArticles = putNavbarArticle <$> nebelhornArticles
    , nebelhornPages = putNavbarPage <$> nebelhornPages
    , nebelhornIndex = putNavbarPage nebelhornIndex
    } where
        putNavbarArticle = addMeta "navbar" $ MetaList $ navItemToMeta True <$> nebelhornNavItems
        putNavbarPage pandoc@(Pandoc meta _) = addMeta "navbar" (MetaList $ f <$> nebelhornNavItems) pandoc where
            f n@NavItem{..} = case lookupMeta "link" meta of
                Just (MetaString link) -> navItemToMeta (unpack navItemLink == "/" <> link) n
                _ -> navItemToMeta False n
        navItemToMeta :: Bool -> NavItem -> MetaValue
        navItemToMeta active NavItem{..} = MetaMap $ Map.fromList
            [ ("name", MetaString $ unpack navItemName)
            , ("link", MetaString $ unpack navItemLink)
        , ("active", MetaBool False)
            , ("active", MetaBool active)
            ]

putNavbarPage :: [NavItem] -> Pandoc -> Pandoc
putNavbarPage navItems pandoc@(Pandoc meta _)
    = addMeta "navbar" (generateNavbarMeta navItems $ lookupMeta "link" meta) pandoc

-- Puts the navbar into all documents of a Nebelhorn.
putNavbar :: Nebelhorn -> Nebelhorn
putNavbar nebelhorn@Nebelhorn{..}
    = nebelhorn{nebelhornArticles = putNavbarPage nebelhornNavItems <$> nebelhornArticles,
    nebelhornPages = putNavbarPage nebelhornNavItems <$> nebelhornPages,
    nebelhornIndex = putNavbarPage nebelhornNavItems nebelhornIndex}

-- | 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)]
+4 −2
Original line number Diff line number Diff line
@@ -21,7 +21,8 @@ data Config = Config {

data NavItem = NavItem {
    navItemName :: Text,
    navItemLink :: Text
    navItemLink :: Text,
    navItemActiveOnArticles :: Bool
} deriving (Show)

instance FromJSON Config where
@@ -34,12 +35,13 @@ instance FromJSON Config where
        <*> v .:? "templatePage" .!= [relfile|template/page.html|]
        <*> v .:? "templateIndex" .!= [relfile|template/index.html|]
        <*> v .:? "foldersToCopy" .!= []
        <*> v .:? "navbar" .!= [NavItem "Home" "/index.html"]
        <*> v .:? "navbar" .!= [NavItem "Home" "/index.html" False]

instance FromJSON NavItem where
    parseJSON = withObject "NavItem" $ \v -> NavItem
        <$> v .: "name"
        <*> v .: "link"
        <*> v .:? "activeOnArticles" .!= True

-- | Loads a config file from the nebelhorn.yaml in the current directory.
loadConfig :: IO (Either ParseException Config)