@@ -31,6 +31,7 @@ module Text.Pandoc.Shared (
3131 -- * List processing
3232 splitBy,
3333 splitByIndices,
34+ substitute,
3435 -- * Text processing
3536 gsub,
3637 joinWithSep,
@@ -79,8 +80,8 @@ import Text.Pandoc.Entities ( decodeEntities, encodeEntities, characterEntity )
7980import Text.Regex ( matchRegexAll, mkRegex, subRegex, Regex )
8081import Text.PrettyPrint.HughesPJ as PP ( text, char, (<>), ($$), nest, Doc,
8182 isEmpty )
82−import Char ( toLower )
83−import List ( find, groupBy )
83+import Data.Char ( toLower )
84+import Data.List ( find, groupBy, isPrefixOf )
8485
8586-- | Parse a string with a given parser and state.
8687readWith :: GenParser Char ParserState a -- ^ parser
@@ -288,6 +289,15 @@ removeTrailingSpace = reverse . removeLeadingSpace . reverse
288289stripFirstAndLast str =
289290 drop 1 $ take ((length str) - 1) str
290291
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)
300+
291301-- | Split list into groups separated by sep.
292302splitBy :: (Eq a) => a -> [a] -> [[a]]
293303splitBy _ [] = []