Skip to content

Commit 6fbb50f

Browse files
committed
Docx writer: repeat reference doc's sectPr for each new section.
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.
1 parent a14fe6f commit 6fbb50f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+17
-22
lines changed

data/templates/default.openxml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,6 @@ $endif$
6060
$for(include-after)$
6161
$include-after$
6262
$endfor$
63-
$-- sectpr will be set to the last sectpr in a reference.docx, if present
64-
$if(sectpr)$
6563
$sectpr$
66-
$else$
67-
<w:sectPr>
68-
<w:footnotePr>
69-
<w:numRestart w:val="eachSect" />
70-
</w:footnotePr>
71-
</w:sectPr>
72-
$endif$
7364
</w:body>
7465
</w:document>

src/Text/Pandoc/Writers/Docx.hs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,20 +239,17 @@ writeDocx opts doc = do
239239
(\q -> qName q == "id" && qPrefix q == Just "r")
240240
idMap
241241
(elChildren sectpr')
242-
in Just . ppElement $
243-
add_attrs (elAttribs sectpr') $ mknode "w:sectPr" [] cs
244-
Nothing -> Nothing
245-
242+
in add_attrs (elAttribs sectpr') $ mknode "w:sectPr" [] cs
243+
Nothing -> mknode "w:sectPr" []
244+
[ mknode "w:footnotePr" []
245+
[ mknode "w:numRestart" [("w:val","eachSect")] () ]
246+
]
246247

247248
((contents, footnotes, comments), st) <- runStateT
248249
(runReaderT
249-
(writeOpenXML opts{ writerWrapText = WrapNone
250-
, writerVariables =
251-
(maybe id (setField "sectpr") sectpr)
252-
(writerVariables opts)
253-
}
250+
(writeOpenXML opts{ writerWrapText = WrapNone }
254251
doc')
255-
env)
252+
env{ envSectPr = Just sectpr })
256253
initialSt
257254
let epochtime = floor $ utcTimeToPOSIXSeconds utctime
258255
let imgs = M.elems $ stImages st

src/Text/Pandoc/Writers/Docx/OpenXML.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ writeOpenXML opts (Pandoc meta blocks) = do
288288
meta
289289
cStyleMap <- gets (smParaStyle . stStyleMaps)
290290
let styleIdOf name = fromStyleId $ getStyleIdFromName name cStyleMap
291+
renderedSectPr <- maybe mempty ppElement <$> asks envSectPr
292+
291293
let context = resetField "body" body
292294
. resetField "toc"
293295
(vcat (map (literal . showElement) toc))
@@ -307,6 +309,7 @@ writeOpenXML opts (Pandoc meta blocks) = do
307309
. resetField "date-style-id" (styleIdOf "Date")
308310
. resetField "abstract-title-style-id" (styleIdOf "AbstractTitle")
309311
. resetField "abstract-style-id" (styleIdOf "Abstract")
312+
. resetField "sectpr" renderedSectPr
310313
$ metadata
311314
tpl <- maybe (lift $ compileDefaultTemplate "openxml") pure $ writerTemplate opts
312315
let rendered = render Nothing $ renderTemplate tpl context
@@ -392,10 +395,12 @@ blockToOpenXML' opts (Header lev (ident,_,kvs) lst) = do
392395
Nothing -> return []
393396
else return []
394397
contents <- (number ++) <$> inlinesToOpenXML opts lst
398+
sectpr <- asks envSectPr
395399
let addSectionBreak
396-
| isSection = (Elem (mknode "w:p" []
397-
(mknode "w:pPr" []
398-
[mknode "w:sectPr" [] ()])) :)
400+
| isSection
401+
, Just sectPrElem <- sectpr
402+
= (Elem (mknode "w:p" []
403+
(mknode "w:pPr" [] [sectPrElem])) :)
399404
| otherwise = id
400405
addSectionBreak <$>
401406
if T.null ident

src/Text/Pandoc/Writers/Docx/Types.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ data WriterEnv = WriterEnv
8989
, envChangesDate :: Text
9090
, envPrintWidth :: Integer
9191
, envLang :: Maybe Text
92+
, envSectPr :: Maybe Element
9293
}
9394

9495
defaultWriterEnv :: WriterEnv
@@ -104,6 +105,7 @@ defaultWriterEnv = WriterEnv
104105
, envChangesDate = "1969-12-31T19:00:00Z"
105106
, envPrintWidth = 1
106107
, envLang = Nothing
108+
, envSectPr = Nothing
107109
}
108110

109111

test/docx/golden/block_quotes.docx

8 Bytes
Binary file not shown.

test/docx/golden/codeblock.docx

8 Bytes
Binary file not shown.

test/docx/golden/comments.docx

9 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.
73 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)