@@ -18,13 +18,11 @@ import Control.Monad.Catch (try)
18import Data.Default (def)
18import Data.Default (def)
19import Data.Version (Version)
19import Data.Version (Version)
20import Foreign.Lua (Peekable, Lua, NumResults)
20import Foreign.Lua (Peekable, Lua, NumResults)
21−import Text.Pandoc.Class.PandocIO (runIO)
22−import Text.Pandoc.Class.PandocMonad (setUserDataDir)
23import Text.Pandoc.Definition ( Pandoc, Meta, MetaValue (..), Block, Inline
21import Text.Pandoc.Definition ( Pandoc, Meta, MetaValue (..), Block, Inline
24 , Citation, Attr, ListAttributes)
22 , Citation, Attr, ListAttributes)
25import Text.Pandoc.Error (PandocError)
23import Text.Pandoc.Error (PandocError)
26import Text.Pandoc.Lua.Marshaling ()
24import Text.Pandoc.Lua.Marshaling ()
27−import Text.Pandoc.Lua.Util (addFunction)
25+import Text.Pandoc.Lua.PandocLua (PandocLua, addFunction, liftPandocLua)
29import qualified Data.Digest.Pure.SHA as SHA
27import qualified Data.Digest.Pure.SHA as SHA
30import qualified Data.ByteString.Lazy as BSL
28import qualified Data.ByteString.Lazy as BSL
@@ -35,14 +33,14 @@ import qualified Text.Pandoc.Filter.JSON as JSONFilter
35import qualified Text.Pandoc.Shared as Shared
33import qualified Text.Pandoc.Shared as Shared
37-- | Push the "pandoc.utils" module to the lua stack.
35-- | Push the "pandoc.utils" module to the lua stack.
38−pushModule :: Maybe FilePath -> Lua NumResults
36+pushModule :: PandocLua NumResults
39−pushModule mbDatadir = do
37+pushModule = do
40− Lua.newtable
38+ liftPandocLua Lua.newtable
41 addFunction "blocks_to_inlines" blocksToInlines
39 addFunction "blocks_to_inlines" blocksToInlines
42 addFunction "equals" equals
40 addFunction "equals" equals
43 addFunction "make_sections" makeSections
41 addFunction "make_sections" makeSections
44 addFunction "normalize_date" normalizeDate
42 addFunction "normalize_date" normalizeDate
45− addFunction "run_json_filter" (runJSONFilter mbDatadir)
43+ addFunction "run_json_filter" runJSONFilter
46 addFunction "sha1" sha1
44 addFunction "sha1" sha1
47 addFunction "stringify" stringify
45 addFunction "stringify" stringify
48 addFunction "to_roman_numeral" toRomanNumeral
46 addFunction "to_roman_numeral" toRomanNumeral
@@ -50,8 +48,8 @@ pushModule mbDatadir = do
52-- | Squashes a list of blocks into inlines.
50-- | Squashes a list of blocks into inlines.
53−blocksToInlines :: [Block] -> Lua.Optional [Inline] -> Lua [Inline]
51+blocksToInlines :: [Block] -> Lua.Optional [Inline] -> PandocLua [Inline]
54−blocksToInlines blks optSep = do
52+blocksToInlines blks optSep = liftPandocLua $ do
55 let sep = case Lua.fromOptional optSep of
53 let sep = case Lua.fromOptional optSep of
56 Just x -> B.fromList x
54 Just x -> B.fromList x
57 Nothing -> Shared.defaultBlocksSeparator
55 Nothing -> Shared.defaultBlocksSeparator
@@ -70,23 +68,17 @@ normalizeDate :: T.Text -> Lua (Lua.Optional T.Text)
70normalizeDate = return . Lua.Optional . Shared.normalizeDate
68normalizeDate = return . Lua.Optional . Shared.normalizeDate
72-- | Run a JSON filter on the given document.
70-- | Run a JSON filter on the given document.
73−runJSONFilter :: Maybe FilePath
71+runJSONFilter :: Pandoc
75 -> FilePath
72 -> FilePath
76 -> Lua.Optional [String]
73 -> Lua.Optional [String]
77− -> Lua NumResults
74+ -> PandocLua Pandoc
78−runJSONFilter mbDatadir doc filterFile optArgs = do
75+runJSONFilter doc filterFile optArgs = do
79 args <- case Lua.fromOptional optArgs of
76 args <- case Lua.fromOptional optArgs of
80 Just x -> return x
77 Just x -> return x
81− Nothing -> do
78+ Nothing -> liftPandocLua $ do
82 Lua.getglobal "FORMAT"
79 Lua.getglobal "FORMAT"
83 (:[]) <$> Lua.popValue
80 (:[]) <$> Lua.popValue
84− filterRes <- Lua.liftIO . runIO $ do
85− setUserDataDir mbDatadir
86− JSONFilter.apply def args filterFile doc
81+ JSONFilter.apply def args filterFile doc
88− Left err -> Lua.raiseError (show err)
89− Right d -> (1 :: NumResults) <$ Lua.push d
91-- | Calculate the hash of the given contents.
83-- | Calculate the hash of the given contents.
92sha1 :: BSL.ByteString
84sha1 :: BSL.ByteString
@@ -96,7 +88,7 @@ sha1 = return . T.pack . SHA.showDigest . SHA.sha1
96-- | Convert pandoc structure to a string with formatting removed.
88-- | Convert pandoc structure to a string with formatting removed.
97-- Footnotes are skipped (since we don't want their contents in link
89-- Footnotes are skipped (since we don't want their contents in link
98-- labels).
90-- labels).
99−stringify :: AstElement -> Lua T.Text
91+stringify :: AstElement -> PandocLua T.Text
100stringify el = return $ case el of
92stringify el = return $ case el of
101 PandocElement pd -> Shared.stringify pd
93 PandocElement pd -> Shared.stringify pd
102 InlineElement i -> Shared.stringify i
94 InlineElement i -> Shared.stringify i
@@ -112,7 +104,7 @@ stringifyMetaValue mv = case mv of
112 MetaString s -> s
104 MetaString s -> s
113 _ -> Shared.stringify mv
105 _ -> Shared.stringify mv
115−equals :: AstElement -> AstElement -> Lua Bool
107+equals :: AstElement -> AstElement -> PandocLua Bool
116equals e1 e2 = return (e1 == e2)
108equals e1 e2 = return (e1 == e2)
118data AstElement
110data AstElement
@@ -141,5 +133,5 @@ instance Peekable AstElement where
141 "Expected an AST element, but could not parse value as such."
133 "Expected an AST element, but could not parse value as such."
143-- | Convert a number < 4000 to uppercase roman numeral.
135-- | Convert a number < 4000 to uppercase roman numeral.
144−toRomanNumeral :: Lua.Integer -> Lua T.Text
136+toRomanNumeral :: Lua.Integer -> PandocLua T.Text
145toRomanNumeral = return . Shared.toRomanNumeral . fromIntegral
137toRomanNumeral = return . Shared.toRomanNumeral . fromIntegral