on master help sign in
Some changes to the regex interface, and version bump.
commit73f6b1ab8cf3cb9468c547db6df1df9596f1470d authorJasper Van der Jeugt <jaspervdj@gmail.com> dateSat 9 Jan 2010, 21:40 (17 years ago) parentcb3e3b1
8 files changed+90−29
hakyll.cabal+4−2 src/Network/Hakyll/SimpleServer.hs+1 src/Text/Hakyll/CompressCSS.hs+6−10 src/Text/Hakyll/Context.hs+3−4 src/Text/Hakyll/Regex.hs+66 src/Text/Hakyll/Tags.hs+1 src/Text/Hakyll/Util.hs−7 tests/Tests.hs+9−6
hakyll.cabal+4−2
@@ -1,5 +1,5 @@
11Name: hakyll
2−Version: 0.4
2+Version: 0.4.1
33
44Synopsis: A simple static site generator library.
55Description:
@@ -30,7 +30,8 @@ library
3030 containers >= 0.1,
3131 bytestring >= 0.9 && < 0.9.2,
3232 pandoc >= 1,
33− regex-compat >= 0.92,
33+ regex-base >= 0.83,
34+ regex-tdfa >= 0.92,
3435 network >= 2,
3536 mtl >= 1.1,
3637 old-locale >= 1,
@@ -45,4 +46,5 @@ library
4546 Text.Hakyll.Util
4647 Text.Hakyll.Tags
4748 Text.Hakyll.Context
49+ Text.Hakyll.Regex
4850 Network.Hakyll.SimpleServer
src/Network/Hakyll/SimpleServer.hs+1
@@ -17,6 +17,7 @@ import qualified Data.ByteString.Char8 as B
1717import qualified Data.Map as M
1818
1919import Text.Hakyll.Util
20+import Text.Hakyll.Regex
2021
2122-- | Function to log from a chan.
2223log :: Chan String -> IO ()
src/Text/Hakyll/CompressCSS.hs+6−10
@@ -3,27 +3,23 @@ module Text.Hakyll.CompressCSS
33 ) where
44
55import Data.List (isPrefixOf)
6−import Text.Regex (subRegex, mkRegex)
7−
8−-- | subRegex with arguments flipped for easy function composition.
9−subRegex' :: String -> String -> String -> String
10−subRegex' pattern replacement str = subRegex (mkRegex pattern) str replacement
6+import Text.Hakyll.Regex (substitute)
117
128-- | Compress CSS to speed up your site.
139compressCSS :: String -> String
1410compressCSS = compressSeparators
15− . compressWhitespace
1611 . stripComments
12+ . compressWhitespace
1713
1814-- | Compresses certain forms of separators.
1915compressSeparators :: String -> String
20−compressSeparators = subRegex' ";\\s*}" "}"
21− . subRegex' "\\s*([{};:])\\s*" "\\1"
22− . subRegex' ";;*" ";"
16+compressSeparators = substitute "; *}" "}"
17+ . substitute " *([{};:]) *" "\\1"
18+ . substitute ";;*" ";"
2319
2420-- | Compresses all whitespace.
2521compressWhitespace :: String -> String
26−compressWhitespace = subRegex' "\\s\\s*" " "
22+compressWhitespace = substitute "[ \t\n][ \t\n]*" " "
2723
2824-- | Function that strips CSS comments away.
2925stripComments :: String -> String
src/Text/Hakyll/Context.hs+3−4
@@ -10,11 +10,11 @@ import qualified Data.ByteString.Lazy.Char8 as B
1010
1111import System.Locale (defaultTimeLocale)
1212import System.FilePath (takeFileName)
13−import Text.Regex (subRegex, mkRegex)
1413import Text.Template (Context)
1514import Data.Time.Format (parseTime, formatTime)
1615import Data.Time.Clock (UTCTime)
1716import Data.Maybe (fromMaybe)
17+import Text.Hakyll.Regex (substitute)
1818
1919-- | Type for context manipulating functions.
2020type ContextManipulation = Context -> Context
@@ -38,9 +38,8 @@ renderDate key format defaultValue context =
3838 M.insert (B.pack key) (B.pack value) context
3939 where value = fromMaybe defaultValue pretty
4040 pretty = do filePath <- M.lookup (B.pack "path") context
41− let dateString = subRegex (mkRegex "^([0-9]*-[0-9]*-[0-9]*).*")
42− (takeFileName $ B.unpack filePath)
43− "\\1"
41+ let dateString = substitute "^([0-9]*-[0-9]*-[0-9]*).*" "\\1"
42+ (takeFileName $ B.unpack filePath)
4443 time <- parseTime defaultTimeLocale
4544 "%Y-%m-%d"
4645 dateString :: Maybe UTCTime
src/Text/Hakyll/Regex.hsnew file+66
@@ -0,0 +1,66 @@
1+-- | A module that exports a simple regex interface. This code is mostly copied
2+-- from the regex-compat package at hackage.
3+module Text.Hakyll.Regex
4+ ( split
5+ , substitute
6+ ) where
7+
8+import Text.Regex.TDFA
9+
10+-- | Match a regular expression against a string, returning more information
11+-- about the match.
12+matchRegexAll :: Regex -> String -> Maybe (String, String, String, [String])
13+matchRegexAll p str = matchM p str
14+
15+-- | Replaces every occurance of the given regexp with the replacement string.
16+subRegex :: Regex -- ^ Search pattern
17+ -> String -- ^ Input string
18+ -> String -- ^ Replacement text
19+ -> String -- ^ Output string
20+subRegex _ "" _ = ""
21+subRegex regexp inp replacement =
22+ let -- bre matches a backslash then capture either a backslash or some digits
23+ bre = makeRegex "\\\\(\\\\|[0-9]+)"
24+ lookup' _ [] _ = []
25+ lookup' [] _ _ = []
26+ lookup' match' repl groups =
27+ case matchRegexAll bre repl of
28+ Nothing -> repl
29+ Just (lead, _, trail, bgroups) ->
30+ let newval =
31+ if (head bgroups) == "\\"
32+ then "\\"
33+ else let index :: Int
34+ index = (read (head bgroups)) - 1
35+ in if index == -1
36+ then match'
37+ else groups !! index
38+ in lead ++ newval ++ lookup' match' trail groups
39+ in case matchRegexAll regexp inp of
40+ Nothing -> inp
41+ Just (lead, match', trail, groups) ->
42+ lead ++ lookup' match' replacement groups ++ (subRegex regexp trail replacement)
43+
44+-- | Splits a string based on a regular expression. The regular expression
45+-- should identify one delimiter.
46+splitRegex :: Regex -> String -> [String]
47+splitRegex _ [] = []
48+splitRegex delim strIn = loop strIn where
49+ loop str = case matchOnceText delim str of
50+ Nothing -> [str]
51+ Just (firstline, _, remainder) ->
52+ if null remainder
53+ then [firstline,""]
54+ else firstline : loop remainder
55+
56+-- | Split a list at a certain element.
57+split :: String -> String -> [String]
58+split pattern = filter (not . null)
59+ . splitRegex (makeRegex pattern)
60+
61+-- | Substitute a regex. Simplified interface.
62+substitute :: String -- ^ Pattern to replace (regex).
63+ -> String -- ^ Replacement string.
64+ -> String -- ^ Input string.
65+ -> String -- ^ Result.
66+substitute pattern replacement str = subRegex (makeRegex pattern) str replacement
src/Text/Hakyll/Tags.hs+1
@@ -12,6 +12,7 @@ import Data.List (intercalate)
1212import Control.Monad (foldM)
1313
1414import Text.Hakyll.Context (ContextManipulation, renderValue)
15+import Text.Hakyll.Regex
1516import Text.Hakyll.Util
1617import Text.Hakyll.Page
1718import Control.Arrow (second)
src/Text/Hakyll/Util.hs−7
@@ -1,12 +1,10 @@
11module Text.Hakyll.Util
22 ( trim
3− , split
43 , stripHTML
54 , link
65 ) where
76
87import Data.Char (isSpace)
9−import Text.Regex (splitRegex, mkRegex)
108
119-- | Trim a string (drop spaces and tabs at both sides).
1210trim :: String -> String
@@ -23,11 +21,6 @@ stripHTML str = let (beforeTag, rest) = break (== '<') str
2321 where tail' [] = []
2422 tail' xs = tail xs
2523
26−-- | Split a list at a certain element.
27−split :: String -> String -> [String]
28−split pattern = filter (not . null)
29− . splitRegex (mkRegex pattern)
30−
3124-- | Make a HTML link.
3225--
3326-- > link "foo" "bar.html" == "<a href='bar.html'>foo</a>"
tests/Tests.hs+9−6
@@ -10,6 +10,7 @@ import qualified Data.ByteString.Lazy.Char8 as B
1010
1111import Text.Hakyll.CompressCSS
1212import Text.Hakyll.Util
13+import Text.Hakyll.Regex
1314import Text.Hakyll.Context
1415import Text.Hakyll.File
1516
@@ -21,12 +22,14 @@ tests = [ testGroup "Util group" [ testProperty "trim length" prop_trim_length
2122 , testCase "stripHTML 1" test_strip_html1
2223 , testCase "stripHTML 2" test_strip_html2
2324 , testCase "stripHTML 3" test_strip_html3
24− , testCase "split 1" test_split1
25− , testCase "split 2" test_split2
2625 , testCase "link 1" test_link1
2726 , testCase "link 2" test_link2
2827 ]
2928
29+ , testGroup "Regex group" [ testCase "split 1" test_split1
30+ , testCase "split 2" test_split2
31+ ]
32+
3033 , testGroup "CompressCSS group" [ testProperty "compressCSS length" prop_compress_css_length
3134 , testCase "compressCSS 1" test_compress_css1
3235 , testCase "compressCSS 2" test_compress_css2
@@ -61,14 +64,14 @@ test_strip_html2 = stripHTML "text" @?= "text"
6164test_strip_html3 = stripHTML "<b>Hakyll</b> is an <i>awesome</i> web framework <img src=\"foo.png\" />" @?=
6265 "Hakyll is an awesome web framework "
6366
64−-- Split test cases.
65−test_split1 = split "," "1,2,3" @?= ["1", "2", "3"]
66−test_split2 = split "," ",1,2," @?= ["1", "2"]
67−
6867-- Link test cases.
6968test_link1 = link "foo bar" "/foo/bar.html" @?= "<a href=\"/foo/bar.html\">foo bar</a>"
7069test_link2 = link "back home" "/" @?= "<a href=\"/\">back home</a>"
7170
71+-- Split test cases.
72+test_split1 = split "," "1,2,3" @?= ["1", "2", "3"]
73+test_split2 = split "," ",1,2," @?= ["1", "2"]
74+
7275-- CSS compression should always decrease the text length.
7376prop_compress_css_length str = length str >= length (compressCSS str)
7477