git.iko.soy/typeable/pandoc
on master help sign in
Lua modules: add function pandoc.utils.hierarchicalize
59a4745Albert Krewinkel
doc/lua-filters.md+28
@@ -1433,6 +1433,34 @@ 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+[`hierarchicalize (blocks)`]{#utils-hierarchicalize}
1437+
1438+: Convert list of blocks into an hierarchical list. An
1439+ hierarchical elements is either a normal block (but no
1440+ Header), or a `Sec` element. The latter has the following
1441+ fields:
1442+
1443+ - level: level in the document hierarchy;
1444+ - numbering: list of integers of length `level`,
1445+ specifying the absolute position of the section in the
1446+ document;
1447+ - attr: section attributes (see [Attr](#Attr));
1448+ - contents: nested list of hierarchical elements.
1449+
1450+ Returns:
1451+
1452+ - List of hierarchical elements
1453+
1454+ Usage:
1455+
1456+ local blocks = {
1457+ pandoc.Header(2, pandoc.Str 'first'),
1458+ pandoc.Header(2, pandoc.Str 'second'),
1459+ }
1460+ local elements = pandoc.utils.hierarchicalize(blocks)
1461+ print(table.concat(elements[1].numbering, '.')) -- 0.1
1462+ print(table.concat(elements[2].numbering, '.')) -- 0.2
1463+
1436[`normalize_date (date_string)`]{#utils-normalize_date}
1464[`normalize_date (date_string)`]{#utils-normalize_date}
1437
1465
1438: Parse a date and convert (if possible) to "YYYY-MM-DD"
1466: Parse a date and convert (if possible) to "YYYY-MM-DD"
src/Text/Pandoc/Lua/Module/Utils.hs+12−7
@@ -44,12 +44,24 @@ 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 "hierarchicalize" hierarchicalize
47 addFunction "normalize_date" normalizeDate
48 addFunction "normalize_date" normalizeDate
48 addFunction "sha1" sha1
49 addFunction "sha1" sha1
49 addFunction "stringify" stringify
50 addFunction "stringify" stringify
50 addFunction "to_roman_numeral" toRomanNumeral
51 addFunction "to_roman_numeral" toRomanNumeral
51 return 1
52 return 1
52
53
54+-- | Convert list of Pandoc blocks into (hierarchical) list of Elements.
55+hierarchicalize :: [Block] -> Lua [Shared.Element]
56+hierarchicalize = return . Shared.hierarchicalize
57+
58+-- | Parse a date and convert (if possible) to "YYYY-MM-DD" format. We
59+-- limit years to the range 1601-9999 (ISO 8601 accepts greater than
60+-- or equal to 1583, but MS Word only accepts dates starting 1601).
61+-- Returns nil instead of a string if the conversion failed.
62+normalizeDate :: String -> Lua (OrNil String)
63+normalizeDate = return . OrNil . Shared.normalizeDate
64+
53-- | Calculate the hash of the given contents.
65-- | Calculate the hash of the given contents.
54sha1 :: BSL.ByteString
66sha1 :: BSL.ByteString
55 -> Lua String
67 -> Lua String
@@ -86,10 +98,3 @@ instance FromLuaStack AstElement where
86-- | Convert a number < 4000 to uppercase roman numeral.
98-- | Convert a number < 4000 to uppercase roman numeral.
87toRomanNumeral :: LuaInteger -> Lua String
99toRomanNumeral :: LuaInteger -> Lua String
88toRomanNumeral = return . Shared.toRomanNumeral . fromIntegral
100toRomanNumeral = 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/StackInstances.hs+27−1
@@ -33,13 +33,15 @@ StackValue instances for pandoc types.
33module Text.Pandoc.Lua.StackInstances () where
33module Text.Pandoc.Lua.StackInstances () where
34
34
35import Control.Applicative ((<|>))
35import Control.Applicative ((<|>))
36+import Control.Monad (when)
36import Foreign.Lua (FromLuaStack (peek), Lua, LuaInteger, LuaNumber, StackIndex,
37import Foreign.Lua (FromLuaStack (peek), Lua, LuaInteger, LuaNumber, StackIndex,
37 ToLuaStack (push), Type (..), throwLuaError, tryLua)
38 ToLuaStack (push), Type (..), throwLuaError, tryLua)
38import Text.Pandoc.Definition
39import Text.Pandoc.Definition
39import Text.Pandoc.Lua.Util (adjustIndexBy, getTable, pushViaConstructor)
40import Text.Pandoc.Lua.Util (adjustIndexBy, getTable, pushViaConstructor)
40−import Text.Pandoc.Shared (safeRead)
41+import Text.Pandoc.Shared (Element (Blk, Sec), safeRead)
41
42
42import qualified Foreign.Lua as Lua
43import qualified Foreign.Lua as Lua
44+import qualified Text.Pandoc.Lua.Util as LuaUtil
43
45
44instance ToLuaStack Pandoc where
46instance ToLuaStack Pandoc where
45 push (Pandoc meta blocks) =
47 push (Pandoc meta blocks) =
@@ -306,3 +308,27 @@ instance ToLuaStack LuaAttr where
306
308
307instance FromLuaStack LuaAttr where
309instance FromLuaStack LuaAttr where
308 peek idx = LuaAttr <$> peek idx
310 peek idx = LuaAttr <$> peek idx
311+
312+--
313+-- Hierarchical elements
314+--
315+instance ToLuaStack Element where
316+ push (Blk blk) = push blk
317+ push (Sec lvl num attr label contents) = do
318+ Lua.newtable
319+ LuaUtil.addValue "level" lvl
320+ LuaUtil.addValue "numbering" num
321+ LuaUtil.addValue "attr" (LuaAttr attr)
322+ LuaUtil.addValue "label" label
323+ LuaUtil.addValue "contents" contents
324+ pushSecMetaTable
325+ Lua.setmetatable (-2)
326+ where
327+ pushSecMetaTable :: Lua ()
328+ pushSecMetaTable = do
329+ inexistant <- Lua.newmetatable "PandocElementSec"
330+ when inexistant $ do
331+ LuaUtil.addValue "t" "Sec"
332+ Lua.push "__index"
333+ Lua.pushvalue (-2)
334+ Lua.rawset (-3)
test/Tests/Lua.hs+2−1
@@ -96,7 +96,8 @@ 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 "hierarchicalize: OK")
99− (doc $ mconcat [ plain (str "normalize_date: OK")
100+ , plain (str "normalize_date: OK")
100 , plain (str "pipe: OK")
101 , plain (str "pipe: OK")
101 , plain (str "failing pipe: OK")
102 , plain (str "failing pipe: OK")
102 , plain (str "read: OK")
103 , plain (str "read: OK")
test/lua/test-pandoc-utils.lua+16
@@ -1,5 +1,20 @@
1utils = require 'pandoc.utils'
1utils = require 'pandoc.utils'
2
2
3+-- hierarchicalize
4+------------------------------------------------------------------------
5+function test_hierarchicalize ()
6+ local blks = {
7+ pandoc.Header(1, {pandoc.Str 'First'}),
8+ pandoc.Header(2, {pandoc.Str 'Second'}),
9+ pandoc.Header(2, {pandoc.Str 'Third'}),
10+ }
11+ local hblks = utils.hierarchicalize(blks)
12+ return hblks[1].t == "Sec"
13+ and hblks[1].contents[1].t == "Sec"
14+ and hblks[1].contents[2].numbering[1] == 1
15+ and hblks[1].contents[2].numbering[2] == 2
16+end
17+
3-- SHA1
18-- SHA1
4------------------------------------------------------------------------
19------------------------------------------------------------------------
5function test_sha1 ()
20function test_sha1 ()
@@ -87,6 +102,7 @@ end
87
102
88function Para (el)
103function Para (el)
89 return {
104 return {
105+ pandoc.Plain{pandoc.Str("hierarchicalize: " .. run(test_hierarchicalize))},
90 pandoc.Plain{pandoc.Str("normalize_date: " .. run(test_normalize_date))},
106 pandoc.Plain{pandoc.Str("normalize_date: " .. run(test_normalize_date))},
91 pandoc.Plain{pandoc.Str("pipe: " .. run(test_pipe))},
107 pandoc.Plain{pandoc.Str("pipe: " .. run(test_pipe))},
92 pandoc.Plain{pandoc.Str("failing pipe: " .. run(test_failing_pipe))},
108 pandoc.Plain{pandoc.Str("failing pipe: " .. run(test_failing_pipe))},