git.iko.soy/typeable/pandoc
on master help sign in
Lua modules: add function pandoc.utils.normalize_date
2c66a42Albert Krewinkel
doc/lua-filters.md+11
@@ -1433,6 +1433,17 @@ Lua functions for pandoc scripts.
1433This module exposes internal pandoc functions and utility
1433This module exposes internal pandoc functions and utility
1434functions.
1434functions.
1435
1435
1436+[`normalize_date (date_string)`]{#utils-normalize_date}
1437+
1438+: Parse a date and convert (if possible) to "YYYY-MM-DD"
1439+ format. We limit years to the range 1601-9999 (ISO 8601
1440+ accepts greater than or equal to 1583, but MS Word only
1441+ accepts dates starting 1601).
1442+
1443+ Returns:
1444+
1445+ - A date string, or nil when the conversion failed.
1446+
1436[`sha1 (contents)`]{#utils-sha1}
1447[`sha1 (contents)`]{#utils-sha1}
1437
1448
1438: Returns the SHA1 has of the contents.
1449: Returns the SHA1 has of the contents.
src/Text/Pandoc/Lua/Module/Utils.hs+10−2
@@ -33,7 +33,7 @@ import Control.Applicative ((<|>))
33import Foreign.Lua (FromLuaStack, Lua, LuaInteger, NumResults)
33import 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 ()
36−import Text.Pandoc.Lua.Util (addFunction)
36+import Text.Pandoc.Lua.Util (OrNil (OrNil), addFunction)
37
37
38import qualified Data.Digest.Pure.SHA as SHA
38import qualified Data.Digest.Pure.SHA as SHA
39import qualified Data.ByteString.Lazy as BSL
39import qualified Data.ByteString.Lazy as BSL
@@ -44,9 +44,10 @@ 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 "normalize_date" normalizeDate
48 addFunction "sha1" sha1
48 addFunction "sha1" sha1
49 addFunction "stringify" stringify
49 addFunction "stringify" stringify
50+ addFunction "to_roman_numeral" toRomanNumeral
50 return 1
51 return 1
51
52
52-- | Calculate the hash of the given contents.
53-- | Calculate the hash of the given contents.
@@ -85,3 +86,10 @@ instance FromLuaStack AstElement where
85-- | Convert a number < 4000 to uppercase roman numeral.
86-- | Convert a number < 4000 to uppercase roman numeral.
86toRomanNumeral :: LuaInteger -> Lua String
87toRomanNumeral :: LuaInteger -> Lua String
87toRomanNumeral = return . Shared.toRomanNumeral . fromIntegral
88toRomanNumeral = return . Shared.toRomanNumeral . fromIntegral
89+
90+-- | Parse a date and convert (if possible) to "YYYY-MM-DD" format. We
91+-- limit years to the range 1601-9999 (ISO 8601 accepts greater than
92+-- or equal to 1583, but MS Word only accepts dates starting 1601).
93+-- Returns nil instead of a string if the conversion failed.
94+normalizeDate :: String -> Lua (OrNil String)
95+normalizeDate = return . OrNil . Shared.normalizeDate
src/Text/Pandoc/Lua/Util.hs+4
@@ -125,6 +125,10 @@ instance FromLuaStack a => FromLuaStack (OrNil a) where
125 then return (OrNil Nothing)
125 then return (OrNil Nothing)
126 else OrNil . Just <$> Lua.peek idx
126 else OrNil . Just <$> Lua.peek idx
127
127
128+instance ToLuaStack a => ToLuaStack (OrNil a) where
129+ push (OrNil Nothing) = Lua.pushnil
130+ push (OrNil (Just x)) = Lua.push x
131+
128-- | Helper class for pushing a single value to the stack via a lua function.
132-- | Helper class for pushing a single value to the stack via a lua function.
129-- See @pushViaCall@.
133-- See @pushViaCall@.
130class PushViaCall a where
134class PushViaCall a where
test/Tests/Lua.hs+2−1
@@ -96,11 +96,12 @@ tests = map (localOption (QuickCheckTests 20))
96 assertFilterConversion "pandoc.utils doesn't work as expected."
96 assertFilterConversion "pandoc.utils doesn't work as expected."
97 "test-pandoc-utils.lua"
97 "test-pandoc-utils.lua"
98 (doc $ para "doesn't matter")
98 (doc $ para "doesn't matter")
99− (doc $ mconcat [ plain (str "sha1: OK")
99+ (doc $ mconcat [ plain (str "normalize_date: OK")
100 , plain (str "pipe: OK")
100 , plain (str "pipe: OK")
101 , plain (str "failing pipe: OK")
101 , plain (str "failing pipe: OK")
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 "sha1: OK")
104 , plain (str "stringify: OK")
105 , plain (str "stringify: OK")
105 , plain (str "to_roman_numeral: OK")
106 , plain (str "to_roman_numeral: OK")
106 ])
107 ])
test/lua/test-pandoc-utils.lua+9−1
@@ -72,6 +72,13 @@ function test_to_roman_numeral ()
72 and not pcall(utils.to_roman_numeral, 'not a number')
72 and not pcall(utils.to_roman_numeral, 'not a number')
73end
73end
74
74
75+-- normalize_date
76+------------------------------------------------------------------------
77+function test_normalize_date ()
78+ return utils.normalize_date("12/31/2017") == '2017-12-31'
79+ and utils.normalize_date("pandoc") == nil
80+end
81+
75-- Return result
82-- Return result
76------------------------------------------------------------------------
83------------------------------------------------------------------------
77function run(fn)
84function run(fn)
@@ -80,11 +87,12 @@ end
80
87
81function Para (el)
88function Para (el)
82 return {
89 return {
83− pandoc.Plain{pandoc.Str("sha1: " .. run(test_sha1))},
90+ pandoc.Plain{pandoc.Str("normalize_date: " .. run(test_normalize_date))},
84 pandoc.Plain{pandoc.Str("pipe: " .. run(test_pipe))},
91 pandoc.Plain{pandoc.Str("pipe: " .. run(test_pipe))},
85 pandoc.Plain{pandoc.Str("failing pipe: " .. run(test_failing_pipe))},
92 pandoc.Plain{pandoc.Str("failing pipe: " .. run(test_failing_pipe))},
86 pandoc.Plain{pandoc.Str("read: " .. run(test_read))},
93 pandoc.Plain{pandoc.Str("read: " .. run(test_read))},
87 pandoc.Plain{pandoc.Str("failing read: " .. run(test_failing_read))},
94 pandoc.Plain{pandoc.Str("failing read: " .. run(test_failing_read))},
95+ pandoc.Plain{pandoc.Str("sha1: " .. run(test_sha1))},
88 pandoc.Plain{pandoc.Str("stringify: " .. run(test_stringify))},
96 pandoc.Plain{pandoc.Str("stringify: " .. run(test_stringify))},
89 pandoc.Plain{pandoc.Str("to_roman_numeral: " .. run(test_to_roman_numeral))},
97 pandoc.Plain{pandoc.Str("to_roman_numeral: " .. run(test_to_roman_numeral))},
90 }
98 }