diff options
| author | Albert Krewinkel <[email protected]> | 2023-03-20 13:58:09 +0100 |
|---|---|---|
| committer | Albert Krewinkel <[email protected]> | 2023-03-20 16:06:18 +0100 |
| commit | ed5197f5258fa52de7676980799ddc47fa4f2278 (patch) | |
| tree | 1974f49d075f343527c934089d9fabe940da63d5 /pandoc-lua-engine | |
| parent | 5af2d70b0da46e74672a9089c51b9ad5d883d6ef (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')
| -rw-r--r-- | pandoc-lua-engine/pandoc-lua-engine.cabal | 1 | ||||
| -rw-r--r-- | pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs | 12 | ||||
| -rw-r--r-- | pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Text.hs | 51 | ||||
| -rw-r--r-- | pandoc-lua-engine/test/Tests/Lua/Module.hs | 2 | ||||
| -rw-r--r-- | pandoc-lua-engine/test/lua/module/pandoc-text.lua | 51 |
5 files changed, 114 insertions, 3 deletions
diff --git a/pandoc-lua-engine/pandoc-lua-engine.cabal b/pandoc-lua-engine/pandoc-lua-engine.cabal index 35cfde763..7501eae9e 100644 --- a/pandoc-lua-engine/pandoc-lua-engine.cabal +++ b/pandoc-lua-engine/pandoc-lua-engine.cabal @@ -88,6 +88,7 @@ library , Text.Pandoc.Lua.Module.Structure , Text.Pandoc.Lua.Module.System , Text.Pandoc.Lua.Module.Template + , Text.Pandoc.Lua.Module.Text , Text.Pandoc.Lua.Module.Types , Text.Pandoc.Lua.Module.Utils , Text.Pandoc.Lua.Orphans 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 <[email protected]> + +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 diff --git a/pandoc-lua-engine/test/Tests/Lua/Module.hs b/pandoc-lua-engine/test/Tests/Lua/Module.hs index 08a4343d7..e3556f2cd 100644 --- a/pandoc-lua-engine/test/Tests/Lua/Module.hs +++ b/pandoc-lua-engine/test/Tests/Lua/Module.hs @@ -35,6 +35,8 @@ tests = ("lua" </> "module" </> "pandoc-structure.lua") , testPandocLua "pandoc.template" ("lua" </> "module" </> "pandoc-template.lua") + , testPandocLua "pandoc.text" + ("lua" </> "module" </> "pandoc-text.lua") , testPandocLua "pandoc.types" ("lua" </> "module" </> "pandoc-types.lua") , testPandocLua "pandoc.utils" diff --git a/pandoc-lua-engine/test/lua/module/pandoc-text.lua b/pandoc-lua-engine/test/lua/module/pandoc-text.lua new file mode 100644 index 000000000..71a06c917 --- /dev/null +++ b/pandoc-lua-engine/test/lua/module/pandoc-text.lua @@ -0,0 +1,51 @@ +-- +-- Tests for the pandoc.text module +-- +local text = require 'pandoc.text' +local tasty = require 'tasty' + +local group = tasty.test_group +local test = tasty.test_case +local assert = tasty.assert + +assert.is_function = function (x) + assert.are_equal(type(x), 'function') +end +-- We rely mostly on the tests in the `hslua-module-text` module. The +-- only thing we need to test is whether `pandoc.text` is available, +-- whether all functions are defined, and whether `require 'text'` works +-- (for backwards compatibility). +return { + group 'module' { + test('is table', function () + assert.are_equal(type(text), 'table') + end), + test('can be required as "text"', function () + assert.are_equal(require 'text', require 'pandoc.text') + end) + }, + + group 'functions' { + test('fromencoding', function () + assert.is_function(text.fromencoding) + end), + test('len', function () + assert.is_function(text.len) + end), + test('lower', function () + assert.is_function(text.lower) + end), + test('reverse', function () + assert.is_function(text.reverse) + end), + test('sub', function () + assert.is_function(text.sub) + end), + test('toencoding', function () + assert.is_function(text.toencoding) + end), + test('upper', function () + assert.is_function(text.upper) + end), + }, +} |
