diff options
author | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2010-04-04 15:08:59 +0200 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2010-04-04 15:08:59 +0200 |
commit | ff57c2f660d604bc6fcae104e108f8d991a48906 (patch) | |
tree | 949def7b49d3ab08e5c6610fceb1b64e802861b0 | |
parent | 681d46c8ffd643710bff3dcbba29d2b18d8ed12c (diff) | |
download | hakyll-ff57c2f660d604bc6fcae104e108f8d991a48906.tar.gz |
Added fix to allow dashes in pages.
Hakyll splits pages into metadata sections by lines starting
with `---`. Because of this, thing like
Header
------
Some text.
will not parse. This commit fixes this, by fixating the length
of the the dash pattern -- all dashed lines should contain the
same number of dashes. Therefor, you can use something like
---
title: Foobar
---
Header
------
Some text.
-rw-r--r-- | hakyll.cabal | 2 | ||||
-rw-r--r-- | src/Text/Hakyll/Internal/Page.hs | 30 | ||||
-rw-r--r-- | tests/Page.hs | 18 |
3 files changed, 39 insertions, 11 deletions
diff --git a/hakyll.cabal b/hakyll.cabal index ab53efd..6babb9d 100644 --- a/hakyll.cabal +++ b/hakyll.cabal @@ -1,5 +1,5 @@ Name: hakyll -Version: 2.0 +Version: 2.1 Synopsis: A simple static site generator library. Description: A simple static site generator library, mainly aimed at diff --git a/src/Text/Hakyll/Internal/Page.hs b/src/Text/Hakyll/Internal/Page.hs index 0ba8983..d3d0ec1 100644 --- a/src/Text/Hakyll/Internal/Page.hs +++ b/src/Text/Hakyll/Internal/Page.hs @@ -8,6 +8,7 @@ import Data.List (isPrefixOf) import Data.Char (isSpace) import Control.Monad.Reader (liftIO) import System.FilePath +import Control.Monad.State (State, evalState, get, put) import Text.Pandoc @@ -53,16 +54,25 @@ getRenderFunction fileType = writeHtmlString writerOptions readOptions _ = readerOptions -- | Split a page into sections. -splitAtDelimiters :: [String] -> [[String]] -splitAtDelimiters [] = [] -splitAtDelimiters ls@(x:xs) - | isDelimiter x = let (content, rest) = break isDelimiter xs - in (x : content) : splitAtDelimiters rest - | otherwise = [ls] +splitAtDelimiters :: [String] -> State (Maybe String) [[String]] +splitAtDelimiters [] = return [] +splitAtDelimiters ls@(x:xs) = do + delimiter <- get + if not (isDelimiter delimiter x) + then return [ls] + else do let proper = takeWhile (== '-') x + (content, rest) = break (isDelimiter $ Just proper) xs + put $ Just proper + rest' <- splitAtDelimiters rest + return $ (x : content) : rest' + where + isDelimiter old = case old of + Nothing -> isPossibleDelimiter + (Just d) -> (== d) . takeWhile (== '-') -- | Check if the given string is a metadata delimiter. -isDelimiter :: String -> Bool -isDelimiter = isPrefixOf "---" +isPossibleDelimiter :: String -> Bool +isPossibleDelimiter = isPrefixOf "---" -- | Read one section of a page. readSection :: (String -> String) -- ^ Render function. @@ -76,7 +86,7 @@ readSection renderFunction isFirst ls | isFirst = readSimpleMetaData (tail ls) | otherwise = body (tail ls) where - isDelimiter' = isDelimiter (head ls) + isDelimiter' = isPossibleDelimiter (head ls) isNamedDelimiter = head ls `matchesRegex` "^----* *[a-zA-Z0-9][a-zA-Z0-9]*" body ls' = [("body", renderFunction $ unlines ls')] @@ -100,7 +110,7 @@ readPageFromFile path = do -- Read file. contents <- liftIO $ readFile path url <- toUrl path - let sections = splitAtDelimiters $ lines contents + let sections = evalState (splitAtDelimiters $ lines contents) Nothing sectionsData = concat $ zipWith ($) sectionFunctions sections context = M.fromList $ ("url", url) : ("path", path) : category ++ sectionsData diff --git a/tests/Page.hs b/tests/Page.hs index ae1fcd9..1b80578 100644 --- a/tests/Page.hs +++ b/tests/Page.hs @@ -23,6 +23,7 @@ pageGroup = testGroup "Page" [ testCase "test_readPage_1" test_readPage_1 , testCase "test_readPage_2" test_readPage_2 , testCase "test_readPage_3" test_readPage_3 + , testCase "test_readPage_4" test_readPage_4 ] -- | An abstract function to test page reading. @@ -68,3 +69,20 @@ test_readPage_3 = test_readPage fileName content assertion @? "test_readPage_3" content = unlines [ "No metadata here, sorry." ] assertion page = M.lookup "body" page == Just "No metadata here, sorry.\n" + +-- | readPage test case 4. +test_readPage_4 = test_readPage fileName content assertion @? "test_readPage_4" + where + fileName = "test_readPage_4.txt" + content = unlines [ "--- section" + , "This is a section." + , "---" + , "Header" + , "------" + , "The header is not a separate section." + ] + assertion page = M.lookup "body" page == Just body + body = unlines [ "Header" + , "------" + , "The header is not a separate section." + ] |