Skip to content

Commit 68d80cc

Browse files
committed
Lua: add functions pandoc.text.superscript and subscript.
The functions convert numbers and parentheses to superscript and subscript, respectively.
1 parent 511ddc3 commit 68d80cc

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

doc/lua-filters.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6886,6 +6886,48 @@ Returns:
68866886

68876887
*Since: 2.0.3*
68886888

6889+
### subscript {#pandoc.text.subscript}
6890+
6891+
`subscript (input)`
6892+
6893+
Tries to convert the string into a Unicode subscript version of
6894+
the string. Returns `nil` if not all characters of the input can
6895+
be mapped to a subscript Unicode character. Supported characters
6896+
include numbers, parentheses, and plus/minus.
6897+
6898+
Parameters:
6899+
6900+
`input`
6901+
: string to convert to subscript characters (string)
6902+
6903+
Returns:
6904+
6905+
- Subscript version of the input, or `nil` if not all characters
6906+
could be converted. (string\|nil)
6907+
6908+
*Since: 3.8*
6909+
6910+
### superscript {#pandoc.text.superscript}
6911+
6912+
`superscript (input)`
6913+
6914+
Tries to convert the string into a Unicode superscript version of
6915+
the string. Returns `nil` if not all characters of the input can
6916+
be mapped to a superscript Unicode character. Supported characters
6917+
include numbers, parentheses, and plus/minus.
6918+
6919+
Parameters:
6920+
6921+
`input`
6922+
: string to convert to superscript characters (string)
6923+
6924+
Returns:
6925+
6926+
- Superscript version of the input, or `nil` if not all characters
6927+
could be converted. (string\|nil)
6928+
6929+
*Since: 3.8*
6930+
68896931
### toencoding {#pandoc.text.toencoding}
68906932

68916933
`toencoding (s[, enc])`

pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Text.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Data.Version (makeVersion)
1515
import HsLua
1616
import Text.Pandoc.Error (PandocError)
1717
import Text.Pandoc.Lua.PandocLua ()
18+
import Text.Pandoc.Writers.Shared (toSubscript, toSuperscript)
1819

1920
import qualified Data.Text as T
2021
import qualified HsLua.Module.Text as TM
@@ -29,6 +30,8 @@ documentedModule = TM.documentedModule
2930
, TM.lower `since` v[2,0,3]
3031
, TM.reverse `since` v[2,0,3]
3132
, TM.sub `since` v[2,0,3]
33+
, subscript `since` v[3,8]
34+
, superscript `since` v[3,8]
3235
, TM.toencoding `since` v[3,0]
3336
, TM.upper `since` v[2,0,3]
3437
]
@@ -49,3 +52,29 @@ documentedModule = TM.documentedModule
4952
}
5053
where
5154
v = makeVersion
55+
56+
-- | Convert all chars in a string to Unicode subscript.
57+
subscript :: LuaError e => DocumentedFunction e
58+
subscript = defun "subscript"
59+
### pure . traverse toSubscript
60+
<#> stringParam "input" "string to convert to subscript characters"
61+
=#> functionResult (maybe pushnil pushString) "string|nil"
62+
"Subscript version of the input, or `nil` if not all characters\
63+
\ could be converted."
64+
#? "Tries to convert the string into a Unicode subscript version of the\
65+
\ string. Returns `nil` if not all characters of the input can be\
66+
\ mapped to a subscript Unicode character.\
67+
\ Supported characters include numbers, parentheses, and plus/minus."
68+
69+
-- | Convert all chars in a string to Unicode superscript.
70+
superscript :: LuaError e => DocumentedFunction e
71+
superscript = defun "superscript"
72+
### pure . traverse toSuperscript
73+
<#> stringParam "input" "string to convert to superscript characters"
74+
=#> functionResult (maybe pushnil pushString) "string|nil"
75+
"Superscript version of the input, or `nil` if not all characters\
76+
\ could be converted."
77+
#? "Tries to convert the string into a Unicode superscript version of the\
78+
\ string. Returns `nil` if not all characters of the input can be\
79+
\ mapped to a superscript Unicode character.\
80+
\ Supported characters include numbers, parentheses, and plus/minus."

pandoc-lua-engine/test/lua/module/pandoc-text.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ return {
4141
test('sub', function ()
4242
assert.is_function(text.sub)
4343
end),
44+
group 'subscript' {
45+
test('is a function', function ()
46+
assert.is_function(text.subscript)
47+
end),
48+
test('converts a string to Unicode subscript chars', function ()
49+
assert.are_equal(text.subscript '1+(9-7)', '₁₊₍₉₋₇₎')
50+
end),
51+
test('returns nil if the input contains unsupported chars', function ()
52+
assert.is_nil(text.subscript '00ä')
53+
end),
54+
},
55+
group 'superscript' {
56+
test('is a function', function ()
57+
assert.is_function(text.superscript)
58+
end),
59+
test('converts a string to Unicode superscript chars', function ()
60+
assert.are_equal(text.superscript '1+(9-7)', '¹⁺⁽⁹⁻⁷⁾')
61+
end),
62+
test('returns nil if the input contains unsupported chars', function ()
63+
assert.is_nil(text.superscript '00ä')
64+
end),
65+
},
4466
test('toencoding', function ()
4567
assert.is_function(text.toencoding)
4668
end),

0 commit comments

Comments
 (0)