git.iko.soy/typeable/pandoc
on master help sign in
Lua modules: add stringify function to pandoc.utils
23edb95Albert Krewinkel
src/Text/Pandoc/Lua/Module/Utils.hs+35−6
@@ -29,22 +29,51 @@ module Text.Pandoc.Lua.Module.Utils
29 ( pushModule
29 ( pushModule
30 ) where
30 ) where
31
31
32−import Data.Digest.Pure.SHA (sha1, showDigest)
32+import Control.Applicative ((<|>))
33−import Foreign.Lua (Lua, NumResults)
33+import Foreign.Lua (FromLuaStack, Lua, NumResults)
34+import Text.Pandoc.Definition (Pandoc, Meta, Block, Inline)
34import Text.Pandoc.Lua.StackInstances ()
35import Text.Pandoc.Lua.StackInstances ()
35import Text.Pandoc.Lua.Util (addFunction)
36import Text.Pandoc.Lua.Util (addFunction)
36
37
38+import qualified Data.Digest.Pure.SHA as SHA
37import qualified Data.ByteString.Lazy as BSL
39import qualified Data.ByteString.Lazy as BSL
38import qualified Foreign.Lua as Lua
40import qualified Foreign.Lua as Lua
41+import qualified Text.Pandoc.Shared as Shared
39
42
40-- | Push the "pandoc.utils" module to the lua stack.
43-- | Push the "pandoc.utils" module to the lua stack.
41pushModule :: Lua NumResults
44pushModule :: Lua NumResults
42pushModule = do
45pushModule = do
43 Lua.newtable
46 Lua.newtable
44− addFunction "sha1" sha1HashFn
47+ addFunction "sha1" sha1
48+ addFunction "stringify" stringify
45 return 1
49 return 1
46
50
47-- | Calculate the hash of the given contents.
51-- | Calculate the hash of the given contents.
48−sha1HashFn :: BSL.ByteString
52+sha1 :: BSL.ByteString
49− -> Lua String
53+ -> Lua String
50−sha1HashFn = return . showDigest . sha1
54+sha1 = return . SHA.showDigest . SHA.sha1
55+
56+stringify :: AstElement -> Lua String
57+stringify el = return $ case el of
58+ PandocElement pd -> Shared.stringify pd
59+ InlineElement i -> Shared.stringify i
60+ BlockElement b -> Shared.stringify b
61+ MetaElement m -> Shared.stringify m
62+
63+data AstElement
64+ = PandocElement Pandoc
65+ | MetaElement Meta
66+ | BlockElement Block
67+ | InlineElement Inline
68+ deriving (Show)
69+
70+instance FromLuaStack AstElement where
71+ peek idx = do
72+ res <- Lua.tryLua $ (PandocElement <$> Lua.peek idx)
73+ <|> (InlineElement <$> Lua.peek idx)
74+ <|> (BlockElement <$> Lua.peek idx)
75+ <|> (MetaElement <$> Lua.peek idx)
76+ case res of
77+ Right x -> return x
78+ Left _ -> Lua.throwLuaError
79+ "Expected an AST element, but could not parse value as such."
test/Tests/Lua.hs+1
@@ -101,6 +101,7 @@ tests = map (localOption (QuickCheckTests 20))
101 , plain (str "failing pipe: OK")
101 , plain (str "failing pipe: OK")
102 , plain (str "read: OK")
102 , plain (str "read: OK")
103 , plain (str "failing read: OK")
103 , plain (str "failing read: OK")
104+ , plain (str "stringify: OK")
104 ])
105 ])
105 ]
106 ]
106
107
test/lua/test-pandoc-utils.lua+16−3
@@ -1,4 +1,4 @@
1−utils = require 'pandoc'
1+utils = require 'pandoc.utils'
2
2
3-- SHA1
3-- SHA1
4------------------------------------------------------------------------
4------------------------------------------------------------------------
@@ -22,7 +22,7 @@ function test_pipe ()
22 warn 'Did not find /bin/sed, skipping test'
22 warn 'Did not find /bin/sed, skipping test'
23 return true
23 return true
24 end
24 end
25− local pipe_result = utils.pipe('/bin/sed', {'-e', 's/a/b/'}, 'abc')
25+ local pipe_result = pandoc.pipe('/bin/sed', {'-e', 's/a/b/'}, 'abc')
26 return pipe_result == 'bbc'
26 return pipe_result == 'bbc'
27end
27end
28
28
@@ -31,7 +31,7 @@ function test_failing_pipe ()
31 warn 'Did not find /bin/false, skipping test'
31 warn 'Did not find /bin/false, skipping test'
32 return true
32 return true
33 end
33 end
34− local res, err = pcall(utils.pipe, '/bin/false', {}, 'abc')
34+ local res, err = pcall(pandoc.pipe, '/bin/false', {}, 'abc')
35 return not res and
35 return not res and
36 err.command == '/bin/false' and
36 err.command == '/bin/false' and
37 err.error_code == 1 and
37 err.error_code == 1 and
@@ -51,6 +51,18 @@ function test_failing_read ()
51 return not res and err:match 'Unknown reader: nosuchreader'
51 return not res and err:match 'Unknown reader: nosuchreader'
52end
52end
53
53
54+-- Stringify
55+------------------------------------------------------------------------
56+function test_stringify ()
57+ local inline = pandoc.Emph{
58+ pandoc.Str 'Cogito',
59+ pandoc.Space(),
60+ pandoc.Str 'ergo',
61+ pandoc.Space(),
62+ pandoc.Str 'sum.',
63+ }
64+ return utils.stringify(inline) == 'Cogito ergo sum.'
65+end
54
66
55-- Return result
67-- Return result
56------------------------------------------------------------------------
68------------------------------------------------------------------------
@@ -65,5 +77,6 @@ function Para (el)
65 pandoc.Plain{pandoc.Str("failing pipe: " .. run(test_failing_pipe))},
77 pandoc.Plain{pandoc.Str("failing pipe: " .. run(test_failing_pipe))},
66 pandoc.Plain{pandoc.Str("read: " .. run(test_read))},
78 pandoc.Plain{pandoc.Str("read: " .. run(test_read))},
67 pandoc.Plain{pandoc.Str("failing read: " .. run(test_failing_read))},
79 pandoc.Plain{pandoc.Str("failing read: " .. run(test_failing_read))},
80+ pandoc.Plain{pandoc.Str("stringify: " .. run(test_stringify))},
68 }
81 }
69end
82end