@@ -39,21 +39,19 @@ import Data.Typeable
39import Scripting.Lua (LuaState, StackValue, callfunc)
39import Scripting.Lua (LuaState, StackValue, callfunc)
40import Text.Pandoc.Writers.Shared
40import Text.Pandoc.Writers.Shared
41import qualified Scripting.Lua as Lua
41import qualified Scripting.Lua as Lua
42−import Text.Pandoc.UTF8 (fromString, toString)
43import qualified Text.Pandoc.UTF8 as UTF8
42import qualified Text.Pandoc.UTF8 as UTF8
44−import Data.ByteString (ByteString)
45−import qualified Data.ByteString as B
46import Data.Monoid
43import Data.Monoid
47import Control.Monad (when)
44import Control.Monad (when)
48import Control.Exception
45import Control.Exception
49import qualified Data.Map as M
46import qualified Data.Map as M
50import Text.Pandoc.Templates
47import Text.Pandoc.Templates
48+import GHC.IO.Encoding (getForeignEncoding,setForeignEncoding, utf8)
52−attrToMap :: Attr -> M.Map ByteString ByteString
50+attrToMap :: Attr -> M.Map String String
53attrToMap (id',classes,keyvals) = M.fromList
51attrToMap (id',classes,keyvals) = M.fromList
54− $ ("id", fromString id')
52+ $ ("id", id')
55− : ("class", fromString $ unwords classes)
53+ : ("class", unwords classes)
56− : map (\(x,y) -> (fromString x, fromString y)) keyvals
54+ : keyvals
58getList :: StackValue a => LuaState -> Int -> IO [a]
56getList :: StackValue a => LuaState -> Int -> IO [a]
59getList lua i' = do
57getList lua i' = do
@@ -67,11 +65,6 @@ getList lua i' = do
67 return (x : rest)
65 return (x : rest)
68 else return []
66 else return []
70−instance StackValue ByteString where
71− push l x = Lua.push l $ toString x
72− peek l n = (fmap . fmap) fromString (Lua.peek l n)
73− valuetype _ = Lua.TSTRING
75instance StackValue a => StackValue [a] where
68instance StackValue a => StackValue [a] where
76 push lua xs = do
69 push lua xs = do
77 Lua.createtable lua (length xs + 1) 0
70 Lua.createtable lua (length xs + 1) 0
@@ -111,12 +104,12 @@ instance (StackValue a, StackValue b) => StackValue (a,b) where
111 valuetype _ = Lua.TTABLE
104 valuetype _ = Lua.TTABLE
113instance StackValue [Inline] where
106instance StackValue [Inline] where
114− push l ils = Lua.push l . toString =<< inlineListToCustom l ils
107+ push l ils = Lua.push l =<< inlineListToCustom l ils
115 peek _ _ = undefined
108 peek _ _ = undefined
116 valuetype _ = Lua.TSTRING
109 valuetype _ = Lua.TSTRING
118instance StackValue [Block] where
111instance StackValue [Block] where
119− push l ils = Lua.push l . toString =<< blockListToCustom l ils
112+ push l ils = Lua.push l =<< blockListToCustom l ils
120 peek _ _ = undefined
113 peek _ _ = undefined
121 valuetype _ = Lua.TSTRING
114 valuetype _ = Lua.TSTRING
@@ -138,7 +131,7 @@ instance StackValue MetaValue where
138instance StackValue Citation where
131instance StackValue Citation where
139 push lua cit = do
132 push lua cit = do
140 Lua.createtable lua 6 0
133 Lua.createtable lua 6 0
141− let addValue ((k :: String), v) = Lua.push lua k >> Lua.push lua v >>
134+ let addValue (k :: String, v) = Lua.push lua k >> Lua.push lua v >>
142 Lua.rawset lua (-3)
135 Lua.rawset lua (-3)
143 addValue ("citationId", citationId cit)
136 addValue ("citationId", citationId cit)
144 addValue ("citationPrefix", citationPrefix cit)
137 addValue ("citationPrefix", citationPrefix cit)
@@ -158,6 +151,8 @@ instance Exception PandocLuaException
158writeCustom :: FilePath -> WriterOptions -> Pandoc -> IO String
151writeCustom :: FilePath -> WriterOptions -> Pandoc -> IO String
159writeCustom luaFile opts doc@(Pandoc meta _) = do
152writeCustom luaFile opts doc@(Pandoc meta _) = do
160 luaScript <- UTF8.readFile luaFile
153 luaScript <- UTF8.readFile luaFile
154+ enc <- getForeignEncoding
155+ setForeignEncoding utf8
161 lua <- Lua.newstate
156 lua <- Lua.newstate
162 Lua.openlibs lua
157 Lua.openlibs lua
163 status <- Lua.loadstring lua luaScript luaFile
158 status <- Lua.loadstring lua luaScript luaFile
@@ -169,18 +164,19 @@ writeCustom luaFile opts doc@(Pandoc meta _) = do
169 -- TODO - call hierarchicalize, so we have that info
164 -- TODO - call hierarchicalize, so we have that info
170 rendered <- docToCustom lua opts doc
165 rendered <- docToCustom lua opts doc
171 context <- metaToJSON opts
166 context <- metaToJSON opts
172− (fmap toString . blockListToCustom lua)
167+ (blockListToCustom lua)
173− (fmap toString . inlineListToCustom lua)
168+ (inlineListToCustom lua)
175 Lua.close lua
170 Lua.close lua
171+ setForeignEncoding enc
176− let body = toString rendered
172+ let body = rendered
177 if writerStandalone opts
173 if writerStandalone opts
179 let context' = setField "body" body context
175 let context' = setField "body" body context
180 return $ renderTemplate' (writerTemplate opts) context'
176 return $ renderTemplate' (writerTemplate opts) context'
181 else return body
177 else return body
183−docToCustom :: LuaState -> WriterOptions -> Pandoc -> IO ByteString
179+docToCustom :: LuaState -> WriterOptions -> Pandoc -> IO String
184docToCustom lua opts (Pandoc (Meta metamap) blocks) = do
180docToCustom lua opts (Pandoc (Meta metamap) blocks) = do
185 body <- blockListToCustom lua blocks
181 body <- blockListToCustom lua blocks
186 callfunc lua "Doc" body metamap (writerVariables opts)
182 callfunc lua "Doc" body metamap (writerVariables opts)
@@ -188,7 +184,7 @@ docToCustom lua opts (Pandoc (Meta metamap) blocks) = do
188-- | Convert Pandoc block element to Custom.
184-- | Convert Pandoc block element to Custom.
189blockToCustom :: LuaState -- ^ Lua state
185blockToCustom :: LuaState -- ^ Lua state
190 -> Block -- ^ Block element
186 -> Block -- ^ Block element
191− -> IO ByteString
187+ -> IO String
193blockToCustom _ Null = return ""
189blockToCustom _ Null = return ""
@@ -200,7 +196,7 @@ blockToCustom lua (Para [Image txt (src,tit)]) =
200blockToCustom lua (Para inlines) = callfunc lua "Para" inlines
196blockToCustom lua (Para inlines) = callfunc lua "Para" inlines
202blockToCustom lua (RawBlock format str) =
198blockToCustom lua (RawBlock format str) =
203− callfunc lua "RawBlock" format (fromString str)
199+ callfunc lua "RawBlock" format str
205blockToCustom lua HorizontalRule = callfunc lua "HorizontalRule"
201blockToCustom lua HorizontalRule = callfunc lua "HorizontalRule"
@@ -208,7 +204,7 @@ blockToCustom lua (Header level attr inlines) =
208 callfunc lua "Header" level inlines (attrToMap attr)
204 callfunc lua "Header" level inlines (attrToMap attr)
210blockToCustom lua (CodeBlock attr str) =
206blockToCustom lua (CodeBlock attr str) =
211− callfunc lua "CodeBlock" (fromString str) (attrToMap attr)
207+ callfunc lua "CodeBlock" str (attrToMap attr)
213blockToCustom lua (BlockQuote blocks) = callfunc lua "BlockQuote" blocks
209blockToCustom lua (BlockQuote blocks) = callfunc lua "BlockQuote" blocks
@@ -229,22 +225,22 @@ blockToCustom lua (Div attr items) =
229-- | Convert list of Pandoc block elements to Custom.
225-- | Convert list of Pandoc block elements to Custom.
230blockListToCustom :: LuaState -- ^ Options
226blockListToCustom :: LuaState -- ^ Options
231 -> [Block] -- ^ List of block elements
227 -> [Block] -- ^ List of block elements
232− -> IO ByteString
228+ -> IO String
233blockListToCustom lua xs = do
229blockListToCustom lua xs = do
234 blocksep <- callfunc lua "Blocksep"
230 blocksep <- callfunc lua "Blocksep"
235 bs <- mapM (blockToCustom lua) xs
231 bs <- mapM (blockToCustom lua) xs
236 return $ mconcat $ intersperse blocksep bs
232 return $ mconcat $ intersperse blocksep bs
238-- | Convert list of Pandoc inline elements to Custom.
234-- | Convert list of Pandoc inline elements to Custom.
239−inlineListToCustom :: LuaState -> [Inline] -> IO ByteString
235+inlineListToCustom :: LuaState -> [Inline] -> IO String
240inlineListToCustom lua lst = do
236inlineListToCustom lua lst = do
241 xs <- mapM (inlineToCustom lua) lst
237 xs <- mapM (inlineToCustom lua) lst
242− return $ B.concat xs
238+ return $ concat xs
244-- | Convert Pandoc inline element to Custom.
240-- | Convert Pandoc inline element to Custom.
245−inlineToCustom :: LuaState -> Inline -> IO ByteString
241+inlineToCustom :: LuaState -> Inline -> IO String
247−inlineToCustom lua (Str str) = callfunc lua "Str" $ fromString str
243+inlineToCustom lua (Str str) = callfunc lua "Str" str
249inlineToCustom lua Space = callfunc lua "Space"
245inlineToCustom lua Space = callfunc lua "Space"
@@ -267,24 +263,24 @@ inlineToCustom lua (Quoted DoubleQuote lst) = callfunc lua "DoubleQuoted" lst
267inlineToCustom lua (Cite cs lst) = callfunc lua "Cite" lst cs
263inlineToCustom lua (Cite cs lst) = callfunc lua "Cite" lst cs
269inlineToCustom lua (Code attr str) =
265inlineToCustom lua (Code attr str) =
270− callfunc lua "Code" (fromString str) (attrToMap attr)
266+ callfunc lua "Code" str (attrToMap attr)
272inlineToCustom lua (Math DisplayMath str) =
268inlineToCustom lua (Math DisplayMath str) =
273− callfunc lua "DisplayMath" (fromString str)
269+ callfunc lua "DisplayMath" str
275inlineToCustom lua (Math InlineMath str) =
271inlineToCustom lua (Math InlineMath str) =
276− callfunc lua "InlineMath" (fromString str)
272+ callfunc lua "InlineMath" str
278inlineToCustom lua (RawInline format str) =
274inlineToCustom lua (RawInline format str) =
279− callfunc lua "RawInline" format (fromString str)
275+ callfunc lua "RawInline" format str
281inlineToCustom lua (LineBreak) = callfunc lua "LineBreak"
277inlineToCustom lua (LineBreak) = callfunc lua "LineBreak"
283inlineToCustom lua (Link txt (src,tit)) =
279inlineToCustom lua (Link txt (src,tit)) =
284− callfunc lua "Link" txt (fromString src) (fromString tit)
280+ callfunc lua "Link" txt src tit
286inlineToCustom lua (Image alt (src,tit)) =
282inlineToCustom lua (Image alt (src,tit)) =
287− callfunc lua "Image" alt (fromString src) (fromString tit)
283+ callfunc lua "Image" alt src tit
289inlineToCustom lua (Note contents) = callfunc lua "Note" contents
285inlineToCustom lua (Note contents) = callfunc lua "Note" contents