{-# 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 = Name -> Module PandocError
forall e. Name -> Module e
defmodule Name
"pandoc.text"
  Module PandocError
-> [DocumentedFunction PandocError] -> Module PandocError
forall e. Module e -> [DocumentedFunction e] -> Module e
`withFunctions`
    [ DocumentedFunction PandocError
forall e. LuaError e => DocumentedFunction e
TM.fromencoding DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
v[Int
3,Int
0]
    , DocumentedFunction PandocError
forall e. DocumentedFunction e
TM.len          DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
v[Int
2,Int
0,Int
3]
    , DocumentedFunction PandocError
forall e. DocumentedFunction e
TM.lower        DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
v[Int
2,Int
0,Int
3]
    , DocumentedFunction PandocError
forall e. DocumentedFunction e
TM.reverse      DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
v[Int
2,Int
0,Int
3]
    , DocumentedFunction PandocError
forall e. DocumentedFunction e
TM.sub          DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
v[Int
2,Int
0,Int
3]
    , DocumentedFunction PandocError
forall e. LuaError e => DocumentedFunction e
subscript       DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
v[Int
3,Int
8]
    , DocumentedFunction PandocError
forall e. LuaError e => DocumentedFunction e
superscript     DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
v[Int
3,Int
8]
    , DocumentedFunction PandocError
forall e. LuaError e => DocumentedFunction e
TM.toencoding   DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
v[Int
3,Int
0]
    , DocumentedFunction PandocError
forall e. DocumentedFunction e
TM.upper        DocumentedFunction PandocError
-> Version -> DocumentedFunction PandocError
forall e. DocumentedFunction e -> Version -> DocumentedFunction e
`since` [Int] -> Version
v[Int
2,Int
0,Int
3]
    ]
  Module PandocError -> Text -> Module PandocError
forall a. HasDescription a => a -> Text -> a
`withDescription` [Text] -> Text
T.unlines
    [ Text
"UTF-8 aware text manipulation functions, implemented in Haskell."
    , Text
""
    , Text
"The text module can also be loaded under the name `text`, although"
    , Text
"this is discouraged and deprecated."
    , Text
""
    , Text
"``` lua"
    , Text
"-- uppercase all regular text in a document:"
    , Text
"function Str (s)"
    , Text
"  s.text = pandoc.text.upper(s.text)"
    , Text
"  return s"
    , Text
"end"
    , Text
"```"
    ]
 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."