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

Convert list of Pandoc blocks into (hierarchical) list of Elements.

commit59a47454574d9eab424ef3d6d9ef6b238515f479 authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 23 Dec 2017, 22:39 (9 years ago) parent790dc25
5 files changed+85−9
doc/lua-filters.md+28 src/Text/Pandoc/Lua/Module/Utils.hs+12−7 src/Text/Pandoc/Lua/StackInstances.hs+27−1 test/Tests/Lua.hs+2−1 test/lua/test-pandoc-utils.lua+16
doc/lua-filters.md+28
@@ -1433,6 +1433,34 @@ Lua functions for pandoc scripts.
14331433This module exposes internal pandoc functions and utility
14341434functions.
14351435
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+
14361464[`normalize_date (date_string)`]{#utils-normalize_date}
14371465
14381466: 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
4444pushModule :: Lua NumResults
4545pushModule = do
4646 Lua.newtable
47+ addFunction "hierarchicalize" hierarchicalize
4748 addFunction "normalize_date" normalizeDate
4849 addFunction "sha1" sha1
4950 addFunction "stringify" stringify
5051 addFunction "to_roman_numeral" toRomanNumeral
5152 return 1
5253
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+
5365-- | Calculate the hash of the given contents.
5466sha1 :: BSL.ByteString
5567 -> Lua String
@@ -86,10 +98,3 @@ instance FromLuaStack AstElement where
8698-- | Convert a number < 4000 to uppercase roman numeral.
8799toRomanNumeral :: LuaInteger -> Lua String
88100toRomanNumeral = 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.
3333module Text.Pandoc.Lua.StackInstances () where
3434
3535import Control.Applicative ((<|>))
36+import Control.Monad (when)
3637import Foreign.Lua (FromLuaStack (peek), Lua, LuaInteger, LuaNumber, StackIndex,
3738 ToLuaStack (push), Type (..), throwLuaError, tryLua)
3839import Text.Pandoc.Definition
3940import Text.Pandoc.Lua.Util (adjustIndexBy, getTable, pushViaConstructor)
40−import Text.Pandoc.Shared (safeRead)
41+import Text.Pandoc.Shared (Element (Blk, Sec), safeRead)
4142
4243import qualified Foreign.Lua as Lua
44+import qualified Text.Pandoc.Lua.Util as LuaUtil
4345
4446instance ToLuaStack Pandoc where
4547 push (Pandoc meta blocks) =
@@ -306,3 +308,27 @@ instance ToLuaStack LuaAttr where
306308
307309instance FromLuaStack LuaAttr where
308310 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))
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 "normalize_date: OK")
99+ (doc $ mconcat [ plain (str "hierarchicalize: OK")
100+ , plain (str "normalize_date: OK")
100101 , plain (str "pipe: OK")
101102 , plain (str "failing pipe: OK")
102103 , plain (str "read: OK")
test/lua/test-pandoc-utils.lua+16
@@ -1,5 +1,20 @@
11utils = require 'pandoc.utils'
22
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+
318-- SHA1
419------------------------------------------------------------------------
520function test_sha1 ()
@@ -87,6 +102,7 @@ end
87102
88103function Para (el)
89104 return {
105+ pandoc.Plain{pandoc.Str("hierarchicalize: " .. run(test_hierarchicalize))},
90106 pandoc.Plain{pandoc.Str("normalize_date: " .. run(test_normalize_date))},
91107 pandoc.Plain{pandoc.Str("pipe: " .. run(test_pipe))},
92108 pandoc.Plain{pandoc.Str("failing pipe: " .. run(test_failing_pipe))},