on master help sign in
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.

commitff57c2f660d604bc6fcae104e108f8d991a48906 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateSun 4 Apr 2010, 15:08 (17 years ago) parent681d46c
3 files changed+39−11
hakyll.cabal+1−1 src/Text/Hakyll/Internal/Page.hs+20−10 tests/Page.hs+18
hakyll.cabal+1−1
@@ -1,5 +1,5 @@
11Name: hakyll
2−Version: 2.0
2+Version: 2.1
33
44Synopsis: A simple static site generator library.
55Description: A simple static site generator library, mainly aimed at
src/Text/Hakyll/Internal/Page.hs+20−10
@@ -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
tests/Page.hs+18
@@ -23,6 +23,7 @@ pageGroup = testGroup "Page"
2323 [ testCase "test_readPage_1" test_readPage_1
2424 , testCase "test_readPage_2" test_readPage_2
2525 , testCase "test_readPage_3" test_readPage_3
26+ , testCase "test_readPage_4" test_readPage_4
2627 ]
2728
2829-- | An abstract function to test page reading.
@@ -68,3 +69,20 @@ test_readPage_3 = test_readPage fileName content assertion @? "test_readPage_3"
6869 content = unlines [ "No metadata here, sorry."
6970 ]
7071 assertion page = M.lookup "body" page == Just "No metadata here, sorry.\n"
72+
73+-- | readPage test case 4.
74+test_readPage_4 = test_readPage fileName content assertion @? "test_readPage_4"
75+ where
76+ fileName = "test_readPage_4.txt"
77+ content = unlines [ "--- section"
78+ , "This is a section."
79+ , "---"
80+ , "Header"
81+ , "------"
82+ , "The header is not a separate section."
83+ ]
84+ assertion page = M.lookup "body" page == Just body
85+ body = unlines [ "Header"
86+ , "------"
87+ , "The header is not a separate section."
88+ ]