Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add importField in Hakyll.Web.Template.Context #927

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/Hakyll/Core/Provider/Metadata.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{-# LANGUAGE RecordWildCards #-}
module Hakyll.Core.Provider.Metadata
( loadMetadata
, loadMetadataFile
, parsePage

, MetadataException (..)
Expand Down
20 changes: 19 additions & 1 deletion lib/Hakyll/Web/Template/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ import Hakyll.Core.Identifier
import Hakyll.Core.Item
import Hakyll.Core.Metadata
import Hakyll.Core.Provider
import Hakyll.Core.Provider.Metadata (loadMetadataFile)
import Hakyll.Core.Util.String (needlePrefix, splitAll)
import Hakyll.Web.Html
import Prelude hiding (id)
import System.FilePath (dropExtension, splitDirectories,
takeBaseName)
takeBaseName, takeDirectory, (</>))


--------------------------------------------------------------------------------
Expand Down Expand Up @@ -456,6 +457,23 @@ teaserFieldWithSeparator separator key snapshot = field key $ \item -> do
Just t -> return t


--------------------------------------------------------------------------------
-- | If an "import" field can be found in the metadata which contains
-- a file path, parse the file as metadata and map any field to it.
importField :: Context a
importField = Context $ \k _ i -> do
let id = itemIdentifier i
metadata <- getMetadata id
case lookupString "import" metadata of
Just fileName -> do
let fileDir = takeDirectory $ toFilePath id
fp = fileDir </> fileName
empty' = noResult $ "No '" ++ k ++ "' field in imported metadata of " ++ show (fromFilePath fp)
metadata' <- unsafeCompiler $ loadMetadataFile fp
maybe empty' (return . StringField) (lookupString k metadata')
Nothing -> noResult $ "No 'import' field found in " ++ show id


--------------------------------------------------------------------------------
-- | Constantly reports any field as missing. Mostly for internal usage,
-- it is the last choice in every context used in a template application.
Expand Down