Skip to content

Commit

Permalink
Docx writer: repeat reference doc's sectPr for each new section.
Browse files Browse the repository at this point in the history
Previously we were only carrying over the reference doc's sectPr
at the end of the document, so it wouldn't affect the intermediate
sections that are now added if `--top-level-division` is `chapter`
or `part`. This could lead to bad results (e.g. page numbering
starting only on the last chapter).

Closes #10577.
  • Loading branch information
jgm committed Jan 31, 2025
1 parent a14fe6f commit 6fbb50f
Show file tree
Hide file tree
Showing 42 changed files with 17 additions and 22 deletions.
9 changes: 0 additions & 9 deletions data/templates/default.openxml
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,6 @@ $endif$
$for(include-after)$
$include-after$
$endfor$
$-- sectpr will be set to the last sectpr in a reference.docx, if present
$if(sectpr)$
$sectpr$
$else$
<w:sectPr>
<w:footnotePr>
<w:numRestart w:val="eachSect" />
</w:footnotePr>
</w:sectPr>
$endif$
</w:body>
</w:document>
17 changes: 7 additions & 10 deletions src/Text/Pandoc/Writers/Docx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,17 @@ writeDocx opts doc = do
(\q -> qName q == "id" && qPrefix q == Just "r")
idMap
(elChildren sectpr')
in Just . ppElement $
add_attrs (elAttribs sectpr') $ mknode "w:sectPr" [] cs
Nothing -> Nothing

in add_attrs (elAttribs sectpr') $ mknode "w:sectPr" [] cs
Nothing -> mknode "w:sectPr" []
[ mknode "w:footnotePr" []
[ mknode "w:numRestart" [("w:val","eachSect")] () ]
]

((contents, footnotes, comments), st) <- runStateT
(runReaderT
(writeOpenXML opts{ writerWrapText = WrapNone
, writerVariables =
(maybe id (setField "sectpr") sectpr)
(writerVariables opts)
}
(writeOpenXML opts{ writerWrapText = WrapNone }
doc')
env)
env{ envSectPr = Just sectpr })
initialSt
let epochtime = floor $ utcTimeToPOSIXSeconds utctime
let imgs = M.elems $ stImages st
Expand Down
11 changes: 8 additions & 3 deletions src/Text/Pandoc/Writers/Docx/OpenXML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ writeOpenXML opts (Pandoc meta blocks) = do
meta
cStyleMap <- gets (smParaStyle . stStyleMaps)
let styleIdOf name = fromStyleId $ getStyleIdFromName name cStyleMap
renderedSectPr <- maybe mempty ppElement <$> asks envSectPr

let context = resetField "body" body
. resetField "toc"
(vcat (map (literal . showElement) toc))
Expand All @@ -307,6 +309,7 @@ writeOpenXML opts (Pandoc meta blocks) = do
. resetField "date-style-id" (styleIdOf "Date")
. resetField "abstract-title-style-id" (styleIdOf "AbstractTitle")
. resetField "abstract-style-id" (styleIdOf "Abstract")
. resetField "sectpr" renderedSectPr
$ metadata
tpl <- maybe (lift $ compileDefaultTemplate "openxml") pure $ writerTemplate opts
let rendered = render Nothing $ renderTemplate tpl context
Expand Down Expand Up @@ -392,10 +395,12 @@ blockToOpenXML' opts (Header lev (ident,_,kvs) lst) = do
Nothing -> return []
else return []
contents <- (number ++) <$> inlinesToOpenXML opts lst
sectpr <- asks envSectPr
let addSectionBreak
| isSection = (Elem (mknode "w:p" []
(mknode "w:pPr" []
[mknode "w:sectPr" [] ()])) :)
| isSection
, Just sectPrElem <- sectpr
= (Elem (mknode "w:p" []
(mknode "w:pPr" [] [sectPrElem])) :)
| otherwise = id
addSectionBreak <$>
if T.null ident
Expand Down
2 changes: 2 additions & 0 deletions src/Text/Pandoc/Writers/Docx/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ data WriterEnv = WriterEnv
, envChangesDate :: Text
, envPrintWidth :: Integer
, envLang :: Maybe Text
, envSectPr :: Maybe Element
}

defaultWriterEnv :: WriterEnv
Expand All @@ -104,6 +105,7 @@ defaultWriterEnv = WriterEnv
, envChangesDate = "1969-12-31T19:00:00Z"
, envPrintWidth = 1
, envLang = Nothing
, envSectPr = Nothing
}


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_9994.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/task_list.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 6fbb50f

Please sign in to comment.