on master help sign in
Added Text.Hakyll.Internal.FileType module.
commitd023eb5bf66bd45c1460c9dd331af409d8294763 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateMon 8 Mar 2010, 18:44 (17 years ago) parent94400da
4 files changed+61−26
hakyll.cabal+1 src/Text/Hakyll/File.hs+2−15 src/Text/Hakyll/Internal/FileType.hs+45 src/Text/Hakyll/Internal/Page.hs+13−11
hakyll.cabal+1
@@ -54,6 +54,7 @@ library
5454 Text.Hakyll.Rss
5555 Text.Hakyll.Internal.Cache
5656 Text.Hakyll.Internal.CompressCss
57+ Text.Hakyll.Internal.FileType
5758 Text.Hakyll.Internal.Page
5859 Text.Hakyll.Internal.Render
5960 Text.Hakyll.Internal.Template
src/Text/Hakyll/File.hs+2−15
@@ -24,6 +24,7 @@ import Data.Ord (comparing)
2424import Control.Monad.Reader (liftIO)
2525
2626import Text.Hakyll.Hakyll
27+import Text.Hakyll.Internal.FileType (isRenderableFile)
2728
2829-- | Auxiliary function to remove pathSeparators form the start. We don't deal
2930-- with absolute paths here. We also remove $root from the start.
@@ -62,7 +63,7 @@ toUrl :: FilePath -> Hakyll FilePath
6263toUrl path = do enableIndexUrl' <- askHakyll enableIndexUrl
6364 -- If the file does not have a renderable extension, like for
6465 -- example favicon.ico, we don't have to change it at all.
65− return $ if not hasRenderableExtension
66+ return $ if not (isRenderableFile path)
6667 then path
6768 -- If index url's are enabled, we create pick it
6869 -- unless the page is an index already.
@@ -70,20 +71,6 @@ toUrl path = do enableIndexUrl' <- askHakyll enableIndexUrl
7071 then indexUrl
7172 else withSimpleHtmlExtension
7273 where
73− hasRenderableExtension = takeExtension path `elem` [ ".markdown"
74− , ".md"
75− , ".mdn"
76− , ".mdwn"
77− , ".mkd"
78− , ".mkdn"
79− , ".mkdwn"
80− , ".rst"
81− , ".text"
82− , ".tex"
83− , ".lhs"
84− , ".htm"
85− , ".html"
86− ]
8774 isIndex = dropExtension (takeFileName path) == "index"
8875 withSimpleHtmlExtension = flip addExtension ".html" $ dropExtension path
8976 indexUrl = dropExtension path ++ "/"
src/Text/Hakyll/Internal/FileType.hsnew file+45
@@ -0,0 +1,45 @@
1+-- | A module dealing with file extensions and associated file types.
2+module Text.Hakyll.Internal.FileType
3+ ( FileType (..)
4+ , getFileType
5+ , isRenderable
6+ , isRenderableFile
7+ ) where
8+
9+import System.FilePath (takeExtension)
10+
11+-- | Datatype to represent the different file types Hakyll can deal with.
12+data FileType = Html
13+ | LaTeX
14+ | LiterateHaskellMarkdown
15+ | Markdown
16+ | ReStructuredText
17+ | UnknownFileType
18+ deriving (Eq, Ord, Show, Read)
19+
20+-- | Get the file type for a certain file. The type is determined by extension.
21+getFileType :: FilePath -> FileType
22+getFileType = getFileType' . takeExtension
23+ where
24+ getFileType' ".htm" = Html
25+ getFileType' ".html" = Html
26+ getFileType' ".lhs" = LiterateHaskellMarkdown
27+ getFileType' ".markdown" = Markdown
28+ getFileType' ".md" = Markdown
29+ getFileType' ".mdn" = Markdown
30+ getFileType' ".mdown" = Markdown
31+ getFileType' ".mdwn" = Markdown
32+ getFileType' ".mkd" = Markdown
33+ getFileType' ".mkdwn" = Markdown
34+ getFileType' ".rst" = ReStructuredText
35+ getFileType' ".tex" = LaTeX
36+ getFileType' _ = UnknownFileType
37+
38+-- | Check if a certain @FileType@ is renderable.
39+isRenderable :: FileType -> Bool
40+isRenderable UnknownFileType = False
41+isRenderable _ = True
42+
43+-- | Check if a certain file is renderable.
44+isRenderableFile :: FilePath -> Bool
45+isRenderableFile = isRenderable . getFileType
src/Text/Hakyll/Internal/Page.hs+13−11
@@ -17,6 +17,7 @@ import Text.Hakyll.Hakyll
1717import Text.Hakyll.Regex (substituteRegex, matchesRegex)
1818import Text.Hakyll.Util (trim)
1919import Text.Hakyll.Internal.Cache
20+import Text.Hakyll.Internal.FileType
2021
2122-- | The default reader options for pandoc parsing.
2223readerOptions :: ParserState
@@ -35,18 +36,19 @@ writerOptions = defaultWriterOptions
3536 }
3637
3738-- | Get a render function for a given extension.
38−getRenderFunction :: String -> (String -> String)
39−getRenderFunction ".html" = id
40−getRenderFunction ".htm" = id
41−getRenderFunction ext = writeHtmlString writerOptions
42− . readFunction ext (readOptions ext)
39+getRenderFunction :: FileType -> (String -> String)
40+getRenderFunction Html = id
41+getRenderFunction fileType = writeHtmlString writerOptions
42+ . readFunction fileType (readOptions fileType)
4343 where
44− readFunction ".rst" = readRST
45− readFunction ".tex" = readLaTeX
46− readFunction _ = readMarkdown
44+ readFunction ReStructuredText = readRST
45+ readFunction LaTeX = readLaTeX
46+ readFunction Markdown = readMarkdown
47+ readFunction t = error $ "Cannot render file " ++ show t
4748
48− readOptions ".lhs" = readerOptions { stateLiterateHaskell = True }
49− readOptions _ = readerOptions
49+ readOptions LiterateHaskellMarkdown =
50+ readerOptions { stateLiterateHaskell = True }
51+ readOptions _ = readerOptions
5052
5153-- | Split a page into sections.
5254splitAtDelimiters :: [String] -> [[String]]
@@ -89,7 +91,7 @@ readSection renderFunction isFirst ls
8991-- has a @.markdown@ extension, it will be rendered using pandoc.
9092readPageFromFile :: FilePath -> Hakyll Context
9193readPageFromFile path = do
92− let renderFunction = getRenderFunction $ takeExtension path
94+ let renderFunction = getRenderFunction $ getFileType path
9395 sectionFunctions = map (readSection renderFunction)
9496 (True : repeat False)
9597