git.iko.soy/typeable/pandoc
on master help sign in
Lua modules: add function pandoc.utils.to_roman_numeral
35f0567Albert Krewinkel
doc/lua-filters.md+18−3
@@ -173,10 +173,11 @@ Some pandoc functions have been made available in lua:
173 documents;
173 documents;
174- [`pipe`](#pipe) runs an external command with input from and
174- [`pipe`](#pipe) runs an external command with input from and
175 output to strings;
175 output to strings;
176−- [`sha1`](#utils-sha1) generates a SHA1 hash;
177- the [`pandoc.mediabag`](#module-pandoc.mediabag) module allows
176- the [`pandoc.mediabag`](#module-pandoc.mediabag) module allows
178 access to the "mediabag," which stores binary content such as
177 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.
180
181
181# Lua interpreter initialization
182# Lua interpreter initialization
182
183
@@ -1456,9 +1457,23 @@ functions.
1456 Usage:
1457 Usage:
1457
1458
1458 local inline = pandoc.Emph{pandoc.Str 'Moin'}
1459 local inline = pandoc.Emph{pandoc.Str 'Moin'}
1459− -- outputs "Moin"
1460+ -- outputs "Moin"
1460 print(pandoc.utils.stringify(inline))
1461 print(pandoc.utils.stringify(inline))
1461
1462
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+
1462
1477
1463# Module pandoc.mediabag
1478# Module pandoc.mediabag
1464
1479
src/Text/Pandoc/Lua/Module/Utils.hs+9−1
@@ -30,7 +30,7 @@ module Text.Pandoc.Lua.Module.Utils
30 ) where
30 ) where
31
31
32import Control.Applicative ((<|>))
32import Control.Applicative ((<|>))
33−import Foreign.Lua (FromLuaStack, Lua, NumResults)
33+import Foreign.Lua (FromLuaStack, Lua, LuaInteger, NumResults)
34import Text.Pandoc.Definition (Pandoc, Meta, Block, Inline)
34import Text.Pandoc.Definition (Pandoc, Meta, Block, Inline)
35import Text.Pandoc.Lua.StackInstances ()
35import Text.Pandoc.Lua.StackInstances ()
36import Text.Pandoc.Lua.Util (addFunction)
36import Text.Pandoc.Lua.Util (addFunction)
@@ -44,6 +44,7 @@ import qualified Text.Pandoc.Shared as Shared
44pushModule :: Lua NumResults
44pushModule :: Lua NumResults
45pushModule = do
45pushModule = do
46 Lua.newtable
46 Lua.newtable
47+ addFunction "to_roman_numeral" toRomanNumeral
47 addFunction "sha1" sha1
48 addFunction "sha1" sha1
48 addFunction "stringify" stringify
49 addFunction "stringify" stringify
49 return 1
50 return 1
@@ -53,6 +54,9 @@ sha1 :: BSL.ByteString
53 -> Lua String
54 -> Lua String
54sha1 = return . SHA.showDigest . SHA.sha1
55sha1 = return . SHA.showDigest . SHA.sha1
55
56
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).
56stringify :: AstElement -> Lua String
60stringify :: AstElement -> Lua String
57stringify el = return $ case el of
61stringify el = return $ case el of
58 PandocElement pd -> Shared.stringify pd
62 PandocElement pd -> Shared.stringify pd
@@ -77,3 +81,7 @@ instance FromLuaStack AstElement where
77 Right x -> return x
81 Right x -> return x
78 Left _ -> Lua.throwLuaError
82 Left _ -> Lua.throwLuaError
79 "Expected an AST element, but could not parse value as such."
83 "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))
102 , plain (str "read: OK")
102 , plain (str "read: OK")
103 , plain (str "failing read: OK")
103 , plain (str "failing read: OK")
104 , plain (str "stringify: OK")
104 , plain (str "stringify: OK")
105+ , plain (str "to_roman_numeral: OK")
105 ])
106 ])
106 ]
107 ]
107
108
test/lua/test-pandoc-utils.lua+9
@@ -64,6 +64,14 @@ function test_stringify ()
64 return utils.stringify(inline) == 'Cogito ergo sum.'
64 return utils.stringify(inline) == 'Cogito ergo sum.'
65end
65end
66
66
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+
67-- Return result
75-- Return result
68------------------------------------------------------------------------
76------------------------------------------------------------------------
69function run(fn)
77function run(fn)
@@ -78,5 +86,6 @@ function Para (el)
78 pandoc.Plain{pandoc.Str("read: " .. run(test_read))},
86 pandoc.Plain{pandoc.Str("read: " .. run(test_read))},
79 pandoc.Plain{pandoc.Str("failing read: " .. run(test_failing_read))},
87 pandoc.Plain{pandoc.Str("failing read: " .. run(test_failing_read))},
80 pandoc.Plain{pandoc.Str("stringify: " .. run(test_stringify))},
88 pandoc.Plain{pandoc.Str("stringify: " .. run(test_stringify))},
89+ pandoc.Plain{pandoc.Str("to_roman_numeral: " .. run(test_to_roman_numeral))},
81 }
90 }
82end
91end