aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTuong Nguyen Manh <[email protected]>2025-12-13 22:28:59 +0100
committerGitHub <[email protected]>2025-12-13 22:28:59 +0100
commite8ed40ef01efdba315ec995c99da31a55bf55c57 (patch)
tree0b788bbba1f1fe25d693eafb7fd6c9add655518f /src
parent5673316d4c4b54aea491ec1ead8e54d7788102a8 (diff)
DocBook reader: Fix adding wrong metadata (#11347)
Now keep track of the current element stack to only add metadata if inside an appropriate parent element. Closes #11300.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Readers/DocBook.hs21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/Text/Pandoc/Readers/DocBook.hs b/src/Text/Pandoc/Readers/DocBook.hs
index 38e67e87e..66128b12e 100644
--- a/src/Text/Pandoc/Readers/DocBook.hs
+++ b/src/Text/Pandoc/Readers/DocBook.hs
@@ -548,6 +548,7 @@ data DBState = DBState{ dbSectionLevel :: Int
, dbBook :: Bool
, dbContent :: [Content]
, dbLiteralLayout :: Bool
+ , dbElementStack :: [Text]
} deriving Show
instance Default DBState where
@@ -556,7 +557,9 @@ instance Default DBState where
, dbMeta = mempty
, dbBook = False
, dbContent = []
- , dbLiteralLayout = False }
+ , dbLiteralLayout = False
+ , dbElementStack = []
+ }
readDocBook :: (PandocMonad m, ToSources a)
@@ -621,9 +624,13 @@ named s e = qName (elName e) == s
--
addMetadataFromElement :: PandocMonad m => Element -> DB m Blocks
-addMetadataFromElement e =
- mempty <$ mapM_ handleMetadataElement
+addMetadataFromElement e = do
+ -- Add metadata if at root or appropriate parent element
+ elementStack <- gets dbElementStack
+ if take 1 elementStack `elem` [[], ["book"], ["article"]]
+ then mempty <$ mapM_ handleMetadataElement
(filterChildren ((isMetadataField . qName . elName)) e)
+ else return mempty
where
handleMetadataElement elt =
case qName (elName elt) of
@@ -853,9 +860,11 @@ getMediaobject e = do
fmap (imageWith attr imageUrl tit) capt
getBlocks :: PandocMonad m => Element -> DB m Blocks
-getBlocks e = mconcat <$>
- mapM parseBlock (elContent e)
-
+getBlocks e = do
+ modify (\st -> st{ dbElementStack = qName (elName e) : dbElementStack st })
+ blocks <- mconcat <$> mapM parseBlock (elContent e)
+ modify (\st -> st{ dbElementStack = drop 1 $ dbElementStack st })
+ return blocks
parseBlock :: PandocMonad m => Content -> DB m Blocks
parseBlock (Text (CData CDataRaw _ _)) = return mempty -- DOCTYPE