{-# LANGUAGE FlexibleInstances  #-}
{-# LANGUAGE OverloadedStrings  #-}
{- |
   Module      : Text.Pandoc.Writers.Custom
   Copyright   : Copyright (C) 2012-2020 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

Conversion of 'Pandoc' documents to custom markup using
a lua writer.
-}
module Text.Pandoc.Writers.Custom ( writeCustom ) where
import Control.Arrow ((***))
import Control.Exception
import Control.Monad (when)
import Data.List (intersperse)
import qualified Data.Map as M
import qualified Data.Text as T
import Data.Text (Text, pack)
import Foreign.Lua (Lua, Pushable)
import Text.DocLayout (render, literal)
import Text.Pandoc.Class.PandocIO (PandocIO)
import Text.Pandoc.Definition
import Text.Pandoc.Lua (Global (..), runLua, setGlobals)
import Text.Pandoc.Lua.Util (addField, dofileWithTraceback)
import Text.Pandoc.Options
import Text.Pandoc.Templates (renderTemplate)
import Text.Pandoc.Writers.Shared

import qualified Foreign.Lua as Lua

attrToMap :: Attr -> M.Map T.Text T.Text
attrToMap :: Attr -> Map Text Text
attrToMap (Text
id',[Text]
classes,[(Text, Text)]
keyvals) = [(Text, Text)] -> Map Text Text
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
    ([(Text, Text)] -> Map Text Text)
-> [(Text, Text)] -> Map Text Text
forall a b. (a -> b) -> a -> b
$ (Text
"id", Text
id')
    (Text, Text) -> [(Text, Text)] -> [(Text, Text)]
forall a. a -> [a] -> [a]
: (Text
"class", [Text] -> Text
T.unwords [Text]
classes)
    (Text, Text) -> [(Text, Text)] -> [(Text, Text)]
forall a. a -> [a] -> [a]
: [(Text, Text)]
keyvals

newtype Stringify a = Stringify a

instance Pushable (Stringify Format) where
  push :: Stringify Format -> Lua ()
push (Stringify (Format Text
f)) = Text -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push (Text -> Text
T.toLower Text
f)

instance Pushable (Stringify [Inline]) where
  push :: Stringify [Inline] -> Lua ()
push (Stringify [Inline]
ils) = String -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push (String -> Lua ()) -> Lua String -> Lua ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [Inline] -> Lua String
inlineListToCustom [Inline]
ils

instance Pushable (Stringify [Block]) where
  push :: Stringify [Block] -> Lua ()
push (Stringify [Block]
blks) = String -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push (String -> Lua ()) -> Lua String -> Lua ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [Block] -> Lua String
blockListToCustom [Block]
blks

instance Pushable (Stringify MetaValue) where
  push :: Stringify MetaValue -> Lua ()
push (Stringify (MetaMap Map Text MetaValue
m))       = Map Text (Stringify MetaValue) -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push ((MetaValue -> Stringify MetaValue)
-> Map Text MetaValue -> Map Text (Stringify MetaValue)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MetaValue -> Stringify MetaValue
forall a. a -> Stringify a
Stringify Map Text MetaValue
m)
  push (Stringify (MetaList [MetaValue]
xs))     = [Stringify MetaValue] -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push ((MetaValue -> Stringify MetaValue)
-> [MetaValue] -> [Stringify MetaValue]
forall a b. (a -> b) -> [a] -> [b]
map MetaValue -> Stringify MetaValue
forall a. a -> Stringify a
Stringify [MetaValue]
xs)
  push (Stringify (MetaBool Bool
x))      = Bool -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push Bool
x
  push (Stringify (MetaString Text
s))    = Text -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push Text
s
  push (Stringify (MetaInlines [Inline]
ils)) = Stringify [Inline] -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
ils)
  push (Stringify (MetaBlocks [Block]
bs))   = Stringify [Block] -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push ([Block] -> Stringify [Block]
forall a. a -> Stringify a
Stringify [Block]
bs)

instance Pushable (Stringify Citation) where
  push :: Stringify Citation -> Lua ()
push (Stringify Citation
cit) = do
    Int -> Int -> Lua ()
Lua.createtable Int
6 Int
0
    String -> Text -> Lua ()
forall a. Pushable a => String -> a -> Lua ()
addField String
"citationId" (Text -> Lua ()) -> Text -> Lua ()
forall a b. (a -> b) -> a -> b
$ Citation -> Text
citationId Citation
cit
    String -> Stringify [Inline] -> Lua ()
forall a. Pushable a => String -> a -> Lua ()
addField String
"citationPrefix" (Stringify [Inline] -> Lua ())
-> ([Inline] -> Stringify [Inline]) -> [Inline] -> Lua ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify ([Inline] -> Lua ()) -> [Inline] -> Lua ()
forall a b. (a -> b) -> a -> b
$ Citation -> [Inline]
citationPrefix Citation
cit
    String -> Stringify [Inline] -> Lua ()
forall a. Pushable a => String -> a -> Lua ()
addField String
"citationSuffix" (Stringify [Inline] -> Lua ())
-> ([Inline] -> Stringify [Inline]) -> [Inline] -> Lua ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify ([Inline] -> Lua ()) -> [Inline] -> Lua ()
forall a b. (a -> b) -> a -> b
$ Citation -> [Inline]
citationSuffix Citation
cit
    String -> String -> Lua ()
forall a. Pushable a => String -> a -> Lua ()
addField String
"citationMode" (String -> Lua ()) -> String -> Lua ()
forall a b. (a -> b) -> a -> b
$ CitationMode -> String
forall a. Show a => a -> String
show (Citation -> CitationMode
citationMode Citation
cit)
    String -> Int -> Lua ()
forall a. Pushable a => String -> a -> Lua ()
addField String
"citationNoteNum" (Int -> Lua ()) -> Int -> Lua ()
forall a b. (a -> b) -> a -> b
$ Citation -> Int
citationNoteNum Citation
cit
    String -> Int -> Lua ()
forall a. Pushable a => String -> a -> Lua ()
addField String
"citationHash" (Int -> Lua ()) -> Int -> Lua ()
forall a b. (a -> b) -> a -> b
$ Citation -> Int
citationHash Citation
cit

-- | Key-value pair, pushed as a table with @a@ as the only key and @v@ as the
-- associated value.
newtype KeyValue a b = KeyValue (a, b)

instance (Pushable a, Pushable b) => Pushable (KeyValue a b) where
  push :: KeyValue a b -> Lua ()
push (KeyValue (a
k, b
v)) = do
    Lua ()
Lua.newtable
    a -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push a
k
    b -> Lua ()
forall a. Pushable a => a -> Lua ()
Lua.push b
v
    StackIndex -> Lua ()
Lua.rawset (CInt -> StackIndex
Lua.nthFromTop CInt
3)

-- | Convert Pandoc to custom markup.
writeCustom :: FilePath -> WriterOptions -> Pandoc -> PandocIO Text
writeCustom :: String -> WriterOptions -> Pandoc -> PandocIO Text
writeCustom String
luaFile WriterOptions
opts doc :: Pandoc
doc@(Pandoc Meta
meta [Block]
_) = do
  let globals :: [Global]
globals = [ Pandoc -> Global
PANDOC_DOCUMENT Pandoc
doc
                , String -> Global
PANDOC_SCRIPT_FILE String
luaFile
                ]
  Either PandocError (Text, Context Text)
res <- Lua (Text, Context Text)
-> PandocIO (Either PandocError (Text, Context Text))
forall a. Lua a -> PandocIO (Either PandocError a)
runLua (Lua (Text, Context Text)
 -> PandocIO (Either PandocError (Text, Context Text)))
-> Lua (Text, Context Text)
-> PandocIO (Either PandocError (Text, Context Text))
forall a b. (a -> b) -> a -> b
$ do
    [Global] -> Lua ()
setGlobals [Global]
globals
    Status
stat <- String -> Lua Status
dofileWithTraceback String
luaFile
    -- check for error in lua script (later we'll change the return type
    -- to handle this more gracefully):
    Bool -> Lua () -> Lua ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Status
stat Status -> Status -> Bool
forall a. Eq a => a -> a -> Bool
/= Status
Lua.OK)
      Lua ()
forall a. Lua a
Lua.throwTopMessage
    String
rendered <- WriterOptions -> Pandoc -> Lua String
docToCustom WriterOptions
opts Pandoc
doc
    Context Text
context <- WriterOptions
-> ([Block] -> Lua (Doc Text))
-> ([Inline] -> Lua (Doc Text))
-> Meta
-> Lua (Context Text)
forall (m :: * -> *) a.
(Monad m, TemplateTarget a) =>
WriterOptions
-> ([Block] -> m (Doc a))
-> ([Inline] -> m (Doc a))
-> Meta
-> m (Context a)
metaToContext WriterOptions
opts
               ((String -> Doc Text) -> Lua String -> Lua (Doc Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (Text -> Doc Text) -> (String -> Text) -> String -> Doc Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
pack) (Lua String -> Lua (Doc Text))
-> ([Block] -> Lua String) -> [Block] -> Lua (Doc Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Block] -> Lua String
blockListToCustom)
               ((String -> Doc Text) -> Lua String -> Lua (Doc Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (Text -> Doc Text) -> (String -> Text) -> String -> Doc Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
pack) (Lua String -> Lua (Doc Text))
-> ([Inline] -> Lua String) -> [Inline] -> Lua (Doc Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Lua String
inlineListToCustom)
               Meta
meta
    (Text, Context Text) -> Lua (Text, Context Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Text
pack String
rendered, Context Text
context)
  case Either PandocError (Text, Context Text)
res of
    Left PandocError
msg -> PandocError -> PandocIO Text
forall a e. Exception e => e -> a
throw PandocError
msg
    Right (Text
body, Context Text
context) -> Text -> PandocIO Text
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> PandocIO Text) -> Text -> PandocIO Text
forall a b. (a -> b) -> a -> b
$
      case WriterOptions -> Maybe (Template Text)
writerTemplate WriterOptions
opts of
        Maybe (Template Text)
Nothing  -> Text
body
        Just Template Text
tpl -> Maybe Int -> Doc Text -> Text
forall a. HasChars a => Maybe Int -> Doc a -> a
render Maybe Int
forall a. Maybe a
Nothing (Doc Text -> Text) -> Doc Text -> Text
forall a b. (a -> b) -> a -> b
$
                    Template Text -> Context Text -> Doc Text
forall a b.
(TemplateTarget a, ToContext a b) =>
Template a -> b -> Doc a
renderTemplate Template Text
tpl (Context Text -> Doc Text) -> Context Text -> Doc Text
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Context Text -> Context Text
forall a b. ToContext a b => Text -> b -> Context a -> Context a
setField Text
"body" Text
body Context Text
context

docToCustom :: WriterOptions -> Pandoc -> Lua String
docToCustom :: WriterOptions -> Pandoc -> Lua String
docToCustom WriterOptions
opts (Pandoc (Meta Map Text MetaValue
metamap) [Block]
blocks) = do
  String
body <- [Block] -> Lua String
blockListToCustom [Block]
blocks
  String
-> String
-> Map Text (Stringify MetaValue)
-> Context Text
-> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Doc" String
body ((MetaValue -> Stringify MetaValue)
-> Map Text MetaValue -> Map Text (Stringify MetaValue)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MetaValue -> Stringify MetaValue
forall a. a -> Stringify a
Stringify Map Text MetaValue
metamap) (WriterOptions -> Context Text
writerVariables WriterOptions
opts)

-- | Convert Pandoc block element to Custom.
blockToCustom :: Block         -- ^ Block element
              -> Lua String

blockToCustom :: Block -> Lua String
blockToCustom Block
Null = String -> Lua String
forall (m :: * -> *) a. Monad m => a -> m a
return String
""

blockToCustom (Plain [Inline]
inlines) = String -> Stringify [Inline] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Plain" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
inlines)

blockToCustom (Para [Image Attr
attr [Inline]
txt (Text
src,Text
tit)]) =
  String
-> Text
-> Text
-> Stringify [Inline]
-> Map Text Text
-> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"CaptionedImage" Text
src Text
tit ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
txt) (Attr -> Map Text Text
attrToMap Attr
attr)

blockToCustom (Para [Inline]
inlines) = String -> Stringify [Inline] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Para" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
inlines)

blockToCustom (LineBlock [[Inline]]
linesList) =
  String -> [Stringify [Inline]] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"LineBlock" (([Inline] -> Stringify [Inline])
-> [[Inline]] -> [Stringify [Inline]]
forall a b. (a -> b) -> [a] -> [b]
map [Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [[Inline]]
linesList)

blockToCustom (RawBlock Format
format Text
str) =
  String -> Stringify Format -> Text -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"RawBlock" (Format -> Stringify Format
forall a. a -> Stringify a
Stringify Format
format) Text
str

blockToCustom Block
HorizontalRule = String -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"HorizontalRule"

blockToCustom (Header Int
level Attr
attr [Inline]
inlines) =
  String -> Int -> Stringify [Inline] -> Map Text Text -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Header" Int
level ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
inlines) (Attr -> Map Text Text
attrToMap Attr
attr)

blockToCustom (CodeBlock Attr
attr Text
str) =
  String -> Text -> Map Text Text -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"CodeBlock" Text
str (Attr -> Map Text Text
attrToMap Attr
attr)

blockToCustom (BlockQuote [Block]
blocks) =
  String -> Stringify [Block] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"BlockQuote" ([Block] -> Stringify [Block]
forall a. a -> Stringify a
Stringify [Block]
blocks)

blockToCustom (Table Attr
_ Caption
blkCapt [ColSpec]
specs TableHead
thead [TableBody]
tbody TableFoot
tfoot) =
  let ([Inline]
capt, [Alignment]
aligns, [Double]
widths, [[Block]]
headers, [[[Block]]]
rows) = Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> ([Inline], [Alignment], [Double], [[Block]], [[[Block]]])
toLegacyTable Caption
blkCapt [ColSpec]
specs TableHead
thead [TableBody]
tbody TableFoot
tfoot
      aligns' :: [String]
aligns' = (Alignment -> String) -> [Alignment] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Alignment -> String
forall a. Show a => a -> String
show [Alignment]
aligns
      capt' :: Stringify [Inline]
capt' = [Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
capt
      headers' :: [Stringify [Block]]
headers' = ([Block] -> Stringify [Block]) -> [[Block]] -> [Stringify [Block]]
forall a b. (a -> b) -> [a] -> [b]
map [Block] -> Stringify [Block]
forall a. a -> Stringify a
Stringify [[Block]]
headers
      rows' :: [[Stringify [Block]]]
rows' = ([[Block]] -> [Stringify [Block]])
-> [[[Block]]] -> [[Stringify [Block]]]
forall a b. (a -> b) -> [a] -> [b]
map (([Block] -> Stringify [Block]) -> [[Block]] -> [Stringify [Block]]
forall a b. (a -> b) -> [a] -> [b]
map [Block] -> Stringify [Block]
forall a. a -> Stringify a
Stringify) [[[Block]]]
rows
  in String
-> Stringify [Inline]
-> [String]
-> [Double]
-> [Stringify [Block]]
-> [[Stringify [Block]]]
-> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Table" Stringify [Inline]
capt' [String]
aligns' [Double]
widths [Stringify [Block]]
headers' [[Stringify [Block]]]
rows'

blockToCustom (BulletList [[Block]]
items) =
  String -> [Stringify [Block]] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"BulletList" (([Block] -> Stringify [Block]) -> [[Block]] -> [Stringify [Block]]
forall a b. (a -> b) -> [a] -> [b]
map [Block] -> Stringify [Block]
forall a. a -> Stringify a
Stringify [[Block]]
items)

blockToCustom (OrderedList (Int
num,ListNumberStyle
sty,ListNumberDelim
delim) [[Block]]
items) =
  String
-> [Stringify [Block]] -> Int -> String -> String -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"OrderedList" (([Block] -> Stringify [Block]) -> [[Block]] -> [Stringify [Block]]
forall a b. (a -> b) -> [a] -> [b]
map [Block] -> Stringify [Block]
forall a. a -> Stringify a
Stringify [[Block]]
items) Int
num (ListNumberStyle -> String
forall a. Show a => a -> String
show ListNumberStyle
sty) (ListNumberDelim -> String
forall a. Show a => a -> String
show ListNumberDelim
delim)

blockToCustom (DefinitionList [([Inline], [[Block]])]
items) =
  String
-> [KeyValue (Stringify [Inline]) [Stringify [Block]]]
-> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"DefinitionList"
               ((([Inline], [[Block]])
 -> KeyValue (Stringify [Inline]) [Stringify [Block]])
-> [([Inline], [[Block]])]
-> [KeyValue (Stringify [Inline]) [Stringify [Block]]]
forall a b. (a -> b) -> [a] -> [b]
map ((Stringify [Inline], [Stringify [Block]])
-> KeyValue (Stringify [Inline]) [Stringify [Block]]
forall a b. (a, b) -> KeyValue a b
KeyValue ((Stringify [Inline], [Stringify [Block]])
 -> KeyValue (Stringify [Inline]) [Stringify [Block]])
-> (([Inline], [[Block]])
    -> (Stringify [Inline], [Stringify [Block]]))
-> ([Inline], [[Block]])
-> KeyValue (Stringify [Inline]) [Stringify [Block]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify ([Inline] -> Stringify [Inline])
-> ([[Block]] -> [Stringify [Block]])
-> ([Inline], [[Block]])
-> (Stringify [Inline], [Stringify [Block]])
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** ([Block] -> Stringify [Block]) -> [[Block]] -> [Stringify [Block]]
forall a b. (a -> b) -> [a] -> [b]
map [Block] -> Stringify [Block]
forall a. a -> Stringify a
Stringify)) [([Inline], [[Block]])]
items)

blockToCustom (Div Attr
attr [Block]
items) =
  String -> Stringify [Block] -> Map Text Text -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Div" ([Block] -> Stringify [Block]
forall a. a -> Stringify a
Stringify [Block]
items) (Attr -> Map Text Text
attrToMap Attr
attr)

-- | Convert list of Pandoc block elements to Custom.
blockListToCustom :: [Block]       -- ^ List of block elements
                  -> Lua String
blockListToCustom :: [Block] -> Lua String
blockListToCustom [Block]
xs = do
  String
blocksep <- String -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Blocksep"
  [String]
bs <- (Block -> Lua String) -> [Block] -> Lua [String]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Block -> Lua String
blockToCustom [Block]
xs
  String -> Lua String
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Lua String) -> String -> Lua String
forall a b. (a -> b) -> a -> b
$ [String] -> String
forall a. Monoid a => [a] -> a
mconcat ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ String -> [String] -> [String]
forall a. a -> [a] -> [a]
intersperse String
blocksep [String]
bs

-- | Convert list of Pandoc inline elements to Custom.
inlineListToCustom :: [Inline] -> Lua String
inlineListToCustom :: [Inline] -> Lua String
inlineListToCustom [Inline]
lst = do
  [String]
xs <- (Inline -> Lua String) -> [Inline] -> Lua [String]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Inline -> Lua String
inlineToCustom [Inline]
lst
  String -> Lua String
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Lua String) -> String -> Lua String
forall a b. (a -> b) -> a -> b
$ [String] -> String
forall a. Monoid a => [a] -> a
mconcat [String]
xs

-- | Convert Pandoc inline element to Custom.
inlineToCustom :: Inline -> Lua String

inlineToCustom :: Inline -> Lua String
inlineToCustom (Str Text
str) = String -> Text -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Str" Text
str

inlineToCustom Inline
Space = String -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Space"

inlineToCustom Inline
SoftBreak = String -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"SoftBreak"

inlineToCustom (Emph [Inline]
lst) = String -> Stringify [Inline] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Emph" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
lst)

inlineToCustom (Underline [Inline]
lst) = String -> Stringify [Inline] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Underline" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
lst)

inlineToCustom (Strong [Inline]
lst) = String -> Stringify [Inline] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Strong" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
lst)

inlineToCustom (Strikeout [Inline]
lst) = String -> Stringify [Inline] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Strikeout" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
lst)

inlineToCustom (Superscript [Inline]
lst) = String -> Stringify [Inline] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Superscript" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
lst)

inlineToCustom (Subscript [Inline]
lst) = String -> Stringify [Inline] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Subscript" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
lst)

inlineToCustom (SmallCaps [Inline]
lst) = String -> Stringify [Inline] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"SmallCaps" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
lst)

inlineToCustom (Quoted QuoteType
SingleQuote [Inline]
lst) = String -> Stringify [Inline] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"SingleQuoted" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
lst)

inlineToCustom (Quoted QuoteType
DoubleQuote [Inline]
lst) = String -> Stringify [Inline] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"DoubleQuoted" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
lst)

inlineToCustom (Cite [Citation]
cs [Inline]
lst) = String -> Stringify [Inline] -> [Stringify Citation] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Cite" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
lst) ((Citation -> Stringify Citation)
-> [Citation] -> [Stringify Citation]
forall a b. (a -> b) -> [a] -> [b]
map Citation -> Stringify Citation
forall a. a -> Stringify a
Stringify [Citation]
cs)

inlineToCustom (Code Attr
attr Text
str) =
  String -> Text -> Map Text Text -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Code" Text
str (Attr -> Map Text Text
attrToMap Attr
attr)

inlineToCustom (Math MathType
DisplayMath Text
str) =
  String -> Text -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"DisplayMath" Text
str

inlineToCustom (Math MathType
InlineMath Text
str) =
  String -> Text -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"InlineMath" Text
str

inlineToCustom (RawInline Format
format Text
str) =
  String -> Stringify Format -> Text -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"RawInline" (Format -> Stringify Format
forall a. a -> Stringify a
Stringify Format
format) Text
str

inlineToCustom Inline
LineBreak = String -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"LineBreak"

inlineToCustom (Link Attr
attr [Inline]
txt (Text
src,Text
tit)) =
  String
-> Stringify [Inline]
-> Text
-> Text
-> Map Text Text
-> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Link" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
txt) Text
src Text
tit (Attr -> Map Text Text
attrToMap Attr
attr)

inlineToCustom (Image Attr
attr [Inline]
alt (Text
src,Text
tit)) =
  String
-> Stringify [Inline]
-> Text
-> Text
-> Map Text Text
-> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Image" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
alt) Text
src Text
tit (Attr -> Map Text Text
attrToMap Attr
attr)

inlineToCustom (Note [Block]
contents) = String -> Stringify [Block] -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Note" ([Block] -> Stringify [Block]
forall a. a -> Stringify a
Stringify [Block]
contents)

inlineToCustom (Span Attr
attr [Inline]
items) =
  String -> Stringify [Inline] -> Map Text Text -> Lua String
forall a. LuaCallFunc a => String -> a
Lua.callFunc String
"Span" ([Inline] -> Stringify [Inline]
forall a. a -> Stringify a
Stringify [Inline]
items) (Attr -> Map Text Text
attrToMap Attr
attr)