Skip to content

Commit

Permalink
Fix metadata in docx writer.
Browse files Browse the repository at this point in the history
The new OpenXML template had spaces for metadata that need
to be filled with OpenXML fragments with the proper shape.
This patch ensures that everything is the right shape.

Closes #9828.
  • Loading branch information
jgm committed May 31, 2024
1 parent 439e586 commit 872d7bc
Show file tree
Hide file tree
Showing 37 changed files with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/Text/Pandoc/Writers/Docx/OpenXML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,22 @@ writeOpenXML opts (Pandoc meta blocks) = do
Just (MetaInlines ils) -> pure $ stringify ils
Just (MetaString s) -> pure s
_ -> translateTerm Abstract
abstract <- case lookupMetaBlocks "abstract" meta of
[] -> return []
xs -> withParaPropM (pStyleM "Abstract") $ blocksToOpenXML opts xs
abstract <-
case lookupMetaBlocks "abstract" meta of
[] -> return mempty
xs -> vcat . map (literal . showContent) <$>
withParaPropM (pStyleM "Abstract") (blocksToOpenXML opts xs)

let toInlineMeta field = hcat . map (literal . showContent) <$>
inlinesToOpenXML opts (lookupMetaInlines field meta)

title <- toInlineMeta "title"
subtitle <- toInlineMeta "subtitle"
date <- toInlineMeta "date"

author <- mapM
(fmap (hcat . map (literal . showContent)) . inlinesToOpenXML opts)
(docAuthors meta)

let convertSpace (Str x : Space : Str y : xs) = Str (x <> " " <> y) : xs
convertSpace (Str x : Str y : xs) = Str (x <> y) : xs
Expand Down Expand Up @@ -219,12 +232,15 @@ writeOpenXML opts (Pandoc meta blocks) = do
(fmap (vcat . map (literal . showContent)) . blocksToOpenXML opts)
(fmap (hcat . map (literal . showContent)) . inlinesToOpenXML opts)
meta
let context = defField "body" body
. defField "toc"
let context = resetField "body" body
. resetField "toc"
(vcat (map (literal . showElement) toc))
. defField "abstract"
(vcat (map (literal . showContent) abstract))
. defField "abstract-title" abstractTitle
. resetField "title" title
. resetField "subtitle" subtitle
. resetField "author" author
. resetField "date" date
. resetField "abstract-title" abstractTitle
. resetField "abstract" abstract
$ metadata
tpl <- maybe (lift $ compileDefaultTemplate "openxml") pure $ writerTemplate opts
let rendered = render Nothing $ renderTemplate tpl context
Expand Down
Binary file modified test/docx/golden/block_quotes.docx
Binary file not shown.
Binary file modified test/docx/golden/codeblock.docx
Binary file not shown.
Binary file modified test/docx/golden/comments.docx
Binary file not shown.
Binary file modified test/docx/golden/custom_style_no_reference.docx
Binary file not shown.
Binary file modified test/docx/golden/custom_style_preserve.docx
Binary file not shown.
Binary file modified test/docx/golden/custom_style_reference.docx
Binary file not shown.
Binary file modified test/docx/golden/definition_list.docx
Binary file not shown.
Binary file modified test/docx/golden/document-properties-short-desc.docx
Binary file not shown.
Binary file modified test/docx/golden/document-properties.docx
Binary file not shown.
Binary file modified test/docx/golden/headers.docx
Binary file not shown.
Binary file modified test/docx/golden/image.docx
Binary file not shown.
Binary file modified test/docx/golden/inline_code.docx
Binary file not shown.
Binary file modified test/docx/golden/inline_formatting.docx
Binary file not shown.
Binary file modified test/docx/golden/inline_images.docx
Binary file not shown.
Binary file modified test/docx/golden/link_in_notes.docx
Binary file not shown.
Binary file modified test/docx/golden/links.docx
Binary file not shown.
Binary file modified test/docx/golden/lists.docx
Binary file not shown.
Binary file modified test/docx/golden/lists_continuing.docx
Binary file not shown.
Binary file modified test/docx/golden/lists_div_bullets.docx
Binary file not shown.
Binary file modified test/docx/golden/lists_multiple_initial.docx
Binary file not shown.
Binary file modified test/docx/golden/lists_restarting.docx
Binary file not shown.
Binary file modified test/docx/golden/nested_anchors_in_header.docx
Binary file not shown.
Binary file modified test/docx/golden/notes.docx
Binary file not shown.
Binary file modified test/docx/golden/raw-blocks.docx
Binary file not shown.
Binary file modified test/docx/golden/raw-bookmarks.docx
Binary file not shown.
Binary file modified test/docx/golden/table_one_row.docx
Binary file not shown.
Binary file modified test/docx/golden/table_with_list_cell.docx
Binary file not shown.
Binary file modified test/docx/golden/tables-default-widths.docx
Binary file not shown.
Binary file modified test/docx/golden/tables.docx
Binary file not shown.
Binary file modified test/docx/golden/tables_separated_with_rawblock.docx
Binary file not shown.
Binary file modified test/docx/golden/track_changes_deletion.docx
Binary file not shown.
Binary file modified test/docx/golden/track_changes_insertion.docx
Binary file not shown.
Binary file modified test/docx/golden/track_changes_move.docx
Binary file not shown.
Binary file modified test/docx/golden/track_changes_scrubbed_metadata.docx
Binary file not shown.
Binary file modified test/docx/golden/unicode.docx
Binary file not shown.
Binary file modified test/docx/golden/verbatim_subsuper.docx
Binary file not shown.

0 comments on commit 872d7bc

Please sign in to comment.