@@ -8,6 +8,7 @@ import Data.List (isPrefixOf)
88import Data.Char (isSpace)
99import Control.Monad.Reader (liftIO)
1010import System.FilePath
11+import Control.Monad.State (State, evalState, get, put)
1112
1213import Text.Pandoc
1314
@@ -53,16 +54,25 @@ getRenderFunction fileType = writeHtmlString writerOptions
5354 readOptions _ = readerOptions
5455
5556-- | Split a page into sections.
56−splitAtDelimiters :: [String] -> [[String]]
57−splitAtDelimiters [] = []
58−splitAtDelimiters ls@(x:xs)
59− | isDelimiter x = let (content, rest) = break isDelimiter xs
60− in (x : content) : splitAtDelimiters rest
61− | otherwise = [ls]
57+splitAtDelimiters :: [String] -> State (Maybe String) [[String]]
58+splitAtDelimiters [] = return []
59+splitAtDelimiters ls@(x:xs) = do
60+ delimiter <- get
61+ if not (isDelimiter delimiter x)
62+ then return [ls]
63+ else do let proper = takeWhile (== '-') x
64+ (content, rest) = break (isDelimiter $ Just proper) xs
65+ put $ Just proper
66+ rest' <- splitAtDelimiters rest
67+ return $ (x : content) : rest'
68+ where
69+ isDelimiter old = case old of
70+ Nothing -> isPossibleDelimiter
71+ (Just d) -> (== d) . takeWhile (== '-')
6272
6373-- | Check if the given string is a metadata delimiter.
64−isDelimiter :: String -> Bool
65−isDelimiter = isPrefixOf "---"
74+isPossibleDelimiter :: String -> Bool
75+isPossibleDelimiter = isPrefixOf "---"
6676
6777-- | Read one section of a page.
6878readSection :: (String -> String) -- ^ Render function.
@@ -76,7 +86,7 @@ readSection renderFunction isFirst ls
7686 | isFirst = readSimpleMetaData (tail ls)
7787 | otherwise = body (tail ls)
7888 where
79− isDelimiter' = isDelimiter (head ls)
89+ isDelimiter' = isPossibleDelimiter (head ls)
8090 isNamedDelimiter = head ls `matchesRegex` "^----* *[a-zA-Z0-9][a-zA-Z0-9]*"
8191 body ls' = [("body", renderFunction $ unlines ls')]
8292
@@ -100,7 +110,7 @@ readPageFromFile path = do
100110 -- Read file.
101111 contents <- liftIO $ readFile path
102112 url <- toUrl path
103− let sections = splitAtDelimiters $ lines contents
113+ let sections = evalState (splitAtDelimiters $ lines contents) Nothing
104114 sectionsData = concat $ zipWith ($) sectionFunctions sections
105115 context = M.fromList $
106116 ("url", url) : ("path", path) : category ++ sectionsData