Commit 2c45cd93 authored by Nicolas Lenz's avatar Nicolas Lenz

Add activeOnArticles option for navItems

Refactor putNavbar
parent dcf8e94b
Pipeline #288 passed with stage
in 3 minutes and 3 seconds
......@@ -10,31 +10,24 @@ 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
[ ("name", MetaString $ unpack navItemName)
, ("link", MetaString $ unpack navItemLink)
, ("active", MetaBool False)
]
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.
-- | 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}
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 active)
]
-- | Gets the previous and next neigbors for each article in the list.
-- This only works if every pandocument has the link meta value set.
......
......@@ -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)
......
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