git.iko.soy/typeable/pandoc
on master help sign in
Lua Utils module: add function blocks_to_inlines (#4799)

Exposes a function converting which flattenes a list of blocks into a list of inlines. An example use case would be the conversion of Note elements into other inlines.

commitfb94c0f6a1b98d4f7ff34107d3b63c2c1d0afe1f authorAlbert Krewinkel <albert+github@zeitkraut.de> dateMon 30 Jul 2018, 19:55 (8 years ago) parentbf56181
5 files changed+69−3
doc/lua-filters.md+31 src/Text/Pandoc/Lua/Module/Utils.hs+10 src/Text/Pandoc/Shared.hs+11−2 test/Tests/Lua.hs+2−1 test/lua/test-pandoc-utils.lua+15
doc/lua-filters.md+31
@@ -1438,6 +1438,37 @@ Lua functions for pandoc scripts.
14381438This module exposes internal pandoc functions and utility
14391439functions.
14401440
1441+[`blocks_to_inlines (blocks[, sep])`]{#utils-blocks_to_inlines}
1442+
1443+: Squash a list of blocks into a list of inlines.
1444+
1445+ Parameters:
1446+
1447+ `blocks`:
1448+ : List of blocks to be flattened.
1449+
1450+ `sep`:
1451+ : List of inlines inserted as separator between two
1452+ consecutive blocks; defaults to `{ pandoc.Space(),
1453+ pandoc.Str'¶', pandoc.Space()}`.
1454+
1455+ Returns:
1456+
1457+ - ({[Inline][#Inline]}) List of inlines
1458+
1459+ Usage:
1460+
1461+ local blocks = {
1462+ pandoc.Para{ pandoc.Str 'Paragraph1' },
1463+ pandoc.Para{ pandoc.Emph 'Paragraph2' }
1464+ }
1465+ local inlines = pandoc.utils.blocks_to_inlines(blocks)
1466+ -- inlines = {
1467+ -- pandoc.Str 'Paragraph1',
1468+ -- pandoc.Space(), pandoc.Str'¶', pandoc.Space(),
1469+ -- pandoc.Emph{ pandoc.Str 'Paragraph2' }
1470+ -- }
1471+
14411472[`hierarchicalize (blocks)`]{#utils-hierarchicalize}
14421473
14431474: Convert list of blocks into an hierarchical list. An
src/Text/Pandoc/Lua/Module/Utils.hs+10
@@ -42,6 +42,7 @@ import Text.Pandoc.Lua.Util (addFunction, popValue)
4242import qualified Data.Digest.Pure.SHA as SHA
4343import qualified Data.ByteString.Lazy as BSL
4444import qualified Foreign.Lua as Lua
45+import qualified Text.Pandoc.Builder as B
4546import qualified Text.Pandoc.Filter.JSON as JSONFilter
4647import qualified Text.Pandoc.Shared as Shared
4748
@@ -49,6 +50,7 @@ import qualified Text.Pandoc.Shared as Shared
4950pushModule :: Maybe FilePath -> Lua NumResults
5051pushModule mbDatadir = do
5152 Lua.newtable
53+ addFunction "blocks_to_inlines" blocksToInlines
5254 addFunction "hierarchicalize" hierarchicalize
5355 addFunction "normalize_date" normalizeDate
5456 addFunction "run_json_filter" (runJSONFilter mbDatadir)
@@ -57,6 +59,14 @@ pushModule mbDatadir = do
5759 addFunction "to_roman_numeral" toRomanNumeral
5860 return 1
5961
62+-- | Squashes a list of blocks into inlines.
63+blocksToInlines :: [Block] -> Lua.Optional [Inline] -> Lua [Inline]
64+blocksToInlines blks optSep = do
65+ let sep = case Lua.fromOptional optSep of
66+ Just x -> B.fromList x
67+ Nothing -> Shared.defaultBlocksSeparator
68+ return $ B.toList (Shared.blocksToInlinesWithSep sep blks)
69+
6070-- | Convert list of Pandoc blocks into (hierarchical) list of Elements.
6171hierarchicalize :: [Block] -> Lua [Shared.Element]
6272hierarchicalize = return . Shared.hierarchicalize
src/Text/Pandoc/Shared.hs+11−2
@@ -94,6 +94,8 @@ module Text.Pandoc.Shared (
9494 -- * for squashing blocks
9595 blocksToInlines,
9696 blocksToInlines',
97+ blocksToInlinesWithSep,
98+ defaultBlocksSeparator,
9799 -- * Safe read
98100 safeRead,
99101 -- * Temp directory
@@ -757,12 +759,19 @@ blocksToInlinesWithSep sep =
757759 mconcat . intersperse sep . map blockToInlines
758760
759761blocksToInlines' :: [Block] -> Inlines
760−blocksToInlines' = blocksToInlinesWithSep parSep
761− where parSep = B.space <> B.str "¶" <> B.space
762+blocksToInlines' = blocksToInlinesWithSep defaultBlocksSeparator
762763
763764blocksToInlines :: [Block] -> [Inline]
764765blocksToInlines = B.toList . blocksToInlines'
765766
767+-- | Inline elements used to separate blocks when squashing blocks into
768+-- inlines.
769+defaultBlocksSeparator :: Inlines
770+defaultBlocksSeparator =
771+ -- This is used in the pandoc.utils.blocks_to_inlines function. Docs
772+ -- there should be updated if this is changed.
773+ B.space <> B.str "¶" <> B.space
774+
766775
767776--
768777-- Safe read
test/Tests/Lua.hs+2−1
@@ -109,7 +109,8 @@ tests = map (localOption (QuickCheckTests 20))
109109 assertFilterConversion "pandoc.utils doesn't work as expected."
110110 "test-pandoc-utils.lua"
111111 (doc $ para "doesn't matter")
112− (doc $ mconcat [ plain (str "hierarchicalize: OK")
112+ (doc $ mconcat [ plain (str "blocks_to_inlines: OK")
113+ , plain (str "hierarchicalize: OK")
113114 , plain (str "normalize_date: OK")
114115 , plain (str "pipe: OK")
115116 , plain (str "failing pipe: OK")
test/lua/test-pandoc-utils.lua+15
@@ -1,5 +1,19 @@
11utils = require 'pandoc.utils'
22
3+-- Squash blocks to inlines
4+------------------------------------------------------------------------
5+function test_blocks_to_inlines ()
6+ local blocks = {
7+ pandoc.Para{ pandoc.Str 'Paragraph1' },
8+ pandoc.Para{ pandoc.Emph 'Paragraph2' }
9+ }
10+ local inlines = utils.blocks_to_inlines(blocks, {pandoc.LineBreak()})
11+ return #inlines == 3
12+ and inlines[1].text == "Paragraph1"
13+ and inlines[2].t == 'LineBreak'
14+ and inlines[3].content[1].text == "Paragraph2"
15+end
16+
317-- hierarchicalize
418------------------------------------------------------------------------
519function test_hierarchicalize ()
@@ -110,6 +124,7 @@ end
110124
111125function Para (el)
112126 return {
127+ pandoc.Plain{pandoc.Str("blocks_to_inlines: " .. run(test_blocks_to_inlines))},
113128 pandoc.Plain{pandoc.Str("hierarchicalize: " .. run(test_hierarchicalize))},
114129 pandoc.Plain{pandoc.Str("normalize_date: " .. run(test_normalize_date))},
115130 pandoc.Plain{pandoc.Str("pipe: " .. run(test_pipe))},