@@ -31,6 +31,7 @@ module Text.Pandoc.Shared (
31 -- * List processing
31 -- * List processing
33 splitByIndices,
33 splitByIndices,
34 -- * Text processing
35 -- * Text processing
36 joinWithSep,
37 joinWithSep,
@@ -79,8 +80,8 @@ import Text.Pandoc.Entities ( decodeEntities, encodeEntities, characterEntity )
79import Text.Regex ( matchRegexAll, mkRegex, subRegex, Regex )
80import Text.Regex ( matchRegexAll, mkRegex, subRegex, Regex )
80import Text.PrettyPrint.HughesPJ as PP ( text, char, (<>), ($$), nest, Doc,
81import Text.PrettyPrint.HughesPJ as PP ( text, char, (<>), ($$), nest, Doc,
82−import Char ( toLower )
83+import Data.Char ( toLower )
83−import List ( find, groupBy )
84+import Data.List ( find, groupBy, isPrefixOf )
85-- | Parse a string with a given parser and state.
86-- | Parse a string with a given parser and state.
86readWith :: GenParser Char ParserState a -- ^ parser
87readWith :: GenParser Char ParserState a -- ^ parser
@@ -288,6 +289,15 @@ removeTrailingSpace = reverse . removeLeadingSpace . reverse
288stripFirstAndLast str =
289stripFirstAndLast str =
289 drop 1 $ take ((length str) - 1) str
290 drop 1 $ take ((length str) - 1) str
292+-- | Replace each occurrence of one sublist in a list with another.
293+substitute :: (Eq a) => [a] -> [a] -> [a] -> [a]
294+substitute _ _ [] = []
295+substitute [] _ lst = lst
296+substitute target replacement lst =
297+ if isPrefixOf target lst
298+ then replacement ++ (substitute target replacement $ drop (length target) lst)
299+ else (head lst):(substitute target replacement $ tail lst)
291-- | Split list into groups separated by sep.
301-- | Split list into groups separated by sep.
292splitBy :: (Eq a) => a -> [a] -> [[a]]
302splitBy :: (Eq a) => a -> [a] -> [[a]]
293splitBy _ [] = []
303splitBy _ [] = []