diff options
| author | Albert Krewinkel <albert@zeitkraut.de> | 2022-01-04 10:38:02 +0100 |
|---|---|---|
| committer | John MacFarlane <jgm@berkeley.edu> | 2022-01-04 11:55:59 -0800 |
| commit | 1f8638fb5410c1ec1710a610a178376143cab311 (patch) | |
| tree | fca15186c0a001964a20591f2407f2b48189dfd3 /src/Text/Pandoc/Lua/Module | |
| parent | 974a9d353a91c4a23a92d82d0d5a92b55018e086 (diff) | |
Lua: add `pandoc.template` module
The module provides a `compile` function to use strings as templates.
Diffstat (limited to 'src/Text/Pandoc/Lua/Module')
| -rw-r--r-- | src/Text/Pandoc/Lua/Module/Pandoc.hs | 2 | ||||
| -rw-r--r-- | src/Text/Pandoc/Lua/Module/Template.hs | 61 |
2 files changed, 62 insertions, 1 deletions
diff --git a/src/Text/Pandoc/Lua/Module/Pandoc.hs b/src/Text/Pandoc/Lua/Module/Pandoc.hs index c0127cdfe..9864da0db 100644 --- a/src/Text/Pandoc/Lua/Module/Pandoc.hs +++ b/src/Text/Pandoc/Lua/Module/Pandoc.hs @@ -29,6 +29,7 @@ import HsLua hiding (pushModule) import HsLua.Class.Peekable (PeekError) import System.Exit (ExitCode (..)) import Text.Pandoc.Definition +import Text.Pandoc.Error (PandocError (..)) import Text.Pandoc.Lua.Orphans () import Text.Pandoc.Lua.Marshal.AST import Text.Pandoc.Lua.Marshal.Filter (peekFilter) @@ -50,7 +51,6 @@ import qualified Data.ByteString.Lazy.Char8 as BSL import qualified Data.Text as T import qualified Text.Pandoc.Lua.Util as LuaUtil import qualified Text.Pandoc.UTF8 as UTF8 -import Text.Pandoc.Error -- | Push the "pandoc" package to the Lua stack. Requires the `List` -- module to be loadable. diff --git a/src/Text/Pandoc/Lua/Module/Template.hs b/src/Text/Pandoc/Lua/Module/Template.hs new file mode 100644 index 000000000..cd66ce1c1 --- /dev/null +++ b/src/Text/Pandoc/Lua/Module/Template.hs @@ -0,0 +1,61 @@ +{-# LANGUAGE OverloadedStrings #-} +{- | + Module : Text.Pandoc.Lua.Module.Template + Copyright : Copyright © 2022 Albert Krewinkel, John MacFarlane + License : GNU GPL, version 2 or above + Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de> + +Lua module to handle pandoc templates. +-} +module Text.Pandoc.Lua.Module.Template + ( documentedModule + ) where + +import HsLua +import Text.Pandoc.Error (PandocError) +import Text.Pandoc.Lua.Marshal.Template (pushTemplate) +import Text.Pandoc.Lua.PandocLua (PandocLua (unPandocLua), liftPandocLua) +import Text.Pandoc.Templates + (compileTemplate, getDefaultTemplate, runWithPartials, runWithDefaultPartials) + +import qualified Data.Text as T + +-- | The "pandoc.template" module. +documentedModule :: Module PandocError +documentedModule = Module + { moduleName = "pandoc.template" + , moduleDescription = T.unlines + [ "Lua functions for pandoc templates." + ] + , moduleFields = [] + , moduleOperations = [] + , moduleFunctions = functions + } + +-- | Template module functions. +functions :: [DocumentedFunction PandocError] +functions = + [ defun "compile" + ### (\template mfilepath -> unPandocLua $ + case mfilepath of + Just fp -> runWithPartials (compileTemplate fp template) + Nothing -> runWithDefaultPartials + (compileTemplate "templates/default" template)) + <#> parameter peekText "string" "template" "template string" + <#> optionalParameter peekString "string" "templ_path" "template path" + =#> functionResult (either failLua pushTemplate) "pandoc Template" + "compiled template" + + , defun "default" + ### (\mformat -> unPandocLua $ do + let getFORMAT = liftPandocLua $ do + getglobal "FORMAT" + forcePeek $ peekText top `lastly` pop 1 + format <- maybe getFORMAT pure mformat + getDefaultTemplate format) + <#> optionalParameter peekText "string" "writer" + "writer for which the template should be returned." + =#> functionResult pushText "string" + "string representation of the writer's default template" + + ] |
