on master help sign in
Clean up yesterday's work, fix tests
371bc03Jasper Van der Jeugt
src/Text/Hakyll/Page.hs+23−16
@@ -17,9 +17,16 @@ import Text.Hakyll.HakyllAction
17import Text.Hakyll.Regex (substituteRegex, matchesRegex)
17import Text.Hakyll.Regex (substituteRegex, matchesRegex)
18import Text.Hakyll.Util (trim)
18import Text.Hakyll.Util (trim)
19
19
20−-- | Page info handle: (key, value, needs rendering)
20+-- | A page is first parsed into a number of page sections. A page section
21+-- consists of:
21--
22--
23+-- * A key
24+--
25+-- * A value
26+--
27+-- * A 'Bool' flag, indicating if the value is applicable for rendering
28+--
22−data PageSection = PageSection {unPageSection :: [(String, String, Bool)]}
29+data PageSection = PageSection {unPageSection :: (String, String, Bool)}
23 deriving (Show)
30 deriving (Show)
24
31
25-- | Split a page into sections.
32-- | Split a page into sections.
@@ -48,26 +55,26 @@ isPossibleDelimiter = isPrefixOf "---"
48--
55--
49readSection :: Bool -- ^ If this section is the first section in the page.
56readSection :: Bool -- ^ If this section is the first section in the page.
50 -> [String] -- ^ Lines in the section.
57 -> [String] -- ^ Lines in the section.
51− -> PageSection -- ^ Key-values extracted.
58+ -> [PageSection] -- ^ Key-values extracted.
52−readSection _ [] = PageSection []
59+readSection _ [] = []
53readSection isFirst ls
60readSection isFirst ls
54− | not isDelimiter' = body ls
61+ | not isDelimiter' = [body ls]
55− | isNamedDelimiter = PageSection $ readSectionMetaData ls
62+ | isNamedDelimiter = readSectionMetaData ls
56− | isFirst = PageSection $ readSimpleMetaData (drop 1 ls)
63+ | isFirst = readSimpleMetaData (drop 1 ls)
57− | otherwise = body (drop 1 ls)
64+ | otherwise = [body (drop 1 ls)]
58 where
65 where
59 isDelimiter' = isPossibleDelimiter (head ls)
66 isDelimiter' = isPossibleDelimiter (head ls)
60 isNamedDelimiter = head ls `matchesRegex` "^----* *[a-zA-Z0-9][a-zA-Z0-9]*"
67 isNamedDelimiter = head ls `matchesRegex` "^----* *[a-zA-Z0-9][a-zA-Z0-9]*"
61− body ls' = PageSection [("body", unlines ls', True)]
68+ body ls' = PageSection ("body", unlines ls', True)
62
69
63 readSimpleMetaData = map readPair . filter (not . all isSpace)
70 readSimpleMetaData = map readPair . filter (not . all isSpace)
64 readPair = trimPair . break (== ':')
71 readPair = trimPair . break (== ':')
65− trimPair (key, value) = (trim key, trim (drop 1 value), False)
72+ trimPair (key, value) = PageSection (trim key, trim (drop 1 value), False)
66
73
67 readSectionMetaData [] = []
74 readSectionMetaData [] = []
68 readSectionMetaData (header:value) =
75 readSectionMetaData (header:value) =
69 let key = substituteRegex "[^a-zA-Z0-9]" "" header
76 let key = substituteRegex "[^a-zA-Z0-9]" "" header
70− in [(key, unlines value, True)]
77+ in [PageSection (key, unlines value, True)]
71
78
72-- | Read a page from a file. Metadata is supported.
79-- | Read a page from a file. Metadata is supported.
73--
80--
@@ -79,14 +86,14 @@ readPage path = do
79 contents <- liftIO $ readFile path
86 contents <- liftIO $ readFile path
80 url <- toUrl path
87 url <- toUrl path
81 let sections = evalState (splitAtDelimiters $ lines contents) Nothing
88 let sections = evalState (splitAtDelimiters $ lines contents) Nothing
82− sectionsData = zipWith ($) sectionFunctions sections
89+ sectionsData = concat $ zipWith ($) sectionFunctions sections
83
90
84− return $ PageSection [ ("url", url, False)
91+ return $ PageSection ("url", url, False)
85− , ("path", path, False)
92+ : PageSection ("path", path, False)
86− ] : category : sectionsData
93+ : (category ++ sectionsData)
87 where
94 where
88 category = let dirs = splitDirectories $ takeDirectory path
95 category = let dirs = splitDirectories $ takeDirectory path
89− in PageSection [("category", last dirs, False) | not (null dirs)]
96+ in [PageSection ("category", last dirs, False) | not (null dirs)]
90
97
91-- | Read a page from a file. Metadata is supported.
98-- | Read a page from a file. Metadata is supported.
92--
99--
src/Text/Hakyll/Pandoc.hs+2−2
@@ -38,11 +38,11 @@ getRenderFunction fileType = do
38 { stateLiterateHaskell = True }
38 { stateLiterateHaskell = True }
39 readOptions options _ = options
39 readOptions options _ = options
40
40
41−-- | Path must be there
41+-- | An action that renders the list of page sections to a context using pandoc
42--
42--
43renderAction :: HakyllAction [PageSection] Context
43renderAction :: HakyllAction [PageSection] Context
44renderAction = createHakyllAction $ \sections -> do
44renderAction = createHakyllAction $ \sections -> do
45− let triples = unPageSection =<< sections
45+ let triples = map unPageSection sections
46 path = fromMaybe "unknown" $ lookup "path"
46 path = fromMaybe "unknown" $ lookup "path"
47 $ map (\(x, y, _) -> (x, y))
47 $ map (\(x, y, _) -> (x, y))
48 $ triples
48 $ triples
tests/Main.hs+1
@@ -12,6 +12,7 @@ import Template
12import Util
12import Util
13
13
14-- | Run all tests.
14-- | Run all tests.
15+main :: IO ()
15main = defaultMain [ compressCssGroup
16main = defaultMain [ compressCssGroup
16 , fileGroup
17 , fileGroup
17 , pageGroup
18 , pageGroup
tests/Page.hs+3−2
@@ -13,7 +13,8 @@ import Test.HUnit
13import System.Directory (getTemporaryDirectory, removeFile)
13import System.Directory (getTemporaryDirectory, removeFile)
14import System.FilePath ((</>))
14import System.FilePath ((</>))
15
15
16−import Text.Hakyll.Internal.Page
16+import Text.Hakyll.Page
17+import Text.Hakyll.Pandoc
17import Text.Hakyll.Context
18import Text.Hakyll.Context
18import Text.Hakyll.HakyllAction
19import Text.Hakyll.HakyllAction
19import Text.Hakyll
20import Text.Hakyll
@@ -35,7 +36,7 @@ test_readPage fileName content assertion = do
35 temporaryDir <- getTemporaryDirectory
36 temporaryDir <- getTemporaryDirectory
36 let temporaryFile = temporaryDir </> fileName
37 let temporaryFile = temporaryDir </> fileName
37 writeFile temporaryFile content
38 writeFile temporaryFile content
38− page <- runDefaultHakyll (readPage temporaryFile)
39+ page <- runDefaultHakyll (runHakyllAction $ createPage temporaryFile)
39 removeFile temporaryFile
40 removeFile temporaryFile
40 return $ assertion page
41 return $ assertion page
41
42