Skip to content

Commit

Permalink
Prefer list of items over single item for list content
Browse files Browse the repository at this point in the history
Changes the fuzzy handling of `content` properties on BulletList and
OrderedList. A list of blocks is now interpreted as a list of
single-block list items instead of a single item containing the given
list. This allows for more intuitive behavior of the `content` property
of BulletList and OrderedList elements.
  • Loading branch information
tarleb committed May 6, 2024
1 parent ca7b48f commit 8bd91fa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Text/Pandoc/Lua/Marshal/Block.hs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ setBlockContent _ = \case
c -> throwM . luaException @e $
"expected definition items, got " <> contentTypeDescription c
listItemContent = \case
ContentBlocks blks -> [blks]
ContentBlocks blks -> map (:[]) blks
ContentLines lns -> map ((:[]) . Plain) lns
ContentListItems itms -> itms
c -> throwM . luaException @e $
Expand Down
4 changes: 2 additions & 2 deletions src/Text/Pandoc/Lua/Marshal/Content.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Control.Applicative ((<|>))
import Control.Monad ((<$!>))
import HsLua
import {-# SOURCE #-} Text.Pandoc.Lua.Marshal.Block

Check warning on line 22 in src/Text/Pandoc/Lua/Marshal/Content.hs

View workflow job for this annotation

GitHub Actions / ghc 8.6.5; cabal 3.2; lua -allow-unsafe-gc

The import of ‘peekBlocks’

Check warning on line 22 in src/Text/Pandoc/Lua/Marshal/Content.hs

View workflow job for this annotation

GitHub Actions / ghc 8.8.4; cabal 3.2; lua +pkg-config

The import of ‘peekBlocks’

Check warning on line 22 in src/Text/Pandoc/Lua/Marshal/Content.hs

View workflow job for this annotation

GitHub Actions / ghc 8.10.7; cabal 3.2; lua +apicheck

The import of ‘peekBlocks’
( peekBlocksFuzzy, pushBlocks )
( peekBlocks, peekBlocksFuzzy, pushBlocks )

Check warning on line 23 in src/Text/Pandoc/Lua/Marshal/Content.hs

View workflow job for this annotation

GitHub Actions / ghc 9.0.2; cabal 3.4; lua -export-dynamic

The import of ‘peekBlocks’

Check warning on line 23 in src/Text/Pandoc/Lua/Marshal/Content.hs

View workflow job for this annotation

GitHub Actions / ghc 9.2.5; cabal 3.6; lua +apicheck

The import of ‘peekBlocks’

Check warning on line 23 in src/Text/Pandoc/Lua/Marshal/Content.hs

View workflow job for this annotation

GitHub Actions / ghc 9.4.4; cabal 3.8; lua -apicheck

The import of ‘peekBlocks’
import {-# SOURCE #-} Text.Pandoc.Lua.Marshal.Inline
( peekInlinesFuzzy, pushInlines )
import Text.Pandoc.Lua.Marshal.List (pushPandocList)
Expand All @@ -39,7 +39,7 @@ data Content
| ContentDefItems [([Inline], [[Block]])]
| ContentListItems [[Block]]

-- | Gets the text property of an Inline, if present.
-- | Returns a human-readable type description; used for error messages.
contentTypeDescription :: Content -> String
contentTypeDescription = \case
ContentBlocks {} -> "list of Block items"
Expand Down
36 changes: 30 additions & 6 deletions test/test-block.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ return {
group 'BlockQuote' {
test('access content via property `content`', function ()
local elem = BlockQuote{'word'}
assert.are_same(elem.content, {Plain 'word'})
assert.are_equal(elem.content, Blocks{Plain 'word'})
assert.are_equal(type(elem.content), 'table')

elem.content = {
Expand All @@ -29,16 +29,40 @@ return {
test('access items via property `content`', function ()
local para = Para 'one'
local blist = BulletList{{para}}
assert.are_same({{para}}, blist.content)
assert.are_equal(List{Blocks{para}}, blist.content)
end),
test('property `content` is a list of Block lists', function ()
local items = List{Blocks{Plain 'item 1'}, Blocks{Plain 'item 2'}}
local blist = BulletList{}
blist.content = items
assert.are_equal(items, blist:clone().content)
end),
test('property `content` uses fuzzy marshalling', function ()
local old = Plain 'old'
local new = Plain 'new'
local blist = BulletList{{old}}
local blist = BulletList{{Plain 'old'}}
blist.content = {{new}}
assert.are_same({{new}}, blist:clone().content)
assert.are_equal(List{Blocks{new}}, blist:clone().content)
blist.content = new
assert.are_same({{new}}, blist:clone().content)
assert.are_equal(List{Blocks{new}}, blist:clone().content)
end),
test('property `content` prioritizes lists', function ()
local blist = BulletList{}
local one, two = Para 'one', Plain 'two'
blist.content = {one, two}
assert.are_equal(
List{Blocks{one}, Blocks{two}},
blist:clone().content
)
end),
test('mixing types works', function ()
local one = Plain 'one'
local two = 'two'
local blist = BulletList{}
blist.content = {one, two}
assert.are_same(
List{Blocks{one}, Blocks{Plain(two)}},
blist:clone().content
)
end),
},
group 'CodeBlock' {
Expand Down

0 comments on commit 8bd91fa

Please sign in to comment.