From c73c09270d7605940c1064aef50675c0cff912c8 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Mon, 4 Oct 2010 01:27:03 +0200 Subject: Clean up modules a bit --- src/Text/Hakyll.hs | 4 ++ src/Text/Hakyll/CreateContext.hs | 2 +- src/Text/Hakyll/Internal/Page.hs | 98 ------------------------------------ src/Text/Hakyll/Internal/Template.hs | 2 +- src/Text/Hakyll/Page.hs | 98 ++++++++++++++++++++++++++++++++++++ src/Text/Hakyll/Pandoc.hs | 8 ++- 6 files changed, 110 insertions(+), 102 deletions(-) delete mode 100644 src/Text/Hakyll/Internal/Page.hs create mode 100644 src/Text/Hakyll/Page.hs (limited to 'src') diff --git a/src/Text/Hakyll.hs b/src/Text/Hakyll.hs index 506a0a9..b0fe479 100644 --- a/src/Text/Hakyll.hs +++ b/src/Text/Hakyll.hs @@ -22,6 +22,8 @@ module Text.Hakyll , module Text.Hakyll.Render , module Text.Hakyll.HakyllAction , module Text.Hakyll.Paginate + , module Text.Hakyll.Page + , module Text.Hakyll.Pandoc , module Text.Hakyll.Util , module Text.Hakyll.Tags , module Text.Hakyll.Feed @@ -49,6 +51,8 @@ import Text.Hakyll.Regex import Text.Hakyll.Render import Text.Hakyll.HakyllAction import Text.Hakyll.Paginate +import Text.Hakyll.Page +import Text.Hakyll.Pandoc import Text.Hakyll.Util import Text.Hakyll.Tags import Text.Hakyll.Feed diff --git a/src/Text/Hakyll/CreateContext.hs b/src/Text/Hakyll/CreateContext.hs index d9499ea..a9c3a95 100644 --- a/src/Text/Hakyll/CreateContext.hs +++ b/src/Text/Hakyll/CreateContext.hs @@ -18,7 +18,7 @@ import Control.Arrow ((>>>)) import Text.Hakyll.Context import Text.Hakyll.HakyllAction import Text.Hakyll.Render -import Text.Hakyll.Internal.Page +import Text.Hakyll.Page import Text.Hakyll.Pandoc import Text.Hakyll.Internal.Cache diff --git a/src/Text/Hakyll/Internal/Page.hs b/src/Text/Hakyll/Internal/Page.hs deleted file mode 100644 index 9e715d5..0000000 --- a/src/Text/Hakyll/Internal/Page.hs +++ /dev/null @@ -1,98 +0,0 @@ --- | A module for dealing with @Page@s. This module is mostly internally used. -module Text.Hakyll.Internal.Page - ( PageSection (..) - , readPage - , readPageAction - ) where - -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.Hakyll.File -import Text.Hakyll.HakyllMonad -import Text.Hakyll.HakyllAction -import Text.Hakyll.Regex (substituteRegex, matchesRegex) -import Text.Hakyll.Util (trim) - --- | Page info handle: (key, value, needs rendering) --- -data PageSection = PageSection {unPageSection :: [(String, String, Bool)]} - deriving (Show) - --- | Split a page into sections. --- -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. -isPossibleDelimiter :: String -> Bool -isPossibleDelimiter = isPrefixOf "---" - --- | Read one section of a page. --- -readSection :: Bool -- ^ If this section is the first section in the page. - -> [String] -- ^ Lines in the section. - -> PageSection -- ^ Key-values extracted. -readSection _ [] = PageSection [] -readSection isFirst ls - | not isDelimiter' = body ls - | isNamedDelimiter = PageSection $ readSectionMetaData ls - | isFirst = PageSection $ 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)] - - readSimpleMetaData = map readPair . filter (not . all isSpace) - readPair = trimPair . break (== ':') - trimPair (key, value) = (trim key, trim (drop 1 value), False) - - readSectionMetaData [] = [] - readSectionMetaData (header:value) = - let key = substituteRegex "[^a-zA-Z0-9]" "" header - in [(key, unlines value, True)] - --- | Read a page from a file. Metadata is supported. --- -readPage :: FilePath -> Hakyll [PageSection] -readPage path = do - let sectionFunctions = map readSection $ True : repeat False - - -- Read file. - contents <- liftIO $ readFile path - url <- toUrl path - let sections = evalState (splitAtDelimiters $ lines contents) Nothing - sectionsData = zipWith ($) sectionFunctions sections - - return $ PageSection [ ("url", url, False) - , ("path", path, False) - ] : category : sectionsData - where - category = let dirs = splitDirectories $ takeDirectory path - in PageSection [("category", last dirs, False) | not (null dirs)] - --- | Read a page from a file. Metadata is supported. --- -readPageAction :: FilePath -> HakyllAction () [PageSection] -readPageAction path = HakyllAction - { actionDependencies = [path] - , actionUrl = Left $ toUrl path - , actionFunction = const $ readPage path - } diff --git a/src/Text/Hakyll/Internal/Template.hs b/src/Text/Hakyll/Internal/Template.hs index 9b9d9cf..59235cc 100644 --- a/src/Text/Hakyll/Internal/Template.hs +++ b/src/Text/Hakyll/Internal/Template.hs @@ -20,7 +20,7 @@ import Text.Hakyll.HakyllMonad (Hakyll) import Text.Hakyll.HakyllAction import Text.Hakyll.Pandoc import Text.Hakyll.Internal.Cache -import Text.Hakyll.Internal.Page +import Text.Hakyll.Page import Text.Hakyll.Internal.Template.Template import Text.Hakyll.Internal.Template.Hamlet diff --git a/src/Text/Hakyll/Page.hs b/src/Text/Hakyll/Page.hs new file mode 100644 index 0000000..fc0498e --- /dev/null +++ b/src/Text/Hakyll/Page.hs @@ -0,0 +1,98 @@ +-- | A module for dealing with @Page@s. This module is mostly internally used. +module Text.Hakyll.Page + ( PageSection (..) + , readPage + , readPageAction + ) where + +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.Hakyll.File +import Text.Hakyll.HakyllMonad +import Text.Hakyll.HakyllAction +import Text.Hakyll.Regex (substituteRegex, matchesRegex) +import Text.Hakyll.Util (trim) + +-- | Page info handle: (key, value, needs rendering) +-- +data PageSection = PageSection {unPageSection :: [(String, String, Bool)]} + deriving (Show) + +-- | Split a page into sections. +-- +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. +isPossibleDelimiter :: String -> Bool +isPossibleDelimiter = isPrefixOf "---" + +-- | Read one section of a page. +-- +readSection :: Bool -- ^ If this section is the first section in the page. + -> [String] -- ^ Lines in the section. + -> PageSection -- ^ Key-values extracted. +readSection _ [] = PageSection [] +readSection isFirst ls + | not isDelimiter' = body ls + | isNamedDelimiter = PageSection $ readSectionMetaData ls + | isFirst = PageSection $ 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)] + + readSimpleMetaData = map readPair . filter (not . all isSpace) + readPair = trimPair . break (== ':') + trimPair (key, value) = (trim key, trim (drop 1 value), False) + + readSectionMetaData [] = [] + readSectionMetaData (header:value) = + let key = substituteRegex "[^a-zA-Z0-9]" "" header + in [(key, unlines value, True)] + +-- | Read a page from a file. Metadata is supported. +-- +readPage :: FilePath -> Hakyll [PageSection] +readPage path = do + let sectionFunctions = map readSection $ True : repeat False + + -- Read file. + contents <- liftIO $ readFile path + url <- toUrl path + let sections = evalState (splitAtDelimiters $ lines contents) Nothing + sectionsData = zipWith ($) sectionFunctions sections + + return $ PageSection [ ("url", url, False) + , ("path", path, False) + ] : category : sectionsData + where + category = let dirs = splitDirectories $ takeDirectory path + in PageSection [("category", last dirs, False) | not (null dirs)] + +-- | Read a page from a file. Metadata is supported. +-- +readPageAction :: FilePath -> HakyllAction () [PageSection] +readPageAction path = HakyllAction + { actionDependencies = [path] + , actionUrl = Left $ toUrl path + , actionFunction = const $ readPage path + } diff --git a/src/Text/Hakyll/Pandoc.hs b/src/Text/Hakyll/Pandoc.hs index 9dbe3d4..f6c8383 100644 --- a/src/Text/Hakyll/Pandoc.hs +++ b/src/Text/Hakyll/Pandoc.hs @@ -1,4 +1,8 @@ -module Text.Hakyll.Pandoc where +-- | Module exporting a pandoc arrow +-- +module Text.Hakyll.Pandoc + ( renderAction + ) where import Data.Maybe (fromMaybe) import qualified Data.Map as M @@ -7,7 +11,7 @@ import Control.Arrow (second) import Text.Pandoc import Text.Hakyll.Internal.FileType -import Text.Hakyll.Internal.Page +import Text.Hakyll.Page import Text.Hakyll.HakyllMonad import Text.Hakyll.HakyllAction import Text.Hakyll.Context -- cgit v1.2.3