@@ -1,28 +1,10 @@
1+{-# OPTIONS_GHC -fno-warn-orphans #-}
1{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE LambdaCase #-}
5{-# LANGUAGE LambdaCase #-}
5−{-# OPTIONS_GHC -fno-warn-orphans #-}
7−Copyright © 2012-2019 John MacFarlane <jgm@berkeley.edu>
8− 2017-2019 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
10−This program is free software; you can redistribute it and/or modify
11−it under the terms of the GNU General Public License as published by
12−the Free Software Foundation; either version 2 of the License, or
13−(at your option) any later version.
15−This program is distributed in the hope that it will be useful,
16−but WITHOUT ANY WARRANTY; without even the implied warranty of
17−MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18−GNU General Public License for more details.
20−You should have received a copy of the GNU General Public License
21−along with this program; if not, write to the Free Software
22−Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25− Module : Text.Pandoc.Lua.StackInstances
7+ Module : Text.Pandoc.Lua.Marshaling.AST
26 Copyright : © 2012-2019 John MacFarlane
8 Copyright : © 2012-2019 John MacFarlane
27 © 2017-2019 Albert Krewinkel
9 © 2017-2019 Albert Krewinkel
28 License : GNU GPL, version 2 or above
10 License : GNU GPL, version 2 or above
@@ -30,27 +12,20 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
12 Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
31 Stability : alpha
13 Stability : alpha
33−StackValue instances for pandoc types.
15+Marshaling/unmarshaling instances for document AST elements.
35−module Text.Pandoc.Lua.StackInstances () where
17+module Text.Pandoc.Lua.Marshaling.AST () where
37import Prelude
19import Prelude
38import Control.Applicative ((<|>))
20import Control.Applicative ((<|>))
39−import Data.Data (showConstr, toConstr)
40import Foreign.Lua (Lua, Peekable, Pushable, StackIndex)
21import Foreign.Lua (Lua, Peekable, Pushable, StackIndex)
41−import Foreign.Lua.Types.Peekable (reportValueOnFailure)
42import Foreign.Lua.Userdata ( ensureUserdataMetatable, pushAnyWithMetatable
22import Foreign.Lua.Userdata ( ensureUserdataMetatable, pushAnyWithMetatable
43− , toAnyWithName, metatableName)
23+ , metatableName)
44−import Text.Pandoc.Class (CommonState (..))
45import Text.Pandoc.Definition
24import Text.Pandoc.Definition
46−import Text.Pandoc.Extensions (Extensions)
47−import Text.Pandoc.Logging (LogMessage, showLogMessage)
48import Text.Pandoc.Lua.Util (defineHowTo, pushViaConstructor)
25import Text.Pandoc.Lua.Util (defineHowTo, pushViaConstructor)
49−import Text.Pandoc.Options (ReaderOptions (..), TrackChanges)
26+import Text.Pandoc.Lua.Marshaling.CommonState ()
50import Text.Pandoc.Shared (Element (Blk, Sec))
27import Text.Pandoc.Shared (Element (Blk, Sec))
52−import qualified Data.Map as Map
53−import qualified Data.Set as Set
54import qualified Foreign.Lua as Lua
29import qualified Foreign.Lua as Lua
55import qualified Text.Pandoc.Lua.Util as LuaUtil
30import qualified Text.Pandoc.Lua.Util as LuaUtil
@@ -335,140 +310,3 @@ indexElement = \case
335 "tag" -> Lua.push "Sec"
310 "tag" -> Lua.push "Sec"
336 "t" -> Lua.push "Sec"
311 "t" -> Lua.push "Sec"
337 _ -> Lua.pushnil
312 _ -> Lua.pushnil
343−instance Pushable Extensions where
344− push exts = Lua.push (show exts)
346−instance Pushable TrackChanges where
347− push = Lua.push . showConstr . toConstr
349−instance Pushable ReaderOptions where
352− (extensions :: Extensions)
353− (standalone :: Bool)
356− (indentedCodeClasses :: [String])
357− (abbreviations :: Set.Set String)
358− (defaultImageExtension :: String)
359− (trackChanges :: TrackChanges)
360− (stripComments :: Bool)
363− LuaUtil.addField "extensions" extensions
364− LuaUtil.addField "standalone" standalone
365− LuaUtil.addField "columns" columns
366− LuaUtil.addField "tab_stop" tabStop
367− LuaUtil.addField "indented_code_classes" indentedCodeClasses
368− LuaUtil.addField "abbreviations" abbreviations
369− LuaUtil.addField "default_image_extension" defaultImageExtension
370− LuaUtil.addField "track_changes" trackChanges
371− LuaUtil.addField "strip_comments" stripComments
374− let indexReaderOptions :: AnyValue -> AnyValue -> Lua Lua.NumResults
375− indexReaderOptions _tbl (AnyValue key) = do
376− Lua.ltype key >>= \case
377− Lua.TypeString -> Lua.peek key >>= \case
378− "defaultImageExtension" -> Lua.push defaultImageExtension
379− "indentedCodeClasses" -> Lua.push indentedCodeClasses
380− "stripComments" -> Lua.push stripComments
381− "tabStop" -> Lua.push tabStop
382− "trackChanges" -> Lua.push trackChanges
387− LuaUtil.addFunction "__index" indexReaderOptions
388− Lua.setmetatable (Lua.nthFromTop 2)
390−-- | Dummy type to allow values of arbitrary Lua type.
391−newtype AnyValue = AnyValue StackIndex
394−-- TODO: Much of the following should be abstracted, factored out
395−-- and go into HsLua.
398−instance Peekable AnyValue where
399− peek = return . AnyValue
401−-- | Name used by Lua for the @CommonState@ type.
402−commonStateTypeName :: String
403−commonStateTypeName = "Pandoc CommonState"
405−instance Peekable CommonState where
406− peek idx = reportValueOnFailure commonStateTypeName
407− (`toAnyWithName` commonStateTypeName) idx
409−instance Pushable CommonState where
410− push st = pushAnyWithMetatable pushCommonStateMetatable st
412− pushCommonStateMetatable = ensureUserdataMetatable commonStateTypeName $ do
413− LuaUtil.addFunction "__index" indexCommonState
414− LuaUtil.addFunction "__pairs" pairsCommonState
416−indexCommonState :: CommonState -> AnyValue -> Lua Lua.NumResults
417−indexCommonState st (AnyValue idx) = Lua.ltype idx >>= \case
418− Lua.TypeString -> 1 <$ (Lua.peek idx >>= pushField)
419− _ -> 1 <$ Lua.pushnil
421− pushField :: String -> Lua ()
422− pushField name = case lookup name commonStateFields of
423− Just pushValue -> pushValue st
424− Nothing -> Lua.pushnil
426−pairsCommonState :: CommonState -> Lua Lua.NumResults
427−pairsCommonState st = do
428− Lua.pushHaskellFunction nextFn
433− nextFn :: AnyValue -> AnyValue -> Lua Lua.NumResults
434− nextFn _ (AnyValue idx) =
435− Lua.ltype idx >>= \case
436− Lua.TypeNil -> case commonStateFields of
437− [] -> 2 <$ (Lua.pushnil *> Lua.pushnil)
438− (key, pushValue):_ -> 2 <$ (Lua.push key *> pushValue st)
439− Lua.TypeString -> do
441− case tail $ dropWhile ((/= key) . fst) commonStateFields of
442− [] -> 2 <$ (Lua.pushnil *> Lua.pushnil)
443− (nextKey, pushValue):_ -> 2 <$ (Lua.push nextKey *> pushValue st)
444− _ -> 2 <$ (Lua.pushnil *> Lua.pushnil)
446−commonStateFields :: [(String, CommonState -> Lua ())]
448− [ ("input_files", Lua.push . stInputFiles)
449− , ("output_file", Lua.push . Lua.Optional . stOutputFile)
450− , ("log", Lua.push . stLog)
451− , ("request_headers", Lua.push . Map.fromList . stRequestHeaders)
452− , ("resource_path", Lua.push . stResourcePath)
453− , ("source_url", Lua.push . Lua.Optional . stSourceURL)
454− , ("user_data_dir", Lua.push . Lua.Optional . stUserDataDir)
455− , ("trace", Lua.push . stTrace)
456− , ("verbosity", Lua.push . show . stVerbosity)
459−-- | Name used by Lua for the @CommonState@ type.
460−logMessageTypeName :: String
461−logMessageTypeName = "Pandoc LogMessage"
463−instance Peekable LogMessage where
464− peek idx = reportValueOnFailure logMessageTypeName
465− (`toAnyWithName` logMessageTypeName) idx
467−instance Pushable LogMessage where
468− push msg = pushAnyWithMetatable pushLogMessageMetatable msg
470− pushLogMessageMetatable = ensureUserdataMetatable logMessageTypeName $
471− LuaUtil.addFunction "__tostring" tostringLogMessage
473−tostringLogMessage :: LogMessage -> Lua String
474−tostringLogMessage = return . showLogMessage