on master help sign in
Remove hamlet, add function fields
commit802742cdbed1bb0afa022e072621e621d21158f6 authorJasper Van der Jeugt <m@jaspervdj.be> dateMon 19 Nov 2012, 15:12 (14 years ago) parent88ffd3c
10 files changed+69−142
hakyll.cabal+1−5 src/Hakyll/Web/Feed.hs+3−3 src/Hakyll/Web/Template.hs+4−45 src/Hakyll/Web/Template/Context.hs+10 src/Hakyll/Web/Template/Read.hs+38−6 src/Hakyll/Web/Template/Read/Hakyll.hs−35 src/Hakyll/Web/Template/Read/Hamlet.hs−46 tests/Hakyll/Web/Template/Tests.hs+11−2 tests/data/template.html+1 tests/data/example.md.out → tests/data/template.html.out+1
hakyll.cabal+1−5
@@ -50,8 +50,8 @@ Data-Files:
5050Extra-source-files:
5151 tests/data/example.md
5252 tests/data/example.md.metadata
53− tests/data/example.md.out
5453 tests/data/template.html
54+ tests/data/template.html.out
5555
5656Source-Repository head
5757 Type: git
@@ -82,7 +82,6 @@ Library
8282 deepseq >= 1.3 && < 1.4,
8383 directory >= 1.0 && < 1.3,
8484 filepath >= 1.0 && < 1.4,
85− hamlet >= 1.0 && < 1.2,
8685 lrucache >= 1.1.1 && < 1.2,
8786 mtl >= 1 && < 2.2,
8887 old-locale >= 1.0 && < 1.1,
@@ -145,8 +144,6 @@ Library
145144 Hakyll.Core.Provider.Modified
146145 Hakyll.Core.Rules.Internal
147146 Hakyll.Web.Template.Internal
148− Hakyll.Web.Template.Read.Hakyll
149− Hakyll.Web.Template.Read.Hamlet
150147 Paths_hakyll
151148
152149 If flag(previewServer)
@@ -191,7 +188,6 @@ Test-suite hakyll-tests
191188 deepseq >= 1.3 && < 1.4,
192189 directory >= 1.0 && < 1.3,
193190 filepath >= 1.0 && < 1.4,
194− hamlet >= 1.0 && < 1.2,
195191 lrucache >= 1.1.1 && < 1.2,
196192 mtl >= 1 && < 2.2,
197193 old-locale >= 1.0 && < 1.1,
src/Hakyll/Web/Feed.hs+3−3
@@ -24,8 +24,8 @@ module Hakyll.Web.Feed
2424
2525
2626--------------------------------------------------------------------------------
27−import Control.Monad (forM, (<=<))
28−import Data.Monoid (mconcat)
27+import Control.Monad (forM, (<=<))
28+import Data.Monoid (mconcat)
2929
3030
3131--------------------------------------------------------------------------------
@@ -34,7 +34,7 @@ import Hakyll.Core.Compiler.Internal
3434import Hakyll.Core.Item
3535import Hakyll.Web.Template
3636import Hakyll.Web.Template.Context
37−import Hakyll.Web.Template.Read.Hakyll (readTemplate)
37+import Hakyll.Web.Template.Read
3838
3939
4040--------------------------------------------------------------------------------
src/Hakyll/Web/Template.hs+4−45
@@ -36,32 +36,9 @@
3636--
3737-- Because of it's simplicity, these templates can be used for more than HTML:
3838-- you could make, for example, CSS or JS templates as well.
39−--
40−-- In addition to the native format, Hakyll also supports hamlet templates. For
41−-- more information on hamlet templates, please refer to:
42−-- <http://hackage.haskell.org/package/hamlet>. Internally, hamlet templates are
43−-- converted to hakyll templates -- which means that you can only use variable
44−-- insertion (and not all hamlet's features).
45−--
46−-- This is an example of a valid hamlet template. You should place them in
47−-- files with a @.hamlet@ extension:
48−--
49−-- > !!!
50−-- > <html>
51−-- > <head>
52−-- > <meta charset="UTF-8">
53−-- > <title> MyAweSomeCompany - #{title}
54−-- > <body>
55−-- > <h1> MyAweSomeCompany - #{title}
56−-- > <div id="navigation">
57−-- > <a href="/index.html"> Home
58−-- > <a href="/about.html"> About
59−-- > <a href="/code.html"> Code
60−-- > #{body}
6139module Hakyll.Web.Template
6240 ( Template
6341 , templateCompiler
64− , templateCompilerWith
6542 , applyTemplate
6643 , applyTemplateWith
6744 ) where
@@ -70,14 +47,10 @@ module Hakyll.Web.Template
7047--------------------------------------------------------------------------------
7148import Control.Monad (forM, liftM)
7249import Prelude hiding (id)
73−import System.FilePath (takeExtension)
74−import Text.Hamlet (HamletSettings,
75− defaultHamletSettings)
7650
7751
7852--------------------------------------------------------------------------------
7953import Hakyll.Core.Compiler
80−import Hakyll.Core.Identifier
8154import Hakyll.Core.Item
8255import Hakyll.Web.Template.Context
8356import Hakyll.Web.Template.Internal
@@ -85,25 +58,11 @@ import Hakyll.Web.Template.Read
8558
8659
8760--------------------------------------------------------------------------------
88−-- | Read a template. If the extension of the file we're compiling is
89−-- @.hml@ or @.hamlet@, it will be considered as a Hamlet template, and parsed
90−-- as such.
61+-- | Read a template.
9162templateCompiler :: Compiler (Item Template)
92−templateCompiler = templateCompilerWith defaultHamletSettings
93−
94−
95−--------------------------------------------------------------------------------
96−-- | Version of 'templateCompiler' that enables custom settings.
97−templateCompilerWith :: HamletSettings -> Compiler (Item Template)
98−templateCompilerWith settings =
99− cached "Hakyll.Web.Template.templateCompilerWith" $ do
100− identifier <- getUnderlying
101− item <- getResourceString
102− if takeExtension (toFilePath identifier) `elem` [".hml", ".hamlet"]
103− -- Hamlet template
104− then return $ fmap (readHamletTemplateWith settings) item
105− -- Hakyll template
106− else return $ fmap readTemplate item
63+templateCompiler = cached "Hakyll.Web.Template.templateCompiler" $ do
64+ item <- getResourceString
65+ return $ fmap readTemplate item
10766
10867
10968--------------------------------------------------------------------------------
src/Hakyll/Web/Template/Context.hs+10
@@ -4,6 +4,7 @@ module Hakyll.Web.Template.Context
44 , mapContext
55 , field
66 , constField
7+ , functionField
78
89 , defaultContext
910 , bodyField
@@ -70,6 +71,15 @@ constField :: String -> String -> Context a
7071constField key = field key . const . return
7172
7273
74+--------------------------------------------------------------------------------
75+functionField :: String -> ([String] -> Item a -> Compiler String) -> Context a
76+functionField name value = Context $ \k i -> case words k of
77+ [] -> empty
78+ (n : args)
79+ | n == name -> value args i
80+ | otherwise -> empty
81+
82+
7383--------------------------------------------------------------------------------
7484defaultContext :: Context String
7585defaultContext =
src/Hakyll/Web/Template/Read.hs+38−6
@@ -1,10 +1,42 @@
1−-- | Re-exports all different template reading modules
2−--
1+--------------------------------------------------------------------------------
2+-- | Read templates in Hakyll's native format
33module Hakyll.Web.Template.Read
44 ( readTemplate
5− , readHamletTemplate
6− , readHamletTemplateWith
75 ) where
86
9−import Hakyll.Web.Template.Read.Hakyll
10−import Hakyll.Web.Template.Read.Hamlet
7+
8+--------------------------------------------------------------------------------
9+import Data.List (isPrefixOf)
10+import Data.Char (isAlphaNum)
11+
12+
13+--------------------------------------------------------------------------------
14+import Hakyll.Web.Template.Internal
15+
16+
17+--------------------------------------------------------------------------------
18+-- | Construct a @Template@ from a string.
19+readTemplate :: String -> Template
20+readTemplate = Template . readTemplate'
21+ where
22+ readTemplate' [] = []
23+ readTemplate' string
24+ | "$$" `isPrefixOf` string =
25+ Escaped : readTemplate' (drop 2 string)
26+ | "$" `isPrefixOf` string =
27+ case readKey (drop 1 string) of
28+ Just (key, rest) -> Key key : readTemplate' rest
29+ Nothing -> Chunk "$" : readTemplate' (drop 1 string)
30+ | otherwise =
31+ let (chunk, rest) = break (== '$') string
32+ in Chunk chunk : readTemplate' rest
33+
34+ -- Parse an key into (key, rest) if it's valid, and return
35+ -- Nothing otherwise
36+ readKey string =
37+ let (key, rest) = span validKeyChar string
38+ in if not (null key) && "$" `isPrefixOf` rest
39+ then Just (key, drop 1 rest)
40+ else Nothing
41+
42+ validKeyChar x = isAlphaNum x || x == ' '
src/Hakyll/Web/Template/Read/Hakyll.hsdeleted−35
@@ -1,35 +0,0 @@
1−-- | Read templates in Hakyll's native format
2−--
3−module Hakyll.Web.Template.Read.Hakyll
4− ( readTemplate
5− ) where
6−
7−import Data.List (isPrefixOf)
8−import Data.Char (isAlphaNum)
9−
10−import Hakyll.Web.Template.Internal
11−
12−-- | Construct a @Template@ from a string.
13−--
14−readTemplate :: String -> Template
15−readTemplate = Template . readTemplate'
16− where
17− readTemplate' [] = []
18− readTemplate' string
19− | "$$" `isPrefixOf` string =
20− Escaped : readTemplate' (drop 2 string)
21− | "$" `isPrefixOf` string =
22− case readKey (drop 1 string) of
23− Just (key, rest) -> Key key : readTemplate' rest
24− Nothing -> Chunk "$" : readTemplate' (drop 1 string)
25− | otherwise =
26− let (chunk, rest) = break (== '$') string
27− in Chunk chunk : readTemplate' rest
28−
29− -- Parse an key into (key, rest) if it's valid, and return
30− -- Nothing otherwise
31− readKey string =
32− let (key, rest) = span isAlphaNum string
33− in if not (null key) && "$" `isPrefixOf` rest
34− then Just (key, drop 1 rest)
35− else Nothing
src/Hakyll/Web/Template/Read/Hamlet.hsdeleted−46
@@ -1,46 +0,0 @@
1−-- | Read templates in the hamlet format
2−--
3−{-# LANGUAGE MultiParamTypeClasses #-}
4−module Hakyll.Web.Template.Read.Hamlet
5− ( readHamletTemplate
6− , readHamletTemplateWith
7− ) where
8−
9−import Text.Hamlet (HamletSettings, defaultHamletSettings)
10−import Text.Hamlet.RT
11−
12−import Hakyll.Web.Template.Internal
13−
14−-- | Read a hamlet template using the default settings
15−--
16−readHamletTemplate :: String -> Template
17−readHamletTemplate = readHamletTemplateWith defaultHamletSettings
18−
19−-- | Read a hamlet template using the specified settings
20−--
21−readHamletTemplateWith :: HamletSettings -> String -> Template
22−readHamletTemplateWith settings string =
23− let result = parseHamletRT settings string
24− in case result of
25− Just hamlet -> fromHamletRT hamlet
26− Nothing -> error
27− "Hakyll.Web.Template.Read.Hamlet.readHamletTemplateWith: \
28− \Could not parse Hamlet file"
29−
30−-- | Convert a 'HamletRT' to a 'Template'
31−--
32−fromHamletRT :: HamletRT -- ^ Hamlet runtime template
33− -> Template -- ^ Hakyll template
34−fromHamletRT (HamletRT sd) = Template $ map fromSimpleDoc sd
35− where
36− fromSimpleDoc :: SimpleDoc -> TemplateElement
37− fromSimpleDoc (SDRaw chunk) = Chunk chunk
38− fromSimpleDoc (SDVar [var]) = Key var
39− fromSimpleDoc (SDVar _) = error
40− "Hakyll.Web.Template.Read.Hamlet.fromHamletRT: \
41− \Hakyll does not support '.' in identifier names when using \
42− \hamlet templates."
43− fromSimpleDoc _ = error
44− "Hakyll.Web.Template.Read.Hamlet.fromHamletRT: \
45− \Only simple $key$ identifiers are allowed when using hamlet \
46− \templates."
tests/Hakyll/Web/Template/Tests.hs+11−2
@@ -6,6 +6,7 @@ module Hakyll.Web.Template.Tests
66
77
88--------------------------------------------------------------------------------
9+import Data.Monoid (mconcat)
910import Test.Framework (Test, testGroup)
1011import Test.Framework.Providers.HUnit (testCase)
1112import Test.HUnit (Assertion, (@=?))
@@ -32,9 +33,17 @@ case01 :: Assertion
3233case01 = withTestStore $ \store -> do
3334 provider <- newTestProvider store
3435
35− out <- resourceString provider "example.md.out"
36+ out <- resourceString provider "template.html.out"
3637 tpl <- testCompilerDone store provider "template.html" $ templateCompiler
3738 item <- testCompilerDone store provider "example.md" $
38− pageCompiler >>= applyTemplate (itemBody tpl) defaultContext
39+ pageCompiler >>= applyTemplate (itemBody tpl) testContext
3940
4041 out @=? itemBody item
42+
43+
44+--------------------------------------------------------------------------------
45+testContext :: Context String
46+testContext = mconcat
47+ [ functionField "echo" (\args _ -> return $ unwords args)
48+ , defaultContext
49+ ]
tests/data/template.html+1
@@ -1,3 +1,4 @@
11<div>
2+ $echo test$
23 $body$
34</div>
tests/data/example.md.out → tests/data/template.html.outrenamed+1
@@ -1,3 +1,4 @@
11<div>
2+ test
23 <p>This is an example.</p>
34</div>