aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/lua-filters.md42
-rw-r--r--pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Text.hs29
-rw-r--r--pandoc-lua-engine/test/lua/module/pandoc-text.lua22
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),