Skip to content

Commit

Permalink
Docx: single numbering ID for examples - fixes #7895 (#7900)
Browse files Browse the repository at this point in the history
This change ensures that example list items all belong to a single
number sequence, so that if items are added or deleted in a word
processor, the other items will renumber automatically.
  • Loading branch information
mjfs authored Feb 9, 2022
1 parent 93b1dbf commit 63deba4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Text/Pandoc/Writers/Docx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Data.Char (isSpace, isLetter)
import Data.List (intercalate, isPrefixOf, isSuffixOf)
import Data.String (fromString)
import qualified Data.Map as M
import Data.Maybe (fromMaybe, isNothing, mapMaybe, maybeToList)
import Data.Maybe (fromMaybe, isNothing, mapMaybe, maybeToList, isJust)
import qualified Data.Set as Set
import qualified Data.Text as T
import Data.Text (Text)
Expand Down Expand Up @@ -942,7 +942,10 @@ blockToOpenXML' opts el
addOpenXMLList marker lst = do
addList marker
numid <- getNumId
l <- asList $ concat `fmap` mapM (listItemToOpenXML opts numid) lst
exampleid <- case marker of
NumberMarker Example _ _ -> gets stExampleId
_ -> return Nothing
l <- asList $ concat `fmap` mapM (listItemToOpenXML opts $ fromMaybe numid exampleid) lst
setFirstPara
return l
blockToOpenXML' opts (DefinitionList items) = do
Expand All @@ -963,7 +966,16 @@ definitionListItemToOpenXML opts (term,defs) = do
addList :: (PandocMonad m) => ListMarker -> WS m ()
addList marker = do
lists <- gets stLists
modify $ \st -> st{ stLists = lists ++ [marker] }
lastExampleId <- gets stExampleId
modify $ \st -> st{ stLists = lists ++ case marker of
-- Use only first occurence of Example for list declaration to avoid overhead
NumberMarker Example _ _ | isJust lastExampleId -> []
_ -> [marker]
, stExampleId = case marker of
-- Reuse the same identifier for all other occurences of Example
NumberMarker Example _ _ -> lastExampleId <|> Just (baseListId + length lists)
_ -> lastExampleId
}

listItemToOpenXML :: (PandocMonad m)
=> WriterOptions
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 @@ -107,6 +107,7 @@ data WriterState = WriterState{
, stExternalLinks :: M.Map Text Text
, stImages :: M.Map FilePath (String, String, Maybe MimeType, B.ByteString)
, stLists :: [ListMarker]
, stExampleId :: Maybe Int
, stInsId :: Int
, stDelId :: Int
, stStyleMaps :: StyleMaps
Expand All @@ -131,6 +132,7 @@ defaultWriterState = WriterState{
, stExternalLinks = M.empty
, stImages = M.empty
, stLists = [NoMarker]
, stExampleId = Nothing
, stInsId = 1
, stDelId = 1
, stStyleMaps = StyleMaps M.empty M.empty
Expand Down

0 comments on commit 63deba4

Please sign in to comment.