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

The function parses a date and converts it (if possible) to "YYYY-MM-DD" format.

commit2c66a42ab81d40e771eda0f054c62f22ad45f3d0 authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 23 Dec 2017, 13:35 (9 years ago) parent35f0567
5 files changed+36−4
doc/lua-filters.md+11 src/Text/Pandoc/Lua/Module/Utils.hs+10−2 src/Text/Pandoc/Lua/Util.hs+4 test/Tests/Lua.hs+2−1 test/lua/test-pandoc-utils.lua+9−1
doc/lua-filters.md+11
@@ -1433,6 +1433,17 @@ Lua functions for pandoc scripts.
14331433This module exposes internal pandoc functions and utility
14341434functions.
14351435
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+
14361447[`sha1 (contents)`]{#utils-sha1}
14371448
14381449: Returns the SHA1 has of the contents.
src/Text/Pandoc/Lua/Module/Utils.hs+10−2
@@ -33,7 +33,7 @@ import Control.Applicative ((<|>))
3333import Foreign.Lua (FromLuaStack, Lua, LuaInteger, NumResults)
3434import Text.Pandoc.Definition (Pandoc, Meta, Block, Inline)
3535import Text.Pandoc.Lua.StackInstances ()
36−import Text.Pandoc.Lua.Util (addFunction)
36+import Text.Pandoc.Lua.Util (OrNil (OrNil), addFunction)
3737
3838import qualified Data.Digest.Pure.SHA as SHA
3939import qualified Data.ByteString.Lazy as BSL
@@ -44,9 +44,10 @@ import qualified Text.Pandoc.Shared as Shared
4444pushModule :: Lua NumResults
4545pushModule = do
4646 Lua.newtable
47− addFunction "to_roman_numeral" toRomanNumeral
47+ addFunction "normalize_date" normalizeDate
4848 addFunction "sha1" sha1
4949 addFunction "stringify" stringify
50+ addFunction "to_roman_numeral" toRomanNumeral
5051 return 1
5152
5253-- | Calculate the hash of the given contents.
@@ -85,3 +86,10 @@ instance FromLuaStack AstElement where
8586-- | Convert a number < 4000 to uppercase roman numeral.
8687toRomanNumeral :: LuaInteger -> Lua String
8788toRomanNumeral = 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
125125 then return (OrNil Nothing)
126126 else OrNil . Just <$> Lua.peek idx
127127
128+instance ToLuaStack a => ToLuaStack (OrNil a) where
129+ push (OrNil Nothing) = Lua.pushnil
130+ push (OrNil (Just x)) = Lua.push x
131+
128132-- | Helper class for pushing a single value to the stack via a lua function.
129133-- See @pushViaCall@.
130134class PushViaCall a where
test/Tests/Lua.hs+2−1
@@ -96,11 +96,12 @@ tests = map (localOption (QuickCheckTests 20))
9696 assertFilterConversion "pandoc.utils doesn't work as expected."
9797 "test-pandoc-utils.lua"
9898 (doc $ para "doesn't matter")
99− (doc $ mconcat [ plain (str "sha1: OK")
99+ (doc $ mconcat [ plain (str "normalize_date: OK")
100100 , plain (str "pipe: OK")
101101 , plain (str "failing pipe: OK")
102102 , plain (str "read: OK")
103103 , plain (str "failing read: OK")
104+ , plain (str "sha1: OK")
104105 , plain (str "stringify: OK")
105106 , plain (str "to_roman_numeral: OK")
106107 ])
test/lua/test-pandoc-utils.lua+9−1
@@ -72,6 +72,13 @@ function test_to_roman_numeral ()
7272 and not pcall(utils.to_roman_numeral, 'not a number')
7373end
7474
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+
7582-- Return result
7683------------------------------------------------------------------------
7784function run(fn)
@@ -80,11 +87,12 @@ end
8087
8188function Para (el)
8289 return {
83− pandoc.Plain{pandoc.Str("sha1: " .. run(test_sha1))},
90+ pandoc.Plain{pandoc.Str("normalize_date: " .. run(test_normalize_date))},
8491 pandoc.Plain{pandoc.Str("pipe: " .. run(test_pipe))},
8592 pandoc.Plain{pandoc.Str("failing pipe: " .. run(test_failing_pipe))},
8693 pandoc.Plain{pandoc.Str("read: " .. run(test_read))},
8794 pandoc.Plain{pandoc.Str("failing read: " .. run(test_failing_read))},
95+ pandoc.Plain{pandoc.Str("sha1: " .. run(test_sha1))},
8896 pandoc.Plain{pandoc.Str("stringify: " .. run(test_stringify))},
8997 pandoc.Plain{pandoc.Str("to_roman_numeral: " .. run(test_to_roman_numeral))},
9098 }