{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Text.Pandoc.Lua.Module.Types
   Copyright   : © 2019-2026 Albert Krewinkel
   License     : GNU GPL, version 2 or above

   Maintainer  : Albert Krewinkel <albert+pandoc@tarleb.com>
   Stability   : alpha

Pandoc data type constructors.
-}
module Text.Pandoc.Lua.Module.Types
  ( documentedModule
  ) where

import Data.Version (Version, makeVersion)
import HsLua ( DocumentedFunction, Module (..)
             , (###), (<#>), (=#>), (#?), associateType
             , defmodule, defun, functionResult, parameter, since
             , withDescription, withFunctions
             )
import HsLua.Module.Version (peekVersionFuzzy, pushVersion, typeVersion)
import Text.Pandoc.Error (PandocError)
import Text.Pandoc.Lua.Marshal.Sources (peekSources, pushSources, typeSource)
import Text.Pandoc.Lua.PandocLua ()

-- | Push the pandoc.types module on the Lua stack.
documentedModule :: Module PandocError
documentedModule :: Module PandocError
documentedModule = Name -> Module PandocError
forall e. Name -> Module e
defmodule Name
"pandoc.types"
  Module PandocError -> Text -> Module PandocError
forall a. HasDescription a => a -> Text -> a
`withDescription`
      Text
"Constructors for types that are not part of the pandoc AST."
  Module PandocError
-> [DocumentedFunction PandocError] -> Module PandocError
forall e. Module e -> [DocumentedFunction e] -> Module e
`withFunctions`
    [ DocumentedFunction PandocError
mkVersion DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
v[Int
2,Int
7,Int
3]
    , DocumentedFunction PandocError
mkSources DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
v[Int
3,Int
9,Int
1]
    ]
  Module PandocError
-> DocumentedType PandocError Source -> Module PandocError
forall e a.
LuaError e =>
Module e -> DocumentedType e a -> Module e
`associateType` DocumentedType PandocError Source
forall e. LuaError e => DocumentedType e Source
typeSource
  Module PandocError
-> DocumentedType PandocError Version -> Module PandocError
forall e a.
LuaError e =>
Module e -> DocumentedType e a -> Module e
`associateType` DocumentedType PandocError Version
forall e. LuaError e => DocumentedTypeWithList e Version Int
typeVersion
 where
  v :: [Int] -> Version
  v :: [Int] -> Version
v = [Int] -> Version
makeVersion

mkVersion :: DocumentedFunction PandocError
mkVersion :: DocumentedFunction PandocError
mkVersion = Name
-> (Version -> LuaE PandocError Version)
-> HsFnPrecursor PandocError (Version -> LuaE PandocError Version)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"Version"
  ### return
  HsFnPrecursor PandocError (Version -> LuaE PandocError Version)
-> Parameter PandocError Version
-> HsFnPrecursor PandocError (LuaE PandocError Version)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError Version
-> TypeSpec -> Text -> Text -> Parameter PandocError Version
forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
parameter Peeker PandocError Version
forall e. LuaError e => Peeker e Version
peekVersionFuzzy TypeSpec
"string|number|{integer,...}|Version"
        Text
"version_specifier"
        ([Text] -> Text
forall a. Monoid a => [a] -> a
mconcat [ Text
"A version string like `'2.7.3'`, "
                 , Text
"a Lua number like `2.0`, "
                 , Text
"a list of integers like `{2,7,3}`, "
                 , Text
"or a Version object."
                 ])
  HsFnPrecursor PandocError (LuaE PandocError Version)
-> FunctionResults PandocError Version
-> DocumentedFunction PandocError
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher PandocError Version
-> TypeSpec -> Text -> FunctionResults PandocError Version
forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
functionResult Pusher PandocError Version
forall e. LuaError e => Pusher e Version
pushVersion TypeSpec
"Version" Text
"New Version object."

mkSources :: DocumentedFunction PandocError
mkSources :: DocumentedFunction PandocError
mkSources = Name
-> (Sources -> LuaE PandocError Sources)
-> HsFnPrecursor PandocError (Sources -> LuaE PandocError Sources)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"Sources"
  ### pure
  HsFnPrecursor PandocError (Sources -> LuaE PandocError Sources)
-> Parameter PandocError Sources
-> HsFnPrecursor PandocError (LuaE PandocError Sources)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError Sources
-> TypeSpec -> Text -> Text -> Parameter PandocError Sources
forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
parameter Peeker PandocError Sources
forall e. LuaError e => Peeker e Sources
peekSources TypeSpec
"string|{string,...}|table" Text
"srcs"
        Text
"sources"
  HsFnPrecursor PandocError (LuaE PandocError Sources)
-> FunctionResults PandocError Sources
-> DocumentedFunction PandocError
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher PandocError Sources
-> TypeSpec -> Text -> FunctionResults PandocError Sources
forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
functionResult Pusher PandocError Sources
forall e. LuaError e => Pusher e Sources
pushSources TypeSpec
"{Source,...}" Text
"new Sources object"
  #?
    Text
"Creates a new Sources element, i.e., a list of [[Source]] items.\n\
    \\n\
    \ Pandoc's text readers expect the input text to be paired\
    \ with information on where the text originated, e.g., a\
    \ file name. This abstraction is provided via the `Sources` type.\n\
    \\n\
    \Pandoc accepts a range of objects wherever a *Sources* list is\
    \ expected:\n\
    \\n\
    \  - a list of [[Source]] items;\n\
    \  - a simple string, which becomes an unnamed source;\n\
    \  - a list of table objects, where each table contains the fields\n\
    \    `name` (the filepath) and `text` (the file contents)\n\
    \\n\
    \A Sources list can be converted to a string via the default `tostring`\
    \ Lua function. This will concatenate all source items."