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

Allow specifying a metadata provider #780

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ip1981
Copy link

@ip1981 ip1981 commented Jun 4, 2020

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

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
@jwiegley
Copy link

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

@Alf0nso
Copy link

Alf0nso commented Dec 10, 2023

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Pandoc Meta data to Context
3 participants