Docx: single numbering ID for examples - fixes #7895 (#7900)

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.
This commit is contained in:
mjfs 2022-02-09 19:15:01 +01:00 committed by GitHub
parent 93b1dbfdac
commit 63deba49d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View file

@ -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)
@ -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
@ -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

View file

@ -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
@ -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