From 859a65e61d9dc6cd68f048fda0f0da4b9bcc1396 Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Wed, 29 Jan 2025 20:37:50 +0100 Subject: extra metadata loading from a yaml file --- Types.hs | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) (limited to 'Types.hs') diff --git a/Types.hs b/Types.hs index e42dafa..78f0013 100644 --- a/Types.hs +++ b/Types.hs @@ -20,9 +20,7 @@ -- | Separated-out main types of the deployment scriptage. module Types where -import AesonUtils import Control.Monad.Trans.State.Lazy -import qualified Data.ByteString.UTF8 import qualified Data.Map as M import qualified Data.Set as S import qualified Data.Yaml as Y @@ -40,6 +38,14 @@ data PageInfo = PageInfo makeLenses ''PageInfo +-- | Information about where to source all extra metadata +data MetaSpec + = MetaSpecInline String + | MetaSpecFile FilePath + deriving (Show) + +makeLenses ''MetaSpec + -- | Complete internal state of the deployment process that holds all data data SiteState = SiteState { _pages :: M.Map FilePath PageInfo -- ^ Map of page mounts to `PageInfo` @@ -62,6 +68,7 @@ data SiteState = SiteState , _tagTemplate :: FilePath -- ^ Name of the template for category pages , _listTemplate :: FilePath -- ^ Name of the template for listing pages , _extraMeta :: Y.Value -- ^ Extra metadata added to rendering of all templates + , _extraMetaSpec :: [MetaSpec] -- ^ sources for the extra metadata , _metadataSuffix :: FilePath -- ^ File suffix to search for a extra metadata (e.g., if the suffix is ".extra", the extra metadata for file "page.md" will be looked for in "page.md.extra"). These are best autogenerated with a script that sources the data from git or so. , _indexFile :: FilePath -- ^ Name of the "index" files to be generated. , _urlBase :: FilePath -- ^ "Root route" to prepend to all absolute links. @@ -151,20 +158,20 @@ siteOptions' = do "Suffix for YAML files with base metadata for each markdown page. Metadata from files override the extra metadata specified on commandline, but are overridden by metadata specified directly in the markdown header of the pages." <> value ".metadata.yml" <> showDefault - _extraMeta <- - let processKeyVal :: String -> Y.Value - processKeyVal opt = - case Y.decodeEither' $ Data.ByteString.UTF8.fromString opt of - Right v -> v - Left err -> - error - $ "cannot parse YAML in --extra-metadata: " - ++ Y.prettyPrintParseException err - in fmap (foldl objMerge Y.Null . map processKeyVal) . many . strOption - $ long "extra-metadata" - <> short 'e' - <> help - "Extra metadata to add to pages rendering in YAML format. May be specified multiple times." + _extraMetaSpec <- + many + $ asum + [ fmap MetaSpecInline . strOption + $ long "extra-metadata" + <> short 'e' + <> help + "Extra metadata to add to pages rendering in YAML format. May be specified multiple times." + , fmap MetaSpecFile . strOption + $ long "extra-metadata-file" + <> short 'E' + <> help + "Extra metadata to add to pages rendering, loaded from a YAML file. May be specified multiple times." + ] _urlBase <- strOption $ long "url-base" @@ -199,6 +206,7 @@ siteOptions' = do , _installs = S.empty , _targets = S.empty , _templates = M.empty + , _extraMeta = Y.Null , .. } -- cgit v1.2.3