Skip to content

Commit

Permalink
Docx writer: better handling of chapters.
Browse files Browse the repository at this point in the history
When `--top-level-division=chapter` is used, a paragraph with
section properties is inserted before each level-1 heading.
By default, this causes the new heading to start on a new page
(though this default can be adjusted in Word).

This change should also make it possible to number footnotes
by chapter (#2773), though that change isn't yet made.
  • Loading branch information
jgm committed Dec 22, 2024
1 parent d9281d1 commit dc6a7d0
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/Text/Pandoc/Writers/Docx/OpenXML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ blockToOpenXML' opts (Div (ident,_classes,kvs) bs) = do
wrapBookmark ident $ header <> contents
blockToOpenXML' opts (Header lev (ident,_,kvs) lst) = do
setFirstPara
let isChapter = lev == 1 && writerTopLevelDivision opts == TopLevelChapter
paraProps <- withParaPropM (pStyleM (fromString $ "Heading "++show lev)) $
getParaProps False
number <-
Expand All @@ -388,14 +389,20 @@ blockToOpenXML' opts (Header lev (ident,_,kvs) lst) = do
Nothing -> return []
else return []
contents <- (number ++) <$> inlinesToOpenXML opts lst
if T.null ident
then return [Elem $ mknode "w:p" [] (map Elem paraProps ++ contents)]
else do
let bookmarkName = ident
modify $ \s -> s{ stSectionIds = Set.insert bookmarkName
$ stSectionIds s }
bookmarkedContents <- wrapBookmark bookmarkName contents
return [Elem $ mknode "w:p" [] (map Elem paraProps ++ bookmarkedContents)]
let addSectionBreak
| isChapter = (Elem (mknode "w:p" []
(mknode "w:pPr" []
[mknode "w:sectPr" [] ()])) :)
| otherwise = id
addSectionBreak <$>
if T.null ident
then return [Elem $ mknode "w:p" [] (map Elem paraProps ++ contents)]
else do
let bookmarkName = ident
modify $ \s -> s{ stSectionIds = Set.insert bookmarkName
$ stSectionIds s }
bookmarkedContents <- wrapBookmark bookmarkName contents
return [Elem $ mknode "w:p" [] (map Elem paraProps ++ bookmarkedContents)]
blockToOpenXML' opts (Plain lst) = do
isInTable <- gets stInTable
isInList <- gets stInList
Expand Down

0 comments on commit dc6a7d0

Please sign in to comment.