diff options
-rw-r--r-- | src/Text/Hakyll/Page.hs | 39 | ||||
-rw-r--r-- | src/Text/Hakyll/Pandoc.hs | 4 | ||||
-rw-r--r-- | tests/Main.hs | 1 | ||||
-rw-r--r-- | tests/Page.hs | 5 |
4 files changed, 29 insertions, 20 deletions
diff --git a/src/Text/Hakyll/Page.hs b/src/Text/Hakyll/Page.hs index fc0498e..347e10a 100644 --- a/src/Text/Hakyll/Page.hs +++ b/src/Text/Hakyll/Page.hs @@ -17,9 +17,16 @@ import Text.Hakyll.HakyllAction import Text.Hakyll.Regex (substituteRegex, matchesRegex) import Text.Hakyll.Util (trim) --- | Page info handle: (key, value, needs rendering) +-- | A page is first parsed into a number of page sections. A page section +-- consists of: -- -data PageSection = PageSection {unPageSection :: [(String, String, Bool)]} +-- * A key +-- +-- * A value +-- +-- * A 'Bool' flag, indicating if the value is applicable for rendering +-- +data PageSection = PageSection {unPageSection :: (String, String, Bool)} deriving (Show) -- | Split a page into sections. @@ -48,26 +55,26 @@ isPossibleDelimiter = isPrefixOf "---" -- readSection :: Bool -- ^ If this section is the first section in the page. -> [String] -- ^ Lines in the section. - -> PageSection -- ^ Key-values extracted. -readSection _ [] = PageSection [] + -> [PageSection] -- ^ Key-values extracted. +readSection _ [] = [] readSection isFirst ls - | not isDelimiter' = body ls - | isNamedDelimiter = PageSection $ readSectionMetaData ls - | isFirst = PageSection $ readSimpleMetaData (drop 1 ls) - | otherwise = body (drop 1 ls) + | not isDelimiter' = [body ls] + | isNamedDelimiter = readSectionMetaData ls + | isFirst = readSimpleMetaData (drop 1 ls) + | otherwise = [body (drop 1 ls)] where isDelimiter' = isPossibleDelimiter (head ls) isNamedDelimiter = head ls `matchesRegex` "^----* *[a-zA-Z0-9][a-zA-Z0-9]*" - body ls' = PageSection [("body", unlines ls', True)] + body ls' = PageSection ("body", unlines ls', True) readSimpleMetaData = map readPair . filter (not . all isSpace) readPair = trimPair . break (== ':') - trimPair (key, value) = (trim key, trim (drop 1 value), False) + trimPair (key, value) = PageSection (trim key, trim (drop 1 value), False) readSectionMetaData [] = [] readSectionMetaData (header:value) = let key = substituteRegex "[^a-zA-Z0-9]" "" header - in [(key, unlines value, True)] + in [PageSection (key, unlines value, True)] -- | Read a page from a file. Metadata is supported. -- @@ -79,14 +86,14 @@ readPage path = do contents <- liftIO $ readFile path url <- toUrl path let sections = evalState (splitAtDelimiters $ lines contents) Nothing - sectionsData = zipWith ($) sectionFunctions sections + sectionsData = concat $ zipWith ($) sectionFunctions sections - return $ PageSection [ ("url", url, False) - , ("path", path, False) - ] : category : sectionsData + return $ PageSection ("url", url, False) + : PageSection ("path", path, False) + : (category ++ sectionsData) where category = let dirs = splitDirectories $ takeDirectory path - in PageSection [("category", last dirs, False) | not (null dirs)] + in [PageSection ("category", last dirs, False) | not (null dirs)] -- | Read a page from a file. Metadata is supported. -- diff --git a/src/Text/Hakyll/Pandoc.hs b/src/Text/Hakyll/Pandoc.hs index f6c8383..f3626d9 100644 --- a/src/Text/Hakyll/Pandoc.hs +++ b/src/Text/Hakyll/Pandoc.hs @@ -38,11 +38,11 @@ getRenderFunction fileType = do { stateLiterateHaskell = True } readOptions options _ = options --- | Path must be there +-- | An action that renders the list of page sections to a context using pandoc -- renderAction :: HakyllAction [PageSection] Context renderAction = createHakyllAction $ \sections -> do - let triples = unPageSection =<< sections + let triples = map unPageSection sections path = fromMaybe "unknown" $ lookup "path" $ map (\(x, y, _) -> (x, y)) $ triples diff --git a/tests/Main.hs b/tests/Main.hs index 2c2717a..adcb613 100644 --- a/tests/Main.hs +++ b/tests/Main.hs @@ -12,6 +12,7 @@ import Template import Util -- | Run all tests. +main :: IO () main = defaultMain [ compressCssGroup , fileGroup , pageGroup diff --git a/tests/Page.hs b/tests/Page.hs index 27163b2..be49ad0 100644 --- a/tests/Page.hs +++ b/tests/Page.hs @@ -13,7 +13,8 @@ import Test.HUnit import System.Directory (getTemporaryDirectory, removeFile) import System.FilePath ((</>)) -import Text.Hakyll.Internal.Page +import Text.Hakyll.Page +import Text.Hakyll.Pandoc import Text.Hakyll.Context import Text.Hakyll.HakyllAction import Text.Hakyll @@ -35,7 +36,7 @@ test_readPage fileName content assertion = do temporaryDir <- getTemporaryDirectory let temporaryFile = temporaryDir </> fileName writeFile temporaryFile content - page <- runDefaultHakyll (readPage temporaryFile) + page <- runDefaultHakyll (runHakyllAction $ createPage temporaryFile) removeFile temporaryFile return $ assertion page |