|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +{- | |
| 3 | +Copyright : © 2021-2024 Albert Krewinkel |
| 4 | +SPDX-License-Identifier : MIT |
| 5 | +Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de> |
| 6 | +
|
| 7 | +Marshaling and unmarshaling of 'Caption' elements. |
| 8 | +-} |
| 9 | +module Text.Pandoc.Lua.Marshal.Caption |
| 10 | + ( peekCaption |
| 11 | + , peekCaptionFuzzy |
| 12 | + , pushCaption |
| 13 | + -- * Constructor |
| 14 | + , mkCaption |
| 15 | + ) where |
| 16 | + |
| 17 | +import Control.Applicative ((<|>), optional) |
| 18 | +import Control.Monad ((<$!>)) |
| 19 | +import Data.Aeson (encode) |
| 20 | +import Data.Maybe (fromMaybe) |
| 21 | +import HsLua |
| 22 | +import {-# SOURCE #-} Text.Pandoc.Lua.Marshal.Block |
| 23 | + ( peekBlocksFuzzy, pushBlocks ) |
| 24 | +import {-# SOURCE #-} Text.Pandoc.Lua.Marshal.Inline |
| 25 | + ( peekInlinesFuzzy, pushInlines ) |
| 26 | +import Text.Pandoc.Definition |
| 27 | + |
| 28 | +-- | Caption object type. |
| 29 | +typeCaption :: LuaError e => DocumentedType e Caption |
| 30 | +typeCaption = deftype "Caption" |
| 31 | + [ operation Eq $ lambda |
| 32 | + ### liftPure2 (\a b -> fromMaybe False ((==) <$> a <*> b)) |
| 33 | + <#> parameter (optional . peekCaption) "Caption" "a" "" |
| 34 | + <#> parameter (optional . peekCaption) "Caption" "b" "" |
| 35 | + =#> functionResult pushBool "boolean" "whether the two are equal" |
| 36 | + , operation Tostring $ lambda |
| 37 | + ### liftPure show |
| 38 | + <#> udparam typeCaption "x" "" |
| 39 | + =#> functionResult pushString "string" "native Haskell representation" |
| 40 | + , operation (CustomOperation "__tojson") $ lambda |
| 41 | + ### liftPure encode |
| 42 | + <#> udparam typeCaption "self" "" |
| 43 | + =#> functionResult pushLazyByteString "string" "JSON representation" |
| 44 | + ] |
| 45 | + [ property' "short" |
| 46 | + "Inlines|nil" |
| 47 | + "short caption used to describe the object" |
| 48 | + (maybe pushnil pushInlines, \(Caption short _) -> short) |
| 49 | + (peekNilOr peekInlinesFuzzy, \(Caption _ long) shrt -> Caption shrt long) |
| 50 | + , property "long" "full caption text" |
| 51 | + (pushBlocks, \(Caption _ long) -> long) |
| 52 | + (peekBlocksFuzzy, \(Caption short _) long -> Caption short long) |
| 53 | + , method $ defun "clone" |
| 54 | + ### return |
| 55 | + <#> parameter peekCaption "Caption" "capt" "" |
| 56 | + =#> functionResult pushCaption "Caption" "cloned Caption element" |
| 57 | + ] |
| 58 | + |
| 59 | +-- | Push Caption element |
| 60 | +pushCaption :: LuaError e => Pusher e Caption |
| 61 | +pushCaption = pushUD typeCaption |
| 62 | + |
| 63 | +-- | Peek Caption element from userdata. |
| 64 | +peekCaption :: LuaError e => Peeker e Caption |
| 65 | +peekCaption = peekUD typeCaption |
| 66 | + |
| 67 | +-- | Peek Caption element from a table. |
| 68 | +peekCaptionTable :: LuaError e => Peeker e Caption |
| 69 | +peekCaptionTable idx = do |
| 70 | + short <- optional $ peekFieldRaw peekInlinesFuzzy "short" idx |
| 71 | + long <- peekFieldRaw peekBlocksFuzzy "long" idx |
| 72 | + return $! Caption short long |
| 73 | + |
| 74 | +peekCaptionFuzzy :: LuaError e => Peeker e Caption |
| 75 | +peekCaptionFuzzy = retrieving "Caption" . \idx -> do |
| 76 | + peekCaption idx |
| 77 | + <|> peekCaptionTable idx |
| 78 | + <|> (Caption Nothing <$!> peekBlocksFuzzy idx) |
| 79 | + <|> (failPeek =<< |
| 80 | + typeMismatchMessage "Caption, list of Blocks, or compatible element" idx) |
| 81 | + |
| 82 | +-- | Constructor for 'Caption'. |
| 83 | +mkCaption :: LuaError e => DocumentedFunction e |
| 84 | +mkCaption = defun "Caption" |
| 85 | + ### (\mLong short -> |
| 86 | + let long = fromMaybe mempty mLong |
| 87 | + in pure (Caption short long)) |
| 88 | + <#> opt (parameter peekBlocksFuzzy "Blocks" "long" "full caption") |
| 89 | + <#> opt (parameter peekInlinesFuzzy "Inlines" "short" "short summary caption") |
| 90 | + =#> functionResult pushCaption "Caption" "new Caption object" |
| 91 | + #? "Creates a new Caption object." |
0 commit comments