{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Text.Pandoc.Lua.Module.Structure
   Copyright   : © 2023 Albert Krewinkel
   License     : GPL-2.0-or-later
   Maintainer  : Albert Krewinkel <albert+pandoc@tarleb.com>

Command line helpers
-}
module Text.Pandoc.Lua.Module.Structure
  ( documentedModule
  ) where

import Control.Applicative ((<|>), optional)
import Data.Default (Default (..))
import Data.Maybe (fromMaybe)
import Data.Version (makeVersion)
import HsLua ( DocumentedFunction, LuaError, Module (..), Peeker
             , (###), (<#>), (=#>), (#?)
             , defun, functionResult, getfield, isnil, lastly, liftLua
             , opt, liftPure, parameter , peekBool, peekIntegral
             , peekFieldRaw, peekText, pop, pushIntegral, since, top )
import Text.Pandoc.Chunks ( ChunkedDoc (..), PathTemplate (..)
                          , tocToList, splitIntoChunks )
import Text.Pandoc.Definition (Pandoc (..), Block)
import Text.Pandoc.Error (PandocError)
import Text.Pandoc.Lua.PandocLua ()
import Text.Pandoc.Lua.Marshal.AST ( peekBlocksFuzzy, peekPandoc
                                   , pushBlock, pushBlocks )
import Text.Pandoc.Lua.Marshal.Chunks
import Text.Pandoc.Lua.Marshal.WriterOptions ( peekWriterOptions )
import Text.Pandoc.Options (WriterOptions (writerTOCDepth,
                                           writerNumberSections))
import Text.Pandoc.Slides (getSlideLevel, prepSlides)
import Text.Pandoc.Writers.Shared (toTableOfContents)
import qualified Data.Text as T
import qualified Text.Pandoc.Shared as Shared

-- | Push the pandoc.structure module on the Lua stack.
documentedModule :: Module PandocError
documentedModule :: Module PandocError
documentedModule = Module
  { moduleName :: Name
moduleName = Name
"pandoc.structure"
  , moduleDescription :: Text
moduleDescription =
    Text
"Access to the higher-level document structure, including " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
    Text
"hierarchical sections and the table of contents."
  , moduleFields :: [Field PandocError]
moduleFields = []
  , moduleFunctions :: [DocumentedFunction PandocError]
moduleFunctions =
      [ DocumentedFunction PandocError
forall e. LuaError e => DocumentedFunction e
make_sections     DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
makeVersion [Int
3,Int
0]
      , DocumentedFunction PandocError
forall e. LuaError e => DocumentedFunction e
slide_level       DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
makeVersion [Int
3,Int
0]
      , DocumentedFunction PandocError
forall e. LuaError e => DocumentedFunction e
split_into_chunks DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
makeVersion [Int
3,Int
0]
      , DocumentedFunction PandocError
table_of_contents DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
makeVersion [Int
3,Int
0]
      ]
  , moduleOperations :: [(Operation, DocumentedFunction PandocError)]
moduleOperations = []
  , moduleTypeInitializers :: [LuaE PandocError Name]
moduleTypeInitializers = []
  }

make_sections :: LuaError e => DocumentedFunction e
make_sections :: forall e. LuaError e => DocumentedFunction e
make_sections = Name
-> ([Block]
    -> Maybe (Bool, Maybe Int, Maybe Int) -> LuaE e [Block])
-> HsFnPrecursor
     e ([Block] -> Maybe (Bool, Maybe Int, Maybe Int) -> LuaE e [Block])
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"make_sections"
  ### (\blks mopts ->
         let (numSects, baseLevel, mslideLevel) =
               fromMaybe (defNumSec, Nothing, Nothing) mopts
             blks' = case mslideLevel of
                       Just l | l <= 0 -> prepSlides (getSlideLevel blks) blks
                       Just sl -> prepSlides sl blks
                       Nothing -> blks
         in pure $ Shared.makeSections numSects baseLevel blks')
  HsFnPrecursor
  e ([Block] -> Maybe (Bool, Maybe Int, Maybe Int) -> LuaE e [Block])
-> Parameter e [Block]
-> HsFnPrecursor
     e (Maybe (Bool, Maybe Int, Maybe Int) -> LuaE e [Block])
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker e [Block] -> TypeSpec -> Text -> Text -> Parameter e [Block]
forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
parameter Peeker e [Block]
forall e. LuaError e => Peeker e [Block]
peekBodyBlocks TypeSpec
"Blocks|Pandoc" Text
"blocks"
        Text
"document blocks to process"
  HsFnPrecursor
  e (Maybe (Bool, Maybe Int, Maybe Int) -> LuaE e [Block])
-> Parameter e (Maybe (Bool, Maybe Int, Maybe Int))
-> HsFnPrecursor e (LuaE e [Block])
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Parameter e (Bool, Maybe Int, Maybe Int)
-> Parameter e (Maybe (Bool, Maybe Int, Maybe Int))
forall e a. Parameter e a -> Parameter e (Maybe a)
opt (Peeker e (Bool, Maybe Int, Maybe Int)
-> TypeSpec
-> Text
-> Text
-> Parameter e (Bool, Maybe Int, Maybe Int)
forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
parameter Peeker e (Bool, Maybe Int, Maybe Int)
forall {e} {a} {a}.
(LuaError e, Integral a, Integral a, Read a, Read a) =>
StackIndex -> Peek e (Bool, Maybe a, Maybe a)
peekOpts TypeSpec
"table" Text
"opts" Text
"options")
  HsFnPrecursor e (LuaE e [Block])
-> FunctionResults e [Block] -> DocumentedFunction e
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher e [Block] -> TypeSpec -> Text -> FunctionResults e [Block]
forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
functionResult Pusher e [Block]
forall e. LuaError e => Pusher e [Block]
pushBlocks TypeSpec
"Blocks"
        Text
"processed blocks"
  #? T.unlines
     [ "Puts [[Blocks]] into a hierarchical structure: a list of sections"
     , "(each a Div with class \"section\" and first element a Header)."
     , ""
     , "The optional `opts` argument can be a table; two settings are"
     , "recognized: If `number_sections` is true, a `number` attribute"
     , "containing the section number will be added to each `Header`. If"
     , "`base_level` is an integer, then `Header` levels will be"
     , "reorganized so that there are no gaps, with numbering levels"
     , "shifted by the given value. Finally, an integer `slide_level`"
     , "value triggers the creation of slides at that heading level."
     , ""
     , "Note that a [[WriterOptions]] object can be passed as the opts"
     , "table; this will set the `number_section` and `slide_level` values"
     , "to those defined on the command line."
     , ""
     , "Usage:"
     , ""
     , "    local blocks = {"
     , "      pandoc.Header(2, pandoc.Str 'first'),"
     , "      pandoc.Header(2, pandoc.Str 'second'),"
     , "    }"
     , "    local opts = PANDOC_WRITER_OPTIONS"
     , "    local newblocks = pandoc.structure.make_sections(blocks, opts)"
     ]
  where
    defNumSec :: Bool
defNumSec = Bool
False
    peekOpts :: StackIndex -> Peek e (Bool, Maybe a, Maybe a)
peekOpts StackIndex
idx = do
      Bool
numberSections <- Bool -> Maybe Bool -> Bool
forall a. a -> Maybe a -> a
fromMaybe Bool
defNumSec (Maybe Bool -> Bool) -> Peek e (Maybe Bool) -> Peek e Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> do
        LuaE e Type -> Peek e Type
forall e a. LuaE e a -> Peek e a
liftLua (LuaE e Type -> Peek e Type) -> LuaE e Type -> Peek e Type
forall a b. (a -> b) -> a -> b
$ StackIndex -> Name -> LuaE e Type
forall e. LuaError e => StackIndex -> Name -> LuaE e Type
getfield StackIndex
idx Name
"number_sections"
        Peek e Bool -> Peek e (Maybe Bool)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Peeker e Bool
forall e. Peeker e Bool
peekBool StackIndex
top Peek e Bool -> LuaE e () -> Peek e Bool
forall e a b. Peek e a -> LuaE e b -> Peek e a
`lastly` Int -> LuaE e ()
forall e. Int -> LuaE e ()
pop Int
1)
      Maybe a
baseLevel <- do
        LuaE e Type -> Peek e Type
forall e a. LuaE e a -> Peek e a
liftLua (LuaE e Type -> Peek e Type) -> LuaE e Type -> Peek e Type
forall a b. (a -> b) -> a -> b
$ StackIndex -> Name -> LuaE e Type
forall e. LuaError e => StackIndex -> Name -> LuaE e Type
getfield StackIndex
idx Name
"base_level"
        Peek e a -> Peek e (Maybe a)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Peeker e a
forall a e. (Integral a, Read a) => Peeker e a
peekIntegral StackIndex
top Peek e a -> LuaE e () -> Peek e a
forall e a b. Peek e a -> LuaE e b -> Peek e a
`lastly` Int -> LuaE e ()
forall e. Int -> LuaE e ()
pop Int
1)
      Maybe a
slideLevel <- do
        LuaE e Type -> Peek e Type
forall e a. LuaE e a -> Peek e a
liftLua (LuaE e Type -> Peek e Type) -> LuaE e Type -> Peek e Type
forall a b. (a -> b) -> a -> b
$ StackIndex -> Name -> LuaE e Type
forall e. LuaError e => StackIndex -> Name -> LuaE e Type
getfield StackIndex
idx Name
"slide_level"
        Peek e a -> Peek e (Maybe a)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Peeker e a
forall a e. (Integral a, Read a) => Peeker e a
peekIntegral StackIndex
top Peek e a -> LuaE e () -> Peek e a
forall e a b. Peek e a -> LuaE e b -> Peek e a
`lastly` Int -> LuaE e ()
forall e. Int -> LuaE e ()
pop Int
1)
      (Bool, Maybe a, Maybe a) -> Peek e (Bool, Maybe a, Maybe a)
forall a. a -> Peek e a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
numberSections, Maybe a
baseLevel, Maybe a
slideLevel)

slide_level :: LuaError e => DocumentedFunction e
slide_level :: forall e. LuaError e => DocumentedFunction e
slide_level = Name
-> ([Block] -> LuaE e Int)
-> HsFnPrecursor e ([Block] -> LuaE e Int)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"slide_level"
  ### liftPure getSlideLevel
  HsFnPrecursor e ([Block] -> LuaE e Int)
-> Parameter e [Block] -> HsFnPrecursor e (LuaE e Int)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker e [Block] -> TypeSpec -> Text -> Text -> Parameter e [Block]
forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
parameter Peeker e [Block]
forall e. LuaError e => Peeker e [Block]
peekBodyBlocks TypeSpec
"Blocks|Pandoc" Text
"blocks" Text
"document body"
  HsFnPrecursor e (LuaE e Int)
-> FunctionResults e Int -> DocumentedFunction e
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher e Int -> TypeSpec -> Text -> FunctionResults e Int
forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
functionResult Pusher e Int
forall a e. (Integral a, Show a) => a -> LuaE e ()
pushIntegral TypeSpec
"integer" Text
"slide level"
  #? T.unlines
  [ "Find level of header that starts slides (defined as the least"
  , "header level that occurs before a non-header/non-hrule in the"
  , "blocks)."
  ]

-- | Split 'Pandoc' into 'Chunk's.
split_into_chunks :: LuaError e => DocumentedFunction e
split_into_chunks :: forall e. LuaError e => DocumentedFunction e
split_into_chunks = Name
-> (Pandoc
    -> Maybe (PathTemplate, Bool, Maybe Int, Int) -> LuaE e ChunkedDoc)
-> HsFnPrecursor
     e
     (Pandoc
      -> Maybe (PathTemplate, Bool, Maybe Int, Int) -> LuaE e ChunkedDoc)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"split_into_chunks"
  ### (\doc mopts -> pure $
          let defOpts = (defPathTmpl, defNumSects, Nothing, defLvl)
              (pathTempl, numberSect, mbBaseLevel, chunkLevel) =
                fromMaybe defOpts mopts
          in splitIntoChunks pathTempl numberSect mbBaseLevel chunkLevel doc)
  HsFnPrecursor
  e
  (Pandoc
   -> Maybe (PathTemplate, Bool, Maybe Int, Int) -> LuaE e ChunkedDoc)
-> Parameter e Pandoc
-> HsFnPrecursor
     e (Maybe (PathTemplate, Bool, Maybe Int, Int) -> LuaE e ChunkedDoc)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker e Pandoc -> TypeSpec -> Text -> Text -> Parameter e Pandoc
forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
parameter Peeker e Pandoc
forall e. LuaError e => Peeker e Pandoc
peekPandoc TypeSpec
"Pandoc" Text
"doc" Text
"document to split"
  HsFnPrecursor
  e (Maybe (PathTemplate, Bool, Maybe Int, Int) -> LuaE e ChunkedDoc)
-> Parameter e (Maybe (PathTemplate, Bool, Maybe Int, Int))
-> HsFnPrecursor e (LuaE e ChunkedDoc)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Parameter e (PathTemplate, Bool, Maybe Int, Int)
-> Parameter e (Maybe (PathTemplate, Bool, Maybe Int, Int))
forall e a. Parameter e a -> Parameter e (Maybe a)
opt (Peeker e (PathTemplate, Bool, Maybe Int, Int)
-> TypeSpec
-> Text
-> Text
-> Parameter e (PathTemplate, Bool, Maybe Int, Int)
forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
parameter Peeker e (PathTemplate, Bool, Maybe Int, Int)
forall {e} {a}.
(LuaError e, Integral a, Read a) =>
StackIndex -> Peek e (PathTemplate, Bool, Maybe a, Int)
peekSplitOpts TypeSpec
"table" Text
"opts" Text
optionsDescr)
  HsFnPrecursor e (LuaE e ChunkedDoc)
-> FunctionResults e ChunkedDoc -> DocumentedFunction e
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher e ChunkedDoc
-> TypeSpec -> Text -> FunctionResults e ChunkedDoc
forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
functionResult Pusher e ChunkedDoc
forall e. LuaError e => Pusher e ChunkedDoc
pushChunkedDoc TypeSpec
"ChunkedDoc" Text
""
  #? T.unlines
     [ "Converts a [[Pandoc]] document into a [[ChunkedDoc]]." ]
 where
  defPathTmpl :: PathTemplate
defPathTmpl = Text -> PathTemplate
PathTemplate Text
"chunk-%n"
  defNumSects :: Bool
defNumSects = Bool
False
  defLvl :: Int
defLvl = Int
1
  peekSplitOpts :: StackIndex -> Peek e (PathTemplate, Bool, Maybe a, Int)
peekSplitOpts StackIndex
idx = (,,,)
    (PathTemplate
 -> Bool -> Maybe a -> Int -> (PathTemplate, Bool, Maybe a, Int))
-> Peek e PathTemplate
-> Peek
     e (Bool -> Maybe a -> Int -> (PathTemplate, Bool, Maybe a, Int))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Peeker e PathTemplate -> Name -> Peeker e PathTemplate
forall e a. LuaError e => Peeker e a -> Name -> Peeker e a
peekFieldRaw (((Text -> PathTemplate) -> Peek e Text -> Peek e PathTemplate
forall a b. (a -> b) -> Peek e a -> Peek e b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> PathTemplate
PathTemplate (Peek e Text -> Peek e PathTemplate)
-> (StackIndex -> Peek e Text) -> Peeker e PathTemplate
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> Peek e Text
forall e. Peeker e Text
peekText) Peeker e PathTemplate -> PathTemplate -> Peeker e PathTemplate
forall {e} {b}.
(StackIndex -> Peek e b) -> b -> StackIndex -> Peek e b
`orDefault` PathTemplate
defPathTmpl)
                     Name
"path_template" StackIndex
idx
    Peek
  e (Bool -> Maybe a -> Int -> (PathTemplate, Bool, Maybe a, Int))
-> Peek e Bool
-> Peek e (Maybe a -> Int -> (PathTemplate, Bool, Maybe a, Int))
forall a b. Peek e (a -> b) -> Peek e a -> Peek e b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Peeker e Bool -> Name -> Peeker e Bool
forall e a. LuaError e => Peeker e a -> Name -> Peeker e a
peekFieldRaw (Peeker e Bool
forall e. Peeker e Bool
peekBool Peeker e Bool -> Bool -> Peeker e Bool
forall {e} {b}.
(StackIndex -> Peek e b) -> b -> StackIndex -> Peek e b
`orDefault` Bool
defNumSects) Name
"number_sections" StackIndex
idx
    Peek e (Maybe a -> Int -> (PathTemplate, Bool, Maybe a, Int))
-> Peek e (Maybe a)
-> Peek e (Int -> (PathTemplate, Bool, Maybe a, Int))
forall a b. Peek e (a -> b) -> Peek e a -> Peek e b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Peeker e (Maybe a) -> Name -> Peeker e (Maybe a)
forall e a. LuaError e => Peeker e a -> Name -> Peeker e a
peekFieldRaw (Peek e a -> Peek e (Maybe a)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Peek e a -> Peek e (Maybe a))
-> (StackIndex -> Peek e a) -> Peeker e (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> Peek e a
forall a e. (Integral a, Read a) => Peeker e a
peekIntegral) Name
"base_heading_level" StackIndex
idx
    Peek e (Int -> (PathTemplate, Bool, Maybe a, Int))
-> Peek e Int -> Peek e (PathTemplate, Bool, Maybe a, Int)
forall a b. Peek e (a -> b) -> Peek e a -> Peek e b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Peeker e Int -> Name -> Peeker e Int
forall e a. LuaError e => Peeker e a -> Name -> Peeker e a
peekFieldRaw (Peeker e Int
forall a e. (Integral a, Read a) => Peeker e a
peekIntegral Peeker e Int -> Int -> Peeker e Int
forall {e} {b}.
(StackIndex -> Peek e b) -> b -> StackIndex -> Peek e b
`orDefault` Int
defLvl) Name
"chunk_level" StackIndex
idx
  orDefault :: (StackIndex -> Peek e b) -> b -> StackIndex -> Peek e b
orDefault StackIndex -> Peek e b
p b
defaultValue StackIndex
idx' = LuaE e Bool -> Peek e Bool
forall e a. LuaE e a -> Peek e a
liftLua (StackIndex -> LuaE e Bool
forall e. StackIndex -> LuaE e Bool
isnil StackIndex
idx') Peek e Bool -> (Bool -> Peek e b) -> Peek e b
forall a b. Peek e a -> (a -> Peek e b) -> Peek e b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Bool
True  -> b -> Peek e b
forall a. a -> Peek e a
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
defaultValue
    Bool
False -> StackIndex -> Peek e b
p StackIndex
idx'
  optionsDescr :: Text
optionsDescr = [Text] -> Text
T.unlines
    [ Text
"Splitting options."
    , Text
""
    , Text
"The following options are supported:"
    , Text
""
    , Text
"    `path_template`"
    , Text
"    :   template used to generate the chunks' filepaths"
    , Text
"        `%n` will be replaced with the chunk number (padded with"
    , Text
"        leading 0s to 3 digits), `%s` with the section number of"
    , Text
"        the heading, `%h` with the (stringified) heading text,"
    , Text
"        `%i` with the section identifier. For example,"
    , Text
"        `\"section-%s-%i.html\"` might be resolved to"
    , Text
"        `\"section-1.2-introduction.html\"`."
    , Text
""
    , Text
"        Default is `\"chunk-%n\"` (string)"
    , Text
""
    , Text
"    `number_sections`"
    , Text
"    :   whether sections should be numbered; default is `false`"
    , Text
"        (boolean)"
    , Text
""
    , Text
"    `chunk_level`"
    , Text
"    :   The heading level the document should be split into"
    , Text
"        chunks. The default is to split at the top-level, i.e.,"
    , Text
"        `1`. (integer)"
    , Text
""
    , Text
"    `base_heading_level`"
    , Text
"    :   The base level to be used for numbering. Default is `nil`"
    , Text
"        (integer|nil)"
    ]

-- | Generate a table of contents.
table_of_contents :: DocumentedFunction PandocError
table_of_contents :: DocumentedFunction PandocError
table_of_contents = Name
-> (Either [Block] (Tree SecInfo)
    -> Maybe WriterOptions -> LuaE PandocError Block)
-> HsFnPrecursor
     PandocError
     (Either [Block] (Tree SecInfo)
      -> Maybe WriterOptions -> LuaE PandocError Block)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"table_of_contents"
  ### (\tocSource mwriterOpts -> pure $
          let writerOpts = fromMaybe def mwriterOpts
          in case tocSource of
               Left blks  -> toTableOfContents writerOpts blks
               Right tree -> tocToList (writerNumberSections writerOpts)
                                       (writerTOCDepth writerOpts) tree
      )
  HsFnPrecursor
  PandocError
  (Either [Block] (Tree SecInfo)
   -> Maybe WriterOptions -> LuaE PandocError Block)
-> Parameter PandocError (Either [Block] (Tree SecInfo))
-> HsFnPrecursor
     PandocError (Maybe WriterOptions -> LuaE PandocError Block)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError (Either [Block] (Tree SecInfo))
-> TypeSpec
-> Text
-> Text
-> Parameter PandocError (Either [Block] (Tree SecInfo))
forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
parameter Peeker PandocError (Either [Block] (Tree SecInfo))
forall {e}.
LuaError e =>
StackIndex -> Peek e (Either [Block] (Tree SecInfo))
peekTocSource TypeSpec
"Blocks|Pandoc|ChunkedDoc" Text
"toc_source"
        Text
"list of command line arguments"
  HsFnPrecursor
  PandocError (Maybe WriterOptions -> LuaE PandocError Block)
-> Parameter PandocError (Maybe WriterOptions)
-> HsFnPrecursor PandocError (LuaE PandocError Block)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Parameter PandocError WriterOptions
-> Parameter PandocError (Maybe WriterOptions)
forall e a. Parameter e a -> Parameter e (Maybe a)
opt (Peeker PandocError WriterOptions
-> TypeSpec -> Text -> Text -> Parameter PandocError WriterOptions
forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
parameter Peeker PandocError WriterOptions
peekWriterOptions TypeSpec
"WriterOptions" Text
"opts" Text
"options")
  HsFnPrecursor PandocError (LuaE PandocError Block)
-> FunctionResults PandocError Block
-> DocumentedFunction PandocError
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher PandocError Block
-> TypeSpec -> Text -> FunctionResults PandocError Block
forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
functionResult Pusher PandocError Block
forall e. LuaError e => Pusher e Block
pushBlock TypeSpec
"Block"
        Text
"Table of contents as a BulletList object"
  #? T.unlines
     [ "Generates a table of contents for the given object." ]
 where
  peekTocSource :: StackIndex -> Peek e (Either [Block] (Tree SecInfo))
peekTocSource StackIndex
idx =
    ([Block] -> Either [Block] (Tree SecInfo)
forall a b. a -> Either a b
Left ([Block] -> Either [Block] (Tree SecInfo))
-> Peek e [Block] -> Peek e (Either [Block] (Tree SecInfo))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Peeker e [Block]
forall e. LuaError e => Peeker e [Block]
peekBodyBlocks StackIndex
idx) Peek e (Either [Block] (Tree SecInfo))
-> Peek e (Either [Block] (Tree SecInfo))
-> Peek e (Either [Block] (Tree SecInfo))
forall a. Peek e a -> Peek e a -> Peek e a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
    (Tree SecInfo -> Either [Block] (Tree SecInfo)
forall a b. b -> Either a b
Right (Tree SecInfo -> Either [Block] (Tree SecInfo))
-> (ChunkedDoc -> Tree SecInfo)
-> ChunkedDoc
-> Either [Block] (Tree SecInfo)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChunkedDoc -> Tree SecInfo
chunkedTOC (ChunkedDoc -> Either [Block] (Tree SecInfo))
-> Peek e ChunkedDoc -> Peek e (Either [Block] (Tree SecInfo))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Peeker e ChunkedDoc
forall e. LuaError e => Peeker e ChunkedDoc
peekChunkedDoc StackIndex
idx)

-- | Retrieves the body blocks of a 'Pandoc' object or from a list of
-- blocks.
peekBodyBlocks :: LuaError e => Peeker e [Block]
peekBodyBlocks :: forall e. LuaError e => Peeker e [Block]
peekBodyBlocks StackIndex
idx =
  ((\(Pandoc Meta
_ [Block]
blks) -> [Block]
blks) (Pandoc -> [Block]) -> Peek e Pandoc -> Peek e [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Peeker e Pandoc
forall e. LuaError e => Peeker e Pandoc
peekPandoc StackIndex
idx) Peek e [Block] -> Peek e [Block] -> Peek e [Block]
forall a. Peek e a -> Peek e a -> Peek e a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
  Peeker e [Block]
forall e. LuaError e => Peeker e [Block]
peekBlocksFuzzy StackIndex
idx