summaryrefslogtreecommitdiff
path: root/pandoc-lua-engine/src
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2023-03-20 13:58:09 +0100
committerAlbert Krewinkel <albert@zeitkraut.de>2023-03-20 16:06:18 +0100
commited5197f5258fa52de7676980799ddc47fa4f2278 (patch)
tree1974f49d075f343527c934089d9fabe940da63d5 /pandoc-lua-engine/src
parent5af2d70b0da46e74672a9089c51b9ad5d883d6ef (diff)
Lua: load text module as `pandoc.text`.
This only affects the name in the Lua-internal documentation. It is still possible to load the modules via `require 'text'`, although this is deprecated.
Diffstat (limited to 'pandoc-lua-engine/src')
-rw-r--r--pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs12
-rw-r--r--pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Text.hs51
2 files changed, 60 insertions, 3 deletions
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs
index f41206bdc..60d39670a 100644
--- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs
+++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs
@@ -35,7 +35,6 @@ import qualified Lua.LPeg as LPeg
import qualified HsLua.Aeson
import qualified HsLua.Module.DocLayout as Module.Layout
import qualified HsLua.Module.Path as Module.Path
-import qualified HsLua.Module.Text as Module.Text
import qualified HsLua.Module.Zip as Module.Zip
import qualified Text.Pandoc.Lua.Module.CLI as Pandoc.CLI
import qualified Text.Pandoc.Lua.Module.Format as Pandoc.Format
@@ -46,6 +45,7 @@ import qualified Text.Pandoc.Lua.Module.Scaffolding as Pandoc.Scaffolding
import qualified Text.Pandoc.Lua.Module.Structure as Pandoc.Structure
import qualified Text.Pandoc.Lua.Module.System as Pandoc.System
import qualified Text.Pandoc.Lua.Module.Template as Pandoc.Template
+import qualified Text.Pandoc.Lua.Module.Text as Pandoc.Text
import qualified Text.Pandoc.Lua.Module.Types as Pandoc.Types
import qualified Text.Pandoc.Lua.Module.Utils as Pandoc.Utils
@@ -93,14 +93,13 @@ loadedModules =
, Pandoc.Structure.documentedModule
, Pandoc.System.documentedModule
, Pandoc.Template.documentedModule
+ , Pandoc.Text.documentedModule
, Pandoc.Types.documentedModule
, Pandoc.Utils.documentedModule
, Module.Layout.documentedModule { moduleName = "pandoc.layout" }
`allSince` [2,18]
, Module.Path.documentedModule { moduleName = "pandoc.path" }
`allSince` [2,12]
- , Module.Text.documentedModule
- `allSince` [2,0,3]
, Module.Zip.documentedModule { moduleName = "pandoc.zip" }
`allSince` [3,0]
]
@@ -126,6 +125,13 @@ initLuaState = do
-- load modules and add them to the `pandoc` module table.
forM_ loadedModules $ \mdl -> do
registerModule mdl
+ -- pandoc.text must be require-able as 'text' for backwards compat.
+ when (moduleName mdl == "pandoc.text") $ do
+ getfield registryindex loaded
+ pushvalue (nth 2)
+ setfield (nth 2) "text"
+ pop 1 -- _LOADED
+ -- Shorten name, drop everything before the first dot (if any).
let fieldname (Name mdlname) = Name .
maybe mdlname snd . Char8.uncons . snd $
Char8.break (== '.') mdlname
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Text.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Text.hs
new file mode 100644
index 000000000..6705a5f62
--- /dev/null
+++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Text.hs
@@ -0,0 +1,51 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-|
+Module : Text.Pandoc.Lua.Module.Text
+Copyright : © 2023 Albert Krewinkel
+License : MIT
+Maintainer : Albert Krewinkel <albert@hslua.org>
+
+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 qualified Data.Text as T
+import qualified HsLua.Module.Text as TM
+
+-- | The @aeson@ module specification.
+documentedModule :: Module PandocError
+documentedModule = 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]
+ , 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 = makeVersion