git.iko.soy/typeable/pandoc
on master help sign in
Lua modules: add stringify function to pandoc.utils

The new function `pandoc.utils.stringify` converts any AST element to a string with formatting removed.

commit23edb958dbf0210ff82fd0284563c8280ab79bf1 authorAlbert Krewinkel <albert@zeitkraut.de> dateFri 22 Dec 2017, 20:08 (9 years ago) parent9ddf840
3 files changed+52−9
src/Text/Pandoc/Lua/Module/Utils.hs+35−6 test/Tests/Lua.hs+1 test/lua/test-pandoc-utils.lua+16−3
src/Text/Pandoc/Lua/Module/Utils.hs+35−6
@@ -29,22 +29,51 @@ module Text.Pandoc.Lua.Module.Utils
2929 ( pushModule
3030 ) where
3131
32−import Data.Digest.Pure.SHA (sha1, showDigest)
33−import Foreign.Lua (Lua, NumResults)
32+import Control.Applicative ((<|>))
33+import Foreign.Lua (FromLuaStack, Lua, NumResults)
34+import Text.Pandoc.Definition (Pandoc, Meta, Block, Inline)
3435import Text.Pandoc.Lua.StackInstances ()
3536import Text.Pandoc.Lua.Util (addFunction)
3637
38+import qualified Data.Digest.Pure.SHA as SHA
3739import qualified Data.ByteString.Lazy as BSL
3840import qualified Foreign.Lua as Lua
41+import qualified Text.Pandoc.Shared as Shared
3942
4043-- | Push the "pandoc.utils" module to the lua stack.
4144pushModule :: Lua NumResults
4245pushModule = do
4346 Lua.newtable
44− addFunction "sha1" sha1HashFn
47+ addFunction "sha1" sha1
48+ addFunction "stringify" stringify
4549 return 1
4650
4751-- | Calculate the hash of the given contents.
48−sha1HashFn :: BSL.ByteString
49− -> Lua String
50−sha1HashFn = return . showDigest . sha1
52+sha1 :: BSL.ByteString
53+ -> Lua String
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))
101101 , plain (str "failing pipe: OK")
102102 , plain (str "read: OK")
103103 , plain (str "failing read: OK")
104+ , plain (str "stringify: OK")
104105 ])
105106 ]
106107
test/lua/test-pandoc-utils.lua+16−3
@@ -1,4 +1,4 @@
1−utils = require 'pandoc'
1+utils = require 'pandoc.utils'
22
33-- SHA1
44------------------------------------------------------------------------
@@ -22,7 +22,7 @@ function test_pipe ()
2222 warn 'Did not find /bin/sed, skipping test'
2323 return true
2424 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')
2626 return pipe_result == 'bbc'
2727end
2828
@@ -31,7 +31,7 @@ function test_failing_pipe ()
3131 warn 'Did not find /bin/false, skipping test'
3232 return true
3333 end
34− local res, err = pcall(utils.pipe, '/bin/false', {}, 'abc')
34+ local res, err = pcall(pandoc.pipe, '/bin/false', {}, 'abc')
3535 return not res and
3636 err.command == '/bin/false' and
3737 err.error_code == 1 and
@@ -51,6 +51,18 @@ function test_failing_read ()
5151 return not res and err:match 'Unknown reader: nosuchreader'
5252end
5353
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
5466
5567-- Return result
5668------------------------------------------------------------------------
@@ -65,5 +77,6 @@ function Para (el)
6577 pandoc.Plain{pandoc.Str("failing pipe: " .. run(test_failing_pipe))},
6678 pandoc.Plain{pandoc.Str("read: " .. run(test_read))},
6779 pandoc.Plain{pandoc.Str("failing read: " .. run(test_failing_read))},
80+ pandoc.Plain{pandoc.Str("stringify: " .. run(test_stringify))},
6881 }
6982end