Custom writers now work with --template
.
Removed HTML header scaffolding from data/sample.lua.
This commit is contained in:
parent
aa1ac5a0af
commit
0e9d3db244
2 changed files with 18 additions and 21 deletions
|
@ -67,28 +67,15 @@ end
|
||||||
|
|
||||||
-- This function is called once for the whole document. Parameters:
|
-- This function is called once for the whole document. Parameters:
|
||||||
-- body is a string, metadata is a table, variables is a table.
|
-- body is a string, metadata is a table, variables is a table.
|
||||||
-- One could use some kind of templating
|
-- This gives you a fragment. You could use the metadata table to
|
||||||
-- system here; this just gives you a simple standalone HTML file.
|
-- fill variables in a custom lua template. Or, pass `--template=...`
|
||||||
|
-- to pandoc, and pandoc will add do the template processing as
|
||||||
|
-- usual.
|
||||||
function Doc(body, metadata, variables)
|
function Doc(body, metadata, variables)
|
||||||
local buffer = {}
|
local buffer = {}
|
||||||
local function add(s)
|
local function add(s)
|
||||||
table.insert(buffer, s)
|
table.insert(buffer, s)
|
||||||
end
|
end
|
||||||
add('<!DOCTYPE html>')
|
|
||||||
add('<html>')
|
|
||||||
add('<head>')
|
|
||||||
add('<title>' .. (metadata['title'] or '') .. '</title>')
|
|
||||||
add('</head>')
|
|
||||||
add('<body>')
|
|
||||||
if metadata['title'] and metadata['title'] ~= "" then
|
|
||||||
add('<h1 class="title">' .. metadata['title'] .. '</h1>')
|
|
||||||
end
|
|
||||||
for _, author in pairs(metadata['author'] or {}) do
|
|
||||||
add('<h2 class="author">' .. author .. '</h2>')
|
|
||||||
end
|
|
||||||
if metadata['date'] and metadata['date'] ~= "" then
|
|
||||||
add('<h3 class="date">' .. metadata.date .. '</h3>')
|
|
||||||
end
|
|
||||||
add(body)
|
add(body)
|
||||||
if #notes > 0 then
|
if #notes > 0 then
|
||||||
add('<ol class="footnotes">')
|
add('<ol class="footnotes">')
|
||||||
|
@ -97,8 +84,6 @@ function Doc(body, metadata, variables)
|
||||||
end
|
end
|
||||||
add('</ol>')
|
add('</ol>')
|
||||||
end
|
end
|
||||||
add('</body>')
|
|
||||||
add('</html>')
|
|
||||||
return table.concat(buffer,'\n')
|
return table.concat(buffer,'\n')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -36,12 +36,15 @@ import Text.Pandoc.Options
|
||||||
import Data.List ( intersperse )
|
import Data.List ( intersperse )
|
||||||
import Data.Char ( toLower )
|
import Data.Char ( toLower )
|
||||||
import Scripting.Lua (LuaState, StackValue, callfunc)
|
import Scripting.Lua (LuaState, StackValue, callfunc)
|
||||||
|
import Text.Pandoc.Writers.Shared
|
||||||
import qualified Scripting.Lua as Lua
|
import qualified Scripting.Lua as Lua
|
||||||
import Text.Pandoc.UTF8 (fromString, toString)
|
import Text.Pandoc.UTF8 (fromString, toString)
|
||||||
import Data.ByteString (ByteString)
|
import Data.ByteString (ByteString)
|
||||||
import qualified Data.ByteString.Char8 as C8
|
import qualified Data.ByteString.Char8 as C8
|
||||||
import Data.Monoid
|
import Data.Monoid
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
|
import Text.Pandoc.Templates
|
||||||
|
import Data.IORef
|
||||||
|
|
||||||
attrToMap :: Attr -> M.Map ByteString ByteString
|
attrToMap :: Attr -> M.Map ByteString ByteString
|
||||||
attrToMap (id',classes,keyvals) = M.fromList
|
attrToMap (id',classes,keyvals) = M.fromList
|
||||||
|
@ -145,7 +148,7 @@ instance StackValue Citation where
|
||||||
|
|
||||||
-- | Convert Pandoc to custom markup.
|
-- | Convert Pandoc to custom markup.
|
||||||
writeCustom :: FilePath -> WriterOptions -> Pandoc -> IO String
|
writeCustom :: FilePath -> WriterOptions -> Pandoc -> IO String
|
||||||
writeCustom luaFile opts doc = do
|
writeCustom luaFile opts doc@(Pandoc meta _) = do
|
||||||
luaScript <- C8.unpack `fmap` C8.readFile luaFile
|
luaScript <- C8.unpack `fmap` C8.readFile luaFile
|
||||||
lua <- Lua.newstate
|
lua <- Lua.newstate
|
||||||
Lua.openlibs lua
|
Lua.openlibs lua
|
||||||
|
@ -153,8 +156,17 @@ writeCustom luaFile opts doc = do
|
||||||
Lua.call lua 0 0
|
Lua.call lua 0 0
|
||||||
-- TODO - call hierarchicalize, so we have that info
|
-- TODO - call hierarchicalize, so we have that info
|
||||||
rendered <- docToCustom lua opts doc
|
rendered <- docToCustom lua opts doc
|
||||||
|
context <- metaToJSON opts
|
||||||
|
(fmap toString . blockListToCustom lua)
|
||||||
|
(fmap toString . inlineListToCustom lua)
|
||||||
|
meta
|
||||||
Lua.close lua
|
Lua.close lua
|
||||||
return $ toString rendered
|
let body = toString rendered
|
||||||
|
if writerStandalone opts
|
||||||
|
then do
|
||||||
|
let context' = setField "body" body context
|
||||||
|
return $ renderTemplate' (writerTemplate opts) context'
|
||||||
|
else return body
|
||||||
|
|
||||||
docToCustom :: LuaState -> WriterOptions -> Pandoc -> IO ByteString
|
docToCustom :: LuaState -> WriterOptions -> Pandoc -> IO ByteString
|
||||||
docToCustom lua opts (Pandoc (Meta metamap) blocks) = do
|
docToCustom lua opts (Pandoc (Meta metamap) blocks) = do
|
||||||
|
|
Loading…
Add table
Reference in a new issue