summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hakyll.cabal3
-rw-r--r--src/Hakyll/Web/CompressCss.hs44
-rw-r--r--tests/Hakyll/Web/CompressCss/Tests.hs59
-rw-r--r--tests/TestSuite.hs2
-rw-r--r--web/examples.markdown4
-rw-r--r--web/tutorials/github-pages-tutorial.md12
6 files changed, 109 insertions, 15 deletions
diff --git a/hakyll.cabal b/hakyll.cabal
index 4966d72..28f5492 100644
--- a/hakyll.cabal
+++ b/hakyll.cabal
@@ -223,6 +223,7 @@ Test-suite hakyll-tests
Hakyll.Core.Store.Tests
Hakyll.Core.UnixFilter.Tests
Hakyll.Core.Util.String.Tests
+ Hakyll.Web.CompressCss.Tests
Hakyll.Web.Html.RelativizeUrls.Tests
Hakyll.Web.Html.Tests
Hakyll.Web.Pandoc.FileType.Tests
@@ -268,7 +269,7 @@ Test-suite hakyll-tests
time-locale-compat >= 0.1 && < 0.2,
unordered-containers >= 0.2 && < 0.3,
vector >= 0.11 && < 0.12,
- yaml >= 0.8 && < 0.9,
+ yaml >= 0.8.11 && < 0.9,
optparse-applicative >= 0.12 && < 0.13
If flag(previewServer)
diff --git a/src/Hakyll/Web/CompressCss.hs b/src/Hakyll/Web/CompressCss.hs
index 58d52b4..0371d8b 100644
--- a/src/Hakyll/Web/CompressCss.hs
+++ b/src/Hakyll/Web/CompressCss.hs
@@ -8,14 +8,12 @@ module Hakyll.Web.CompressCss
--------------------------------------------------------------------------------
-import Data.Char (isSpace)
import Data.List (isPrefixOf)
--------------------------------------------------------------------------------
import Hakyll.Core.Compiler
import Hakyll.Core.Item
-import Hakyll.Core.Util.String
--------------------------------------------------------------------------------
@@ -33,27 +31,55 @@ compressCss = compressSeparators . stripComments . compressWhitespace
--------------------------------------------------------------------------------
-- | Compresses certain forms of separators.
compressSeparators :: String -> String
-compressSeparators =
- replaceAll "; *}" (const "}") .
- replaceAll " *([{};]) *" (take 1 . dropWhile isSpace) .
- replaceAll ";+" (const ";")
-
+compressSeparators [] = []
+compressSeparators str
+ | isConstant = head str : retainConstants compressSeparators (head str) (drop 1 str)
+ | stripFirst = compressSeparators (drop 1 str)
+ | stripSecond = compressSeparators (head str : (drop 2 str))
+ | otherwise = head str : compressSeparators (drop 1 str)
+ where
+ isConstant = or $ map (isOfPrefix str) ["\"", "'"]
+ stripFirst = or $ map (isOfPrefix str) [" ", " {", " }", " :", ";;", ";}"]
+ stripSecond = or $ map (isOfPrefix str) ["{ ", "} ", ": ", "; "]
--------------------------------------------------------------------------------
-- | Compresses all whitespace.
compressWhitespace :: String -> String
-compressWhitespace = replaceAll "[ \t\n\r]+" (const " ")
-
+compressWhitespace [] = []
+compressWhitespace str
+ | isConstant = head str : retainConstants compressWhitespace (head str) (drop 1 str)
+ | replaceOne = compressWhitespace (' ' : (drop 1 str))
+ | replaceTwo = compressWhitespace (' ' : (drop 2 str))
+ | otherwise = head str : compressWhitespace (drop 1 str)
+ where
+ isConstant = or $ map (isOfPrefix str) ["\"", "'"]
+ replaceOne = or $ map (isOfPrefix str) ["\t", "\n", "\r"]
+ replaceTwo = or $ map (isOfPrefix str) [" \t", " \n", " \r", " "]
--------------------------------------------------------------------------------
-- | Function that strips CSS comments away.
stripComments :: String -> String
stripComments [] = []
stripComments str
+ | isConstant = head str : retainConstants stripComments (head str) (drop 1 str)
| isPrefixOf "/*" str = stripComments $ eatComments $ drop 2 str
| otherwise = head str : stripComments (drop 1 str)
where
+ isConstant = or $ map (isOfPrefix str) ["\"", "'"]
eatComments str'
| null str' = []
| isPrefixOf "*/" str' = drop 2 str'
| otherwise = eatComments $ drop 1 str'
+
+--------------------------------------------------------------------------------
+-- | Helper function to handle string constants correctly.
+retainConstants :: (String -> String) -> Char -> String -> String
+retainConstants f delim str
+ | null str = []
+ | isPrefixOf [delim] str = head str : f (drop 1 str)
+ | otherwise = head str : retainConstants f delim (drop 1 str)
+
+--------------------------------------------------------------------------------
+-- | Helper function to determine whether a string is a substring.
+isOfPrefix :: String -> String -> Bool
+isOfPrefix = flip isPrefixOf
diff --git a/tests/Hakyll/Web/CompressCss/Tests.hs b/tests/Hakyll/Web/CompressCss/Tests.hs
new file mode 100644
index 0000000..b356388
--- /dev/null
+++ b/tests/Hakyll/Web/CompressCss/Tests.hs
@@ -0,0 +1,59 @@
+--------------------------------------------------------------------------------
+module Hakyll.Web.CompressCss.Tests
+ ( tests
+ ) where
+
+
+--------------------------------------------------------------------------------
+import Data.Char (toUpper)
+import Test.Framework (Test, testGroup)
+import Test.HUnit (assert, (@=?))
+
+
+--------------------------------------------------------------------------------
+import Hakyll.Web.CompressCss
+import TestSuite.Util
+
+
+--------------------------------------------------------------------------------
+tests :: Test
+tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat
+ [ fromAssertions "compressCss"
+ [
+ -- compress whitespace
+ " something something " @=?
+ compressCss " something \n\t\r something "
+ -- do not compress whitespace in constants
+ , "abc \" \t\n\r \" xyz" @=?
+ compressCss "abc \" \t\n\r \" xyz"
+ , "abc ' \t\n\r ' xyz" @=?
+ compressCss "abc ' \t\n\r ' xyz"
+
+ -- strip comments
+ , "before after" @=? compressCss "before /* abc { } ;; \n\t\r */ after"
+ -- don't strip comments inside constants
+ , "before \"/* abc { } ;; \n\t\r */\" after"
+ @=? compressCss "before \"/* abc { } ;; \n\t\r */\" after"
+
+ -- compress separators
+ , "}" @=? compressCss "; }"
+ , "{};" @=? compressCss " { } ; "
+ -- compress whitespace even after this curly brace
+ , "}" @=? compressCss "; } "
+ -- but do not compress separators inside of constants
+ , "\" { } ; \"" @=? compressCss "\" { } ; \""
+ -- don't compress separators at the start or end of constants
+ , "\" }\"" @=? compressCss "\" }\""
+ , "\"{ \"" @=? compressCss "\"{ \""
+ -- don't get irritated by the wrong constant terminator
+ , "\" ' \"" @=? compressCss "\" ' \""
+ , "' \" '" @=? compressCss "' \" '"
+ -- don't compress whitespace around separators in constants in the middle of a string
+ , "abc '{ '" @=? compressCss "abc '{ '"
+ , "abc \"{ \"" @=? compressCss "abc \"{ \""
+ -- compress whitespace after colons
+ , "abc:xyz" @=? compressCss "abc : xyz"
+ -- compress multiple semicolons
+ , ";" @=? compressCss ";;;;;;;"
+ ]
+ ]
diff --git a/tests/TestSuite.hs b/tests/TestSuite.hs
index 3622301..79eb314 100644
--- a/tests/TestSuite.hs
+++ b/tests/TestSuite.hs
@@ -19,6 +19,7 @@ import qualified Hakyll.Core.Runtime.Tests
import qualified Hakyll.Core.Store.Tests
import qualified Hakyll.Core.UnixFilter.Tests
import qualified Hakyll.Core.Util.String.Tests
+import qualified Hakyll.Web.CompressCss.Tests
import qualified Hakyll.Web.Html.RelativizeUrls.Tests
import qualified Hakyll.Web.Html.Tests
import qualified Hakyll.Web.Pandoc.FileType.Tests
@@ -39,6 +40,7 @@ main = defaultMain
, Hakyll.Core.Store.Tests.tests
, Hakyll.Core.UnixFilter.Tests.tests
, Hakyll.Core.Util.String.Tests.tests
+ , Hakyll.Web.CompressCss.Tests.tests
, Hakyll.Web.Html.RelativizeUrls.Tests.tests
, Hakyll.Web.Html.Tests.tests
, Hakyll.Web.Pandoc.FileType.Tests.tests
diff --git a/web/examples.markdown b/web/examples.markdown
index 9b2c0c6..9e33345 100644
--- a/web/examples.markdown
+++ b/web/examples.markdown
@@ -66,8 +66,6 @@ directly with the default Hakyll site.
[literate source](http://scr.stunts.hu/hakyll.html)
- <http://www.eanalytica.com/>,
[literate source](http://www.eanalytica.com/site/)
-- <http://dikmax.name/>,
- [source](https://github.com/dikmax/dikmax.name)
- <http://cse.iitk.ac.in/users/ppk>,
[source](https://github.com/piyush-kurur-pages/website)
- <http://web.engr.oregonstate.edu/~walkiner/>,
@@ -160,6 +158,8 @@ directly with the default Hakyll site.
[source](https://github.com/kuznero/kuznero.github.io)
- <http://nicolerauch.de/>,
[source](https://github.com/NicoleRauch/BusinessWebsite)
+- <http://johnduhamel.io/>,
+ [source](https://github.com/jjduhamel/blog)
## Hakyll 3.X
diff --git a/web/tutorials/github-pages-tutorial.md b/web/tutorials/github-pages-tutorial.md
index 67410bd..7681353 100644
--- a/web/tutorials/github-pages-tutorial.md
+++ b/web/tutorials/github-pages-tutorial.md
@@ -86,7 +86,7 @@ stack exec myblog build
Update the local list of remote branches to ensure we're able to checkout the branch we want in the next step.
```
-git fetch -all
+git fetch --all
```
Switch to the `master` branch.
@@ -142,7 +142,7 @@ stack exec myblog clean
stack exec myblog build
# Get previous files
-git fetch -all
+git fetch --all
git checkout -b master --track origin/master
# Overwrite existing files with new files
@@ -168,7 +168,13 @@ git stash pop
Earlier it was mentioned a flaw is that deleted files will persist in the published site until deleted manually. This is easily overcome by using `rsync` instead of `cp`.
```
-rsync -a --filter='P _site/' --filter='P _cache/' --filter='P .git/' --filter='P .gitignore' --delete-excluded _site/ .
+rsync -a --filter='P _site/' \
+ --filter='P _cache/' \
+ --filter='P .git/' \
+ --filter='P .gitignore' \
+ --filter='P .stack-work' \
+ --delete-excluded \
+ _site/ .
```
The only drawback this approach has is the requirement that *every* file in your site "go through" Hakyll. Fortunately, in many cases this is not an issue.