From 1e84178431b3fc18de92b86c7f09f4908d955a92 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Wed, 22 Jul 2020 10:33:33 -0700
Subject: Docx writer: support --number-sections.

Closes #1413.
---
 src/Text/Pandoc/Writers/Docx.hs | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

(limited to 'src')

diff --git a/src/Text/Pandoc/Writers/Docx.hs b/src/Text/Pandoc/Writers/Docx.hs
index 89f100720..f448c4ce2 100644
--- a/src/Text/Pandoc/Writers/Docx.hs
+++ b/src/Text/Pandoc/Writers/Docx.hs
@@ -210,8 +210,11 @@ writeDocx :: (PandocMonad m)
           => WriterOptions  -- ^ Writer options
           -> Pandoc         -- ^ Document to convert
           -> m BL.ByteString
-writeDocx opts doc@(Pandoc meta _) = do
-  let doc' = walk fixDisplayMath doc
+writeDocx opts doc = do
+  let Pandoc meta blocks = walk fixDisplayMath doc
+  let blocks' = makeSections True Nothing blocks
+  let doc' = Pandoc meta blocks'
+
   username <- P.lookupEnv "USERNAME"
   utctime <- P.getCurrentTime
   oldUserDataDir <- P.getUserDataDir
@@ -898,11 +901,21 @@ blockToOpenXML' opts (Div (ident,_classes,kvs) bs) = do
   header <- dirmod $ stylemod $ blocksToOpenXML opts hs
   contents <- dirmod $ bibmod $ stylemod $ blocksToOpenXML opts bs'
   wrapBookmark ident $ header <> contents
-blockToOpenXML' opts (Header lev (ident,_,_) lst) = do
+blockToOpenXML' opts (Header lev (ident,_,kvs) lst) = do
   setFirstPara
   paraProps <- withParaPropM (pStyleM (fromString $ "Heading "++show lev)) $
                     getParaProps False
-  contents <- inlinesToOpenXML opts lst
+  number <-
+        if writerNumberSections opts
+           then
+             case lookup "number" kvs of
+                Just n -> do
+                   num <- withTextPropM (rStyleM "SectionNumber")
+                            (inlineToOpenXML opts (Str n))
+                   return $ num ++ [mknode "w:r" [] [mknode "w:tab" [] ()]]
+                Nothing -> return []
+           else return []
+  contents <- (number ++) <$> inlinesToOpenXML opts lst
   if T.null ident
      then return [mknode "w:p" [] (paraProps ++ contents)]
      else do
-- 
cgit v1.2.3