aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Readers/Docx/Util.hs
blob: b8c51534b6d6ab910cafab55932868cccdafbd67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Text.Pandoc.Readers.Docx.Util
   Copyright   : © 2014-2020 Jesse Rosenthal <[email protected]>,
                   2014-2024 John MacFarlane <[email protected]>,
                   2015 Nikolay Yakimov <[email protected]>
   License     : GNU GPL, version 2 or above

   Maintainer  : Jesse Rosenthal <[email protected]>
   Stability   : alpha
   Portability : portable

Docx reader utility functions.
-}
module Text.Pandoc.Readers.Docx.Util (
                                        NameSpaces
                                      , elemName
                                      , isElem
                                      , elemToNameSpaces
                                      , findChildByName
                                      , findChildrenByName
                                      , findElementByName
                                      , findAttrByName
                                      , extractChildren
                                      ) where

import Data.List (partition)
import Text.Pandoc.XML.Light
import Text.Pandoc.Readers.OOXML.Shared
  (NameSpaces, elemName, isElem, elemToNameSpaces,
   findChildByName, findChildrenByName, findElementByName, findAttrByName)


-- | Removes child elements that satisfy a given condition.
-- Returns the modified element and the list of removed children.
extractChildren :: Element -> (Element -> Bool) -> Maybe (Element, [Element])
extractChildren el condition
  | null removedChildren = Nothing  -- No children removed, return Nothing
  | otherwise = Just (modifiedElement, removedChildren)  -- Children removed, return Just
  where
    -- Separate the children based on the condition
    (removedChildren, keptChildren) = partition condition (onlyElems $ elContent el)

    -- Reconstruct the element with the kept children
    modifiedElement = el { elContent = map Elem keptChildren }