From a87f67e83be4a7116632409da46ace2955c43064 Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Tue, 31 Oct 2023 14:48:05 +0100 Subject: [PATCH] \#8986 fix bug of meta by splitting --- src/Text/Pandoc/Readers/Docx.hs | 10 +- test/docx/0_level_headers.native | 234 +++++++++++++++++++++------- test/docx/image-before-title.native | 182 ++++++++++++---------- 3 files changed, 286 insertions(+), 140 deletions(-) diff --git a/src/Text/Pandoc/Readers/Docx.hs b/src/Text/Pandoc/Readers/Docx.hs index 4ab40f5a61ec3..8aea800c923fa 100644 --- a/src/Text/Pandoc/Readers/Docx.hs +++ b/src/Text/Pandoc/Readers/Docx.hs @@ -73,7 +73,7 @@ import Control.Monad.State.Strict import Data.Bifunctor (bimap, first) import qualified Data.ByteString.Lazy as B import Data.Default (Default) -import Data.List (delete, intersect, foldl') +import Data.List (partition, delete, intersect, foldl') import Data.Char (isSpace) import qualified Data.Map as M import qualified Data.Text as T @@ -177,7 +177,13 @@ metaStyles = M.fromList [ ("Title", "title") , ("Abstract", "abstract")] sepBodyParts :: [BodyPart] -> ([BodyPart], [BodyPart]) -sepBodyParts = span (\bp -> isMetaPar bp || isEmptyPar bp) +sepBodyParts bps = (metaWithoutEmpty, nonMetaFirst ++ emptyPars ++ nonMetaLast) + where + (nonMetaFirst, rest) = break isMetaOrEmpty bps + (meta, nonMetaLast) = span isMetaOrEmpty rest + isMetaOrEmpty bp = isMetaPar bp || isEmptyPar bp + + (metaWithoutEmpty, emptyPars) = partition (not . isEmptyPar) meta isMetaPar :: BodyPart -> Bool isMetaPar (Paragraph pPr _) = diff --git a/test/docx/0_level_headers.native b/test/docx/0_level_headers.native index 7080063f95470..ebf74cd31d368 100644 --- a/test/docx/0_level_headers.native +++ b/test/docx/0_level_headers.native @@ -1,52 +1,182 @@ -[Table ("",[],[]) (Caption Nothing - []) - [(AlignDefault,ColWidth 1.0)] - (TableHead ("",[],[]) - []) - [(TableBody ("",[],[]) (RowHeadColumns 0) - [] - [Row ("",[],[]) - [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) - []] - ,Row ("",[],[]) - [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) - [Plain [Str "User\8217s",Space,Str "Guide"]]] - ,Row ("",[],[]) - [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) - []] - ,Row ("",[],[]) - [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) - []] - ,Row ("",[],[]) - [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) - []] - ,Row ("",[],[]) - [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) - [Plain [Str "11",Space,Str "August",Space,Str "2017"]]] - ,Row ("",[],[]) - [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) - []] - ,Row ("",[],[]) - [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) - []] - ,Row ("",[],[]) - [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) - []] - ,Row ("",[],[]) - [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) - []]])] - (TableFoot ("",[],[]) - []) -,Para [Str "CONTENTS"] -,Para [Strong [Str "Section",Space,Str "Page"]] -,Para [Str "FIGURES",Space,Link ("",[],[]) [Str "iv"] ("#figures","")] -,Para [Str "TABLES",Space,Link ("",[],[]) [Str "v"] ("#tables","")] -,Para [Str "SECTION",Space,Str "1",Space,Str "Introduction",Space,Link ("",[],[]) [Str "2"] ("#introduction","")] -,Header 1 ("figures",["Heading-0"],[]) [Str "FIGURES"] -,Para [Strong [Str "Figure",Space,Str "Page"]] -,Para [Strong [Str "No",Space,Str "table",Space,Str "of",Space,Str "figures",Space,Str "entries",Space,Str "found."]] -,Header 1 ("tables",["Heading-0"],[]) [Str "TABLES"] -,Para [Strong [Str "Table",Space,Str "Page"]] -,Para [Strong [Str "No",Space,Str "table",Space,Str "of",Space,Str "figures",Space,Str "entries",Space,Str "found."]] -,Header 1 ("introduction",[],[]) [Str "Introduction"] -,Para [Str "Nothing",Space,Str "to",Space,Str "introduce,",Space,Str "yet."]] +Pandoc + Meta + { unMeta = + fromList [ ( "title" , MetaInlines [ Str "CONTENTS" ] ) ] + } + [ Table + ( "" , [] , [] ) + (Caption Nothing []) + [ ( AlignDefault , ColWidth 1.0 ) ] + (TableHead ( "" , [] , [] ) []) + [ TableBody + ( "" , [] , [] ) + (RowHeadColumns 0) + [] + [ Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [] + ] + , Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "User\8217s" , Space , Str "Guide" ] ] + ] + , Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [] + ] + , Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [] + ] + , Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [] + ] + , Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain + [ Str "11" + , Space + , Str "August" + , Space + , Str "2017" + ] + ] + ] + , Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [] + ] + , Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [] + ] + , Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [] + ] + , Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [] + ] + ] + ] + (TableFoot ( "" , [] , [] ) []) + , Para [ Strong [ Str "Section" , Space , Str "Page" ] ] + , Para + [ Str "FIGURES" + , Space + , Link ( "" , [] , [] ) [ Str "iv" ] ( "#figures" , "" ) + ] + , Para + [ Str "TABLES" + , Space + , Link ( "" , [] , [] ) [ Str "v" ] ( "#tables" , "" ) + ] + , Para + [ Str "SECTION" + , Space + , Str "1" + , Space + , Str "Introduction" + , Space + , Link ( "" , [] , [] ) [ Str "2" ] ( "#introduction" , "" ) + ] + , Header + 1 ( "figures" , [ "Heading-0" ] , [] ) [ Str "FIGURES" ] + , Para [ Strong [ Str "Figure" , Space , Str "Page" ] ] + , Para + [ Strong + [ Str "No" + , Space + , Str "table" + , Space + , Str "of" + , Space + , Str "figures" + , Space + , Str "entries" + , Space + , Str "found." + ] + ] + , Header + 1 ( "tables" , [ "Heading-0" ] , [] ) [ Str "TABLES" ] + , Para [ Strong [ Str "Table" , Space , Str "Page" ] ] + , Para + [ Strong + [ Str "No" + , Space + , Str "table" + , Space + , Str "of" + , Space + , Str "figures" + , Space + , Str "entries" + , Space + , Str "found." + ] + ] + , Header + 1 ( "introduction" , [] , [] ) [ Str "Introduction" ] + , Para + [ Str "Nothing" + , Space + , Str "to" + , Space + , Str "introduce," + , Space + , Str "yet." + ] + ] diff --git a/test/docx/image-before-title.native b/test/docx/image-before-title.native index 7f9b5905b655a..9fd43e58f31bf 100644 --- a/test/docx/image-before-title.native +++ b/test/docx/image-before-title.native @@ -1,87 +1,97 @@ -[ Para - [ Image - ( "" - , [] - , [ ( "width" , "6.268055555555556in" ) - , ( "height" , "6.268055555555556in" ) +Pandoc + Meta + { unMeta = + fromList + [ ( "subtitle" + , MetaInlines + [ Str "This" + , Space + , Str "is" + , Space + , Str "a" + , Space + , Str "subtitle" + ] + ) + , ( "title" + , MetaInlines + [ Str "This" + , Space + , Str "is" + , Space + , Str "a" + , Space + , Str "title" + ] + ) ] - ) - [ Str "A" - , Space - , Str "cat" - , Space - , Str "holding" - , Space - , Str "a" - , Space - , Str "rainbow" - , Space - , Str "flag." - ] - ( "media/image1.jpeg" , "" ) - ] -, Title - [ Str "This" - , Space - , Str "is" - , Space - , Str "a" - , Space - , Str "title" - ] -, Subtitle - [ Str "This" - , Space - , Str "is" - , Space - , Str "a" - , Space - , Str "subtitle" - ] -, Header - 1 - ( "this-is-a-heading-1" , [] , [] ) - [ Str "This" - , Space - , Str "is" - , Space - , Str "a" - , Space - , Str "heading" - , Space - , Str "1" - ] -, Para - [ Str "This" - , Space - , Str "is" - , Space - , Str "some" - , Space - , Str "text." - ] -, Header - 2 - ( "this-is-a-heading-2" , [] , [] ) - [ Str "This" - , Space - , Str "is" - , Space - , Str "a" - , Space - , Str "heading" - , Space - , Str "2" - ] -, Para - [ Str "This" - , Space - , Str "is" - , Space - , Str "also" - , Space - , Str "some" - , Space - , Str "text." - ] -] + } + [ Para + [ Image + ( "" + , [] + , [ ( "width" , "6.268055555555556in" ) + , ( "height" , "6.268055555555556in" ) + ] + ) + [ Str "A" + , Space + , Str "cat" + , Space + , Str "holding" + , Space + , Str "a" + , Space + , Str "rainbow" + , Space + , Str "flag." + ] + ( "media/image1.jpeg" , "" ) + ] + , Header + 1 + ( "this-is-a-heading-1" , [] , [] ) + [ Str "This" + , Space + , Str "is" + , Space + , Str "a" + , Space + , Str "heading" + , Space + , Str "1" + ] + , Para + [ Str "This" + , Space + , Str "is" + , Space + , Str "some" + , Space + , Str "text." + ] + , Header + 2 + ( "this-is-a-heading-2" , [] , [] ) + [ Str "This" + , Space + , Str "is" + , Space + , Str "a" + , Space + , Str "heading" + , Space + , Str "2" + ] + , Para + [ Str "This" + , Space + , Str "is" + , Space + , Str "also" + , Space + , Str "some" + , Space + , Str "text." + ] + ]