@@ -32,6 +32,7 @@ module Text.Pandoc.Shared (
3232 -- * List processing
3333 splitBy,
3434 splitByIndices,
35+ splitStringByIndices,
3536 substitute,
3637 -- * Text processing
3738 backslashEscapes,
@@ -82,6 +83,7 @@ import Data.Generics (Typeable, Data)
8283import qualified Control.Monad.State as S
8384import Paths_pandoc (getDataFileName)
8485import Text.Pandoc.Highlighting (Style, pygments)
86+import Text.Pandoc.Pretty (charWidth)
8587
8688--
8789-- List processing
@@ -95,12 +97,23 @@ splitBy isSep lst =
9597 rest' = dropWhile isSep rest
9698 in first:(splitBy isSep rest')
9799
98−-- | Split list into chunks divided at specified indices.
99100splitByIndices :: [Int] -> [a] -> [[a]]
100101splitByIndices [] lst = [lst]
101−splitByIndices (x:xs) lst =
102− let (first, rest) = splitAt x lst in
103− first:(splitByIndices (map (\y -> y - x) xs) rest)
102+splitByIndices (x:xs) lst = first:(splitByIndices (map (\y -> y - x) xs) rest)
103+ where (first, rest) = splitAt x lst
104+
105+-- | Split string into chunks divided at specified indices.
106+splitStringByIndices :: [Int] -> [Char] -> [[Char]]
107+splitStringByIndices [] lst = [lst]
108+splitStringByIndices (x:xs) lst =
109+ let (first, rest) = splitAt' x lst in
110+ first : (splitStringByIndices (map (\y -> y - x) xs) rest)
111+
112+splitAt' :: Int -> [Char] -> ([Char],[Char])
113+splitAt' _ [] = ([],[])
114+splitAt' n xs | n <= 0 = ([],xs)
115+splitAt' n (x:xs) = (x:ys,zs)
116+ where (ys,zs) = splitAt' (n - charWidth x) xs
104117
105118-- | Replace each occurrence of one sublist in a list with another.
106119substitute :: (Eq a) => [a] -> [a] -> [a] -> [a]