git.iko.soy/typeable/pandoc
on master help sign in
Docx writer: better handle list items whose contents are lists (#6522)

If the first element of a bulleted or ordered list is another list, then that first item will disappear if the target format is docx. This changes the docx writer so that it prepends an empty string for those cases. With this, no items will disappear. Closes #5948.

commit74bd5a4f4758d06be43bc807f6f9b82a20970a0b authorMichael Hoffmann <brennan.brisad@gmail.com> dateFri 2 Oct 2020, 18:30 (6 years ago) parent27b4c21
4 files changed+26−3
src/Text/Pandoc/Writers/Docx.hs+13−3 test/Tests/Writers/Docx.hs+5 test/docx/golden/lists_multiple_initial.docx test/docx/lists_multiple_initial.native+8
src/Text/Pandoc/Writers/Docx.hs+13−3
@@ -3,6 +3,7 @@
33{-# LANGUAGE ScopedTypeVariables #-}
44{-# LANGUAGE ViewPatterns #-}
55{-# LANGUAGE FlexibleContexts #-}
6+{-# LANGUAGE LambdaCase #-}
67{-# LANGUAGE OverloadedStrings #-}
78{- |
89 Module : Text.Pandoc.Writers.Docx
@@ -1087,11 +1088,20 @@ listItemToOpenXML _ _ [] = return []
10871088listItemToOpenXML opts numid (first:rest) = do
10881089 oldInList <- gets stInList
10891090 modify $ \st -> st{ stInList = True }
1090− first' <- withNumId numid $ blockToOpenXML opts first
1091+ let isListBlock = \case
1092+ BulletList{} -> True
1093+ OrderedList{} -> True
1094+ _ -> False
1095+ -- Prepend an empty string if the first entry is another
1096+ -- list. Otherwise the outer bullet will disappear.
1097+ let (first', rest') = if isListBlock first
1098+ then (Plain [Str ""] , first:rest)
1099+ else (first, rest)
1100+ first'' <- withNumId numid $ blockToOpenXML opts first'
10911101 -- baseListId is the code for no list marker:
1092− rest' <- withNumId baseListId $ blocksToOpenXML opts rest
1102+ rest'' <- withNumId baseListId $ blocksToOpenXML opts rest'
10931103 modify $ \st -> st{ stInList = oldInList }
1094− return $ first' ++ rest'
1104+ return $ first'' ++ rest''
10951105
10961106alignmentToString :: Alignment -> [Char]
10971107alignmentToString alignment = case alignment of
test/Tests/Writers/Docx.hs+5
@@ -83,6 +83,11 @@ tests = [ testGroup "inlines"
8383 def
8484 "docx/lists_restarting.native"
8585 "docx/golden/lists_restarting.docx"
86+ , docxTest
87+ "lists with multiple initial list levels"
88+ def
89+ "docx/lists_multiple_initial.native"
90+ "docx/golden/lists_multiple_initial.docx"
8691 , docxTest
8792 "definition lists"
8893 def
test/docx/golden/lists_multiple_initial.docxnew file

Binary file not shown.

test/docx/lists_multiple_initial.nativenew file+8
@@ -0,0 +1,8 @@
1+[OrderedList (1,Decimal,Period)
2+ [[OrderedList (1,LowerAlpha,TwoParens)
3+ [[Para [Str "foo"]]
4+ ,[Para [Str "bar"]]]]]
5+,BulletList
6+ [[BulletList
7+ [[Para [Str "foo"]]
8+ ,[Para [Str "bar"]]]]]]