Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#8986 filter elements that only contain visuals like images from normal meta #9170

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/Text/Pandoc/Readers/Docx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,36 @@ metaStyles = M.fromList [ ("Title", "title")
, ("Abstract", "abstract")]

sepBodyParts :: [BodyPart] -> ([BodyPart], [BodyPart])
sepBodyParts = span (\bp -> isMetaPar bp || isEmptyPar bp)
sepBodyParts = span seperator
where
-- | Meta is either empty, specifically has a meta style, but never is exclusively
-- containing visuals.
seperator :: BodyPart -> Bool
seperator par = isEmptyPar par || (isMetaPar par && not (isVisualBodyPart par))

isVisualBodyPart :: BodyPart -> Bool
isVisualBodyPart (Paragraph _ []) = False
isVisualBodyPart (Paragraph _ pps) = isVisualParParts pps
isVisualBodyPart _ = False

isVisualParParts :: [ParPart] -> Bool
isVisualParParts = all isVisualParPart

isVisualParPart :: ParPart -> Bool
isVisualParPart (Field _ pps) = isVisualParParts pps
isVisualParPart (PlainRun run) = isVisualRun run
isVisualParPart Drawing{} = True
isVisualParPart Chart = True
isVisualParPart Diagram = True
isVisualParPart PlainOMath{} = True
isVisualParPart OMathPara{} = True
isVisualParPart _ = False

isVisualRun :: Run -> Bool
isVisualRun InlineDrawing{} = True
isVisualRun InlineDiagram{} = True
isVisualRun InlineChart{} = True
isVisualRun _ = False

isMetaPar :: BodyPart -> Bool
isMetaPar (Paragraph pPr _) =
Expand Down
4 changes: 4 additions & 0 deletions test/Tests/Readers/Docx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ tests = [ testGroup "document"
"i18n blocks (headers and blockquotes)"
"docx/i18n_blocks.docx"
"docx/i18n_blocks.native"
, testCompare
"Image as Title"
"docx/image-as-title.docx"
"docx/image-as-title.native"
, testCompare
"lists"
"docx/lists.docx"
Expand Down
Binary file added test/docx/image-as-title.docx
Binary file not shown.
37 changes: 37 additions & 0 deletions test/docx/image-as-title.native
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[ Para
[ Image
( ""
, []
, [ ( "width" , "6.268055555555556in" )
, ( "height" , "6.268055555555556in" )
]
)
[]
( "media/image1.png" , "" )
]
, Para
[ Str "An"
, Space
, Str "Image"
, Space
, Str "as"
, Space
, Str "a"
, Space
, Str "title"
, Space
, Str "should"
, Space
, Str "be"
, Space
, Str "downgraded"
, Space
, Str "to"
, Space
, Str "a"
, Space
, Str "normal"
, Space
, Str "paragraph!"
]
]