From 644e3239a9d1fc92f7409db8612ced39e38fde2f Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 27 Jun 2013 22:55:10 -0700 Subject: Custom writer: Pass full metadata, not just tit/auth/date. --- data/sample.lua | 14 +++++++------- src/Text/Pandoc/Writers/Custom.hs | 20 +++++++++++++++----- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/data/sample.lua b/data/sample.lua index fe425b749..36eb9f6ef 100644 --- a/data/sample.lua +++ b/data/sample.lua @@ -66,10 +66,10 @@ function Blocksep() end -- This function is called once for the whole document. Parameters: --- body, title, date are strings; authors is an array of strings; --- variables is a table. One could use some kind of templating +-- body is a string, metadata is a table, variables is a table. +-- One could use some kind of templating -- system here; this just gives you a simple standalone HTML file. -function Doc(body, title, authors, date, variables) +function Doc(body, metadata, variables) local buffer = {} local function add(s) table.insert(buffer, s) @@ -77,17 +77,17 @@ function Doc(body, title, authors, date, variables) add('') add('') add('') - add('' .. title .. '') + add('' .. metadata.title .. '') add('') add('') if title ~= "" then - add('

' .. title .. '

') + add('

' .. metadata.title .. '

') end - for _, author in pairs(authors) do + for _, author in pairs(metadata.author) do add('

' .. author .. '

') end if date ~= "" then - add('

' .. date .. '

') + add('

' .. metadata.date .. '

') end add(body) if #notes > 0 then diff --git a/src/Text/Pandoc/Writers/Custom.hs b/src/Text/Pandoc/Writers/Custom.hs index e6d912e78..732497616 100644 --- a/src/Text/Pandoc/Writers/Custom.hs +++ b/src/Text/Pandoc/Writers/Custom.hs @@ -107,6 +107,19 @@ instance StackValue [Block] where peek _ _ = undefined valuetype _ = Lua.TSTRING +instance StackValue MetaValue where + push l (MetaMap m) = Lua.push l m + push l (MetaList xs) = Lua.push l xs + push l (MetaString s) = Lua.push l s + push l (MetaInlines ils) = Lua.push l ils + push l (MetaBlocks bs) = Lua.push l bs + peek _ _ = undefined + valuetype (MetaMap _) = Lua.TTABLE + valuetype (MetaList _) = Lua.TTABLE + valuetype (MetaString _) = Lua.TSTRING + valuetype (MetaInlines _) = Lua.TSTRING + valuetype (MetaBlocks _) = Lua.TSTRING + -- | Convert Pandoc to custom markup. writeCustom :: FilePath -> WriterOptions -> Pandoc -> IO String writeCustom luaFile opts doc = do @@ -121,12 +134,9 @@ writeCustom luaFile opts doc = do return $ toString rendered docToCustom :: LuaState -> WriterOptions -> Pandoc -> IO ByteString -docToCustom lua opts (Pandoc meta blocks) = do - title' <- inlineListToCustom lua $ docTitle meta - authors' <- mapM (inlineListToCustom lua) $ docAuthors meta - date' <- inlineListToCustom lua $ docDate meta +docToCustom lua opts (Pandoc (Meta metamap) blocks) = do body <- blockListToCustom lua blocks - callfunc lua "Doc" body title' authors' date' (writerVariables opts) + callfunc lua "Doc" body metamap (writerVariables opts) -- | Convert Pandoc block element to Custom. blockToCustom :: LuaState -- ^ Lua state -- cgit v1.2.3