diff options
| author | Albert Krewinkel <[email protected]> | 2025-08-10 12:04:24 +0200 |
|---|---|---|
| committer | Albert Krewinkel <[email protected]> | 2025-08-12 08:47:49 +0200 |
| commit | 68d80cc9d914c23831aace776f0d5c981fd882e9 (patch) | |
| tree | dd1d65de1e52397352799b7f8ed14befd4ea158e | |
| parent | 511ddc322339f40f30c173568da26c4296722cb5 (diff) | |
Lua: add functions `pandoc.text.superscript` and `subscript`.
The functions convert numbers and parentheses to superscript and
subscript, respectively.
| -rw-r--r-- | doc/lua-filters.md | 42 | ||||
| -rw-r--r-- | pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Text.hs | 29 | ||||
| -rw-r--r-- | pandoc-lua-engine/test/lua/module/pandoc-text.lua | 22 |
3 files changed, 93 insertions, 0 deletions
diff --git a/doc/lua-filters.md b/doc/lua-filters.md index b71d4a5a3..bddd98692 100644 --- a/doc/lua-filters.md +++ b/doc/lua-filters.md @@ -6886,6 +6886,48 @@ Returns: *Since: 2.0.3* +### subscript {#pandoc.text.subscript} + +`subscript (input)` + +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. + +Parameters: + +`input` +: string to convert to subscript characters (string) + +Returns: + +- Subscript version of the input, or `nil` if not all characters + could be converted. (string\|nil) + +*Since: 3.8* + +### superscript {#pandoc.text.superscript} + +`superscript (input)` + +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. + +Parameters: + +`input` +: string to convert to superscript characters (string) + +Returns: + +- Superscript version of the input, or `nil` if not all characters + could be converted. (string\|nil) + +*Since: 3.8* + ### toencoding {#pandoc.text.toencoding} `toencoding (s[, enc])` diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Text.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Text.hs index d83dd8014..8c43df526 100644 --- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Text.hs +++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Text.hs @@ -15,6 +15,7 @@ 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 @@ -29,6 +30,8 @@ documentedModule = TM.documentedModule , 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] ] @@ -49,3 +52,29 @@ documentedModule = TM.documentedModule } where v = makeVersion + +-- | Convert all chars in a string to Unicode subscript. +subscript :: LuaError e => DocumentedFunction e +subscript = defun "subscript" + ### pure . traverse toSubscript + <#> stringParam "input" "string to convert to subscript characters" + =#> functionResult (maybe pushnil pushString) "string|nil" + "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 = defun "superscript" + ### pure . traverse toSuperscript + <#> stringParam "input" "string to convert to superscript characters" + =#> functionResult (maybe pushnil pushString) "string|nil" + "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." diff --git a/pandoc-lua-engine/test/lua/module/pandoc-text.lua b/pandoc-lua-engine/test/lua/module/pandoc-text.lua index 71a06c917..c664b755b 100644 --- a/pandoc-lua-engine/test/lua/module/pandoc-text.lua +++ b/pandoc-lua-engine/test/lua/module/pandoc-text.lua @@ -41,6 +41,28 @@ return { test('sub', function () assert.is_function(text.sub) end), + group 'subscript' { + test('is a function', function () + assert.is_function(text.subscript) + end), + test('converts a string to Unicode subscript chars', function () + assert.are_equal(text.subscript '1+(9-7)', '₁₊₍₉₋₇₎') + end), + test('returns nil if the input contains unsupported chars', function () + assert.is_nil(text.subscript '00ä') + end), + }, + group 'superscript' { + test('is a function', function () + assert.is_function(text.superscript) + end), + test('converts a string to Unicode superscript chars', function () + assert.are_equal(text.superscript '1+(9-7)', '¹⁺⁽⁹⁻⁷⁾') + end), + test('returns nil if the input contains unsupported chars', function () + assert.is_nil(text.superscript '00ä') + end), + }, test('toencoding', function () assert.is_function(text.toencoding) end), |
