on master help sign in
Clean up yesterday's work, fix tests
commit371bc0347b21ff6693f9badeae4962433d42cc55 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateMon 4 Oct 2010, 14:14 (16 years ago) parentc73c092
4 files changed+29−20
src/Text/Hakyll/Page.hs+23−16 src/Text/Hakyll/Pandoc.hs+2−2 tests/Main.hs+1 tests/Page.hs+3−2
src/Text/Hakyll/Page.hs+23−16
@@ -17,9 +17,16 @@ import Text.Hakyll.HakyllAction
1717import Text.Hakyll.Regex (substituteRegex, matchesRegex)
1818import Text.Hakyll.Util (trim)
1919
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:
2122--
22−data PageSection = PageSection {unPageSection :: [(String, String, Bool)]}
23+-- * A key
24+--
25+-- * A value
26+--
27+-- * A 'Bool' flag, indicating if the value is applicable for rendering
28+--
29+data PageSection = PageSection {unPageSection :: (String, String, Bool)}
2330 deriving (Show)
2431
2532-- | Split a page into sections.
@@ -48,26 +55,26 @@ isPossibleDelimiter = isPrefixOf "---"
4855--
4956readSection :: Bool -- ^ If this section is the first section in the page.
5057 -> [String] -- ^ Lines in the section.
51− -> PageSection -- ^ Key-values extracted.
52−readSection _ [] = PageSection []
58+ -> [PageSection] -- ^ Key-values extracted.
59+readSection _ [] = []
5360readSection isFirst ls
54− | not isDelimiter' = body ls
55− | isNamedDelimiter = PageSection $ readSectionMetaData ls
56− | isFirst = PageSection $ readSimpleMetaData (drop 1 ls)
57− | otherwise = body (drop 1 ls)
61+ | not isDelimiter' = [body ls]
62+ | isNamedDelimiter = readSectionMetaData ls
63+ | isFirst = readSimpleMetaData (drop 1 ls)
64+ | otherwise = [body (drop 1 ls)]
5865 where
5966 isDelimiter' = isPossibleDelimiter (head ls)
6067 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)
6269
6370 readSimpleMetaData = map readPair . filter (not . all isSpace)
6471 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)
6673
6774 readSectionMetaData [] = []
6875 readSectionMetaData (header:value) =
6976 let key = substituteRegex "[^a-zA-Z0-9]" "" header
70− in [(key, unlines value, True)]
77+ in [PageSection (key, unlines value, True)]
7178
7279-- | Read a page from a file. Metadata is supported.
7380--
@@ -79,14 +86,14 @@ readPage path = do
7986 contents <- liftIO $ readFile path
8087 url <- toUrl path
8188 let sections = evalState (splitAtDelimiters $ lines contents) Nothing
82− sectionsData = zipWith ($) sectionFunctions sections
89+ sectionsData = concat $ zipWith ($) sectionFunctions sections
8390
84− return $ PageSection [ ("url", url, False)
85− , ("path", path, False)
86− ] : category : sectionsData
91+ return $ PageSection ("url", url, False)
92+ : PageSection ("path", path, False)
93+ : (category ++ sectionsData)
8794 where
8895 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)]
9097
9198-- | Read a page from a file. Metadata is supported.
9299--
src/Text/Hakyll/Pandoc.hs+2−2
@@ -38,11 +38,11 @@ getRenderFunction fileType = do
3838 { stateLiterateHaskell = True }
3939 readOptions options _ = options
4040
41−-- | Path must be there
41+-- | An action that renders the list of page sections to a context using pandoc
4242--
4343renderAction :: HakyllAction [PageSection] Context
4444renderAction = createHakyllAction $ \sections -> do
45− let triples = unPageSection =<< sections
45+ let triples = map unPageSection sections
4646 path = fromMaybe "unknown" $ lookup "path"
4747 $ map (\(x, y, _) -> (x, y))
4848 $ triples
tests/Main.hs+1
@@ -12,6 +12,7 @@ import Template
1212import Util
1313
1414-- | Run all tests.
15+main :: IO ()
1516main = defaultMain [ compressCssGroup
1617 , fileGroup
1718 , pageGroup
tests/Page.hs+3−2
@@ -13,7 +13,8 @@ import Test.HUnit
1313import System.Directory (getTemporaryDirectory, removeFile)
1414import System.FilePath ((</>))
1515
16−import Text.Hakyll.Internal.Page
16+import Text.Hakyll.Page
17+import Text.Hakyll.Pandoc
1718import Text.Hakyll.Context
1819import Text.Hakyll.HakyllAction
1920import Text.Hakyll
@@ -35,7 +36,7 @@ test_readPage fileName content assertion = do
3536 temporaryDir <- getTemporaryDirectory
3637 let temporaryFile = temporaryDir </> fileName
3738 writeFile temporaryFile content
38− page <- runDefaultHakyll (readPage temporaryFile)
39+ page <- runDefaultHakyll (runHakyllAction $ createPage temporaryFile)
3940 removeFile temporaryFile
4041 return $ assertion page
4142