git.iko.soy/typeable/pandoc
on master help sign in
Lua modules: add function pandoc.utils.to_roman_numeral

The function allows conversion of numbers below 4000 into roman numerals.

commit35f0567a8fe840ca65f8474d0293942c76a1220f authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 23 Dec 2017, 11:53 (9 years ago) parentef4351c
4 files changed+37−4
doc/lua-filters.md+18−3 src/Text/Pandoc/Lua/Module/Utils.hs+9−1 test/Tests/Lua.hs+1 test/lua/test-pandoc-utils.lua+9
doc/lua-filters.md+18−3
@@ -173,10 +173,11 @@ Some pandoc functions have been made available in lua:
173173 documents;
174174- [`pipe`](#pipe) runs an external command with input from and
175175 output to strings;
176−- [`sha1`](#utils-sha1) generates a SHA1 hash;
177176- the [`pandoc.mediabag`](#module-pandoc.mediabag) module allows
178177 access to the "mediabag," which stores binary content such as
179− images that may be included in the final document.
178+ images that may be included in the final document;
179+- the [`pandoc.utils`](#module-pandoc.utils) module contains
180+ various utility functions.
180181
181182# Lua interpreter initialization
182183
@@ -1456,9 +1457,23 @@ functions.
14561457 Usage:
14571458
14581459 local inline = pandoc.Emph{pandoc.Str 'Moin'}
1459− -- outputs "Moin"
1460+ -- outputs "Moin"
14601461 print(pandoc.utils.stringify(inline))
14611462
1463+[`to_roman_numeral (integer)`]{#utils-to_roman_numeral}
1464+
1465+: Converts an integer < 4000 to uppercase roman numeral.
1466+
1467+ Returns:
1468+
1469+ - A roman numeral string.
1470+
1471+ Usage:
1472+
1473+ local to_roman_numeral = pandoc.utils.to_roman_numeral
1474+ local pandoc_birth_year = to_roman_numeral(2006)
1475+ -- pandoc_birth_year == 'MMVI'
1476+
14621477
14631478# Module pandoc.mediabag
14641479
src/Text/Pandoc/Lua/Module/Utils.hs+9−1
@@ -30,7 +30,7 @@ module Text.Pandoc.Lua.Module.Utils
3030 ) where
3131
3232import Control.Applicative ((<|>))
33−import Foreign.Lua (FromLuaStack, Lua, NumResults)
33+import Foreign.Lua (FromLuaStack, Lua, LuaInteger, NumResults)
3434import Text.Pandoc.Definition (Pandoc, Meta, Block, Inline)
3535import Text.Pandoc.Lua.StackInstances ()
3636import Text.Pandoc.Lua.Util (addFunction)
@@ -44,6 +44,7 @@ import qualified Text.Pandoc.Shared as Shared
4444pushModule :: Lua NumResults
4545pushModule = do
4646 Lua.newtable
47+ addFunction "to_roman_numeral" toRomanNumeral
4748 addFunction "sha1" sha1
4849 addFunction "stringify" stringify
4950 return 1
@@ -53,6 +54,9 @@ sha1 :: BSL.ByteString
5354 -> Lua String
5455sha1 = return . SHA.showDigest . SHA.sha1
5556
57+-- | Convert pandoc structure to a string with formatting removed.
58+-- Footnotes are skipped (since we don't want their contents in link
59+-- labels).
5660stringify :: AstElement -> Lua String
5761stringify el = return $ case el of
5862 PandocElement pd -> Shared.stringify pd
@@ -77,3 +81,7 @@ instance FromLuaStack AstElement where
7781 Right x -> return x
7882 Left _ -> Lua.throwLuaError
7983 "Expected an AST element, but could not parse value as such."
84+
85+-- | Convert a number < 4000 to uppercase roman numeral.
86+toRomanNumeral :: LuaInteger -> Lua String
87+toRomanNumeral = return . Shared.toRomanNumeral . fromIntegral
test/Tests/Lua.hs+1
@@ -102,6 +102,7 @@ tests = map (localOption (QuickCheckTests 20))
102102 , plain (str "read: OK")
103103 , plain (str "failing read: OK")
104104 , plain (str "stringify: OK")
105+ , plain (str "to_roman_numeral: OK")
105106 ])
106107 ]
107108
test/lua/test-pandoc-utils.lua+9
@@ -64,6 +64,14 @@ function test_stringify ()
6464 return utils.stringify(inline) == 'Cogito ergo sum.'
6565end
6666
67+-- to_roman_numeral
68+------------------------------------------------------------------------
69+function test_to_roman_numeral ()
70+ return utils.to_roman_numeral(1888) == 'MDCCCLXXXVIII'
71+ -- calling with a string fails
72+ and not pcall(utils.to_roman_numeral, 'not a number')
73+end
74+
6775-- Return result
6876------------------------------------------------------------------------
6977function run(fn)
@@ -78,5 +86,6 @@ function Para (el)
7886 pandoc.Plain{pandoc.Str("read: " .. run(test_read))},
7987 pandoc.Plain{pandoc.Str("failing read: " .. run(test_failing_read))},
8088 pandoc.Plain{pandoc.Str("stringify: " .. run(test_stringify))},
89+ pandoc.Plain{pandoc.Str("to_roman_numeral: " .. run(test_to_roman_numeral))},
8190 }
8291end