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

Lua module to work with UTF-8 strings.
-}
module Text.Pandoc.Lua.Module.Text
  ( documentedModule
  ) where

import Data.Version (makeVersion)
import HsLua
import Text.Pandoc.Error (PandocError)
import Text.Pandoc.Lua.PandocLua ()
import Text.Pandoc.Writers.Shared (toSubscript, toSuperscript)

import qualified Data.Text as T
import qualified HsLua.Module.Text as TM

-- | The @aeson@ module specification.
documentedModule :: Module PandocError
documentedModule :: Module PandocError
documentedModule = Module PandocError
forall e. LuaError e => Module e
TM.documentedModule
  { moduleName = "pandoc.text"
  , moduleFunctions =
    [ TM.fromencoding `since` v[3,0]
    , TM.len          `since` v[2,0,3]
    , TM.lower        `since` v[2,0,3]
    , TM.reverse      `since` v[2,0,3]
    , TM.sub          `since` v[2,0,3]
    , subscript       `since` v[3,8]
    , superscript     `since` v[3,8]
    , TM.toencoding   `since` v[3,0]
    , TM.upper        `since` v[2,0,3]
    ]
  , moduleDescription = T.unlines
    [ "UTF-8 aware text manipulation functions, implemented in Haskell."
    , ""
    , "The text module can also be loaded under the name `text`, although"
    , "this is discouraged and deprecated."
    , ""
    , "``` lua"
    , "-- uppercase all regular text in a document:"
    , "function Str (s)"
    , "  s.text = pandoc.text.upper(s.text)"
    , "  return s"
    , "end"
    , "```"
    ]
  }
 where
  v :: [Int] -> Version
v = [Int] -> Version
makeVersion

-- | Convert all chars in a string to Unicode subscript.
subscript :: LuaError e => DocumentedFunction e
subscript :: forall e. LuaError e => DocumentedFunction e
subscript = Name
-> (String -> LuaE e (Maybe String))
-> HsFnPrecursor e (String -> LuaE e (Maybe String))
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"subscript"
  ### pure . traverse toSubscript
  HsFnPrecursor e (String -> LuaE e (Maybe String))
-> Parameter e String -> HsFnPrecursor e (LuaE e (Maybe String))
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Text -> Text -> Parameter e String
forall e. Text -> Text -> Parameter e String
stringParam Text
"input" Text
"string to convert to subscript characters"
  HsFnPrecursor e (LuaE e (Maybe String))
-> FunctionResults e (Maybe String) -> DocumentedFunction e
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher e (Maybe String)
-> TypeSpec -> Text -> FunctionResults e (Maybe String)
forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
functionResult (LuaE e () -> (String -> LuaE e ()) -> Pusher e (Maybe String)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe LuaE e ()
forall e. LuaE e ()
pushnil String -> LuaE e ()
forall e. String -> LuaE e ()
pushString) TypeSpec
"string|nil"
      Text
"Subscript version of the input, or `nil` if not all characters\
      \ could be converted."
  #? "Tries to convert the string into a Unicode subscript version of the\
     \ string.  Returns `nil` if not all characters of the input can be\
     \ mapped to a subscript Unicode character.\
     \ Supported characters include numbers, parentheses, and plus/minus."

-- | Convert all chars in a string to Unicode superscript.
superscript :: LuaError e => DocumentedFunction e
superscript :: forall e. LuaError e => DocumentedFunction e
superscript = Name
-> (String -> LuaE e (Maybe String))
-> HsFnPrecursor e (String -> LuaE e (Maybe String))
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"superscript"
  ### pure . traverse toSuperscript
  HsFnPrecursor e (String -> LuaE e (Maybe String))
-> Parameter e String -> HsFnPrecursor e (LuaE e (Maybe String))
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Text -> Text -> Parameter e String
forall e. Text -> Text -> Parameter e String
stringParam Text
"input" Text
"string to convert to superscript characters"
  HsFnPrecursor e (LuaE e (Maybe String))
-> FunctionResults e (Maybe String) -> DocumentedFunction e
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher e (Maybe String)
-> TypeSpec -> Text -> FunctionResults e (Maybe String)
forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
functionResult (LuaE e () -> (String -> LuaE e ()) -> Pusher e (Maybe String)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe LuaE e ()
forall e. LuaE e ()
pushnil String -> LuaE e ()
forall e. String -> LuaE e ()
pushString) TypeSpec
"string|nil"
      Text
"Superscript version of the input, or `nil` if not all characters\
      \ could be converted."
  #? "Tries to convert the string into a Unicode superscript version of the\
     \ string.  Returns `nil` if not all characters of the input can be\
     \ mapped to a superscript Unicode character.\
     \ Supported characters include numbers, parentheses, and plus/minus."