hakyll icon indicating copy to clipboard operation
hakyll copied to clipboard

Allow specifying a metadata provider

Open ip1981 opened this issue 5 years ago • 3 comments

In addition to parsing metadata header & metadata files it will allow any custom routine to extract metadata from the resource files, for example with Pandoc. Closes #643.

Feel free to update it to make it more idiomatic and whatnot.

Example:

A simple Pandoc provider for LaTeX

module PandocMetadata
  ( pandocMetadata
  ) where

import Hakyll

import qualified Data.Aeson.Types as AT
import qualified Data.HashMap.Strict as HM
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import qualified Text.Pandoc.Class as PDC
import qualified Text.Pandoc.Definition as PDD
import qualified Text.Pandoc.Options as PDO
import qualified Text.Pandoc.Readers as PDR
import qualified Text.Pandoc.Writers as PDW

pandocMetadata :: FilePath -> IO Metadata
pandocMetadata file = do
  PDD.Pandoc meta _ <- parseFile file
  print meta
  return $
    HM.fromList .
    map (\(f, t) -> (T.pack f, AT.String t)) .
    filter (not . T.null . snd) .
    map (\(f, ex, wr) -> (f, inlinesTo wr (ex meta))) $
    [ ("title", PDD.docTitle, PDW.writePlain)
    , ("titleHtml", PDD.docTitle, PDW.writeHtml5String)
    , ("abstractHtml", docAbstract, PDW.writeHtml5String)
    , ("date", PDD.docDate, PDW.writePlain)
    ]

parseFile :: FilePath -> IO PDD.Pandoc
parseFile file = do
  cnt <- TIO.readFile file
  case PDC.runPure $ PDR.readLaTeX PDO.def cnt of
    Right t -> return t
    Left e -> error $ "Pandoc read failed: " ++ show e

inlinesTo ::
     (PDO.WriterOptions -> PDD.Pandoc -> PDC.PandocPure T.Text)
  -> [PDD.Inline]
  -> T.Text
inlinesTo wr ill =
  case PDC.runPure . wr PDO.def $ doc of
    Right t -> T.strip t
    Left e -> error $ "Pandoc write failed: " ++ show e
  where
    doc = PDD.Pandoc PDD.nullMeta [PDD.Plain ill]

docAbstract :: PDD.Meta -> [PDD.Inline]
docAbstract meta =
  case PDD.lookupMeta (T.pack "abstract") meta of
    Just (PDD.MetaString s) -> [PDD.Str s]
    Just (PDD.MetaInlines ils) -> ils
    Just (PDD.MetaBlocks [PDD.Plain ils]) -> ils
    Just (PDD.MetaBlocks [PDD.Para ils]) -> ils
    _ -> []


Usage:

main :: IO ()
main = do
  opts <-
    O.execParser
      (O.info
         (parseOptions <**> O.helper)
         (O.fullDesc <> O.header "Static site compiler"))
  let conf =
        Conf.defaultConfiguration
          { Conf.destinationDirectory = outDir opts
          , Conf.providerDirectory = srcDir opts
          , Conf.storeDirectory = cacheDir opts
          , Conf.tmpDirectory = cacheDir opts </> "tmp"
          , Conf.provideMetadata = pandocMetadata
          }
  log <-
    Logger.new
      (if verbose opts
         then Logger.Debug
         else Logger.Message)
  case command opts of
    Build -> Cmd.build conf log rules >>= exitWith
    Clean -> Cmd.clean conf log
    Check chk -> Cmd.check conf log chk >>= exitWith

ip1981 avatar Jun 04 '20 08:06 ip1981

This is exactly what I needed today, in order get metadata from Org-mode properties, rather than a Yaml block.

jwiegley avatar Apr 21 '23 06:04 jwiegley

Any updates on this? I would love for the possibility to just use org-mode metadata, or for a way to specify the format of my metadata...

Alf0nso avatar Dec 10 '23 01:12 Alf0nso

I've been using this for a while now, and would love to see it added into the main release.

jwiegley avatar Jun 09 '24 17:06 jwiegley