git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #2112 from lierdakil/issue2101

Custom Writer: Set foreign encoding to UTF-8

commit2793d986dc5f745e3272832e034581217a00a077 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 26 Apr 2015, 11:16 (12 years ago) parent1868cb5a0ec3e8
1 file changed+29−33
src/Text/Pandoc/Writers/Custom.hs+29−33
src/Text/Pandoc/Writers/Custom.hs+29−33
@@ -39,21 +39,19 @@ import Data.Typeable
3939import Scripting.Lua (LuaState, StackValue, callfunc)
4040import Text.Pandoc.Writers.Shared
4141import qualified Scripting.Lua as Lua
42−import Text.Pandoc.UTF8 (fromString, toString)
4342import qualified Text.Pandoc.UTF8 as UTF8
44−import Data.ByteString (ByteString)
45−import qualified Data.ByteString as B
4643import Data.Monoid
4744import Control.Monad (when)
4845import Control.Exception
4946import qualified Data.Map as M
5047import Text.Pandoc.Templates
48+import GHC.IO.Encoding (getForeignEncoding,setForeignEncoding, utf8)
5149
52−attrToMap :: Attr -> M.Map ByteString ByteString
50+attrToMap :: Attr -> M.Map String String
5351attrToMap (id',classes,keyvals) = M.fromList
54− $ ("id", fromString id')
55− : ("class", fromString $ unwords classes)
56− : map (\(x,y) -> (fromString x, fromString y)) keyvals
52+ $ ("id", id')
53+ : ("class", unwords classes)
54+ : keyvals
5755
5856getList :: StackValue a => LuaState -> Int -> IO [a]
5957getList lua i' = do
@@ -67,11 +65,6 @@ getList lua i' = do
6765 return (x : rest)
6866 else return []
6967
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
74−
7568instance StackValue a => StackValue [a] where
7669 push lua xs = do
7770 Lua.createtable lua (length xs + 1) 0
@@ -111,12 +104,12 @@ instance (StackValue a, StackValue b) => StackValue (a,b) where
111104 valuetype _ = Lua.TTABLE
112105
113106instance StackValue [Inline] where
114− push l ils = Lua.push l . toString =<< inlineListToCustom l ils
107+ push l ils = Lua.push l =<< inlineListToCustom l ils
115108 peek _ _ = undefined
116109 valuetype _ = Lua.TSTRING
117110
118111instance StackValue [Block] where
119− push l ils = Lua.push l . toString =<< blockListToCustom l ils
112+ push l ils = Lua.push l =<< blockListToCustom l ils
120113 peek _ _ = undefined
121114 valuetype _ = Lua.TSTRING
122115
@@ -138,7 +131,7 @@ instance StackValue MetaValue where
138131instance StackValue Citation where
139132 push lua cit = do
140133 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 >>
142135 Lua.rawset lua (-3)
143136 addValue ("citationId", citationId cit)
144137 addValue ("citationPrefix", citationPrefix cit)
@@ -158,6 +151,8 @@ instance Exception PandocLuaException
158151writeCustom :: FilePath -> WriterOptions -> Pandoc -> IO String
159152writeCustom luaFile opts doc@(Pandoc meta _) = do
160153 luaScript <- UTF8.readFile luaFile
154+ enc <- getForeignEncoding
155+ setForeignEncoding utf8
161156 lua <- Lua.newstate
162157 Lua.openlibs lua
163158 status <- Lua.loadstring lua luaScript luaFile
@@ -169,18 +164,19 @@ writeCustom luaFile opts doc@(Pandoc meta _) = do
169164 -- TODO - call hierarchicalize, so we have that info
170165 rendered <- docToCustom lua opts doc
171166 context <- metaToJSON opts
172− (fmap toString . blockListToCustom lua)
173− (fmap toString . inlineListToCustom lua)
167+ (blockListToCustom lua)
168+ (inlineListToCustom lua)
174169 meta
175170 Lua.close lua
176− let body = toString rendered
171+ setForeignEncoding enc
172+ let body = rendered
177173 if writerStandalone opts
178174 then do
179175 let context' = setField "body" body context
180176 return $ renderTemplate' (writerTemplate opts) context'
181177 else return body
182178
183−docToCustom :: LuaState -> WriterOptions -> Pandoc -> IO ByteString
179+docToCustom :: LuaState -> WriterOptions -> Pandoc -> IO String
184180docToCustom lua opts (Pandoc (Meta metamap) blocks) = do
185181 body <- blockListToCustom lua blocks
186182 callfunc lua "Doc" body metamap (writerVariables opts)
@@ -188,7 +184,7 @@ docToCustom lua opts (Pandoc (Meta metamap) blocks) = do
188184-- | Convert Pandoc block element to Custom.
189185blockToCustom :: LuaState -- ^ Lua state
190186 -> Block -- ^ Block element
191− -> IO ByteString
187+ -> IO String
192188
193189blockToCustom _ Null = return ""
194190
@@ -200,7 +196,7 @@ blockToCustom lua (Para [Image txt (src,tit)]) =
200196blockToCustom lua (Para inlines) = callfunc lua "Para" inlines
201197
202198blockToCustom lua (RawBlock format str) =
203− callfunc lua "RawBlock" format (fromString str)
199+ callfunc lua "RawBlock" format str
204200
205201blockToCustom lua HorizontalRule = callfunc lua "HorizontalRule"
206202
@@ -208,7 +204,7 @@ blockToCustom lua (Header level attr inlines) =
208204 callfunc lua "Header" level inlines (attrToMap attr)
209205
210206blockToCustom lua (CodeBlock attr str) =
211− callfunc lua "CodeBlock" (fromString str) (attrToMap attr)
207+ callfunc lua "CodeBlock" str (attrToMap attr)
212208
213209blockToCustom lua (BlockQuote blocks) = callfunc lua "BlockQuote" blocks
214210
@@ -229,22 +225,22 @@ blockToCustom lua (Div attr items) =
229225-- | Convert list of Pandoc block elements to Custom.
230226blockListToCustom :: LuaState -- ^ Options
231227 -> [Block] -- ^ List of block elements
232− -> IO ByteString
228+ -> IO String
233229blockListToCustom lua xs = do
234230 blocksep <- callfunc lua "Blocksep"
235231 bs <- mapM (blockToCustom lua) xs
236232 return $ mconcat $ intersperse blocksep bs
237233
238234-- | Convert list of Pandoc inline elements to Custom.
239−inlineListToCustom :: LuaState -> [Inline] -> IO ByteString
235+inlineListToCustom :: LuaState -> [Inline] -> IO String
240236inlineListToCustom lua lst = do
241237 xs <- mapM (inlineToCustom lua) lst
242− return $ B.concat xs
238+ return $ concat xs
243239
244240-- | Convert Pandoc inline element to Custom.
245−inlineToCustom :: LuaState -> Inline -> IO ByteString
241+inlineToCustom :: LuaState -> Inline -> IO String
246242
247−inlineToCustom lua (Str str) = callfunc lua "Str" $ fromString str
243+inlineToCustom lua (Str str) = callfunc lua "Str" str
248244
249245inlineToCustom lua Space = callfunc lua "Space"
250246
@@ -267,24 +263,24 @@ inlineToCustom lua (Quoted DoubleQuote lst) = callfunc lua "DoubleQuoted" lst
267263inlineToCustom lua (Cite cs lst) = callfunc lua "Cite" lst cs
268264
269265inlineToCustom lua (Code attr str) =
270− callfunc lua "Code" (fromString str) (attrToMap attr)
266+ callfunc lua "Code" str (attrToMap attr)
271267
272268inlineToCustom lua (Math DisplayMath str) =
273− callfunc lua "DisplayMath" (fromString str)
269+ callfunc lua "DisplayMath" str
274270
275271inlineToCustom lua (Math InlineMath str) =
276− callfunc lua "InlineMath" (fromString str)
272+ callfunc lua "InlineMath" str
277273
278274inlineToCustom lua (RawInline format str) =
279− callfunc lua "RawInline" format (fromString str)
275+ callfunc lua "RawInline" format str
280276
281277inlineToCustom lua (LineBreak) = callfunc lua "LineBreak"
282278
283279inlineToCustom lua (Link txt (src,tit)) =
284− callfunc lua "Link" txt (fromString src) (fromString tit)
280+ callfunc lua "Link" txt src tit
285281
286282inlineToCustom lua (Image alt (src,tit)) =
287− callfunc lua "Image" alt (fromString src) (fromString tit)
283+ callfunc lua "Image" alt src tit
288284
289285inlineToCustom lua (Note contents) = callfunc lua "Note" contents
290286