on master help sign in
169 files changed+8448−2948
since splitsnapshots
unifiedsplit
.circleci/config.yml+19 .circleci/tickle.sh+24 .ghci+1−1 .gitignore+7−1 .travis.yml−1 CHANGELOG.md+575 CODE_OF_CONDUCT.md+45 LICENSE+1−1 Makefile+6−6 README.markdown+6−2 data/example/css/default.css+122−37 data/example/index.html+2−5 data/example/posts/2012-08-12-spqr.markdown → data/example/posts/2015-08-12-spqr.markdown data/example/posts/2012-10-07-rosa-rosa-rosam.markdown → data/example/posts/2015-10-07-rosa-rosa-rosam.markdown+1 data/example/posts/2012-11-28-carpe-diem.markdown → data/example/posts/2015-11-28-carpe-diem.markdown data/example/posts/2012-12-07-tu-quoque.markdown → data/example/posts/2015-12-07-tu-quoque.markdown+1 data/example/site.hs+12−17 data/example/templates/archive.html+2−3 data/example/templates/default.html+16−16 data/example/templates/post-item.html−3 data/example/templates/post-list.html+7 data/example/templates/post.html+11−3 data/templates/rss-item.xml+1 data/templates/rss.xml+3−2 hakyll.cabal+198−108 lib/Data/List/Extended.hs+15 lib/Data/Yaml/Extended.hs+24 src/Hakyll.hs → lib/Hakyll.hs+10−8 lib/Hakyll/Check.hs+302 lib/Hakyll/Commands.hs+164 src/Hakyll/Core/Compiler.hs → lib/Hakyll/Core/Compiler.hs+90−16 lib/Hakyll/Core/Compiler/Internal.hs+364 src/Hakyll/Core/Compiler/Require.hs → lib/Hakyll/Core/Compiler/Require.hs+6−11 src/Hakyll/Core/Configuration.hs → lib/Hakyll/Core/Configuration.hs+57−19 src/Hakyll/Core/Dependencies.hs → lib/Hakyll/Core/Dependencies.hs+3−4 lib/Hakyll/Core/File.hs+93 src/Hakyll/Core/Identifier.hs → lib/Hakyll/Core/Identifier.hs+1−2 src/Hakyll/Core/Identifier/Pattern.hs → lib/Hakyll/Core/Identifier/Pattern.hs+66−108 lib/Hakyll/Core/Identifier/Pattern/Internal.hs+92 src/Hakyll/Core/Item.hs → lib/Hakyll/Core/Item.hs−2 src/Hakyll/Core/Item/SomeItem.hs → lib/Hakyll/Core/Item/SomeItem.hs src/Hakyll/Core/Logger.hs → lib/Hakyll/Core/Logger.hs+12−7 lib/Hakyll/Core/Metadata.hs+138 lib/Hakyll/Core/Provider.hs+43 lib/Hakyll/Core/Provider/Internal.hs+202 lib/Hakyll/Core/Provider/Metadata.hs+152 src/Hakyll/Core/Provider/MetadataCache.hs → lib/Hakyll/Core/Provider/MetadataCache.hs+9−15 src/Hakyll/Core/Routes.hs → lib/Hakyll/Core/Routes.hs+73−21 src/Hakyll/Core/Rules.hs → lib/Hakyll/Core/Rules.hs+82−17 src/Hakyll/Core/Rules/Internal.hs → lib/Hakyll/Core/Rules/Internal.hs+30−17 src/Hakyll/Core/Runtime.hs → lib/Hakyll/Core/Runtime.hs+72−33 src/Hakyll/Core/Store.hs → lib/Hakyll/Core/Store.hs+77−32 lib/Hakyll/Core/UnixFilter.hs+160 src/Hakyll/Core/Util/File.hs → lib/Hakyll/Core/Util/File.hs+21−9 lib/Hakyll/Core/Util/Parser.hs+31 src/Hakyll/Core/Util/String.hs → lib/Hakyll/Core/Util/String.hs+36−6 src/Hakyll/Core/Writable.hs → lib/Hakyll/Core/Writable.hs lib/Hakyll/Main.hs+180 lib/Hakyll/Preview/Poll.hs+109 lib/Hakyll/Preview/Server.hs+35 lib/Hakyll/Web/CompressCss.hs+101 src/Hakyll/Web/Feed.hs → lib/Hakyll/Web/Feed.hs+71−27 src/Hakyll/Web/Html.hs → lib/Hakyll/Web/Html.hs+61−20 src/Hakyll/Web/Html/RelativizeUrls.hs → lib/Hakyll/Web/Html/RelativizeUrls.hs lib/Hakyll/Web/Paginate.hs+153 lib/Hakyll/Web/Pandoc.hs+165 src/Hakyll/Web/Pandoc/Biblio.hs → lib/Hakyll/Web/Pandoc/Biblio.hs+47−23 lib/Hakyll/Web/Pandoc/Binary.hs+33 src/Hakyll/Web/Pandoc/FileType.hs → lib/Hakyll/Web/Pandoc/FileType.hs+30−21 lib/Hakyll/Web/Redirect.hs+100 src/Hakyll/Web/Tags.hs → lib/Hakyll/Web/Tags.hs+116−43 lib/Hakyll/Web/Template.hs+178 lib/Hakyll/Web/Template/Context.hs+481 lib/Hakyll/Web/Template/Internal.hs+261 lib/Hakyll/Web/Template/Internal/Element.hs+291 lib/Hakyll/Web/Template/Internal/Trim.hs+95 lib/Hakyll/Web/Template/List.hs+91 src/Hakyll/Core/Compiler/Internal.hs−226 src/Hakyll/Core/Metadata.hs−32 src/Hakyll/Core/Provider.hs−46 src/Hakyll/Core/Provider/Internal.hs−94 src/Hakyll/Core/Provider/Metadata.hs−125 src/Hakyll/Core/Provider/Modified.hs−101 src/Hakyll/Core/UnixFilter.hs−120 src/Hakyll/Core/Writable/CopyFile.hs−43 src/Hakyll/Init.hs−37 src/Hakyll/Main.hs−169 src/Hakyll/Web/CompressCss.hs−60 src/Hakyll/Web/Pandoc.hs−131 src/Hakyll/Web/Preview/Poll.hs−43 src/Hakyll/Web/Preview/Server.hs−40 src/Hakyll/Web/Template.hs−123 src/Hakyll/Web/Template/Context.hs−215 src/Hakyll/Web/Template/Internal.hs−45 src/Hakyll/Web/Template/List.hs−53 src/Hakyll/Web/Template/Read.hs−41 src/Init.hs+123 stack.yaml+33 test.hs+30 tests/Hakyll/Core/Dependencies/Tests.hs+5−4 tests/Hakyll/Core/Identifier/Tests.hs+9−17 tests/Hakyll/Core/Provider/Metadata/Tests.hs+62 tests/Hakyll/Core/Provider/Tests.hs+11−13 tests/Hakyll/Core/Routes/Tests.hs+31−18 tests/Hakyll/Core/Rules/Tests.hs+49−30 tests/Hakyll/Core/Runtime/Tests.hs+62−12 tests/Hakyll/Core/Store/Tests.hs+32−17 tests/Hakyll/Core/UnixFilter/Tests.hs+38−9 tests/Hakyll/Core/Util/String/Tests.hs+13−3 tests/Hakyll/Web/CompressCss/Tests.hs+64 tests/Hakyll/Web/Html/RelativizeUrls/Tests.hs+7−3 tests/Hakyll/Web/Html/Tests.hs+24−11 tests/Hakyll/Web/Pandoc/FileType/Tests.hs+26 tests/Hakyll/Web/Template/Context/Tests.hs+52−8 tests/Hakyll/Web/Template/Tests.hs+123−16 tests/TestSuite.hs+15−2 tests/TestSuite/Util.hs+59−48 tests/data/embed.html+1 tests/data/example.md.metadata+3 tests/data/images/favicon.ico tests/data/just-meta.html+7 tests/data/just-meta.html.out+7 tests/data/partial-helper.html+3 tests/data/partial.html+3 tests/data/partial.html.out+7 tests/data/posts/2018-09-26.md+1 tests/data/posts/2019/05/10/tomorrow.md+1 tests/data/strip.html+34 tests/data/strip.html.out+18 tests/data/template.html+26−1 tests/data/template.html.out+24−1 web/about.markdown+3 web/changelog.markdown−150 web/css/default.css+13−7 web/css/syntax.css+61−18 web/examples.markdown+176−21 web/images/hakyll-system-2.png web/index.markdown+4−2 web/libraries.markdown+21 web/site.hs+68−22 web/templates/default.html+9−9 web/templates/tutorial-item.html−1 web/templates/tutorials.html+47−5 web/tutorials/01-installation.markdown+20−17 web/tutorials/02-basics.markdown+13−8 web/tutorials/03-rules-routes-compilers.markdown+21−1 web/tutorials/04-compilers.markdown+149−26 web/tutorials/05-snapshots-feeds.markdown+129 web/tutorials/06-versions.markdown+68 web/tutorials/guide.markdown → web/tutorials/a-guide-to-the-hakyll-module-zoo.markdown+2−5 web/tutorials/external-add-tags-to-your-hakyll-blog.md+6 web/tutorials/external-blogger-port.md+6 web/tutorials/external-cassius-template.md+6 web/tutorials/external-clean-urls-with-hakyll.md+6 web/tutorials/external-deploying-with-widely.md+6 web/tutorials/external-external-code-inclusion.md+6 web/tutorials/external-functionfield.md+6 web/tutorials/external-github-pages-circle-ci.md+6 web/tutorials/external-inlining-and-compressing-css.md+6 web/tutorials/faq.markdown+59−32 web/tutorials/github-pages-tutorial.md+181 web/tutorials/hakyll-3-to-hakyll4-migration-guide.markdown+78 web/tutorials/robertwpearce-01-setup-and-initial-customization.html+5 web/tutorials/robertwpearce-02-generating-a-sitemap-xml-file.html+5 web/tutorials/robertwpearce-03-generating-rss-and-atom-xml-feeds.html+5 web/tutorials/robertwpearce-04-copying-static-files-for-your-build.html+5 web/tutorials/robertwpearce-05-generating-custom-post-filenames-from-a-title-slug.html+5 web/tutorials/using-clay-with-hakyll.markdown+52 web/tutorials/using-teasers-in-hakyll.markdown+109
.circleci/config.ymlnew file+19
@@ -0,0 +1,19 @@
1+version: 2
2+jobs:
3+ build:
4+ docker:
5+ - image: 'fpco/stack-build:latest'
6+ steps:
7+ - checkout
8+ - restore_cache:
9+ key: 'v3-hakyll-{{ arch }}-{{ .Branch }}'
10+ - run:
11+ # We set jobs to 1 here because that prevents Out-Of-Memory exceptions
12+ # while compiling dependencies.
13+ name: 'Install dependencies'
14+ command: '.circleci/tickle.sh stack build --test --copy-bins --jobs=1'
15+ - save_cache:
16+ key: 'v3-hakyll-{{ arch }}-{{ .Branch }}-{{ .Revision }}'
17+ paths:
18+ - '~/.stack-work'
19+ - '~/.stack'
.circleci/tickle.shnew file+24
@@ -0,0 +1,24 @@
1+#!/bin/bash
2+set -o nounset -o errexit -o pipefail
3+
4+function tickle() {
5+ while [ true ]; do
6+ echo "[$(date +%H:%M:%S)] Tickling..."
7+ sleep 60
8+ done
9+}
10+
11+echo "Forking tickle process..."
12+tickle &
13+TICKLE_PID=$!
14+
15+echo "Forking build process..."
16+eval $@ &
17+BUILD_PID=$!
18+
19+echo "Waiting for build thread ($BUILD_PID)..."
20+wait $BUILD_PID
21+
22+echo "Killing tickle thread ($TICKLE_PID)..."
23+kill $TICKLE_PID
24+echo "All done!"
.ghci+1−1
@@ -1 +1 @@
1−:set -isrc -isrc-interval -itests -idist/build/autogen -DPREVIEW_SERVER
1+:set -isrc -itests -idist/build/autogen -DPREVIEW_SERVER
.gitignore+7−1
@@ -1,4 +1,4 @@
1−# Ignore swap files and cabal output.
1+# Ignore swap files and stack/cabal output.
22*.hi
33*.o
44*.swo
@@ -8,6 +8,9 @@
88TAGS
99dist
1010tags
11+cabal.sandbox.config
12+.cabal-sandbox/
13+.stack-work
1114
1215# Ignore test builds.
1316tests/Main
@@ -20,3 +23,6 @@ web/_cache
2023web/_site
2124web/site
2225web/reference
26+
27+# This is generated from the CHANGELOG
28+web/releases.markdown
.travis.ymldeleted−1
@@ -1 +0,0 @@
1−language: haskell
CHANGELOG.mdnew file+575
@@ -0,0 +1,575 @@
1+---
2+title: Releases
3+---
4+
5+# Releases
6+
7+## Hakyll 4.13.0.0 (2019-08-30)
8+
9+- Improved documentation in many places (contribution by Bergi)
10+- Significantly improve error messages when reading and applying templates
11+ (contribution by Bergi)
12+ * `empty` and `fail` for `Compiler` now fail without or with a message,
13+ allowing for much better debug output
14+ * `renderFeed`, `renderRssWithTemplates` and `renderAtomWithTemplates` now
15+ take `Template` rather than `String` arguments
16+- Add option to specify date in directory structure,e.g.
17+ `posts/2019/05/10/tomorrow.md` (contribution by Taeer Bar-Ya)
18+- Bump QuickCheck to 2.13
19+
20+## Hakyll 4.12.5.2 (2019-05-09)
21+
22+- Bump pandoc to 2.7
23+- Add `srcset` to the list of URL attributes (contribution by c50a326)
24+- Expose `getCategory` in `Hakyll.Web.Tags` (contribution by Ng Wei En)
25+- Fix issue where `published` overwrites the user's context (contribution by
26+ ncaq)
27+
28+## Hakyll 4.12.5.1 (2019-02-03)
29+
30+- Bump pandoc to 2.6
31+- Bump pandoc-citeproc to 0.16
32+
33+## Hakyll 4.12.5.0 (2019-01-12)
34+
35+- Update dependencies (contribution by Hexirp):
36+ * Bump containers to 0.6
37+ * Bump yaml to 0.11
38+ * Bump pandoc to 2.5
39+ * Bump pandoc-citeproc to 0.15
40+ * Bump tasty to 1.2
41+- Correct assertion in unixFilterError test
42+ (contribution by Jim McStanton)
43+- Add renderRssWithTemplates, renderAtomWithTemplates
44+ (contribution by Abhinav Sarkar)
45+- Speed up hashing in cache (contribution by 0xd34df00d)
46+- Update type of fromFilePath to use FilePath instead of String
47+ (contribution by Jim McStanton)
48+- Drop extension when parsing dates in filepaths (contribution by
49+ Gabriel Aumala)
50+
51+## Hakyll 4.12.4.0 (2018-08-13)
52+
53+- Bump yaml to 0.10
54+- Bump file-embed to 0.0.11
55+- Bump QuickCheck to 2.12
56+- Use makeRelativeToProject with embedFile for stack ghci (contribution by
57+ Michael Sloan)
58+
59+## Hakyll 4.12.3.0 (2018-05-29)
60+
61+- Bump tasty to 1.1
62+- Bump fsnotify to 0.3
63+
64+## Hakyll 4.12.2.0
65+
66+- Bump pandoc to 2.2
67+- Fix path resolution in Pandoc.Biblio (contribution by knih)
68+
69+## Hakyll 4.12.1.0
70+
71+- Fix hakyll-init on older GHC versions
72+- Make the Pandoc dependency optional
73+
74+## Hakyll 4.12.0.1
75+
76+- Bump resourcet to 1.2 in test section
77+
78+## Hakyll 4.12.0.0
79+
80+- Add Semigroup instances for existing Monoids (contribution by
81+ Christian Barcenas)
82+- Fix issue with CPP and comment containing `/*`
83+- Add `withTagList` (contribution by Oleg Grenrus)
84+- Improve CSS compression (contribution by Bergi)
85+- Bump pandoc-citeproc to 0.14
86+- Bump resourcet to 1.2
87+- Bump time to 1.9
88+- Bump http-types to 0.12
89+- Bump http-conduit to 2.3
90+- Bump tasty-quickcheck to 0.10
91+
92+## Hakyll 4.11.0.0
93+
94+- Bump binary to 0.9
95+- Bump pandoc to 2.1
96+- Bump pandoc-citeproc to 0.13
97+- Bump http-types to 0.11
98+- Bump QuickCheck to 2.11
99+- Bump tasty to 1.0
100+- Bump tasty-hunit to 0.10
101+- Remove system-filepath dependency
102+- Embed feed templates rather than using data-files (contribution by Roman
103+ Kuznetsov)
104+- Fix pthread link error on GHC-8.2.2 (contribution by Shinya Yamaguchi)
105+- Extend capture with Regex handling (contribution by frederik-h)
106+
107+## Hakyll 4.10.0.0
108+
109+- Bump Pandoc to 2.0 (contribution by Vaibhav Sagar)
110+- Fix compression of calc() in CSS (contribution by Krzysztof Jurewicz)
111+- Make unixFilter output stderr when failing (contribution by Nick Boultbee)
112+- Export Check type from `Hakyll.Commands` (contribution by Futtetennista)
113+- Add overwritten files check to `hakyll-init` (contribution by Ilya Murzinov)
114+- Expose & document `hakyllWithExitCodeAndArgs`, `Options`, and `Command`
115+ (contribution by Michael Walker)
116+- Add check for non-overlapping redirects (contribution by gwern)
117+- Fix early exit when calling check as a library
118+
119+## Hakyll 4.9.8.0
120+
121+- Bump pandoc-citeproc to 0.10.5
122+- Bump optparse-applicative to 0.14
123+- Bump QuickCheck to 2.10
124+- Bump tasty-quickcheck to 0.9
125+- Restructure .cabal to avoid redundant compilation (contribution by
126+ Christopher League)
127+
128+## Hakyll 4.9.7.0
129+
130+- Fix compilation trouble with `Options.Applicative`
131+- Some small CSS compression improvements (contribution by Nicole Rauch)
132+
133+## Hakyll 4.9.6.0
134+
135+- Tighten dependency on `pandoc-citeproc` (contribution by Mikhail Glushenkov)
136+- Enable using a custom parser for command line arguments (contribution by
137+ Alberto)
138+- Update examples to semantic HTML (contribution by Elie Génard)
139+- Better error for `cached` on non-existing file
140+- Provide an `$allPages$` key when doing pagination
141+- Preserve file metadata in `copyFileCompiler` (contribution by frederik-h)
142+
143+## Hakyll 4.9.5.1
144+
145+- Bump blaze-html dependency to 0.9
146+- Bump blaze-markup dependency to 0.8
147+- Bump process dependency to 1.5
148+
149+## Hakyll 4.9.5.0
150+
151+- Fix compilation issue with `Hakyll.Check` if `checkExternal` is disabled
152+ (Fix by Magnus Therning)
153+
154+## Hakyll 4.9.4.0
155+
156+- Make `./site check` concurrent
157+- Bump directory dependency to 1.3
158+- Bump time dependency to 1.7
159+- Bump vector dependency to 0.12
160+
161+## Hakyll 4.9.3.0
162+
163+- Add a `Hakyll.Web.Redirect` module (contribution by gwern)
164+- Expose `Hakyll.Commands`
165+- Fix the exit code behaviour of `./site check`
166+
167+## Hakyll 4.9.2.0
168+
169+- Fix integer fields in YAML metadata (Fix by Nikolaos S. Papaspyrou)
170+- Bump pandoc dependency to 1.19
171+
172+## Hakyll 4.9.1.0
173+
174+- Allow optparse-applicative 0.13, QuickCheck 2.9, and pandoc 1.18
175+ (contributions by Chris Wong and Felix Yan)
176+- Fix extra test files for packaging source files (contribution by Julien
177+ Langlois)
178+
179+## Hakyll 4.9.0.0
180+
181+This release switches over some dependencies to alternatives, in order to clean
182+up some stuff and build on a wider variety of setups (stack/cabal).
183+
184+- Move from `test-framework` to `tasty`
185+- Fix feed generator when item contains CDATA (contribution by Yann Esposito)
186+- Fix CompressCSS to not modify string constants (contribution by Nicole Rauch)
187+- Fix YAML dependency issue (contribution by Jens Peterson)
188+- Move from `cmdargs` to `optparse-applicative` (contribution by sk3r)
189+- Allow for trimming whitespace in templates (contribution by Sam Davis)
190+- Improve error messages for template parsing (contribution by Lorenzo
191+ Tabacchini)
192+- Improvements to the installation instructions (contribution by Thomas Koch)
193+- Move from `snap` to `warp` for preview servier (contribution by Arguggi)
194+- Fix error in CompressCSS (contribution by Luca Molteni)
195+- Move example from XHTML to HTML5 (contribution by Peter Doherty)
196+- Make errors in check less verbose (contribution by Jan Tojnar)
197+- Work on building with GHC 8.0.1 (contribution by Rohan Jain)
198+
199+## Hakyll 4.8.3.2
200+
201+This release is compatible with GHC 8.0.1, although `previewServer` might not
202+work yet on some setups.
203+
204+- Allow data-default 0.7, pandoc-citeproc 0.10, and tagsoup 0.14 (contributions
205+ by Paul van der Walt and Felix Yan)
206+- Allow binary 0.8, process 1.4, time 1.6 (contribution by Sergei Trofimovich)
207+- Fix issue with `.metadata` file reading
208+
209+## Hakyll 4.8.3.1
210+
211+- Bump scientific dependency to 0.3.4
212+
213+## Hakyll 4.8.3.0
214+
215+- Fix another compilation issue wrt. orphan `Show` instance from regex-tdfa
216+ (contribution by Sergei Trofimovich)
217+
218+## Hakyll 4.8.2.0
219+
220+- Fix compilation issue wrt. orphan `Show` instance from regex-tdfa
221+
222+## Hakyll 4.8.1.0
223+
224+- Fix compilation on windows
225+
226+## Hakyll 4.8.0.1
227+
228+- Fix issue with test suite
229+
230+## Hakyll 4.8.0.0
231+
232+- Support full YAML in page metadata
233+- Bump data-default dependency to 0.6
234+- Add snippet field for literal includes in templates (contribution by Nicolas
235+ Mattia)
236+
237+## Hakyll 4.7.5.2
238+
239+- Bump pandoc dependency to 1.17 (contribution by Felix Yan)
240+- Fix `unixFilter` documentation (contribution by Richard Cook)
241+- Bump example posts (contribution by Andrew Barchuk)
242+- Add a template compiler that only uses the template body (contribution by
243+ Bergi)
244+
245+## Hakyll 4.7.5.1
246+
247+- Bump pandoc and pandoc-citeproc dependencies to 1.16 and 0.9 respectively
248+
249+## Hakyll 4.7.5.0
250+
251+- Expose templating engine
252+- Fix bug in feed context precedence (contribution by Yuriy Syrovetskiy)
253+- Bump http-types dependency to 0.9
254+
255+## Hakyll 4.7.4.0
256+
257+- Expose `getItemModificationTime`
258+
259+## Hakyll 4.7.3.1
260+
261+- Bump pandoc-citeproc dependency to 0.8
262+
263+## Hakyll 4.7.3.0
264+
265+- Bump HUnit dependency to 1.3
266+- Add `poster` as an URL attribute (contribution by vtduncan)
267+- Prevent `hakyll-init` from generating directories with leading hyphen
268+ (contribution by Javran Cheng)
269+
270+## Hakyll 4.7.2.3
271+
272+- Fix time dependency in tests
273+
274+## Hakyll 4.7.2.2
275+
276+- Relax time dependency
277+
278+## Hakyll 4.7.2.1
279+
280+- Bump fsnotify dependency to 0.2
281+
282+## Hakyll 4.7.2.0
283+
284+- Improve documentation of `getResourceXXX` functions (contribution by Matthias
285+ C. M. Troffaes)
286+- Allow for empty templates
287+- Bump pandoc dependency to 1.15
288+
289+## Hakyll 4.7.1.0
290+
291+- Drop old-time, old-locale, time-locale-compat dependencies
292+- Add convenicence `pandocBiblioCompiler` (contribution by
293+ Matthias C. M. Troffaes)
294+- Add support for mediawiki (contribution by Chen Lei)
295+
296+## Hakyll 4.7.0.0
297+
298+- Bump pandoc to 1.14. This will break a lot of sites: since the pandoc parser
299+ might now return an error message, it is ran inside the `Compiler` monad where
300+ we can nicely handle the error.
301+
302+## Hakyll 4.6.9.0
303+
304+- Let caller decide exit (fix by Erik Dominikus)
305+- Bump pandoc-citeproc dependency
306+
307+## Hakyll 4.6.8.1
308+
309+- Fix test suite dependencies
310+
311+## Hakyll 4.6.8.0
312+
313+- Fix building on GHC 7.10 (fix by Charles Strahan)
314+- Add support for a custom teaser separator (contribution by Tom Sydney
315+ Kerckhove)
316+- Let Pandoc handle DocBook files (contribution by Joshua SImmons)
317+
318+## Hakyll 4.6.7.1
319+
320+- Bump dependencies
321+
322+## Hakyll 4.6.7.0
323+
324+- Bump dependencies
325+- Fix bug where hakyll-init would create a file called `name.cabal.cabal` (fix
326+ by Hans-Peter Deifel)
327+
328+## Hakyll 4.6.6.0
329+
330+- Fix compilation error when preview server is disabled (fix by Magnus Therning)
331+- Add author name by default to RSS feeds (contribution by Calen Pennington)
332+
333+## Hakyll 4.6.5.0
334+
335+- Bump dependencies
336+- Fix garbled "Listening on 0.0.0.0:8000" message
337+- Add `boolField` (contribution by Ferenc Wágner)
338+
339+## Hakyll 4.6.4.0
340+
341+- Fix another dependency handling bug when using snapshots
342+- Add `matchMetadata` for examining metadata when defining rules
343+
344+## Hakyll 4.6.3.0
345+
346+- Fix dependency handling bug
347+
348+## Hakyll 4.6.2.0
349+
350+- Loosen `binary` dependency
351+- Make dependency handling more granular so you can depend on specific snapshots
352+ of an item
353+
354+## Hakyll 4.6.1.0
355+
356+- Bump `fsnotify` and `pandoc-citeproc` dependencies
357+- Rewrite polling code a bit
358+
359+## Hakyll 4.6.0.0
360+
361+- Added `listFieldWith` function
362+- Improved `rulesExtraDependencies` behaviour
363+- Changed function syntax in templates from `$foo arg1 arg2$` to
364+ `$foo("arg1", "arg2")$`
365+- Support parsing date from directory names in addition to file names
366+
367+## Hakyll 4.5.5.0
368+
369+- Fix Binary instances for `pandoc` and `pandoc-citeproc`
370+- Fix `network-uri` dependency issue
371+
372+## Hakyll 4.5.4.0
373+
374+- Fix issue with HTML entities when running `withUrls` and `demoteHeaders`.
375+- Generate a cabal file for the initialised site.
376+- Add pagination support.
377+
378+## Hakyll 4.5.3.0
379+
380+- Bump Pandoc to 1.12.4 to include the org-mode reader.
381+
382+## Hakyll 4.5.2.0
383+
384+- Fix rebuilding everything issue with latest directory (contribution by Jorge
385+ Israel Peña)
386+- Fix issue with `toSiteRoot` (contribution by Izzy Cecil)
387+- Fix issue with tag dependencies, slightly improve caching
388+
389+## Hakyll 4.5.0.0
390+
391+- Fix issue with syntax highlighting and line numbers (contribution by Adelbert
392+ Chang)
393+- Improve documentation for `Context` (contribution by Daniil Frumin)
394+- Added `IsString` instance for `Template`
395+- Added the `pandocCompilerWithTransformM` function (contribution by Daniil
396+ Frumin)
397+- Make `./site check` return the right exit code (contribution by Andres Loeh)
398+- Use OS threads to make `./site watch` work nicely on Windows (contribution by
399+ Simonas Kazlauskas)
400+- Make the `unixFilter` function work better on windows by calling `shell`
401+ (contribution by Collin J. Doering)
402+- Add a command-line flag to bind on a user-specified host (contribution by
403+ chrisdotcode)
404+
405+## Hakyll 4.4.3.0
406+
407+- Fix issue when using `metadataRoute` after other custom routes
408+
409+## Hakyll 4.4.2.0
410+
411+- Fix issue where Hakyll would not detect a change if a `.metadata` file was
412+ deleted
413+
414+## Hakyll 4.4.1.0
415+
416+- Use Pandoc 1.12 highlighting by default
417+
418+## Hakyll 4.4.0.0
419+
420+- Update to work with Pandoc 1.12. This changes the type of `readPandocBibilio`:
421+ the `CSL` argument is no longer optional (contribution by Jorge Israel Peña)
422+
423+- Fix incorrect output of `toSiteRoot` on windows (contribution by Saeid
424+ Al-Wazzan)
425+
426+- Add a preview port option to `Configuration` (contribution by Jorge Israel
427+ Peña)
428+
429+- Add `watch` command that polls for changes but does not necessarily launch a
430+ server (contribution by Eric Stolten)
431+
432+- Generalise type of `metadataField`
433+
434+- Fix issue where metadata was not correctly loaded when using versions
435+
436+## Hakyll 4.3.3.0
437+
438+- Re-add the `functionField` function
439+
440+## Hakyll 4.3.2.0
441+
442+- Re-add the `mapContext` function
443+
444+- Unescape internal URLs when using `./site check` (contribution by Marc-Antoine
445+ Perennou)
446+
447+## Hakyll 4.3.1.0
448+
449+- Make teasers undefined if no `<!--more-->` comment is found
450+
451+- Sanitize tag URLs (contribution by Simonas Kazlauskas)
452+
453+## Hakyll 4.3.0.0
454+
455+- Add conditionals, partials and for loops to the template system (includes a
456+ contribution by Ivan N. Veselov)
457+
458+- Improvements to the preview functionality on windows (contribution by Jorge
459+ Israel Peña)
460+
461+- Add pagination support (contribution by Anton Dubovik)
462+
463+- Slight speedup for the Hakyll cache (contribution by justnoxx)
464+
465+- Add teaser functionality (contribution by Ivan N. Veselov)
466+
467+- Make `./site check` work with scheme-relative URLs (contribution by Simonas
468+ Kazlauskas)
469+
470+- The `./site deploy` command can now be customized with Haskell code
471+ (contribution by Samuel Tardieu)
472+
473+- Use `hsnotify` for proper polling instead of sleep loop on all platforms
474+ (contribution by Simonas Kazlauskas)
475+
476+- More useful debug info available
477+
478+## Hakyll 4.2.2.0
479+
480+- Fix issue with `Alternative` instance of `Compiler`
481+
482+## Hakyll 4.2.1.1
483+
484+*March 9, 2013*
485+
486+- Make `http-conduit` dependency optional by adding a `checkExternal` cabal flag
487+
488+## Hakyll 4.2.1.0
489+
490+*March 7, 2013*
491+
492+- Fix issue where `copyFileCompiler` ignored `providerDirectory`
493+
494+## Hakyll 4.2.0.0
495+
496+*March 7, 2013*
497+
498+- Read second extension for `.lhs`, e.g. `.md.lhs` or `.tex.lhs` (contribution
499+ by Alexander Vershilov)
500+
501+- Speedup initialization by using modification times instead of hashing files
502+
503+- Speedup initialization with a rewritten resource provider
504+
505+- Fix `./site check` not working with sites that require a user agent (e.g.
506+ <http://www.wikipedia.org/>)
507+
508+- Change `chronological` and `recentFirst` to actually look at the dates of
509+ items. This changes their types from:
510+
511+ chronological, recentFirst :: [Item a] -> [Item a]
512+
513+ to:
514+
515+ chronological, recentFirst
516+ :: MonadMetadata m => [Item a] -> m [Item a]
517+
518+ (contribution by Simonas Kazlauskas)
519+
520+- Add `metadataRoute`, so it is now possible to use metadata when determining
521+ routes
522+
523+- Improve metadata parser for multiline metadata fields (contribution by Peter
524+ Jones)
525+
526+- Add the `getMetadataField` utility
527+
528+## Hakyll 4.1.4.0
529+
530+*January 26, 2013*
531+
532+- Export the flexible `renderTags` function
533+
534+## Hakyll 4.1.3.0
535+
536+*January 26, 2013*
537+
538+- Export the constructor of the `Tags` datatype
539+
540+## Hakyll 4.1.2.0
541+
542+*January 20, 2013*
543+
544+- Fix an issue where a dependency cycle would lead to infinite recursion/stack
545+ overflow
546+
547+## Hakyll 4.1.1.0
548+
549+*January 20, 2013*
550+
551+- Fix an issue regarding `relativizeUrls` expanding `<meta />` to
552+ `<meta></meta>`
553+
554+## Hakyll 4.1.0.0
555+
556+*January 20, 2013*
557+
558+Update to use Pandoc 1.10, this requires changes to your `site.hs` if you're
559+using custom Pandoc options or the `Hakyll.Web.Pandoc.Biblio` module.
560+
561+- `defaultHakyllParserState` renamed to `defaultHakyllReaderOptions`
562+
563+- The type of `readPandocBiblio` changed
564+
565+Because of the many changes, this release is no longer compatible with Pandoc
566+1.9.
567+
568+## Hakyll 4.0.0.0
569+
570+*January 16, 2013*
571+
572+The Initial release of Hakyll 4, see
573+[this blogpost](http://jaspervdj.be/posts/2013-01-16-hakyll-4.0.html) and
574+[the migration guide](/tutorials/hakyll-3-to-hakyll4-migration-guide.html) for
575+an overview of changes.
CODE_OF_CONDUCT.mdnew file+45
@@ -0,0 +1,45 @@
1+# Contributor Covenant Code of Conduct
2+
3+## Our Pledge
4+
5+In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+## Our Standards
8+
9+Examples of behavior that contributes to creating a positive environment include:
10+* Using welcoming and inclusive language
11+* Being respectful of differing viewpoints and experiences
12+* Gracefully accepting constructive criticism
13+* Focusing on what is best for the community
14+* Showing empathy towards other community members
15+
16+Examples of unacceptable behavior by participants include:
17+
18+* The use of sexualized language or imagery and unwelcome sexual attention or advances
19+* Trolling, insulting/derogatory comments, and personal or political attacks
20+* Public or private harassment
21+* Publishing others' private information, such as a physical or electronic address, without explicit permission
22+* Other conduct which could reasonably be considered inappropriate in a professional setting
23+
24+## Our Responsibilities
25+
26+Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
27+
28+Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
29+
30+## Scope
31+
32+This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
33+
34+## Enforcement
35+
36+Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by [contacting the project team at m@jaspervdj.be](mailto:m@jaspervdj.be). All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
37+
38+Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
39+
40+## Attribution
41+
42+This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
43+
44+[homepage]: http://contributor-covenant.org
45+[version]: http://contributor-covenant.org/version/1/4/
LICENSE+1−1
@@ -1,4 +1,4 @@
1−Copyright (c) 2009, Jasper Van der Jeugt
1+Copyright (c) 2009 - 2017, Jasper Van der Jeugt
22 
33All rights reserved.
44 
Makefile+6−6
@@ -1,9 +1,9 @@
1+PACKAGE="$(shell stack list-dependencies --separator='-' | grep hakyll)"
2+LOCAL_DOC_ROOT="$(shell stack path --local-doc-root)"
3+
14# Generate the docs and copy them to the website dir
25haddock:
3− cabal haddock --hyperlink-source
4− rm -rf web/reference/
5− cp -r dist/doc/html/hakyll/ web/reference/
6+ stack build --haddock --no-haddock-deps
7+ rsync -r "$(LOCAL_DOC_ROOT)/$(PACKAGE)/" web/reference/
68
7−# Run the tests
8−test:
9− runghc -isrc -itests tests/TestSuite.hs
9+.PHONY: haddock
README.markdown+6−2
@@ -1,11 +1,15 @@
11# hakyll
22
3−[![Build Status](https://secure.travis-ci.org/jaspervdj/hakyll.png?branch=master)](http://travis-ci.org/jaspervdj/hakyll)
3+[![Build Status](https://img.shields.io/circleci/project/github/jaspervdj/hakyll.svg)](https://circleci.com/gh/jaspervdj/hakyll)
44
55Hakyll is a static site generator library in Haskell. More information
6−(including and a tutorial) can be found on
6+(including a tutorial) can be found on
77[the hakyll homepage](http://jaspervdj.be/hakyll).
88
99You can install this library using cabal:
1010
1111 cabal install hakyll
12+
13+Or using stack:
14+
15+ stack install hakyll
data/example/css/default.css+122−37
@@ -1,56 +1,141 @@
1−body {
2− color: black;
3− font-size: 16px;
4− margin: 0px auto 0px auto;
5− width: 600px;
1+html {
2+ font-size: 62.5%;
63}
74
8−div#header {
9− border-bottom: 2px solid black;
10− margin-bottom: 30px;
11− padding: 12px 0px 12px 0px;
5+body {
6+ font-size: 1.6rem;
7+ color: #000;
128}
139
14−div#logo a {
15− color: black;
16− float: left;
17− font-size: 18px;
18− font-weight: bold;
19− text-decoration: none;
10+header {
11+ border-bottom: 0.2rem solid #000;
2012}
2113
22−div#header #navigation {
23− text-align: right;
14+nav {
15+ text-align: right;
2416}
2517
26−div#header #navigation a {
27− color: black;
28− font-size: 18px;
29− font-weight: bold;
30− margin-left: 12px;
31− text-decoration: none;
32− text-transform: uppercase;
18+nav a {
19+ font-size: 1.8rem;
20+ font-weight: bold;
21+ color: black;
22+ text-decoration: none;
23+ text-transform: uppercase;
3324}
3425
35−div#footer {
36− border-top: solid 2px black;
37− color: #555;
38− font-size: 12px;
39− margin-top: 30px;
40− padding: 12px 0px 12px 0px;
41− text-align: right;
26+footer {
27+ margin-top: 3rem;
28+ padding: 1.2rem 0;
29+ border-top: 0.2rem solid #000;
30+ font-size: 1.2rem;
31+ color: #555;
4232}
4333
4434h1 {
45− font-size: 24px;
35+ font-size: 2.4rem;
4636}
4737
4838h2 {
49− font-size: 20px;
39+ font-size: 2rem;
40+}
41+
42+article .header {
43+ font-size: 1.4rem;
44+ font-style: italic;
45+ color: #555;
46+}
47+
48+.logo a {
49+ font-weight: bold;
50+ color: #000;
51+ text-decoration: none;
52+}
53+
54+@media (max-width: 319px) {
55+ body {
56+ width: 90%;
57+ margin: 0;
58+ padding: 0 5%;
59+ }
60+ header {
61+ margin: 4.2rem 0;
62+ }
63+ nav {
64+ margin: 0 auto 3rem;
65+ text-align: center;
66+ }
67+ footer {
68+ text-align: center;
69+ }
70+ .logo {
71+ text-align: center;
72+ margin: 1rem auto 3rem;
73+ }
74+ .logo a {
75+ font-size: 2.4rem;
76+ }
77+ nav a {
78+ display: block;
79+ line-height: 1.6;
80+ }
81+}
82+
83+@media (min-width: 320px) {
84+ body {
85+ width: 90%;
86+ margin: 0;
87+ padding: 0 5%;
88+ }
89+ header {
90+ margin: 4.2rem 0;
91+ }
92+ nav {
93+ margin: 0 auto 3rem;
94+ text-align: center;
95+ }
96+ footer {
97+ text-align: center;
98+ }
99+ .logo {
100+ text-align: center;
101+ margin: 1rem auto 3rem;
102+ }
103+ .logo a {
104+ font-size: 2.4rem;
105+ }
106+ nav a {
107+ display: inline;
108+ margin: 0 0.6rem;
109+ }
50110}
51111
52−div.info {
53− color: #555;
54− font-size: 14px;
55− font-style: italic;
112+@media (min-width: 640px) {
113+ body {
114+ width: 60rem;
115+ margin: 0 auto;
116+ padding: 0;
117+ }
118+ header {
119+ margin: 0 0 3rem;
120+ padding: 1.2rem 0;
121+ }
122+ nav {
123+ margin: 0;
124+ text-align: right;
125+ }
126+ nav a {
127+ margin: 0 0 0 1.2rem;
128+ display: inline;
129+ }
130+ footer {
131+ text-align: right;
132+ }
133+ .logo {
134+ margin: 0;
135+ text-align: left;
136+ }
137+ .logo a {
138+ float: left;
139+ font-size: 1.8rem;
140+ }
56141}
data/example/index.html+2−5
@@ -11,9 +11,6 @@ title: Home
1111<p>I've reproduced a list of recent posts here for your reading pleasure:</p>
1212
1313<h2>Posts</h2>
14+$partial("templates/post-list.html")$
1415
15−<ul>
16− $posts$
17−</ul>
18−
19−<p>…or you can find more in the <a href="/archive.html">archives</a>.
16+<p>…or you can find more in the <a href="/archive.html">archives</a>.</p>
data/example/posts/2012-08-12-spqr.markdown → data/example/posts/2015-08-12-spqr.markdownrenamed

The content is the same.

data/example/posts/2012-10-07-rosa-rosa-rosam.markdown → data/example/posts/2015-10-07-rosa-rosa-rosam.markdownrenamed+1
@@ -1,5 +1,6 @@
11---
22title: Rosa Rosa Rosam
3+author: Ovidius
34---
45
56Suspendisse pharetra ullamcorper sem et auctor. Suspendisse vitae tellus eu
data/example/posts/2012-11-28-carpe-diem.markdown → data/example/posts/2015-11-28-carpe-diem.markdownrenamed

The content is the same.

data/example/posts/2012-12-07-tu-quoque.markdown → data/example/posts/2015-12-07-tu-quoque.markdownrenamed+1
@@ -1,5 +1,6 @@
11---
22title: Tu Quoque
3+author: Julius
34---
45
56Vestibulum leo turpis, dignissim quis ultrices sit amet, iaculis ac ligula.
data/example/site.hs+12−17
@@ -1,7 +1,6 @@
11--------------------------------------------------------------------------------
22{-# LANGUAGE OverloadedStrings #-}
3−import Control.Applicative ((<$>))
4−import Data.Monoid (mappend)
3+import Data.Monoid (mappend)
54import Hakyll
65
76
@@ -29,12 +28,13 @@ main = hakyll $ do
2928 >>= loadAndApplyTemplate "templates/default.html" postCtx
3029 >>= relativizeUrls
3130
32− match "archive.html" $ do
31+ create ["archive.html"] $ do
3332 route idRoute
3433 compile $ do
34+ posts <- recentFirst =<< loadAll "posts/*"
3535 let archiveCtx =
36− field "posts" (\_ -> postList recentFirst) `mappend`
37− constField "title" "Archives" `mappend`
36+ listField "posts" postCtx (return posts) `mappend`
37+ constField "title" "Archives" `mappend`
3838 defaultContext
3939
4040 makeItem ""
@@ -46,14 +46,18 @@ main = hakyll $ do
4646 match "index.html" $ do
4747 route idRoute
4848 compile $ do
49− let indexCtx = field "posts" $ \_ -> postList (take 3 . recentFirst)
49+ posts <- recentFirst =<< loadAll "posts/*"
50+ let indexCtx =
51+ listField "posts" postCtx (return posts) `mappend`
52+ constField "title" "Home" `mappend`
53+ defaultContext
5054
5155 getResourceBody
5256 >>= applyAsTemplate indexCtx
53− >>= loadAndApplyTemplate "templates/default.html" postCtx
57+ >>= loadAndApplyTemplate "templates/default.html" indexCtx
5458 >>= relativizeUrls
5559
56− match "templates/*" $ compile templateCompiler
60+ match "templates/*" $ compile templateBodyCompiler
5761
5862
5963--------------------------------------------------------------------------------
@@ -61,12 +65,3 @@ postCtx :: Context String
6165postCtx =
6266 dateField "date" "%B %e, %Y" `mappend`
6367 defaultContext
64−
65−
66−--------------------------------------------------------------------------------
67−postList :: ([Item String] -> [Item String]) -> Compiler String
68−postList preprocess = do
69− posts <- preprocess <$> loadAll "posts/*"
70− itemTpl <- loadBody "templates/post-item.html"
71− list <- applyTemplateList itemTpl postCtx posts
72− return list
data/example/templates/archive.html+2−3
@@ -1,3 +1,2 @@
1−<ul>
2− $posts$
3−</ul>
1+Here you can find all my previous posts:
2+$partial("templates/post-list.html")$
data/example/templates/default.html+16−16
@@ -1,33 +1,33 @@
1−<?xml version="1.0" encoding="UTF-8"?>
2−<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3−"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4−<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
1+<!doctype html>
2+<html lang="en">
53 <head>
6− <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
4+ <meta charset="utf-8">
5+ <meta http-equiv="x-ua-compatible" content="ie=edge">
6+ <meta name="viewport" content="width=device-width, initial-scale=1">
77 <title>My Hakyll Blog - $title$</title>
8− <link rel="stylesheet" type="text/css" href="/css/default.css" />
8+ <link rel="stylesheet" href="/css/default.css" />
99 </head>
1010 <body>
11− <div id="header">
12− <div id="logo">
11+ <header>
12+ <div class="logo">
1313 <a href="/">My Hakyll Blog</a>
1414 </div>
15− <div id="navigation">
15+ <nav>
1616 <a href="/">Home</a>
1717 <a href="/about.html">About</a>
1818 <a href="/contact.html">Contact</a>
1919 <a href="/archive.html">Archive</a>
20− </div>
21− </div>
20+ </nav>
21+ </header>
2222
23− <div id="content">
23+ <main role="main">
2424 <h1>$title$</h1>
25−
2625 $body$
27− </div>
28− <div id="footer">
26+ </main>
27+
28+ <footer>
2929 Site proudly generated by
3030 <a href="http://jaspervdj.be/hakyll">Hakyll</a>
31− </div>
31+ </footer>
3232 </body>
3333</html>
data/example/templates/post-item.htmldeleted−3
@@ -1,3 +0,0 @@
1−<li>
2− <a href="$url$">$title$</a> - $date$
3−</li>
data/example/templates/post-list.htmlnew file+7
@@ -0,0 +1,7 @@
1+<ul>
2+ $for(posts)$
3+ <li>
4+ <a href="$url$">$title$</a> - $date$
5+ </li>
6+ $endfor$
7+</ul>
data/example/templates/post.html+11−3
@@ -1,3 +1,11 @@
1−<div class="info">Posted on $date$</div>
2−
3−$body$
1+<article>
2+ <section class="header">
3+ Posted on $date$
4+ $if(author)$
5+ by $author$
6+ $endif$
7+ </section>
8+ <section>
9+ $body$
10+ </section>
11+</article>
data/templates/rss-item.xml+1
@@ -4,4 +4,5 @@
44 <description><![CDATA[$description$]]></description>
55 <pubDate>$published$</pubDate>
66 <guid>$root$$url$</guid>
7+ <dc:creator>$authorName$</dc:creator>
78</item>
data/templates/rss.xml+3−2
@@ -1,5 +1,6 @@
11<?xml version="1.0" encoding="utf-8"?>
2−<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
2+<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"
3+ xmlns:dc="http://purl.org/dc/elements/1.1/">
34 <channel>
45 <title>$title$</title>
56 <link>$root$</link>
@@ -8,5 +9,5 @@
89 type="application/rss+xml" />
910 <lastBuildDate>$updated$</lastBuildDate>
1011 $body$
11− </channel>
12+ </channel>
1213</rss>
hakyll.cabal+198−108
@@ -1,5 +1,5 @@
11Name: hakyll
2−Version: 4.0.0.0
2+Version: 4.13.0.0
33
44Synopsis: A static website compiler library
55Description:
@@ -37,18 +37,18 @@ License: BSD3
3737License-File: LICENSE
3838Category: Web
3939
40−Cabal-Version: >= 1.8
40+Cabal-Version: >= 1.10
4141Build-Type: Simple
4242Data-Dir: data
4343
4444Data-files:
45− example/posts/2012-11-28-carpe-diem.markdown
46− example/posts/2012-10-07-rosa-rosa-rosam.markdown
47− example/posts/2012-12-07-tu-quoque.markdown
48− example/posts/2012-08-12-spqr.markdown
45+ example/posts/2015-11-28-carpe-diem.markdown
46+ example/posts/2015-10-07-rosa-rosa-rosam.markdown
47+ example/posts/2015-12-07-tu-quoque.markdown
48+ example/posts/2015-08-12-spqr.markdown
4949 example/site.hs
5050 example/images/haskell-logo.png
51− example/templates/post-item.html
51+ example/templates/post-list.html
5252 example/templates/default.html
5353 example/templates/archive.html
5454 example/templates/post.html
@@ -56,16 +56,28 @@ Data-files:
5656 example/index.html
5757 example/about.rst
5858 example/contact.markdown
59− templates/atom-item.xml
60− templates/atom.xml
61− templates/rss-item.xml
62− templates/rss.xml
6359
6460Extra-source-files:
61+ CHANGELOG.md
6562 tests/data/example.md
6663 tests/data/example.md.metadata
64+ tests/data/images/favicon.ico
65+ tests/data/just-meta.html
66+ tests/data/just-meta.html.out
67+ tests/data/partial-helper.html
68+ tests/data/partial.html
69+ tests/data/partial.html.out
70+ tests/data/posts/2010-08-26-birthday.md
71+ tests/data/posts/2018-09-26.md
72+ tests/data/russian.md
73+ tests/data/strip.html
74+ tests/data/strip.html.out
6775 tests/data/template.html
6876 tests/data/template.html.out
77+ data/templates/atom-item.xml
78+ data/templates/atom.xml
79+ data/templates/rss-item.xml
80+ data/templates/rss.xml
6981
7082Source-Repository head
7183 Type: git
@@ -75,144 +87,164 @@ Flag previewServer
7587 Description: Include the preview server
7688 Default: True
7789
78−Flag unixFilter
79− Description: Include the UnixFilter module
90+Flag watchServer
91+ Description: Include the watch server
8092 Default: True
8193
82−Library
83− Ghc-Options: -Wall
84− Ghc-Prof-Options: -auto-all -caf-all
85− Hs-Source-Dirs: src
94+Flag checkExternal
95+ Description: Include external link checking
96+ Default: True
8697
87− Build-Depends:
88− base >= 4 && < 5,
89− binary >= 0.5 && < 0.7,
90− blaze-html >= 0.5 && < 0.6,
91− blaze-markup >= 0.5.1 && < 0.6,
92− bytestring >= 0.9 && < 0.11,
93− citeproc-hs >= 0.3.2 && < 0.4,
94− containers >= 0.3 && < 0.6,
95− cryptohash >= 0.7 && < 0.8,
96− deepseq >= 1.3 && < 1.4,
97− directory >= 1.0 && < 1.3,
98− filepath >= 1.0 && < 1.4,
99− lrucache >= 1.1.1 && < 1.2,
100− mtl >= 1 && < 2.2,
101− old-locale >= 1.0 && < 1.1,
102− old-time >= 1.0 && < 1.2,
103− pandoc >= 1.9.3 && < 1.10,
104− parsec >= 3.0 && < 3.2,
105− process >= 1.0 && < 1.2,
106− regex-base >= 0.93 && < 0.94,
107− regex-tdfa >= 1.1 && < 1.2,
108− tagsoup >= 0.12.6 && < 0.13,
109− text >= 0.11 && < 1.12,
110− time >= 1.1 && < 1.5
98+Flag buildWebsite
99+ Description: Build the hakyll website
100+ Default: False
101+ Manual: True
102+
103+Flag usePandoc
104+ Description: Include Pandoc support
105+ Default: True
106+ Manual: True
107+
108+Library
109+ Ghc-Options: -Wall
110+ Hs-Source-Dirs: lib
111+ Default-language: Haskell2010
111112
112113 Exposed-Modules:
113114 Hakyll
115+ Hakyll.Commands
114116 Hakyll.Core.Compiler
117+ Hakyll.Core.Compiler.Internal
115118 Hakyll.Core.Configuration
116119 Hakyll.Core.Dependencies
120+ Hakyll.Core.File
117121 Hakyll.Core.Identifier
118122 Hakyll.Core.Identifier.Pattern
119123 Hakyll.Core.Item
124+ Hakyll.Core.Logger
120125 Hakyll.Core.Metadata
126+ Hakyll.Core.Provider
127+ Hakyll.Core.Provider.Metadata
121128 Hakyll.Core.Routes
122129 Hakyll.Core.Rules
130+ Hakyll.Core.Rules.Internal
131+ Hakyll.Core.Runtime
132+ Hakyll.Core.Store
133+ Hakyll.Core.UnixFilter
134+ Hakyll.Core.Util.File
123135 Hakyll.Core.Util.String
124136 Hakyll.Core.Writable
125− Hakyll.Core.Writable.CopyFile
126137 Hakyll.Main
127138 Hakyll.Web.CompressCss
128139 Hakyll.Web.Feed
129140 Hakyll.Web.Html
130141 Hakyll.Web.Html.RelativizeUrls
131− Hakyll.Web.Pandoc
132− Hakyll.Web.Pandoc.Biblio
133− Hakyll.Web.Pandoc.FileType
142+ Hakyll.Web.Paginate
143+ Hakyll.Web.Redirect
134144 Hakyll.Web.Tags
135145 Hakyll.Web.Template
136146 Hakyll.Web.Template.Context
147+ Hakyll.Web.Template.Internal
148+ Hakyll.Web.Template.Internal.Element
149+ Hakyll.Web.Template.Internal.Trim
137150 Hakyll.Web.Template.List
138− Hakyll.Web.Template.Read
139151
140152 Other-Modules:
141− Hakyll.Core.Compiler.Internal
153+ Data.List.Extended
154+ Data.Yaml.Extended
155+ Hakyll.Check
142156 Hakyll.Core.Compiler.Require
157+ Hakyll.Core.Identifier.Pattern.Internal
143158 Hakyll.Core.Item.SomeItem
144− Hakyll.Core.Logger
145− Hakyll.Core.Provider
146159 Hakyll.Core.Provider.Internal
147− Hakyll.Core.Provider.Metadata
148160 Hakyll.Core.Provider.MetadataCache
149− Hakyll.Core.Provider.Modified
150− Hakyll.Core.Rules.Internal
151− Hakyll.Core.Runtime
152− Hakyll.Core.Store
153− Hakyll.Core.Util.File
154− Hakyll.Web.Template.Internal
161+ Hakyll.Core.Util.Parser
155162 Paths_hakyll
156163
164+ Build-Depends:
165+ base >= 4.8 && < 5,
166+ binary >= 0.5 && < 0.10,
167+ blaze-html >= 0.5 && < 0.10,
168+ blaze-markup >= 0.5.1 && < 0.9,
169+ bytestring >= 0.9 && < 0.11,
170+ containers >= 0.3 && < 0.7,
171+ cryptohash >= 0.7 && < 0.12,
172+ data-default >= 0.4 && < 0.8,
173+ deepseq >= 1.3 && < 1.5,
174+ directory >= 1.0 && < 1.4,
175+ file-embed >= 0.0.10.1 && < 0.0.12,
176+ filepath >= 1.0 && < 1.5,
177+ lrucache >= 1.1.1 && < 1.3,
178+ mtl >= 1 && < 2.3,
179+ network-uri >= 2.6 && < 2.7,
180+ optparse-applicative >= 0.12 && < 0.15,
181+ parsec >= 3.0 && < 3.2,
182+ process >= 1.6 && < 1.7,
183+ random >= 1.0 && < 1.2,
184+ regex-tdfa >= 1.1 && < 1.3,
185+ resourcet >= 1.1 && < 1.3,
186+ scientific >= 0.3.4 && < 0.4,
187+ tagsoup >= 0.13.1 && < 0.15,
188+ template-haskell >= 2.14 && < 2.15,
189+ text >= 0.11 && < 1.3,
190+ time >= 1.8 && < 1.10,
191+ time-locale-compat >= 0.1 && < 0.2,
192+ unordered-containers >= 0.2 && < 0.3,
193+ vector >= 0.11 && < 0.13,
194+ yaml >= 0.8.11 && < 0.12
195+
157196 If flag(previewServer)
158197 Build-depends:
159− snap-core >= 0.6 && < 0.10,
160− snap-server >= 0.6 && < 0.10
198+ wai >= 3.2 && < 3.3,
199+ warp >= 3.2 && < 3.3,
200+ wai-app-static >= 3.1 && < 3.2,
201+ http-types >= 0.9 && < 0.13,
202+ fsnotify >= 0.2 && < 0.4
161203 Cpp-options:
162204 -DPREVIEW_SERVER
163205 Other-modules:
164− Hakyll.Web.Preview.Poll
165− Hakyll.Web.Preview.Server
206+ Hakyll.Preview.Poll
207+ Hakyll.Preview.Server
166208
167− If flag(unixFilter)
209+ If flag(watchServer)
168210 Build-depends:
169− unix >= 2.4 && < 2.7
211+ fsnotify >= 0.2 && < 0.4
170212 Cpp-options:
171− -DUNIX_FILTER
172− Exposed-modules:
173− Hakyll.Core.UnixFilter
213+ -DWATCH_SERVER
214+ Other-modules:
215+ Hakyll.Preview.Poll
174216
175−Test-suite hakyll-tests
176− Type: exitcode-stdio-1.0
177− Hs-source-dirs: src tests
178− Main-is: TestSuite.hs
179− Ghc-options: -Wall
217+ If flag(checkExternal)
218+ Build-depends:
219+ http-conduit >= 2.2 && < 2.4,
220+ http-types >= 0.7 && < 0.13
221+ Cpp-options:
222+ -DCHECK_EXTERNAL
180223
181− Build-Depends:
182− HUnit >= 1.2 && < 1.3,
183− QuickCheck >= 2.4 && < 2.6,
184− test-framework >= 0.4 && < 0.7,
185− test-framework-hunit >= 0.2 && < 0.3,
186− test-framework-quickcheck2 >= 0.2 && < 0.3,
187− -- Copy pasted from hakyll dependencies:
188− base >= 4 && < 5,
189− binary >= 0.5 && < 0.7,
190− blaze-html >= 0.5 && < 0.6,
191− blaze-markup >= 0.5.1 && < 0.6,
192− bytestring >= 0.9 && < 0.11,
193− citeproc-hs >= 0.3.2 && < 0.4,
194− containers >= 0.3 && < 0.6,
195− cryptohash >= 0.7 && < 0.8,
196− deepseq >= 1.3 && < 1.4,
197− directory >= 1.0 && < 1.3,
198− filepath >= 1.0 && < 1.4,
199− lrucache >= 1.1.1 && < 1.2,
200− mtl >= 1 && < 2.2,
201− old-locale >= 1.0 && < 1.1,
202− old-time >= 1.0 && < 1.2,
203− pandoc >= 1.9.3 && < 1.10,
204− parsec >= 3.0 && < 3.2,
205− process >= 1.0 && < 1.2,
206− regex-base >= 0.93 && < 0.94,
207− regex-tdfa >= 1.1 && < 1.2,
208− tagsoup >= 0.12.6 && < 0.13,
209− text >= 0.11 && < 1.12,
210− time >= 1.1 && < 1.5,
211− unix >= 2.4 && < 2.7
224+ If flag(usePandoc)
225+ Exposed-Modules:
226+ Hakyll.Web.Pandoc
227+ Hakyll.Web.Pandoc.Biblio
228+ Hakyll.Web.Pandoc.FileType
229+ Other-Modules:
230+ Hakyll.Web.Pandoc.Binary
231+ Build-Depends:
232+ pandoc >= 2.0.5 && < 2.8,
233+ pandoc-citeproc >= 0.14 && < 0.17
234+ Cpp-options:
235+ -DUSE_PANDOC
236+
237+Test-suite hakyll-tests
238+ Type: exitcode-stdio-1.0
239+ Hs-source-dirs: tests
240+ Main-is: TestSuite.hs
241+ Ghc-options: -Wall -threaded
242+ Default-language: Haskell2010
212243
213244 Other-modules:
214245 Hakyll.Core.Dependencies.Tests
215246 Hakyll.Core.Identifier.Tests
247+ Hakyll.Core.Provider.Metadata.Tests
216248 Hakyll.Core.Provider.Tests
217249 Hakyll.Core.Routes.Tests
218250 Hakyll.Core.Rules.Tests
@@ -220,18 +252,76 @@ Test-suite hakyll-tests
220252 Hakyll.Core.Store.Tests
221253 Hakyll.Core.UnixFilter.Tests
222254 Hakyll.Core.Util.String.Tests
255+ Hakyll.Web.CompressCss.Tests
256+ Hakyll.Web.Html.RelativizeUrls.Tests
257+ Hakyll.Web.Html.Tests
258+ Hakyll.Web.Tags.Tests
223259 Hakyll.Web.Template.Context.Tests
224260 Hakyll.Web.Template.Tests
225− Hakyll.Web.Html.Tests
226− Hakyll.Web.Html.RelativizeUrls.Tests
227261 TestSuite.Util
228262
263+ Build-Depends:
264+ hakyll,
265+ QuickCheck >= 2.8 && < 2.14,
266+ tasty >= 0.11 && < 1.3,
267+ tasty-hunit >= 0.9 && < 0.11,
268+ tasty-quickcheck >= 0.8 && < 0.11,
269+ -- Copy pasted from hakyll dependencies:
270+ base >= 4.8 && < 5,
271+ bytestring >= 0.9 && < 0.11,
272+ containers >= 0.3 && < 0.7,
273+ filepath >= 1.0 && < 1.5,
274+ text >= 0.11 && < 1.3,
275+ unordered-containers >= 0.2 && < 0.3,
276+ yaml >= 0.8.11 && < 0.12
277+
278+ If flag(previewServer)
279+ Cpp-options:
280+ -DPREVIEW_SERVER
281+
282+ If flag(watchServer)
283+ Cpp-options:
284+ -DWATCH_SERVER
285+
286+ If flag(checkExternal)
287+ Cpp-options:
288+ -DCHECK_EXTERNAL
289+
290+ If flag(usePandoc)
291+ Other-modules:
292+ Hakyll.Web.Pandoc.FileType.Tests
293+ Cpp-options:
294+ -DUSE_PANDOC
295+
229296Executable hakyll-init
230− Ghc-options: -Wall
231− Hs-source-dirs: src
232− Main-is: Hakyll/Init.hs
297+ Main-is: Init.hs
298+ Ghc-options: -Wall -threaded
299+ Hs-source-dirs: src
300+ Default-language: Haskell2010
301+
302+ Other-modules:
303+ Paths_hakyll
233304
234305 Build-depends:
306+ hakyll,
235307 base >= 4 && < 5,
236− directory >= 1.0 && < 1.3,
237− filepath >= 1.0 && < 1.4
308+ directory >= 1.0 && < 1.4,
309+ filepath >= 1.0 && < 1.5
310+
311+Executable hakyll-website
312+ Main-is: site.hs
313+ Ghc-options: -Wall -threaded
314+ Hs-source-dirs: web
315+ Default-language: Haskell2010
316+
317+ If flag(buildWebsite)
318+ Buildable: True
319+ Else
320+ Buildable: False
321+
322+ Build-depends:
323+ hakyll,
324+ base >= 4 && < 5,
325+ directory >= 1.0 && < 1.4,
326+ filepath >= 1.0 && < 1.5,
327+ pandoc >= 2.0.5 && < 2.8
lib/Data/List/Extended.hsnew file+15
@@ -0,0 +1,15 @@
1+module Data.List.Extended
2+ ( module Data.List
3+ , breakWhen
4+ ) where
5+
6+import Data.List
7+
8+-- | Like 'break', but can act on the entire tail of the list.
9+breakWhen :: ([a] -> Bool) -> [a] -> ([a], [a])
10+breakWhen predicate = go []
11+ where
12+ go buf [] = (reverse buf, [])
13+ go buf (x : xs)
14+ | predicate (x : xs) = (reverse buf, x : xs)
15+ | otherwise = go (x : buf) xs
lib/Data/Yaml/Extended.hsnew file+24
@@ -0,0 +1,24 @@
1+module Data.Yaml.Extended
2+ ( module Data.Yaml
3+ , toString
4+ , toList
5+ ) where
6+
7+import qualified Data.Text as T
8+import qualified Data.Vector as V
9+import Data.Yaml
10+import Data.Scientific
11+
12+toString :: Value -> Maybe String
13+toString (String t) = Just (T.unpack t)
14+toString (Bool True) = Just "true"
15+toString (Bool False) = Just "false"
16+-- | Make sure that numeric fields containing integer numbers are shown as
17+-- | integers (i.e., "42" instead of "42.0").
18+toString (Number d) | isInteger d = Just (formatScientific Fixed (Just 0) d)
19+ | otherwise = Just (show d)
20+toString _ = Nothing
21+
22+toList :: Value -> Maybe [Value]
23+toList (Array a) = Just (V.toList a)
24+toList _ = Nothing
src/Hakyll.hs → lib/Hakyll.hsrenamed+10−8
@@ -4,61 +4,63 @@
44module Hakyll
55 ( module Hakyll.Core.Compiler
66 , module Hakyll.Core.Configuration
7+ , module Hakyll.Core.File
78 , module Hakyll.Core.Identifier
89 , module Hakyll.Core.Identifier.Pattern
910 , module Hakyll.Core.Item
1011 , module Hakyll.Core.Metadata
1112 , module Hakyll.Core.Routes
1213 , module Hakyll.Core.Rules
13−#ifdef UNIX_FILTER
1414 , module Hakyll.Core.UnixFilter
15−#endif
1615 , module Hakyll.Core.Util.File
1716 , module Hakyll.Core.Util.String
1817 , module Hakyll.Core.Writable
19− , module Hakyll.Core.Writable.CopyFile
2018 , module Hakyll.Main
2119 , module Hakyll.Web.CompressCss
2220 , module Hakyll.Web.Feed
2321 , module Hakyll.Web.Html
2422 , module Hakyll.Web.Html.RelativizeUrls
23+ , module Hakyll.Web.Paginate
24+#ifdef USE_PANDOC
2525 , module Hakyll.Web.Pandoc
2626 , module Hakyll.Web.Pandoc.Biblio
2727 , module Hakyll.Web.Pandoc.FileType
28+#endif
29+ , module Hakyll.Web.Redirect
2830 , module Hakyll.Web.Tags
2931 , module Hakyll.Web.Template
3032 , module Hakyll.Web.Template.Context
3133 , module Hakyll.Web.Template.List
32− , module Hakyll.Web.Template.Read
3334 ) where
3435
3536
3637--------------------------------------------------------------------------------
3738import Hakyll.Core.Compiler
3839import Hakyll.Core.Configuration
40+import Hakyll.Core.File
3941import Hakyll.Core.Identifier
4042import Hakyll.Core.Identifier.Pattern
4143import Hakyll.Core.Item
4244import Hakyll.Core.Metadata
4345import Hakyll.Core.Routes
4446import Hakyll.Core.Rules
45−#ifdef UNIX_FILTER
4647import Hakyll.Core.UnixFilter
47−#endif
4848import Hakyll.Core.Util.File
4949import Hakyll.Core.Util.String
5050import Hakyll.Core.Writable
51−import Hakyll.Core.Writable.CopyFile
5251import Hakyll.Main
5352import Hakyll.Web.CompressCss
5453import Hakyll.Web.Feed
5554import Hakyll.Web.Html
5655import Hakyll.Web.Html.RelativizeUrls
56+import Hakyll.Web.Paginate
57+#ifdef USE_PANDOC
5758import Hakyll.Web.Pandoc
5859import Hakyll.Web.Pandoc.Biblio
5960import Hakyll.Web.Pandoc.FileType
61+#endif
62+import Hakyll.Web.Redirect
6063import Hakyll.Web.Tags
6164import Hakyll.Web.Template
6265import Hakyll.Web.Template.Context
6366import Hakyll.Web.Template.List
64−import Hakyll.Web.Template.Read
lib/Hakyll/Check.hsnew file+302
@@ -0,0 +1,302 @@
1+--------------------------------------------------------------------------------
2+{-# LANGUAGE CPP #-}
3+{-# LANGUAGE OverloadedStrings #-}
4+module Hakyll.Check
5+ ( Check (..)
6+ , check
7+ ) where
8+
9+
10+--------------------------------------------------------------------------------
11+import Control.Concurrent.MVar (MVar, newEmptyMVar, putMVar,
12+ readMVar)
13+import Control.Exception (SomeAsyncException (..),
14+ SomeException (..), throw, try)
15+import Control.Monad (foldM, forM_)
16+import Control.Monad.Reader (ReaderT, ask, runReaderT)
17+import Control.Monad.State (StateT, get, modify, runStateT)
18+import Control.Monad.Trans (liftIO)
19+import Control.Monad.Trans.Resource (runResourceT)
20+import Data.List (isPrefixOf)
21+import qualified Data.Map.Lazy as Map
22+#if MIN_VERSION_base(4,9,0)
23+import Data.Semigroup (Semigroup (..))
24+#endif
25+import Network.URI (unEscapeString)
26+import System.Directory (doesDirectoryExist,
27+ doesFileExist)
28+import System.Exit (ExitCode (..))
29+import System.FilePath (takeDirectory, takeExtension,
30+ (</>))
31+import qualified Text.HTML.TagSoup as TS
32+
33+
34+--------------------------------------------------------------------------------
35+#ifdef CHECK_EXTERNAL
36+import Data.List (intercalate)
37+import Data.Typeable (cast)
38+import Data.Version (versionBranch)
39+import GHC.Exts (fromString)
40+import qualified Network.HTTP.Conduit as Http
41+import qualified Network.HTTP.Types as Http
42+import qualified Paths_hakyll as Paths_hakyll
43+#endif
44+
45+
46+--------------------------------------------------------------------------------
47+import Hakyll.Core.Configuration
48+import Hakyll.Core.Logger (Logger)
49+import qualified Hakyll.Core.Logger as Logger
50+import Hakyll.Core.Util.File
51+import Hakyll.Web.Html
52+
53+
54+--------------------------------------------------------------------------------
55+data Check = All | InternalLinks
56+ deriving (Eq, Ord, Show)
57+
58+
59+--------------------------------------------------------------------------------
60+check :: Configuration -> Logger -> Check -> IO ExitCode
61+check config logger check' = do
62+ ((), state) <- runChecker checkDestination config logger check'
63+ failed <- countFailedLinks state
64+ return $ if failed > 0 then ExitFailure 1 else ExitSuccess
65+
66+
67+--------------------------------------------------------------------------------
68+countFailedLinks :: CheckerState -> IO Int
69+countFailedLinks state = foldM addIfFailure 0 (Map.elems state)
70+ where addIfFailure failures mvar = do
71+ checkerWrite <- readMVar mvar
72+ return $ failures + checkerFaulty checkerWrite
73+
74+
75+--------------------------------------------------------------------------------
76+data CheckerRead = CheckerRead
77+ { checkerConfig :: Configuration
78+ , checkerLogger :: Logger
79+ , checkerCheck :: Check
80+ }
81+
82+
83+--------------------------------------------------------------------------------
84+data CheckerWrite = CheckerWrite
85+ { checkerFaulty :: Int
86+ , checkerOk :: Int
87+ } deriving (Show)
88+
89+
90+--------------------------------------------------------------------------------
91+#if MIN_VERSION_base(4,9,0)
92+instance Semigroup CheckerWrite where
93+ (<>) (CheckerWrite f1 o1) (CheckerWrite f2 o2) =
94+ CheckerWrite (f1 + f2) (o1 + o2)
95+
96+instance Monoid CheckerWrite where
97+ mempty = CheckerWrite 0 0
98+ mappend = (<>)
99+#else
100+instance Monoid CheckerWrite where
101+ mempty = CheckerWrite 0 0
102+ mappend (CheckerWrite f1 o1) (CheckerWrite f2 o2) =
103+ CheckerWrite (f1 + f2) (o1 + o2)
104+#endif
105+
106+
107+--------------------------------------------------------------------------------
108+type CheckerState = Map.Map URL (MVar CheckerWrite)
109+
110+
111+--------------------------------------------------------------------------------
112+type Checker a = ReaderT CheckerRead (StateT CheckerState IO) a
113+
114+
115+--------------------------------------------------------------------------------
116+type URL = String
117+
118+
119+--------------------------------------------------------------------------------
120+runChecker :: Checker a -> Configuration -> Logger -> Check
121+ -> IO (a, CheckerState)
122+runChecker checker config logger check' = do
123+ let read' = CheckerRead
124+ { checkerConfig = config
125+ , checkerLogger = logger
126+ , checkerCheck = check'
127+ }
128+ Logger.flush logger
129+ runStateT (runReaderT checker read') Map.empty
130+
131+
132+--------------------------------------------------------------------------------
133+checkDestination :: Checker ()
134+checkDestination = do
135+ config <- checkerConfig <$> ask
136+ files <- liftIO $ getRecursiveContents
137+ (const $ return False) (destinationDirectory config)
138+
139+ let htmls =
140+ [ destinationDirectory config </> file
141+ | file <- files
142+ , takeExtension file == ".html"
143+ ]
144+
145+ forM_ htmls checkFile
146+
147+
148+--------------------------------------------------------------------------------
149+checkFile :: FilePath -> Checker ()
150+checkFile filePath = do
151+ logger <- checkerLogger <$> ask
152+ contents <- liftIO $ readFile filePath
153+ Logger.header logger $ "Checking file " ++ filePath
154+
155+ let urls = getUrls $ TS.parseTags contents
156+ forM_ urls $ \url -> do
157+ Logger.debug logger $ "Checking link " ++ url
158+ m <- liftIO newEmptyMVar
159+ checkUrlIfNeeded filePath (canonicalizeUrl url) m
160+ where
161+ -- Check scheme-relative links
162+ canonicalizeUrl url = if schemeRelative url then "http:" ++ url else url
163+ schemeRelative = isPrefixOf "//"
164+
165+
166+--------------------------------------------------------------------------------
167+checkUrlIfNeeded :: FilePath -> URL -> MVar CheckerWrite -> Checker ()
168+checkUrlIfNeeded filepath url m = do
169+ logger <- checkerLogger <$> ask
170+ needsCheck <- (== All) . checkerCheck <$> ask
171+ checked <- (url `Map.member`) <$> get
172+ if not needsCheck || checked
173+ then Logger.debug logger "Already checked, skipping"
174+ else do modify $ Map.insert url m
175+ checkUrl filepath url
176+
177+
178+--------------------------------------------------------------------------------
179+checkUrl :: FilePath -> URL -> Checker ()
180+checkUrl filePath url
181+ | isExternal url = checkExternalUrl url
182+ | hasProtocol url = skip url $ Just "Unknown protocol, skipping"
183+ | otherwise = checkInternalUrl filePath url
184+ where
185+ validProtoChars = ['A'..'Z'] ++ ['a'..'z'] ++ ['0'..'9'] ++ "+-."
186+ hasProtocol str = case break (== ':') str of
187+ (proto, ':' : _) -> all (`elem` validProtoChars) proto
188+ _ -> False
189+
190+
191+--------------------------------------------------------------------------------
192+ok :: URL -> Checker ()
193+ok url = putCheckResult url mempty {checkerOk = 1}
194+
195+
196+--------------------------------------------------------------------------------
197+skip :: URL -> Maybe String -> Checker ()
198+skip url maybeReason = do
199+ logger <- checkerLogger <$> ask
200+ case maybeReason of
201+ Nothing -> return ()
202+ Just reason -> Logger.debug logger reason
203+ putCheckResult url mempty {checkerOk = 1}
204+
205+
206+--------------------------------------------------------------------------------
207+faulty :: URL -> Maybe String -> Checker ()
208+faulty url reason = do
209+ logger <- checkerLogger <$> ask
210+ Logger.error logger $ "Broken link to " ++ show url ++ explanation
211+ putCheckResult url mempty {checkerFaulty = 1}
212+ where
213+ formatExplanation = (" (" ++) . (++ ")")
214+ explanation = maybe "" formatExplanation reason
215+
216+
217+--------------------------------------------------------------------------------
218+putCheckResult :: URL -> CheckerWrite -> Checker ()
219+putCheckResult url result = do
220+ state <- get
221+ let maybeMVar = Map.lookup url state
222+ case maybeMVar of
223+ Just m -> liftIO $ putMVar m result
224+ Nothing -> do
225+ logger <- checkerLogger <$> ask
226+ Logger.debug logger "Failed to find existing entry for checked URL"
227+
228+
229+--------------------------------------------------------------------------------
230+checkInternalUrl :: FilePath -> URL -> Checker ()
231+checkInternalUrl base url = case url' of
232+ "" -> ok url
233+ _ -> do
234+ config <- checkerConfig <$> ask
235+ let dest = destinationDirectory config
236+ dir = takeDirectory base
237+ filePath
238+ | "/" `isPrefixOf` url' = dest ++ url'
239+ | otherwise = dir </> url'
240+
241+ exists <- checkFileExists filePath
242+ if exists then ok url else faulty url Nothing
243+ where
244+ url' = stripFragments $ unEscapeString url
245+
246+
247+--------------------------------------------------------------------------------
248+checkExternalUrl :: URL -> Checker ()
249+#ifdef CHECK_EXTERNAL
250+checkExternalUrl url = do
251+ result <- requestExternalUrl url
252+ case result of
253+ Left (SomeException e) ->
254+ case (cast e :: Maybe SomeAsyncException) of
255+ Just ae -> throw ae
256+ _ -> faulty url (Just $ showException e)
257+ Right _ -> ok url
258+ where
259+ -- Convert exception to a concise form
260+ showException e = case cast e of
261+ Just (Http.HttpExceptionRequest _ e') -> show e'
262+ _ -> head $ words $ show e
263+
264+requestExternalUrl :: URL -> Checker (Either SomeException Bool)
265+requestExternalUrl url = liftIO $ try $ do
266+ mgr <- Http.newManager Http.tlsManagerSettings
267+ runResourceT $ do
268+ request <- Http.parseRequest url
269+ response <- Http.http (settings request) mgr
270+ let code = Http.statusCode (Http.responseStatus response)
271+ return $ code >= 200 && code < 300
272+ where
273+ -- Add additional request info
274+ settings r = r
275+ { Http.method = "HEAD"
276+ , Http.redirectCount = 10
277+ , Http.requestHeaders = ("User-Agent", ua) : Http.requestHeaders r
278+ }
279+
280+ -- Nice user agent info
281+ ua = fromString $ "hakyll-check/" ++
282+ (intercalate "." $ map show $ versionBranch Paths_hakyll.version)
283+#else
284+checkExternalUrl url = skip url Nothing
285+#endif
286+
287+
288+--------------------------------------------------------------------------------
289+-- | Wraps doesFileExist, also checks for index.html
290+checkFileExists :: FilePath -> Checker Bool
291+checkFileExists filePath = liftIO $ do
292+ file <- doesFileExist filePath
293+ dir <- doesDirectoryExist filePath
294+ case (file, dir) of
295+ (True, _) -> return True
296+ (_, True) -> doesFileExist $ filePath </> "index.html"
297+ _ -> return False
298+
299+
300+--------------------------------------------------------------------------------
301+stripFragments :: String -> String
302+stripFragments = takeWhile (not . flip elem ['?', '#'])
lib/Hakyll/Commands.hsnew file+164
@@ -0,0 +1,164 @@
1+ --------------------------------------------------------------------------------
2+-- | Implementation of Hakyll commands: build, preview...
3+{-# LANGUAGE CPP #-}
4+module Hakyll.Commands
5+ ( Check(..)
6+ , build
7+ , check
8+ , clean
9+ , preview
10+ , rebuild
11+ , server
12+ , deploy
13+ , watch
14+ ) where
15+
16+
17+--------------------------------------------------------------------------------
18+import Control.Concurrent
19+import System.Exit (ExitCode)
20+
21+
22+--------------------------------------------------------------------------------
23+import Hakyll.Check (Check(..))
24+import qualified Hakyll.Check as Check
25+import Hakyll.Core.Configuration
26+import Hakyll.Core.Logger (Logger)
27+import qualified Hakyll.Core.Logger as Logger
28+import Hakyll.Core.Rules
29+import Hakyll.Core.Rules.Internal
30+import Hakyll.Core.Runtime
31+import Hakyll.Core.Util.File
32+
33+
34+--------------------------------------------------------------------------------
35+#ifdef WATCH_SERVER
36+import Hakyll.Preview.Poll (watchUpdates)
37+#endif
38+
39+#ifdef PREVIEW_SERVER
40+import Hakyll.Preview.Server
41+#endif
42+
43+#ifdef mingw32_HOST_OS
44+import Control.Monad (void)
45+import System.IO.Error (catchIOError)
46+#endif
47+
48+
49+--------------------------------------------------------------------------------
50+-- | Build the site
51+build :: Configuration -> Logger -> Rules a -> IO ExitCode
52+build conf logger rules = fst <$> run conf logger rules
53+
54+
55+--------------------------------------------------------------------------------
56+-- | Run the checker and exit
57+check :: Configuration -> Logger -> Check.Check -> IO ExitCode
58+check = Check.check
59+
60+
61+--------------------------------------------------------------------------------
62+-- | Remove the output directories
63+clean :: Configuration -> Logger -> IO ()
64+clean conf logger = do
65+ remove $ destinationDirectory conf
66+ remove $ storeDirectory conf
67+ remove $ tmpDirectory conf
68+ where
69+ remove dir = do
70+ Logger.header logger $ "Removing " ++ dir ++ "..."
71+ removeDirectory dir
72+
73+
74+--------------------------------------------------------------------------------
75+-- | Preview the site
76+preview :: Configuration -> Logger -> Rules a -> Int -> IO ()
77+#ifdef PREVIEW_SERVER
78+preview conf logger rules port = do
79+ deprecatedMessage
80+ watch conf logger "0.0.0.0" port True rules
81+ where
82+ deprecatedMessage = mapM_ putStrLn [ "The preview command has been deprecated."
83+ , "Use the watch command for recompilation and serving."
84+ ]
85+#else
86+preview _ _ _ _ = previewServerDisabled
87+#endif
88+
89+
90+--------------------------------------------------------------------------------
91+-- | Watch and recompile for changes
92+
93+watch :: Configuration -> Logger -> String -> Int -> Bool -> Rules a -> IO ()
94+#ifdef WATCH_SERVER
95+watch conf logger host port runServer rules = do
96+#ifndef mingw32_HOST_OS
97+ _ <- forkIO $ watchUpdates conf update
98+#else
99+ -- Force windows users to compile with -threaded flag, as otherwise
100+ -- thread is blocked indefinitely.
101+ catchIOError (void $ forkOS $ watchUpdates conf update) $ do
102+ fail $ "Hakyll.Commands.watch: Could not start update watching " ++
103+ "thread. Did you compile with -threaded flag?"
104+#endif
105+ server'
106+ where
107+ update = do
108+ (_, ruleSet) <- run conf logger rules
109+ return $ rulesPattern ruleSet
110+ loop = threadDelay 100000 >> loop
111+ server' = if runServer then server conf logger host port else loop
112+#else
113+watch _ _ _ _ _ _ = watchServerDisabled
114+#endif
115+
116+--------------------------------------------------------------------------------
117+-- | Rebuild the site
118+rebuild :: Configuration -> Logger -> Rules a -> IO ExitCode
119+rebuild conf logger rules =
120+ clean conf logger >> build conf logger rules
121+
122+--------------------------------------------------------------------------------
123+-- | Start a server
124+server :: Configuration -> Logger -> String -> Int -> IO ()
125+#ifdef PREVIEW_SERVER
126+server conf logger host port = do
127+ let destination = destinationDirectory conf
128+ staticServer logger destination host port
129+#else
130+server _ _ _ _ = previewServerDisabled
131+#endif
132+
133+
134+--------------------------------------------------------------------------------
135+-- | Upload the site
136+deploy :: Configuration -> IO ExitCode
137+deploy conf = deploySite conf conf
138+
139+
140+--------------------------------------------------------------------------------
141+-- | Print a warning message about the preview serving not being enabled
142+#ifndef PREVIEW_SERVER
143+previewServerDisabled :: IO ()
144+previewServerDisabled =
145+ mapM_ putStrLn
146+ [ "PREVIEW SERVER"
147+ , ""
148+ , "The preview server is not enabled in the version of Hakyll. To"
149+ , "enable it, set the flag to True and recompile Hakyll."
150+ , "Alternatively, use an external tool to serve your site directory."
151+ ]
152+#endif
153+
154+#ifndef WATCH_SERVER
155+watchServerDisabled :: IO ()
156+watchServerDisabled =
157+ mapM_ putStrLn
158+ [ "WATCH SERVER"
159+ , ""
160+ , "The watch server is not enabled in the version of Hakyll. To"
161+ , "enable it, set the flag to True and recompile Hakyll."
162+ , "Alternatively, use an external tool to serve your site directory."
163+ ]
164+#endif
src/Hakyll/Core/Compiler.hs → lib/Hakyll/Core/Compiler.hsrenamed+90−16
@@ -4,12 +4,13 @@
44module Hakyll.Core.Compiler
55 ( Compiler
66 , getUnderlying
7+ , getUnderlyingExtension
78 , makeItem
89 , getRoute
910 , getResourceBody
1011 , getResourceString
1112 , getResourceLBS
12− , getResourceWith
13+ , getResourceFilePath
1314
1415 , Internal.Snapshot
1516 , saveSnapshot
@@ -23,21 +24,25 @@ module Hakyll.Core.Compiler
2324 , cached
2425 , unsafeCompiler
2526 , debugCompiler
27+ , noResult
28+ , withErrorMessage
2629 ) where
2730
2831
2932--------------------------------------------------------------------------------
30−import Control.Applicative ((<$>))
33+import Control.Monad (unless, when, (>=>))
3134import Data.Binary (Binary)
3235import Data.ByteString.Lazy (ByteString)
36+import qualified Data.List.NonEmpty as NonEmpty
3337import Data.Typeable (Typeable)
34−import Prelude hiding (id, (.))
3538import System.Environment (getProgName)
39+import System.FilePath (takeExtension)
3640
3741
3842--------------------------------------------------------------------------------
3943import Hakyll.Core.Compiler.Internal
4044import qualified Hakyll.Core.Compiler.Require as Internal
45+import Hakyll.Core.Dependencies
4146import Hakyll.Core.Identifier
4247import Hakyll.Core.Item
4348import Hakyll.Core.Logger as Logger
@@ -47,12 +52,20 @@ import qualified Hakyll.Core.Store as Store
4752
4853
4954--------------------------------------------------------------------------------
50−-- | Get the underlying identifier. Only use this if you know what you're doing.
55+-- | Get the underlying identifier.
5156getUnderlying :: Compiler Identifier
5257getUnderlying = compilerUnderlying <$> compilerAsk
5358
5459
5560--------------------------------------------------------------------------------
61+-- | Get the extension of the underlying identifier. Returns something like
62+-- @".html"@
63+getUnderlyingExtension :: Compiler String
64+getUnderlyingExtension = takeExtension . toFilePath <$> getUnderlying
65+
66+
67+--------------------------------------------------------------------------------
68+-- | Create an item from the underlying identifier and a given value.
5669makeItem :: a -> Compiler (Item a)
5770makeItem x = do
5871 identifier <- getUnderlying
@@ -63,28 +76,43 @@ makeItem x = do
6376-- | Get the route for a specified item
6477getRoute :: Identifier -> Compiler (Maybe FilePath)
6578getRoute identifier = do
66− routes <- compilerRoutes <$> compilerAsk
67− return $ runRoutes routes identifier
79+ provider <- compilerProvider <$> compilerAsk
80+ routes <- compilerRoutes <$> compilerAsk
81+ -- Note that this makes us dependend on that identifier: when the metadata
82+ -- of that item changes, the route may change, hence we have to recompile
83+ (mfp, um) <- compilerUnsafeIO $ runRoutes routes provider identifier
84+ when um $ compilerTellDependencies [IdentifierDependency identifier]
85+ return mfp
6886
6987
7088--------------------------------------------------------------------------------
71−-- | Get the body of the underlying resource
89+-- | Get the full contents of the matched source file as a string,
90+-- but without metadata preamble, if there was one.
7291getResourceBody :: Compiler (Item String)
7392getResourceBody = getResourceWith resourceBody
7493
7594
7695--------------------------------------------------------------------------------
77−-- | Get the resource we are compiling as a string
96+-- | Get the full contents of the matched source file as a string.
7897getResourceString :: Compiler (Item String)
7998getResourceString = getResourceWith resourceString
8099
81100
82101--------------------------------------------------------------------------------
83−-- | Get the resource we are compiling as a lazy bytestring
102+-- | Get the full contents of the matched source file as a lazy bytestring.
84103getResourceLBS :: Compiler (Item ByteString)
85104getResourceLBS = getResourceWith resourceLBS
86105
87106
107+--------------------------------------------------------------------------------
108+-- | Get the file path of the resource we are compiling
109+getResourceFilePath :: Compiler FilePath
110+getResourceFilePath = do
111+ provider <- compilerProvider <$> compilerAsk
112+ id' <- compilerUnderlying <$> compilerAsk
113+ return $ resourceFilePath provider id'
114+
115+
88116--------------------------------------------------------------------------------
89117-- | Overloadable function for 'getResourceString' and 'getResourceLBS'
90118getResourceWith :: (Provider -> Identifier -> IO a) -> Compiler (Item a)
@@ -94,21 +122,33 @@ getResourceWith reader = do
94122 let filePath = toFilePath id'
95123 if resourceExists provider id'
96124 then compilerUnsafeIO $ Item id' <$> reader provider id'
97− else compilerThrow $ error' filePath
125+ else fail $ error' filePath
98126 where
99127 error' fp = "Hakyll.Core.Compiler.getResourceWith: resource " ++
100128 show fp ++ " not found"
101129
102130
103131--------------------------------------------------------------------------------
132+-- | Save a snapshot of the item. This function returns the same item, which
133+-- convenient for building '>>=' chains.
104134saveSnapshot :: (Binary a, Typeable a)
105− => Internal.Snapshot -> Item a -> Compiler ()
135+ => Internal.Snapshot -> Item a -> Compiler (Item a)
106136saveSnapshot snapshot item = do
107− store <- compilerStore <$> compilerAsk
108− compilerUnsafeIO $ Internal.saveSnapshot store snapshot item
137+ store <- compilerStore <$> compilerAsk
138+ logger <- compilerLogger <$> compilerAsk
139+ compilerUnsafeIO $ do
140+ Logger.debug logger $ "Storing snapshot: " ++ snapshot
141+ Internal.saveSnapshot store snapshot item
142+
143+ -- Signal that we saved the snapshot.
144+ Compiler $ \_ -> return $ CompilerSnapshot snapshot (return item)
109145
110146
111147--------------------------------------------------------------------------------
148+-- | Turn on caching for a compilation value to avoid recomputing it
149+-- on subsequent Hakyll runs.
150+-- The storage key consists of the underlying identifier of the compiled
151+-- ressource and the given name.
112152cached :: (Binary a, Typeable a)
113153 => String
114154 -> Compiler a
@@ -117,7 +157,11 @@ cached name compiler = do
117157 id' <- compilerUnderlying <$> compilerAsk
118158 store <- compilerStore <$> compilerAsk
119159 provider <- compilerProvider <$> compilerAsk
120− modified <- compilerUnsafeIO $ resourceModified provider id'
160+
161+ -- Give a better error message when the resource is not there at all.
162+ unless (resourceExists provider id') $ fail $ itDoesntEvenExist id'
163+
164+ let modified = resourceModified provider id'
121165 if modified
122166 then do
123167 x <- compiler
@@ -128,20 +172,50 @@ cached name compiler = do
128172 x <- compilerUnsafeIO $ Store.get store [name, show id']
129173 progName <- compilerUnsafeIO getProgName
130174 case x of Store.Found x' -> return x'
131− _ -> compilerThrow (error' progName)
175+ _ -> fail $ error' progName
132176 where
133177 error' progName =
134178 "Hakyll.Core.Compiler.cached: Cache corrupt! " ++
135179 "Try running: " ++ progName ++ " clean"
136180
181+ itDoesntEvenExist id' =
182+ "Hakyll.Core.Compiler.cached: You are trying to (perhaps " ++
183+ "indirectly) use `cached` on a non-existing resource: there " ++
184+ "is no file backing " ++ show id'
185+
137186
138187--------------------------------------------------------------------------------
188+-- | Run an IO computation without dependencies in a Compiler
139189unsafeCompiler :: IO a -> Compiler a
140190unsafeCompiler = compilerUnsafeIO
141191
142192
143193--------------------------------------------------------------------------------
144−-- | Compiler for debugging purposes
194+-- | Fail so that it is treated as non-defined in an @\$if()\$@ branching
195+-- "Hakyll.Web.Template" macro, and alternative
196+-- 'Hakyll.Web.Template.Context.Context's are tried
197+--
198+-- @since 4.13.0
199+noResult :: String -> Compiler a
200+noResult = compilerNoResult . return
201+
202+
203+--------------------------------------------------------------------------------
204+-- | Prepend an error line to the error, if there is one. This allows you to
205+-- add helpful context to error messages.
206+--
207+-- @since 4.13.0
208+withErrorMessage :: String -> Compiler a -> Compiler a
209+withErrorMessage x = do
210+ compilerTry >=> either (compilerResult . CompilerError . prepend) return
211+ where
212+ prepend (CompilationFailure es) = CompilationFailure (x `NonEmpty.cons` es)
213+ prepend (CompilationNoResult es) = CompilationNoResult (x : es)
214+
215+
216+--------------------------------------------------------------------------------
217+-- | Compiler for debugging purposes.
218+-- Passes a message to the debug logger that is printed in verbose mode.
145219debugCompiler :: String -> Compiler ()
146220debugCompiler msg = do
147221 logger <- compilerLogger <$> compilerAsk
lib/Hakyll/Core/Compiler/Internal.hsnew file+364
@@ -0,0 +1,364 @@
1+--------------------------------------------------------------------------------
2+-- | Internally used compiler module
3+{-# LANGUAGE CPP #-}
4+{-# LANGUAGE DeriveFunctor #-}
5+{-# LANGUAGE FlexibleInstances #-}
6+{-# LANGUAGE GADTs #-}
7+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
8+{-# LANGUAGE MultiParamTypeClasses #-}
9+module Hakyll.Core.Compiler.Internal
10+ ( -- * Types
11+ Snapshot
12+ , CompilerRead (..)
13+ , CompilerWrite (..)
14+ , CompilerErrors (..)
15+ , CompilerResult (..)
16+ , Compiler (..)
17+ , runCompiler
18+
19+ -- * Core operations
20+ , compilerResult
21+ , compilerTell
22+ , compilerAsk
23+ , compilerUnsafeIO
24+
25+ -- * Error operations
26+ , compilerThrow
27+ , compilerNoResult
28+ , compilerCatch
29+ , compilerTry
30+ , compilerErrorMessages
31+
32+ -- * Utilities
33+ , compilerDebugEntries
34+ , compilerTellDependencies
35+ , compilerTellCacheHits
36+ ) where
37+
38+
39+--------------------------------------------------------------------------------
40+import Control.Applicative (Alternative (..))
41+import Control.Exception (SomeException, handle)
42+import Control.Monad (forM_)
43+import Control.Monad.Except (MonadError (..))
44+import Data.List.NonEmpty (NonEmpty (..))
45+import qualified Data.List.NonEmpty as NonEmpty
46+#if MIN_VERSION_base(4,9,0)
47+import Data.Semigroup (Semigroup (..))
48+#endif
49+import Data.Set (Set)
50+import qualified Data.Set as S
51+
52+
53+--------------------------------------------------------------------------------
54+import Hakyll.Core.Configuration
55+import Hakyll.Core.Dependencies
56+import Hakyll.Core.Identifier
57+import Hakyll.Core.Identifier.Pattern
58+import qualified Hakyll.Core.Logger as Logger
59+import Hakyll.Core.Metadata
60+import Hakyll.Core.Provider
61+import Hakyll.Core.Routes
62+import Hakyll.Core.Store
63+
64+
65+--------------------------------------------------------------------------------
66+-- | Whilst compiling an item, it possible to save multiple snapshots of it, and
67+-- not just the final result.
68+type Snapshot = String
69+
70+
71+--------------------------------------------------------------------------------
72+-- | Environment in which a compiler runs
73+data CompilerRead = CompilerRead
74+ { -- | Main configuration
75+ compilerConfig :: Configuration
76+ , -- | Underlying identifier
77+ compilerUnderlying :: Identifier
78+ , -- | Resource provider
79+ compilerProvider :: Provider
80+ , -- | List of all known identifiers
81+ compilerUniverse :: Set Identifier
82+ , -- | Site routes
83+ compilerRoutes :: Routes
84+ , -- | Compiler store
85+ compilerStore :: Store
86+ , -- | Logger
87+ compilerLogger :: Logger.Logger
88+ }
89+
90+
91+--------------------------------------------------------------------------------
92+data CompilerWrite = CompilerWrite
93+ { compilerDependencies :: [Dependency]
94+ , compilerCacheHits :: Int
95+ } deriving (Show)
96+
97+
98+--------------------------------------------------------------------------------
99+#if MIN_VERSION_base(4,9,0)
100+instance Semigroup CompilerWrite where
101+ (<>) (CompilerWrite d1 h1) (CompilerWrite d2 h2) =
102+ CompilerWrite (d1 ++ d2) (h1 + h2)
103+
104+instance Monoid CompilerWrite where
105+ mempty = CompilerWrite [] 0
106+ mappend = (<>)
107+#else
108+instance Monoid CompilerWrite where
109+ mempty = CompilerWrite [] 0
110+ mappend (CompilerWrite d1 h1) (CompilerWrite d2 h2) =
111+ CompilerWrite (d1 ++ d2) (h1 + h2)
112+#endif
113+
114+
115+--------------------------------------------------------------------------------
116+-- | Distinguishes reasons in a 'CompilerError'
117+data CompilerErrors a
118+ -- | One or more exceptions occured during compilation
119+ = CompilationFailure (NonEmpty a)
120+ -- | Absence of any result, most notably in template contexts. May still
121+ -- have error messages.
122+ | CompilationNoResult [a]
123+ deriving Functor
124+
125+
126+-- | Unwrap a `CompilerErrors`
127+compilerErrorMessages :: CompilerErrors a -> [a]
128+compilerErrorMessages (CompilationFailure x) = NonEmpty.toList x
129+compilerErrorMessages (CompilationNoResult x) = x
130+
131+
132+--------------------------------------------------------------------------------
133+-- | An intermediate result of a compilation step
134+data CompilerResult a
135+ = CompilerDone a CompilerWrite
136+ | CompilerSnapshot Snapshot (Compiler a)
137+ | CompilerRequire (Identifier, Snapshot) (Compiler a)
138+ | CompilerError (CompilerErrors String)
139+
140+
141+--------------------------------------------------------------------------------
142+-- | A monad which lets you compile items and takes care of dependency tracking
143+-- for you.
144+newtype Compiler a = Compiler
145+ { unCompiler :: CompilerRead -> IO (CompilerResult a)
146+ }
147+
148+
149+--------------------------------------------------------------------------------
150+instance Functor Compiler where
151+ fmap f (Compiler c) = Compiler $ \r -> do
152+ res <- c r
153+ return $ case res of
154+ CompilerDone x w -> CompilerDone (f x) w
155+ CompilerSnapshot s c' -> CompilerSnapshot s (fmap f c')
156+ CompilerRequire i c' -> CompilerRequire i (fmap f c')
157+ CompilerError e -> CompilerError e
158+ {-# INLINE fmap #-}
159+
160+
161+--------------------------------------------------------------------------------
162+instance Monad Compiler where
163+ return x = compilerResult $ CompilerDone x mempty
164+ {-# INLINE return #-}
165+
166+ Compiler c >>= f = Compiler $ \r -> do
167+ res <- c r
168+ case res of
169+ CompilerDone x w -> do
170+ res' <- unCompiler (f x) r
171+ return $ case res' of
172+ CompilerDone y w' -> CompilerDone y (w `mappend` w')
173+ CompilerSnapshot s c' -> CompilerSnapshot s $ do
174+ compilerTell w -- Save dependencies!
175+ c'
176+ CompilerRequire i c' -> CompilerRequire i $ do
177+ compilerTell w -- Save dependencies!
178+ c'
179+ CompilerError e -> CompilerError e
180+
181+ CompilerSnapshot s c' -> return $ CompilerSnapshot s (c' >>= f)
182+ CompilerRequire i c' -> return $ CompilerRequire i (c' >>= f)
183+ CompilerError e -> return $ CompilerError e
184+ {-# INLINE (>>=) #-}
185+
186+ fail = compilerThrow . return
187+ {-# INLINE fail #-}
188+
189+
190+--------------------------------------------------------------------------------
191+instance Applicative Compiler where
192+ pure x = return x
193+ {-# INLINE pure #-}
194+
195+ f <*> x = f >>= \f' -> fmap f' x
196+ {-# INLINE (<*>) #-}
197+
198+
199+--------------------------------------------------------------------------------
200+-- | Access provided metadata from anywhere
201+instance MonadMetadata Compiler where
202+ getMetadata = compilerGetMetadata
203+ getMatches = compilerGetMatches
204+
205+
206+--------------------------------------------------------------------------------
207+-- | Compilation may fail with multiple error messages.
208+-- 'catchError' handles errors from 'throwError', 'fail' and 'Hakyll.Core.Compiler.noResult'
209+instance MonadError [String] Compiler where
210+ throwError = compilerThrow
211+ catchError c = compilerCatch c . (. compilerErrorMessages)
212+
213+
214+--------------------------------------------------------------------------------
215+-- | Like 'unCompiler' but treating IO exceptions as 'CompilerError's
216+runCompiler :: Compiler a -> CompilerRead -> IO (CompilerResult a)
217+runCompiler compiler read' = handle handler $ unCompiler compiler read'
218+ where
219+ handler :: SomeException -> IO (CompilerResult a)
220+ handler e = return $ CompilerError $ CompilationFailure $ show e :| []
221+
222+
223+--------------------------------------------------------------------------------
224+-- | Trying alternative compilers if the first fails, regardless whether through
225+-- 'fail', 'throwError' or 'Hakyll.Core.Compiler.noResult'.
226+-- Aggregates error messages if all fail.
227+instance Alternative Compiler where
228+ empty = compilerNoResult []
229+ x <|> y = x `compilerCatch` (\rx -> y `compilerCatch` (\ry ->
230+ case (rx, ry) of
231+ (CompilationFailure xs, CompilationFailure ys) ->
232+ compilerThrow $ NonEmpty.toList xs ++ NonEmpty.toList ys
233+ (CompilationFailure xs, CompilationNoResult ys) ->
234+ debug ys >> compilerThrow (NonEmpty.toList xs)
235+ (CompilationNoResult xs, CompilationFailure ys) ->
236+ debug xs >> compilerThrow (NonEmpty.toList ys)
237+ (CompilationNoResult xs, CompilationNoResult ys) -> compilerNoResult $ xs ++ ys
238+ ))
239+ where
240+ debug = compilerDebugEntries "Hakyll.Core.Compiler.Internal: Alternative fail suppressed"
241+ {-# INLINE (<|>) #-}
242+
243+
244+--------------------------------------------------------------------------------
245+-- | Put the result back in a compiler
246+compilerResult :: CompilerResult a -> Compiler a
247+compilerResult x = Compiler $ \_ -> return x
248+{-# INLINE compilerResult #-}
249+
250+
251+--------------------------------------------------------------------------------
252+-- | Get the current environment
253+compilerAsk :: Compiler CompilerRead
254+compilerAsk = Compiler $ \r -> return $ CompilerDone r mempty
255+{-# INLINE compilerAsk #-}
256+
257+
258+--------------------------------------------------------------------------------
259+-- | Put a 'CompilerWrite'
260+compilerTell :: CompilerWrite -> Compiler ()
261+compilerTell = compilerResult . CompilerDone ()
262+{-# INLINE compilerTell #-}
263+
264+
265+--------------------------------------------------------------------------------
266+-- | Run an IO computation without dependencies in a Compiler
267+compilerUnsafeIO :: IO a -> Compiler a
268+compilerUnsafeIO io = Compiler $ \_ -> do
269+ x <- io
270+ return $ CompilerDone x mempty
271+{-# INLINE compilerUnsafeIO #-}
272+
273+
274+--------------------------------------------------------------------------------
275+-- | Throw errors in the 'Compiler'.
276+--
277+-- If no messages are given, this is considered a 'CompilationNoResult' error.
278+-- Otherwise, it is treated as a proper compilation failure.
279+compilerThrow :: [String] -> Compiler a
280+compilerThrow = compilerResult . CompilerError .
281+ maybe (CompilationNoResult []) CompilationFailure .
282+ NonEmpty.nonEmpty
283+
284+-- | Put a 'CompilerError' with multiple messages as 'CompilationNoResult'
285+compilerNoResult :: [String] -> Compiler a
286+compilerNoResult = compilerResult . CompilerError . CompilationNoResult
287+
288+
289+--------------------------------------------------------------------------------
290+-- | Allows to distinguish 'CompilerError's and branch on them with 'Either'
291+--
292+-- prop> compilerTry = (`compilerCatch` return . Left) . fmap Right
293+compilerTry :: Compiler a -> Compiler (Either (CompilerErrors String) a)
294+compilerTry (Compiler x) = Compiler $ \r -> do
295+ res <- x r
296+ case res of
297+ CompilerDone res' w -> return (CompilerDone (Right res') w)
298+ CompilerSnapshot s c -> return (CompilerSnapshot s (compilerTry c))
299+ CompilerRequire i c -> return (CompilerRequire i (compilerTry c))
300+ CompilerError e -> return (CompilerDone (Left e) mempty)
301+{-# INLINE compilerTry #-}
302+
303+
304+--------------------------------------------------------------------------------
305+-- | Allows you to recover from 'CompilerError's.
306+-- Uses the same parameter order as 'catchError' so that it can be used infix.
307+--
308+-- prop> c `compilerCatch` f = compilerTry c >>= either f return
309+compilerCatch :: Compiler a -> (CompilerErrors String -> Compiler a) -> Compiler a
310+compilerCatch (Compiler x) f = Compiler $ \r -> do
311+ res <- x r
312+ case res of
313+ CompilerDone res' w -> return (CompilerDone res' w)
314+ CompilerSnapshot s c -> return (CompilerSnapshot s (compilerCatch c f))
315+ CompilerRequire i c -> return (CompilerRequire i (compilerCatch c f))
316+ CompilerError e -> unCompiler (f e) r
317+{-# INLINE compilerCatch #-}
318+
319+
320+--------------------------------------------------------------------------------
321+compilerDebugLog :: [String] -> Compiler ()
322+compilerDebugLog ms = do
323+ logger <- compilerLogger <$> compilerAsk
324+ compilerUnsafeIO $ forM_ ms $ Logger.debug logger
325+
326+--------------------------------------------------------------------------------
327+-- | Pass a list of messages with a heading to the debug logger
328+compilerDebugEntries :: String -> [String] -> Compiler ()
329+compilerDebugEntries msg = compilerDebugLog . (msg:) . map indent
330+ where
331+ indent = unlines . map (" "++) . lines
332+
333+
334+--------------------------------------------------------------------------------
335+compilerTellDependencies :: [Dependency] -> Compiler ()
336+compilerTellDependencies ds = do
337+ compilerDebugLog $ map (\d ->
338+ "Hakyll.Core.Compiler.Internal: Adding dependency: " ++ show d) ds
339+ compilerTell mempty {compilerDependencies = ds}
340+{-# INLINE compilerTellDependencies #-}
341+
342+
343+--------------------------------------------------------------------------------
344+compilerTellCacheHits :: Int -> Compiler ()
345+compilerTellCacheHits ch = compilerTell mempty {compilerCacheHits = ch}
346+{-# INLINE compilerTellCacheHits #-}
347+
348+
349+--------------------------------------------------------------------------------
350+compilerGetMetadata :: Identifier -> Compiler Metadata
351+compilerGetMetadata identifier = do
352+ provider <- compilerProvider <$> compilerAsk
353+ compilerTellDependencies [IdentifierDependency identifier]
354+ compilerUnsafeIO $ resourceMetadata provider identifier
355+
356+
357+--------------------------------------------------------------------------------
358+compilerGetMatches :: Pattern -> Compiler [Identifier]
359+compilerGetMatches pattern = do
360+ universe <- compilerUniverse <$> compilerAsk
361+ let matching = filterMatches pattern $ S.toList universe
362+ set' = S.fromList matching
363+ compilerTellDependencies [PatternDependency pattern set']
364+ return matching
src/Hakyll/Core/Compiler/Require.hs → lib/Hakyll/Core/Compiler/Require.hsrenamed+6−11
@@ -13,7 +13,6 @@ module Hakyll.Core.Compiler.Require
1313
1414
1515--------------------------------------------------------------------------------
16−import Control.Applicative ((<$>))
1716import Control.Monad (when)
1817import Data.Binary (Binary)
1918import qualified Data.Set as S
@@ -31,12 +30,6 @@ import Hakyll.Core.Store (Store)
3130import qualified Hakyll.Core.Store as Store
3231
3332
34−--------------------------------------------------------------------------------
35−-- | Whilst compiling an item, it possible to save multiple snapshots of it, and
36−-- not just the final result.
37−type Snapshot = String
38−
39−
4033--------------------------------------------------------------------------------
4134save :: (Binary a, Typeable a) => Store -> Item a -> IO ()
4235save store item = saveSnapshot store final item
@@ -67,14 +60,14 @@ loadSnapshot id' snapshot = do
6760 universe <- compilerUniverse <$> compilerAsk
6861
6962 -- Quick check for better error messages
70− when (id' `S.notMember` universe) $ compilerThrow notFound
63+ when (id' `S.notMember` universe) $ fail notFound
7164
7265 compilerTellDependencies [IdentifierDependency id']
73− compilerResult $ CompilerRequire id' $ do
66+ compilerResult $ CompilerRequire (id', snapshot) $ do
7467 result <- compilerUnsafeIO $ Store.get store (key id' snapshot)
7568 case result of
76− Store.NotFound -> compilerThrow notFound
77− Store.WrongType e r -> compilerThrow $ wrongType e r
69+ Store.NotFound -> fail notFound
70+ Store.WrongType e r -> fail $ wrongType e r
7871 Store.Found x -> return $ Item id' x
7972 where
8073 notFound =
@@ -98,6 +91,7 @@ loadBody id' = loadSnapshotBody id' final
9891
9992
10093--------------------------------------------------------------------------------
94+-- | A shortcut for only requiring the body for a specific snapshot of an item
10195loadSnapshotBody :: (Binary a, Typeable a)
10296 => Identifier -> Snapshot -> Compiler a
10397loadSnapshotBody id' snapshot = fmap itemBody $ loadSnapshot id' snapshot
@@ -110,6 +104,7 @@ loadAll pattern = loadAllSnapshots pattern final
110104
111105
112106--------------------------------------------------------------------------------
107+-- | Load a specific snapshot for each of dynamic list of items
113108loadAllSnapshots :: (Binary a, Typeable a)
114109 => Pattern -> Snapshot -> Compiler [Item a]
115110loadAllSnapshots pattern snapshot = do
src/Hakyll/Core/Configuration.hs → lib/Hakyll/Core/Configuration.hsrenamed+57−19
@@ -1,20 +1,20 @@
11--------------------------------------------------------------------------------
22-- | Exports a datastructure for the top-level hakyll configuration
33module Hakyll.Core.Configuration
4− ( Verbosity (..)
5− , Configuration (..)
4+ ( Configuration (..)
65 , shouldIgnoreFile
76 , defaultConfiguration
87 ) where
98
109
1110--------------------------------------------------------------------------------
12−import Data.List (isPrefixOf, isSuffixOf)
13−import System.FilePath (normalise, takeFileName)
14−
15−
16−--------------------------------------------------------------------------------
17−import Hakyll.Core.Logger
11+import Data.Default (Default (..))
12+import Data.List (isPrefixOf, isSuffixOf)
13+import System.Directory (canonicalizePath)
14+import System.Exit (ExitCode)
15+import System.FilePath (isAbsolute, normalise, takeFileName)
16+import System.IO.Error (catchIOError)
17+import System.Process (system)
1818
1919
2020--------------------------------------------------------------------------------
@@ -23,6 +23,8 @@ data Configuration = Configuration
2323 destinationDirectory :: FilePath
2424 , -- | Directory where hakyll's internal store is kept
2525 storeDirectory :: FilePath
26+ , -- | Directory in which some temporary files will be kept
27+ tmpDirectory :: FilePath
2628 , -- | Directory where hakyll finds the files to compile. This is @.@ by
2729 -- default.
2830 providerDirectory :: FilePath
@@ -40,7 +42,7 @@ data Configuration = Configuration
4042 --
4143 -- Note that the files in 'destinationDirectory' and 'storeDirectory' will
4244 -- also be ignored. Note that this is the configuration parameter, if you
43− -- want to use the test, you should use 'shouldIgnoreFile'.
45+ -- want to use the test, you should use 'shouldIgnoreFile'.
4446 --
4547 ignoreFile :: FilePath -> Bool
4648 , -- | Here, you can plug in a system command to upload/deploy your site.
@@ -54,13 +56,33 @@ data Configuration = Configuration
5456 -- > ./site deploy
5557 --
5658 deployCommand :: String
59+ , -- | Function to deploy the site from Haskell.
60+ --
61+ -- By default, this command executes the shell command stored in
62+ -- 'deployCommand'. If you override it, 'deployCommand' will not
63+ -- be used implicitely.
64+ --
65+ -- The 'Configuration' object is passed as a parameter to this
66+ -- function.
67+ --
68+ deploySite :: Configuration -> IO ExitCode
5769 , -- | Use an in-memory cache for items. This is faster but uses more
5870 -- memory.
5971 inMemoryCache :: Bool
60− -- | Verbosity for the logger
61− , verbosity :: Verbosity
72+ , -- | Override default host for preview server. Default is "127.0.0.1",
73+ -- which binds only on the loopback address.
74+ -- One can also override the host as a command line argument:
75+ -- ./site preview -h "0.0.0.0"
76+ previewHost :: String
77+ , -- | Override default port for preview server. Default is 8000.
78+ -- One can also override the port as a command line argument:
79+ -- ./site preview -p 1234
80+ previewPort :: Int
6281 }
6382
83+--------------------------------------------------------------------------------
84+instance Default Configuration where
85+ def = defaultConfiguration
6486
6587--------------------------------------------------------------------------------
6688-- | Default configuration for a hakyll application
@@ -68,11 +90,14 @@ defaultConfiguration :: Configuration
6890defaultConfiguration = Configuration
6991 { destinationDirectory = "_site"
7092 , storeDirectory = "_cache"
93+ , tmpDirectory = "_cache/tmp"
7194 , providerDirectory = "."
7295 , ignoreFile = ignoreFile'
73− , deployCommand = "echo 'No deploy command specified'"
96+ , deployCommand = "echo 'No deploy command specified' && exit 1"
97+ , deploySite = system . deployCommand
7498 , inMemoryCache = True
75− , verbosity = Message
99+ , previewHost = "127.0.0.1"
100+ , previewPort = 8000
76101 }
77102 where
78103 ignoreFile' path
@@ -87,10 +112,23 @@ defaultConfiguration = Configuration
87112
88113--------------------------------------------------------------------------------
89114-- | Check if a file should be ignored
90−shouldIgnoreFile :: Configuration -> FilePath -> Bool
91−shouldIgnoreFile conf path =
92− destinationDirectory conf `isPrefixOf` path' ||
93− storeDirectory conf `isPrefixOf` path' ||
94− ignoreFile conf path'
115+shouldIgnoreFile :: Configuration -> FilePath -> IO Bool
116+shouldIgnoreFile conf path = orM
117+ [ inDir (destinationDirectory conf)
118+ , inDir (storeDirectory conf)
119+ , inDir (tmpDirectory conf)
120+ , return (ignoreFile conf path')
121+ ]
95122 where
96− path' = normalise path
123+ path' = normalise path
124+ absolute = isAbsolute path
125+
126+ inDir dir
127+ | absolute = do
128+ dir' <- catchIOError (canonicalizePath dir) (const $ return dir)
129+ return $ dir' `isPrefixOf` path'
130+ | otherwise = return $ dir `isPrefixOf` path'
131+
132+ orM :: [IO Bool] -> IO Bool
133+ orM [] = return False
134+ orM (x : xs) = x >>= \b -> if b then return True else orM xs
src/Hakyll/Core/Dependencies.hs → lib/Hakyll/Core/Dependencies.hsrenamed+3−4
@@ -8,7 +8,6 @@ module Hakyll.Core.Dependencies
88
99
1010--------------------------------------------------------------------------------
11−import Control.Applicative ((<$>), (<*>))
1211import Control.Monad (foldM, forM_, unless, when)
1312import Control.Monad.Reader (ask)
1413import Control.Monad.RWS (RWS, runRWS)
@@ -32,7 +31,7 @@ import Hakyll.Core.Identifier.Pattern
3231
3332--------------------------------------------------------------------------------
3433data Dependency
35− = PatternDependency Pattern [Identifier]
34+ = PatternDependency Pattern (Set Identifier)
3635 | IdentifierDependency Identifier
3736 deriving (Show, Typeable)
3837
@@ -91,7 +90,7 @@ dependenciesFor id' = do
9190 return $ concatMap dependenciesFor' $ fromMaybe [] $ M.lookup id' facts
9291 where
9392 dependenciesFor' (IdentifierDependency i) = [i]
94− dependenciesFor' (PatternDependency _ is) = is
93+ dependenciesFor' (PatternDependency _ is) = S.toList is
9594
9695
9796--------------------------------------------------------------------------------
@@ -116,7 +115,7 @@ checkChangedPatterns = do
116115 go _ ds (IdentifierDependency i) = return $ IdentifierDependency i : ds
117116 go id' ds (PatternDependency p ls) = do
118117 universe <- ask
119− let ls' = filterMatches p universe
118+ let ls' = S.fromList $ filterMatches p universe
120119 if ls == ls'
121120 then return $ PatternDependency p ls : ds
122121 else do
lib/Hakyll/Core/File.hsnew file+93
@@ -0,0 +1,93 @@
1+--------------------------------------------------------------------------------
2+-- | Exports simple compilers to just copy files
3+{-# LANGUAGE CPP #-}
4+{-# LANGUAGE DeriveDataTypeable #-}
5+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6+module Hakyll.Core.File
7+ ( CopyFile (..)
8+ , copyFileCompiler
9+ , TmpFile (..)
10+ , newTmpFile
11+ ) where
12+
13+
14+--------------------------------------------------------------------------------
15+import Data.Binary (Binary (..))
16+import Data.Typeable (Typeable)
17+#if MIN_VERSION_directory(1,2,6)
18+import System.Directory (copyFileWithMetadata)
19+#else
20+import System.Directory (copyFile)
21+#endif
22+import System.Directory (doesFileExist,
23+ renameFile)
24+import System.FilePath ((</>))
25+import System.Random (randomIO)
26+
27+
28+--------------------------------------------------------------------------------
29+import Hakyll.Core.Compiler
30+import Hakyll.Core.Compiler.Internal
31+import Hakyll.Core.Configuration
32+import Hakyll.Core.Item
33+import Hakyll.Core.Provider
34+import qualified Hakyll.Core.Store as Store
35+import Hakyll.Core.Util.File
36+import Hakyll.Core.Writable
37+
38+
39+--------------------------------------------------------------------------------
40+-- | This will copy any file directly by using a system call
41+newtype CopyFile = CopyFile FilePath
42+ deriving (Binary, Eq, Ord, Show, Typeable)
43+
44+
45+--------------------------------------------------------------------------------
46+instance Writable CopyFile where
47+#if MIN_VERSION_directory(1,2,6)
48+ write dst (Item _ (CopyFile src)) = copyFileWithMetadata src dst
49+#else
50+ write dst (Item _ (CopyFile src)) = copyFile src dst
51+#endif
52+--------------------------------------------------------------------------------
53+copyFileCompiler :: Compiler (Item CopyFile)
54+copyFileCompiler = do
55+ identifier <- getUnderlying
56+ provider <- compilerProvider <$> compilerAsk
57+ makeItem $ CopyFile $ resourceFilePath provider identifier
58+
59+
60+--------------------------------------------------------------------------------
61+newtype TmpFile = TmpFile FilePath
62+ deriving (Typeable)
63+
64+
65+--------------------------------------------------------------------------------
66+instance Binary TmpFile where
67+ put _ = return ()
68+ get = error $
69+ "Hakyll.Core.File.TmpFile: You tried to load a TmpFile, however, " ++
70+ "this is not possible since these are deleted as soon as possible."
71+
72+
73+--------------------------------------------------------------------------------
74+instance Writable TmpFile where
75+ write dst (Item _ (TmpFile fp)) = renameFile fp dst
76+
77+
78+--------------------------------------------------------------------------------
79+-- | Create a tmp file
80+newTmpFile :: String -- ^ Suffix and extension
81+ -> Compiler TmpFile -- ^ Resulting tmp path
82+newTmpFile suffix = do
83+ path <- mkPath
84+ compilerUnsafeIO $ makeDirectories path
85+ debugCompiler $ "newTmpFile " ++ path
86+ return $ TmpFile path
87+ where
88+ mkPath = do
89+ rand <- compilerUnsafeIO $ randomIO :: Compiler Int
90+ tmp <- tmpDirectory . compilerConfig <$> compilerAsk
91+ let path = tmp </> Store.hash [show rand] ++ "-" ++ suffix
92+ exists <- compilerUnsafeIO $ doesFileExist path
93+ if exists then mkPath else return path
src/Hakyll/Core/Identifier.hs → lib/Hakyll/Core/Identifier.hsrenamed+1−2
@@ -19,7 +19,6 @@ module Hakyll.Core.Identifier
1919
2020
2121--------------------------------------------------------------------------------
22−import Control.Applicative ((<$>), (<*>))
2322import Control.DeepSeq (NFData (..))
2423import Data.List (intercalate)
2524import System.FilePath (dropTrailingPathSeparator, splitPath)
@@ -63,7 +62,7 @@ instance Show Identifier where
6362
6463--------------------------------------------------------------------------------
6564-- | Parse an identifier from a string
66−fromFilePath :: String -> Identifier
65+fromFilePath :: FilePath -> Identifier
6766fromFilePath = Identifier Nothing .
6867 intercalate "/" . filter (not . null) . split'
6968 where
src/Hakyll/Core/Identifier/Pattern.hs → lib/Hakyll/Core/Identifier/Pattern.hsrenamed+66−108
@@ -27,7 +27,8 @@
2727-- * @\"foo\/*.html\"@ will match all HTML files in the @\"foo\/\"@ directory.
2828--
2929-- The 'capture' function allows the user to get access to the elements captured
30−-- by the capture elements in the pattern.
30+-- by the capture elements in a glob or regex pattern.
31+{-# OPTIONS_GHC -fno-warn-orphans #-}
3132module Hakyll.Core.Identifier.Pattern
3233 ( -- * The pattern type
3334 Pattern
@@ -37,11 +38,13 @@ module Hakyll.Core.Identifier.Pattern
3738 , fromList
3839 , fromRegex
3940 , fromVersion
41+ , hasVersion
42+ , hasNoVersion
4043
41− -- * Manipulating patterns
44+ -- * Composing patterns
45+ , (.&&.)
46+ , (.||.)
4247 , complement
43− , withVersion
44− , fromLiteral
4548
4649 -- * Applying patterns
4750 , matches
@@ -55,79 +58,22 @@ module Hakyll.Core.Identifier.Pattern
5558
5659
5760--------------------------------------------------------------------------------
58−import Control.Applicative (pure, (<$>), (<*>))
59−import Control.Arrow ((&&&), (>>>))
60−import Control.Monad (msum)
61−import Data.Binary (Binary (..), getWord8, putWord8)
62−import Data.List (inits, isPrefixOf, tails)
63−import Data.Maybe (isJust)
64−import Data.Monoid (Monoid, mappend, mempty)
65−import Data.Set (Set)
66−import qualified Data.Set as S
61+import Control.Arrow ((&&&), (>>>))
62+import Control.Monad (msum)
63+import Data.List (inits, isPrefixOf,
64+ tails)
65+import Data.Maybe (isJust)
66+import qualified Data.Set as S
6767
6868
6969--------------------------------------------------------------------------------
70−import GHC.Exts (IsString, fromString)
71−import Text.Regex.TDFA ((=~))
70+import GHC.Exts (IsString, fromString)
71+import Text.Regex.TDFA ((=~))
7272
7373
7474--------------------------------------------------------------------------------
7575import Hakyll.Core.Identifier
76−
77−
78−--------------------------------------------------------------------------------
79−-- | Elements of a glob pattern
80−data GlobComponent
81− = Capture
82− | CaptureMany
83− | Literal String
84− deriving (Eq, Show)
85−
86−
87−--------------------------------------------------------------------------------
88−instance Binary GlobComponent where
89− put Capture = putWord8 0
90− put CaptureMany = putWord8 1
91− put (Literal s) = putWord8 2 >> put s
92−
93− get = getWord8 >>= \t -> case t of
94− 0 -> pure Capture
95− 1 -> pure CaptureMany
96− 2 -> Literal <$> get
97− _ -> error "Data.Binary.get: Invalid GlobComponent"
98−
99−
100−--------------------------------------------------------------------------------
101−-- | Type that allows matching on identifiers
102−data Pattern
103− = Everything
104− | Complement Pattern
105− | And Pattern Pattern
106− | Glob [GlobComponent]
107− | List (Set Identifier)
108− | Regex String
109− | Version (Maybe String)
110− deriving (Show)
111−
112−
113−--------------------------------------------------------------------------------
114−instance Binary Pattern where
115− put Everything = putWord8 0
116− put (Complement p) = putWord8 1 >> put p
117− put (And x y) = putWord8 2 >> put x >> put y
118− put (Glob g) = putWord8 3 >> put g
119− put (List is) = putWord8 4 >> put is
120− put (Regex r) = putWord8 5 >> put r
121− put (Version v) = putWord8 6 >> put v
122−
123− get = getWord8 >>= \t -> case t of
124− 0 -> pure Everything
125− 1 -> Complement <$> get
126− 2 -> And <$> get <*> get
127− 3 -> Glob <$> get
128− 4 -> List <$> get
129− 5 -> Regex <$> get
130− _ -> Version <$> get
76+import Hakyll.Core.Identifier.Pattern.Internal
13177
13278
13379--------------------------------------------------------------------------------
@@ -135,22 +81,6 @@ instance IsString Pattern where
13581 fromString = fromGlob
13682
13783
138−--------------------------------------------------------------------------------
139−instance Monoid Pattern where
140− mempty = Everything
141− mappend x y = optimize $ And x y
142−
143−
144−--------------------------------------------------------------------------------
145−-- | This is necessary for good 'isLiteral' results
146−optimize :: Pattern -> Pattern
147−optimize (Complement x) = Complement (optimize x)
148−optimize (And x Everything) = x
149−optimize (And Everything y) = y
150−optimize (And x y) = And (optimize x) (optimize y)
151−optimize p = p
152−
153−
15484--------------------------------------------------------------------------------
15585-- | Parse a pattern from a string
15686fromGlob :: String -> Pattern
@@ -166,7 +96,24 @@ fromGlob = Glob . parse'
16696
16797
16898--------------------------------------------------------------------------------
169−-- | Create a 'Pattern' from a list of 'Identifier's it should match
99+-- | Create a 'Pattern' from a list of 'Identifier's it should match.
100+--
101+-- /Warning/: use this carefully with 'hasNoVersion' and 'hasVersion'. The
102+-- 'Identifier's in the list /already/ have versions assigned, and the pattern
103+-- will then only match the intersection of both versions.
104+--
105+-- A more concrete example,
106+--
107+-- > fromList ["foo.markdown"] .&&. hasVersion "pdf"
108+--
109+-- will not match anything! The @"foo.markdown"@ 'Identifier' has no version
110+-- assigned, so the LHS of '.&&.' will only match this 'Identifier' with no
111+-- version. The RHS only matches 'Identifier's with version set to @"pdf"@ --
112+-- hence, this pattern matches nothing.
113+--
114+-- The correct way to use this is:
115+--
116+-- > fromList $ map (setVersion $ Just "pdf") ["foo.markdown"]
170117fromList :: [Identifier] -> Pattern
171118fromList = List . S.fromList
172119
@@ -188,34 +135,43 @@ fromVersion = Version
188135
189136
190137--------------------------------------------------------------------------------
191−-- | Inverts a pattern, e.g.
192−--
193−-- > complement "foo/bar.html"
138+-- | Specify a version, e.g.
194139--
195−-- will match /anything/ except @\"foo\/bar.html\"@
196−complement :: Pattern -> Pattern
197−complement = Complement
140+-- > "foo/*.markdown" .&&. hasVersion "pdf"
141+hasVersion :: String -> Pattern
142+hasVersion = fromVersion . Just
198143
199144
200145--------------------------------------------------------------------------------
201−-- | Specify a version, e.g.
146+-- | Match only if the identifier has no version set, e.g.
202147--
203−-- > "foo/*.markdown" `withVersion` "pdf"
204−withVersion :: Pattern -> String -> Pattern
205−withVersion p v = optimize $ And p $ fromVersion $ Just v
148+-- > "foo/*.markdown" .&&. hasNoVersion
149+hasNoVersion :: Pattern
150+hasNoVersion = fromVersion Nothing
206151
207152
208153--------------------------------------------------------------------------------
209−-- | Check if a pattern is a literal. @\"*.markdown\"@ is not a literal but
210−-- @\"posts.markdown\"@ is.
211−fromLiteral :: Pattern -> Maybe Identifier
212−fromLiteral pattern = case pattern of
213− Glob p -> fmap fromFilePath $ foldr fromLiteral' (Just "") p
214− _ -> Nothing
215− where
216− fromLiteral' (Literal x) (Just y) = Just $ x ++ y
217− fromLiteral' _ _ = Nothing
154+-- | '&&' for patterns: the given identifier must match both subterms
155+(.&&.) :: Pattern -> Pattern -> Pattern
156+x .&&. y = And x y
157+infixr 3 .&&.
158+
218159
160+--------------------------------------------------------------------------------
161+-- | '||' for patterns: the given identifier must match any subterm
162+(.||.) :: Pattern -> Pattern -> Pattern
163+x .||. y = complement (complement x `And` complement y) -- De Morgan's law
164+infixr 2 .||.
165+
166+
167+--------------------------------------------------------------------------------
168+-- | Inverts a pattern, e.g.
169+--
170+-- > complement "foo/bar.html"
171+--
172+-- will match /anything/ except @\"foo\/bar.html\"@
173+complement :: Pattern -> Pattern
174+complement = Complement
219175
220176
221177--------------------------------------------------------------------------------
@@ -244,9 +200,11 @@ splits = inits &&& tails >>> uncurry zip >>> reverse
244200
245201
246202--------------------------------------------------------------------------------
247−-- | Match a glob against a pattern, generating a list of captures
203+-- | Match a glob or regex pattern against an identifier, generating a list of captures
248204capture :: Pattern -> Identifier -> Maybe [String]
249205capture (Glob p) i = capture' p (toFilePath i)
206+capture (Regex pat) i = Just groups
207+ where (_, _, _, groups) = ((toFilePath i) =~ pat) :: (String, String, String, [String])
250208capture _ _ = Nothing
251209
252210
@@ -274,7 +232,7 @@ capture' (CaptureMany : ms) str =
274232--
275233-- Example:
276234--
277−-- > fromCapture (parseGlob "tags/*") "foo"
235+-- > fromCapture (fromGlob "tags/*") "foo"
278236--
279237-- Result:
280238--
lib/Hakyll/Core/Identifier/Pattern/Internal.hsnew file+92
@@ -0,0 +1,92 @@
1+-- | This internal module is mostly here to prevent CPP conflicting with Haskell
2+-- comments.
3+{-# LANGUAGE CPP #-}
4+module Hakyll.Core.Identifier.Pattern.Internal
5+ ( GlobComponent (..)
6+ , Pattern (..)
7+ ) where
8+
9+
10+--------------------------------------------------------------------------------
11+import Data.Binary (Binary (..), getWord8, putWord8)
12+import Data.Set (Set)
13+
14+
15+--------------------------------------------------------------------------------
16+#if MIN_VERSION_base(4,9,0)
17+import Data.Semigroup (Semigroup (..))
18+#endif
19+
20+
21+--------------------------------------------------------------------------------
22+import Hakyll.Core.Identifier
23+
24+
25+--------------------------------------------------------------------------------
26+-- | Elements of a glob pattern
27+data GlobComponent
28+ = Capture
29+ | CaptureMany
30+ | Literal String
31+ deriving (Eq, Show)
32+
33+
34+--------------------------------------------------------------------------------
35+instance Binary GlobComponent where
36+ put Capture = putWord8 0
37+ put CaptureMany = putWord8 1
38+ put (Literal s) = putWord8 2 >> put s
39+
40+ get = getWord8 >>= \t -> case t of
41+ 0 -> pure Capture
42+ 1 -> pure CaptureMany
43+ 2 -> Literal <$> get
44+ _ -> error "Data.Binary.get: Invalid GlobComponent"
45+
46+
47+--------------------------------------------------------------------------------
48+-- | Type that allows matching on identifiers
49+data Pattern
50+ = Everything
51+ | Complement Pattern
52+ | And Pattern Pattern
53+ | Glob [GlobComponent]
54+ | List (Set Identifier)
55+ | Regex String
56+ | Version (Maybe String)
57+ deriving (Show)
58+
59+
60+--------------------------------------------------------------------------------
61+instance Binary Pattern where
62+ put Everything = putWord8 0
63+ put (Complement p) = putWord8 1 >> put p
64+ put (And x y) = putWord8 2 >> put x >> put y
65+ put (Glob g) = putWord8 3 >> put g
66+ put (List is) = putWord8 4 >> put is
67+ put (Regex r) = putWord8 5 >> put r
68+ put (Version v) = putWord8 6 >> put v
69+
70+ get = getWord8 >>= \t -> case t of
71+ 0 -> pure Everything
72+ 1 -> Complement <$> get
73+ 2 -> And <$> get <*> get
74+ 3 -> Glob <$> get
75+ 4 -> List <$> get
76+ 5 -> Regex <$> get
77+ _ -> Version <$> get
78+
79+
80+--------------------------------------------------------------------------------
81+#if MIN_VERSION_base(4,9,0)
82+instance Semigroup Pattern where
83+ (<>) = And
84+
85+instance Monoid Pattern where
86+ mempty = Everything
87+ mappend = (<>)
88+#else
89+instance Monoid Pattern where
90+ mempty = Everything
91+ mappend = And
92+#endif
src/Hakyll/Core/Item.hs → lib/Hakyll/Core/Item.hsrenamed−2
@@ -10,10 +10,8 @@ module Hakyll.Core.Item
1010
1111
1212--------------------------------------------------------------------------------
13−import Control.Applicative (Applicative, (<$>), (<*>))
1413import Data.Binary (Binary (..))
1514import Data.Foldable (Foldable (..))
16−import Data.Traversable (Traversable (..))
1715import Data.Typeable (Typeable)
1816import Prelude hiding (foldr)
1917
src/Hakyll/Core/Item/SomeItem.hs → lib/Hakyll/Core/Item/SomeItem.hsrenamed

The content is the same.

src/Hakyll/Core/Logger.hs → lib/Hakyll/Core/Logger.hsrenamed+12−7
@@ -13,12 +13,12 @@ module Hakyll.Core.Logger
1313
1414
1515--------------------------------------------------------------------------------
16−import Control.Applicative (pure, (<$>), (<*>))
1716import Control.Concurrent (forkIO)
1817import Control.Concurrent.Chan (Chan, newChan, readChan, writeChan)
1918import Control.Concurrent.MVar (MVar, newEmptyMVar, putMVar, takeMVar)
2019import Control.Monad (forever)
2120import Control.Monad.Trans (MonadIO, liftIO)
21+import Data.List (intercalate)
2222import Prelude hiding (error)
2323
2424
@@ -42,10 +42,10 @@ data Logger = Logger
4242
4343--------------------------------------------------------------------------------
4444-- | Create a new logger
45−new :: Verbosity -> (String -> IO ()) -> IO Logger
46−new vbty sink = do
45+new :: Verbosity -> IO Logger
46+new vbty = do
4747 logger <- Logger <$>
48− newChan <*> newEmptyMVar <*> pure sink <*> pure vbty
48+ newChan <*> newEmptyMVar <*> pure putStrLn <*> pure vbty
4949 _ <- forkIO $ loggerThread logger
5050 return logger
5151 where
@@ -80,7 +80,7 @@ string l v m
8080
8181--------------------------------------------------------------------------------
8282error :: MonadIO m => Logger -> String -> m ()
83−error l m = string l Error $ " [ERROR] " ++ m
83+error l m = string l Error $ " [ERROR] " ++ indent m
8484
8585
8686--------------------------------------------------------------------------------
@@ -90,9 +90,14 @@ header l = string l Message
9090
9191--------------------------------------------------------------------------------
9292message :: MonadIO m => Logger -> String -> m ()
93−message l m = string l Message $ " " ++ m
93+message l m = string l Message $ " " ++ indent m
9494
9595
9696--------------------------------------------------------------------------------
9797debug :: MonadIO m => Logger -> String -> m ()
98−debug l m = string l Debug $ " [DEBUG] " ++ m
98+debug l m = string l Debug $ " [DEBUG] " ++ indent m
99+
100+
101+--------------------------------------------------------------------------------
102+indent :: String -> String
103+indent = intercalate "\n " . lines
lib/Hakyll/Core/Metadata.hsnew file+138
@@ -0,0 +1,138 @@
1+--------------------------------------------------------------------------------
2+module Hakyll.Core.Metadata
3+ ( Metadata
4+ , lookupString
5+ , lookupStringList
6+
7+ , MonadMetadata (..)
8+ , getMetadataField
9+ , getMetadataField'
10+ , makePatternDependency
11+
12+ , BinaryMetadata (..)
13+ ) where
14+
15+
16+--------------------------------------------------------------------------------
17+import Control.Arrow (second)
18+import Control.Monad (forM)
19+import Data.Binary (Binary (..), getWord8,
20+ putWord8, Get)
21+import qualified Data.HashMap.Strict as HMS
22+import qualified Data.Set as S
23+import qualified Data.Text as T
24+import qualified Data.Vector as V
25+import qualified Data.Yaml.Extended as Yaml
26+import Hakyll.Core.Dependencies
27+import Hakyll.Core.Identifier
28+import Hakyll.Core.Identifier.Pattern
29+
30+
31+--------------------------------------------------------------------------------
32+type Metadata = Yaml.Object
33+
34+
35+--------------------------------------------------------------------------------
36+lookupString :: String -> Metadata -> Maybe String
37+lookupString key meta = HMS.lookup (T.pack key) meta >>= Yaml.toString
38+
39+
40+--------------------------------------------------------------------------------
41+lookupStringList :: String -> Metadata -> Maybe [String]
42+lookupStringList key meta =
43+ HMS.lookup (T.pack key) meta >>= Yaml.toList >>= mapM Yaml.toString
44+
45+
46+--------------------------------------------------------------------------------
47+class Monad m => MonadMetadata m where
48+ getMetadata :: Identifier -> m Metadata
49+ getMatches :: Pattern -> m [Identifier]
50+
51+ getAllMetadata :: Pattern -> m [(Identifier, Metadata)]
52+ getAllMetadata pattern = do
53+ matches' <- getMatches pattern
54+ forM matches' $ \id' -> do
55+ metadata <- getMetadata id'
56+ return (id', metadata)
57+
58+
59+--------------------------------------------------------------------------------
60+getMetadataField :: MonadMetadata m => Identifier -> String -> m (Maybe String)
61+getMetadataField identifier key = do
62+ metadata <- getMetadata identifier
63+ return $ lookupString key metadata
64+
65+
66+--------------------------------------------------------------------------------
67+-- | Version of 'getMetadataField' which throws an error if the field does not
68+-- exist.
69+getMetadataField' :: MonadMetadata m => Identifier -> String -> m String
70+getMetadataField' identifier key = do
71+ field <- getMetadataField identifier key
72+ case field of
73+ Just v -> return v
74+ Nothing -> fail $ "Hakyll.Core.Metadata.getMetadataField': " ++
75+ "Item " ++ show identifier ++ " has no metadata field " ++ show key
76+
77+
78+--------------------------------------------------------------------------------
79+makePatternDependency :: MonadMetadata m => Pattern -> m Dependency
80+makePatternDependency pattern = do
81+ matches' <- getMatches pattern
82+ return $ PatternDependency pattern (S.fromList matches')
83+
84+
85+--------------------------------------------------------------------------------
86+-- | Newtype wrapper for serialization.
87+newtype BinaryMetadata = BinaryMetadata
88+ {unBinaryMetadata :: Metadata}
89+
90+
91+instance Binary BinaryMetadata where
92+ put (BinaryMetadata obj) = put (BinaryYaml $ Yaml.Object obj)
93+ get = do
94+ BinaryYaml (Yaml.Object obj) <- get
95+ return $ BinaryMetadata obj
96+
97+
98+--------------------------------------------------------------------------------
99+newtype BinaryYaml = BinaryYaml {unBinaryYaml :: Yaml.Value}
100+
101+
102+--------------------------------------------------------------------------------
103+instance Binary BinaryYaml where
104+ put (BinaryYaml yaml) = case yaml of
105+ Yaml.Object obj -> do
106+ putWord8 0
107+ let list :: [(T.Text, BinaryYaml)]
108+ list = map (second BinaryYaml) $ HMS.toList obj
109+ put list
110+
111+ Yaml.Array arr -> do
112+ putWord8 1
113+ let list = map BinaryYaml (V.toList arr) :: [BinaryYaml]
114+ put list
115+
116+ Yaml.String s -> putWord8 2 >> put s
117+ Yaml.Number n -> putWord8 3 >> put n
118+ Yaml.Bool b -> putWord8 4 >> put b
119+ Yaml.Null -> putWord8 5
120+
121+ get = do
122+ tag <- getWord8
123+ case tag of
124+ 0 -> do
125+ list <- get :: Get [(T.Text, BinaryYaml)]
126+ return $ BinaryYaml $ Yaml.Object $
127+ HMS.fromList $ map (second unBinaryYaml) list
128+
129+ 1 -> do
130+ list <- get :: Get [BinaryYaml]
131+ return $ BinaryYaml $
132+ Yaml.Array $ V.fromList $ map unBinaryYaml list
133+
134+ 2 -> BinaryYaml . Yaml.String <$> get
135+ 3 -> BinaryYaml . Yaml.Number <$> get
136+ 4 -> BinaryYaml . Yaml.Bool <$> get
137+ 5 -> return $ BinaryYaml Yaml.Null
138+ _ -> fail "Data.Binary.get: Invalid Binary Metadata"
lib/Hakyll/Core/Provider.hsnew file+43
@@ -0,0 +1,43 @@
1+--------------------------------------------------------------------------------
2+-- | This module provides an wrapper API around the file system which does some
3+-- caching.
4+module Hakyll.Core.Provider
5+ ( -- * Constructing resource providers
6+ Internal.Provider
7+ , newProvider
8+
9+ -- * Querying resource properties
10+ , Internal.resourceList
11+ , Internal.resourceExists
12+ , Internal.resourceFilePath
13+ , Internal.resourceModified
14+ , Internal.resourceModificationTime
15+
16+ -- * Access to raw resource content
17+ , Internal.resourceString
18+ , Internal.resourceLBS
19+
20+ -- * Access to metadata and body content
21+ , Internal.resourceMetadata
22+ , Internal.resourceBody
23+ ) where
24+
25+
26+--------------------------------------------------------------------------------
27+import qualified Hakyll.Core.Provider.Internal as Internal
28+import qualified Hakyll.Core.Provider.MetadataCache as Internal
29+import Hakyll.Core.Store (Store)
30+
31+
32+--------------------------------------------------------------------------------
33+-- | Create a resource provider
34+newProvider :: Store -- ^ Store to use
35+ -> (FilePath -> IO Bool) -- ^ Should we ignore this file?
36+ -> FilePath -- ^ Search directory
37+ -> IO Internal.Provider -- ^ Resulting provider
38+newProvider store ignore directory = do
39+ -- Delete metadata cache where necessary
40+ p <- Internal.newProvider store ignore directory
41+ mapM_ (Internal.resourceInvalidateMetadataCache p) $
42+ filter (Internal.resourceModified p) $ Internal.resourceList p
43+ return p
lib/Hakyll/Core/Provider/Internal.hsnew file+202
@@ -0,0 +1,202 @@
1+--------------------------------------------------------------------------------
2+{-# LANGUAGE CPP #-}
3+{-# LANGUAGE DeriveDataTypeable #-}
4+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5+module Hakyll.Core.Provider.Internal
6+ ( ResourceInfo (..)
7+ , Provider (..)
8+ , newProvider
9+
10+ , resourceList
11+ , resourceExists
12+
13+ , resourceFilePath
14+ , resourceString
15+ , resourceLBS
16+
17+ , resourceModified
18+ , resourceModificationTime
19+ ) where
20+
21+
22+--------------------------------------------------------------------------------
23+import Control.DeepSeq (NFData (..), deepseq)
24+import Control.Monad (forM)
25+import Data.Binary (Binary (..))
26+import qualified Data.ByteString.Lazy as BL
27+import Data.Map (Map)
28+import qualified Data.Map as M
29+import Data.Maybe (fromMaybe)
30+import Data.Set (Set)
31+import qualified Data.Set as S
32+import Data.Time (Day (..), UTCTime (..))
33+import Data.Typeable (Typeable)
34+import System.Directory (getModificationTime)
35+import System.FilePath (addExtension, (</>))
36+
37+
38+--------------------------------------------------------------------------------
39+#if !MIN_VERSION_directory(1,2,0)
40+import Data.Time (readTime)
41+import System.Locale (defaultTimeLocale)
42+import System.Time (formatCalendarTime, toCalendarTime)
43+#endif
44+
45+
46+--------------------------------------------------------------------------------
47+import Hakyll.Core.Identifier
48+import Hakyll.Core.Store (Store)
49+import qualified Hakyll.Core.Store as Store
50+import Hakyll.Core.Util.File
51+
52+
53+--------------------------------------------------------------------------------
54+-- | Because UTCTime doesn't have a Binary instance...
55+newtype BinaryTime = BinaryTime {unBinaryTime :: UTCTime}
56+ deriving (Eq, NFData, Ord, Show, Typeable)
57+
58+
59+--------------------------------------------------------------------------------
60+instance Binary BinaryTime where
61+ put (BinaryTime (UTCTime (ModifiedJulianDay d) dt)) =
62+ put d >> put (toRational dt)
63+
64+ get = fmap BinaryTime $ UTCTime
65+ <$> (ModifiedJulianDay <$> get)
66+ <*> (fromRational <$> get)
67+
68+
69+--------------------------------------------------------------------------------
70+data ResourceInfo = ResourceInfo
71+ { resourceInfoModified :: BinaryTime
72+ , resourceInfoMetadata :: Maybe Identifier
73+ } deriving (Show, Typeable)
74+
75+
76+--------------------------------------------------------------------------------
77+instance Binary ResourceInfo where
78+ put (ResourceInfo mtime meta) = put mtime >> put meta
79+ get = ResourceInfo <$> get <*> get
80+
81+
82+--------------------------------------------------------------------------------
83+instance NFData ResourceInfo where
84+ rnf (ResourceInfo mtime meta) = rnf mtime `seq` rnf meta `seq` ()
85+
86+
87+--------------------------------------------------------------------------------
88+-- | Responsible for retrieving and listing resources
89+data Provider = Provider
90+ { -- Top of the provided directory
91+ providerDirectory :: FilePath
92+ , -- | A list of all files found
93+ providerFiles :: Map Identifier ResourceInfo
94+ , -- | A list of the files from the previous run
95+ providerOldFiles :: Map Identifier ResourceInfo
96+ , -- | Underlying persistent store for caching
97+ providerStore :: Store
98+ } deriving (Show)
99+
100+
101+--------------------------------------------------------------------------------
102+-- | Create a resource provider
103+newProvider :: Store -- ^ Store to use
104+ -> (FilePath -> IO Bool) -- ^ Should we ignore this file?
105+ -> FilePath -- ^ Search directory
106+ -> IO Provider -- ^ Resulting provider
107+newProvider store ignore directory = do
108+ list <- map fromFilePath <$> getRecursiveContents ignore directory
109+ let universe = S.fromList list
110+ files <- fmap (maxmtime . M.fromList) $ forM list $ \identifier -> do
111+ rInfo <- getResourceInfo directory universe identifier
112+ return (identifier, rInfo)
113+
114+ -- Get the old files from the store, and then immediately replace them by
115+ -- the new files.
116+ oldFiles <- fromMaybe mempty . Store.toMaybe <$> Store.get store oldKey
117+ oldFiles `deepseq` Store.set store oldKey files
118+
119+ return $ Provider directory files oldFiles store
120+ where
121+ oldKey = ["Hakyll.Core.Provider.Internal.newProvider", "oldFiles"]
122+
123+ -- Update modified if metadata is modified
124+ maxmtime files = flip M.map files $ \rInfo@(ResourceInfo mtime meta) ->
125+ let metaMod = fmap resourceInfoModified $ meta >>= flip M.lookup files
126+ in rInfo {resourceInfoModified = maybe mtime (max mtime) metaMod}
127+
128+
129+--------------------------------------------------------------------------------
130+getResourceInfo :: FilePath -> Set Identifier -> Identifier -> IO ResourceInfo
131+getResourceInfo directory universe identifier = do
132+ mtime <- fileModificationTime $ directory </> toFilePath identifier
133+ return $ ResourceInfo (BinaryTime mtime) $
134+ if mdRsc `S.member` universe then Just mdRsc else Nothing
135+ where
136+ mdRsc = fromFilePath $ flip addExtension "metadata" $ toFilePath identifier
137+
138+
139+--------------------------------------------------------------------------------
140+resourceList :: Provider -> [Identifier]
141+resourceList = M.keys . providerFiles
142+
143+
144+--------------------------------------------------------------------------------
145+-- | Check if a given resource exists
146+resourceExists :: Provider -> Identifier -> Bool
147+resourceExists provider =
148+ (`M.member` providerFiles provider) . setVersion Nothing
149+
150+
151+--------------------------------------------------------------------------------
152+resourceFilePath :: Provider -> Identifier -> FilePath
153+resourceFilePath p i = providerDirectory p </> toFilePath i
154+
155+
156+--------------------------------------------------------------------------------
157+-- | Get the raw body of a resource as string
158+resourceString :: Provider -> Identifier -> IO String
159+resourceString p i = readFile $ resourceFilePath p i
160+
161+
162+--------------------------------------------------------------------------------
163+-- | Get the raw body of a resource of a lazy bytestring
164+resourceLBS :: Provider -> Identifier -> IO BL.ByteString
165+resourceLBS p i = BL.readFile $ resourceFilePath p i
166+
167+
168+--------------------------------------------------------------------------------
169+-- | A resource is modified if it or its metadata has changed
170+resourceModified :: Provider -> Identifier -> Bool
171+resourceModified p r = case (ri, oldRi) of
172+ (Nothing, _) -> False
173+ (Just _, Nothing) -> True
174+ (Just n, Just o) ->
175+ resourceInfoModified n > resourceInfoModified o ||
176+ resourceInfoMetadata n /= resourceInfoMetadata o
177+ where
178+ normal = setVersion Nothing r
179+ ri = M.lookup normal (providerFiles p)
180+ oldRi = M.lookup normal (providerOldFiles p)
181+
182+
183+--------------------------------------------------------------------------------
184+resourceModificationTime :: Provider -> Identifier -> UTCTime
185+resourceModificationTime p i =
186+ case M.lookup (setVersion Nothing i) (providerFiles p) of
187+ Just ri -> unBinaryTime $ resourceInfoModified ri
188+ Nothing -> error $
189+ "Hakyll.Core.Provider.Internal.resourceModificationTime: " ++
190+ "resource " ++ show i ++ " does not exist"
191+
192+
193+--------------------------------------------------------------------------------
194+fileModificationTime :: FilePath -> IO UTCTime
195+fileModificationTime fp = do
196+#if MIN_VERSION_directory(1,2,0)
197+ getModificationTime fp
198+#else
199+ ct <- toCalendarTime =<< getModificationTime fp
200+ let str = formatCalendarTime defaultTimeLocale "%s" ct
201+ return $ readTime defaultTimeLocale "%s" str
202+#endif
lib/Hakyll/Core/Provider/Metadata.hsnew file+152
@@ -0,0 +1,152 @@
1+--------------------------------------------------------------------------------
2+-- | Internal module to parse metadata
3+{-# LANGUAGE BangPatterns #-}
4+{-# LANGUAGE RecordWildCards #-}
5+module Hakyll.Core.Provider.Metadata
6+ ( loadMetadata
7+ , parsePage
8+
9+ , MetadataException (..)
10+ ) where
11+
12+
13+--------------------------------------------------------------------------------
14+import Control.Arrow (second)
15+import Control.Exception (Exception, throwIO)
16+import Control.Monad (guard)
17+import qualified Data.ByteString as B
18+import qualified Data.ByteString.Char8 as BC
19+import Data.List.Extended (breakWhen)
20+import qualified Data.Map as M
21+import Data.Maybe (fromMaybe)
22+import Data.Monoid ((<>))
23+import qualified Data.Text as T
24+import qualified Data.Text.Encoding as T
25+import qualified Data.Yaml as Yaml
26+import Hakyll.Core.Identifier
27+import Hakyll.Core.Metadata
28+import Hakyll.Core.Provider.Internal
29+import System.IO as IO
30+import System.IO.Error (modifyIOError, ioeSetLocation)
31+
32+
33+--------------------------------------------------------------------------------
34+loadMetadata :: Provider -> Identifier -> IO (Metadata, Maybe String)
35+loadMetadata p identifier = do
36+ hasHeader <- probablyHasMetadataHeader fp
37+ (md, body) <- if hasHeader
38+ then second Just <$> loadMetadataHeader fp
39+ else return (mempty, Nothing)
40+
41+ emd <- case mi of
42+ Nothing -> return mempty
43+ Just mi' -> loadMetadataFile $ resourceFilePath p mi'
44+
45+ return (md <> emd, body)
46+ where
47+ normal = setVersion Nothing identifier
48+ fp = resourceFilePath p identifier
49+ mi = M.lookup normal (providerFiles p) >>= resourceInfoMetadata
50+
51+
52+--------------------------------------------------------------------------------
53+loadMetadataHeader :: FilePath -> IO (Metadata, String)
54+loadMetadataHeader fp = do
55+ fileContent <- modifyIOError (`ioeSetLocation` "loadMetadataHeader") $ readFile fp
56+ case parsePage fileContent of
57+ Right x -> return x
58+ Left err -> throwIO $ MetadataException fp err
59+
60+
61+--------------------------------------------------------------------------------
62+loadMetadataFile :: FilePath -> IO Metadata
63+loadMetadataFile fp = do
64+ fileContent <- modifyIOError (`ioeSetLocation` "loadMetadataFile") $ B.readFile fp
65+ let errOrMeta = Yaml.decodeEither' fileContent
66+ either (fail . show) return errOrMeta
67+
68+
69+--------------------------------------------------------------------------------
70+-- | Check if a file "probably" has a metadata header. The main goal of this is
71+-- to exclude binary files (which are unlikely to start with "---").
72+probablyHasMetadataHeader :: FilePath -> IO Bool
73+probablyHasMetadataHeader fp = do
74+ handle <- IO.openFile fp IO.ReadMode
75+ bs <- BC.hGet handle 1024
76+ IO.hClose handle
77+ return $ isMetadataHeader bs
78+ where
79+ isMetadataHeader bs =
80+ let pre = BC.takeWhile (\x -> x /= '\n' && x /= '\r') bs
81+ in BC.length pre >= 3 && BC.all (== '-') pre
82+
83+
84+--------------------------------------------------------------------------------
85+-- | Parse the page metadata and body.
86+splitMetadata :: String -> (Maybe String, String)
87+splitMetadata str0 = fromMaybe (Nothing, str0) $ do
88+ guard $ leading >= 3
89+ let !str1 = drop leading str0
90+ guard $ all isNewline (take 1 str1)
91+ let !(!meta, !content0) = breakWhen isTrailing str1
92+ guard $ not $ null content0
93+ let !content1 = drop (leading + 1) content0
94+ !content2 = dropWhile isNewline $ dropWhile isInlineSpace content1
95+ -- Adding this newline fixes the line numbers reported by the YAML parser.
96+ -- It's a bit ugly but it works.
97+ return (Just ('\n' : meta), content2)
98+ where
99+ -- Parse the leading "---"
100+ !leading = length $ takeWhile (== '-') str0
101+
102+ -- Predicate to recognize the trailing "---" or "..."
103+ isTrailing [] = False
104+ isTrailing (x : xs) =
105+ isNewline x && length (takeWhile isDash xs) == leading
106+
107+ -- Characters
108+ isNewline c = c == '\n' || c == '\r'
109+ isDash c = c == '-' || c == '.'
110+ isInlineSpace c = c == '\t' || c == ' '
111+
112+
113+--------------------------------------------------------------------------------
114+parseMetadata :: String -> Either Yaml.ParseException Metadata
115+parseMetadata = Yaml.decodeEither' . T.encodeUtf8 . T.pack
116+
117+
118+--------------------------------------------------------------------------------
119+parsePage :: String -> Either Yaml.ParseException (Metadata, String)
120+parsePage fileContent = case mbMetaBlock of
121+ Nothing -> return (mempty, content)
122+ Just metaBlock -> case parseMetadata metaBlock of
123+ Left err -> Left err
124+ Right meta -> return (meta, content)
125+ where
126+ !(!mbMetaBlock, !content) = splitMetadata fileContent
127+
128+
129+--------------------------------------------------------------------------------
130+-- | Thrown in the IO monad if things go wrong. Provides a nice-ish error
131+-- message.
132+data MetadataException = MetadataException FilePath Yaml.ParseException
133+
134+
135+--------------------------------------------------------------------------------
136+instance Exception MetadataException
137+
138+
139+--------------------------------------------------------------------------------
140+instance Show MetadataException where
141+ show (MetadataException fp err) =
142+ fp ++ ": " ++ Yaml.prettyPrintParseException err ++ hint
143+
144+ where
145+ hint = case err of
146+ Yaml.InvalidYaml (Just (Yaml.YamlParseException {..}))
147+ | yamlProblem == problem -> "\n" ++
148+ "Hint: if the metadata value contains characters such\n" ++
149+ "as ':' or '-', try enclosing it in quotes."
150+ _ -> ""
151+
152+ problem = "mapping values are not allowed in this context"
src/Hakyll/Core/Provider/MetadataCache.hs → lib/Hakyll/Core/Provider/MetadataCache.hsrenamed+9−15
@@ -7,10 +7,7 @@ module Hakyll.Core.Provider.MetadataCache
77
88
99--------------------------------------------------------------------------------
10−import qualified Data.Map as M
11−
12−
13−--------------------------------------------------------------------------------
10+import Control.Monad (unless)
1411import Hakyll.Core.Identifier
1512import Hakyll.Core.Metadata
1613import Hakyll.Core.Provider.Internal
@@ -21,10 +18,11 @@ import qualified Hakyll.Core.Store as Store
2118--------------------------------------------------------------------------------
2219resourceMetadata :: Provider -> Identifier -> IO Metadata
2320resourceMetadata p r
24− | not (resourceExists p r) = return M.empty
21+ | not (resourceExists p r) = return mempty
2522 | otherwise = do
23+ -- TODO keep time in md cache
2624 load p r
27− Store.Found md <- Store.get (providerStore p)
25+ Store.Found (BinaryMetadata md) <- Store.get (providerStore p)
2826 [name, toFilePath r, "metadata"]
2927 return md
3028
@@ -48,15 +46,11 @@ resourceInvalidateMetadataCache p r = do
4846--------------------------------------------------------------------------------
4947load :: Provider -> Identifier -> IO ()
5048load p r = do
51− mmd <- Store.get store mdk :: IO (Store.Result Metadata)
52− case mmd of
53− -- Already loaded
54− Store.Found _ -> return ()
55− -- Not yet loaded
56− _ -> do
57− (metadata, body) <- loadMetadata p r
58− Store.set store mdk metadata
59− Store.set store bk body
49+ mmof <- Store.isMember store mdk
50+ unless mmof $ do
51+ (md, body) <- loadMetadata p r
52+ Store.set store mdk (BinaryMetadata md)
53+ Store.set store bk body
6054 where
6155 store = providerStore p
6256 mdk = [name, toFilePath r, "metadata"]
src/Hakyll/Core/Routes.hs → lib/Hakyll/Core/Routes.hsrenamed+73−21
@@ -25,9 +25,11 @@
2525-- not appear in your site directory.
2626--
2727-- * If an item matches multiple routes, the first rule will be chosen.
28+{-# LANGUAGE CPP #-}
2829{-# LANGUAGE Rank2Types #-}
2930module Hakyll.Core.Routes
30− ( Routes
31+ ( UsedMetadata
32+ , Routes
3133 , runRoutes
3234 , idRoute
3335 , setExtension
@@ -35,44 +37,82 @@ module Hakyll.Core.Routes
3537 , customRoute
3638 , constRoute
3739 , gsubRoute
40+ , metadataRoute
3841 , composeRoutes
3942 ) where
4043
4144
4245--------------------------------------------------------------------------------
43−import Data.Monoid (Monoid, mempty, mappend)
44−import Control.Monad (mplus)
45−import System.FilePath (replaceExtension)
46+#if MIN_VERSION_base(4,9,0)
47+import Data.Semigroup (Semigroup (..))
48+#endif
49+import System.FilePath (replaceExtension)
4650
4751
4852--------------------------------------------------------------------------------
49−import Hakyll.Core.Identifier
50−import Hakyll.Core.Identifier.Pattern
51−import Hakyll.Core.Util.String
53+import Hakyll.Core.Identifier
54+import Hakyll.Core.Identifier.Pattern
55+import Hakyll.Core.Metadata
56+import Hakyll.Core.Provider
57+import Hakyll.Core.Util.String
58+
59+
60+--------------------------------------------------------------------------------
61+-- | When you ran a route, it's useful to know whether or not this used
62+-- metadata. This allows us to do more granular dependency analysis.
63+type UsedMetadata = Bool
64+
65+
66+--------------------------------------------------------------------------------
67+data RoutesRead = RoutesRead
68+ { routesProvider :: Provider
69+ , routesUnderlying :: Identifier
70+ }
5271
5372
5473--------------------------------------------------------------------------------
5574-- | Type used for a route
56−newtype Routes = Routes {unRoutes :: Identifier -> Maybe FilePath}
75+newtype Routes = Routes
76+ { unRoutes :: RoutesRead -> Identifier -> IO (Maybe FilePath, UsedMetadata)
77+ }
5778
5879
5980--------------------------------------------------------------------------------
81+#if MIN_VERSION_base(4,9,0)
82+instance Semigroup Routes where
83+ (<>) (Routes f) (Routes g) = Routes $ \p id' -> do
84+ (mfp, um) <- f p id'
85+ case mfp of
86+ Nothing -> g p id'
87+ Just _ -> return (mfp, um)
88+
89+instance Monoid Routes where
90+ mempty = Routes $ \_ _ -> return (Nothing, False)
91+ mappend = (<>)
92+#else
6093instance Monoid Routes where
61− mempty = Routes $ const Nothing
62− mappend (Routes f) (Routes g) = Routes $ \id' -> f id' `mplus` g id'
94+ mempty = Routes $ \_ _ -> return (Nothing, False)
95+ mappend (Routes f) (Routes g) = Routes $ \p id' -> do
96+ (mfp, um) <- f p id'
97+ case mfp of
98+ Nothing -> g p id'
99+ Just _ -> return (mfp, um)
100+#endif
63101
64102
65103--------------------------------------------------------------------------------
66104-- | Apply a route to an identifier
67−runRoutes :: Routes -> Identifier -> Maybe FilePath
68−runRoutes = unRoutes
105+runRoutes :: Routes -> Provider -> Identifier
106+ -> IO (Maybe FilePath, UsedMetadata)
107+runRoutes routes provider identifier =
108+ unRoutes routes (RoutesRead provider identifier) identifier
69109
70110
71111--------------------------------------------------------------------------------
72112-- | A route that uses the identifier as filepath. For example, the target with
73113-- ID @foo\/bar@ will be written to the file @foo\/bar@.
74114idRoute :: Routes
75−idRoute = Routes $ Just . toFilePath
115+idRoute = customRoute toFilePath
76116
77117
78118--------------------------------------------------------------------------------
@@ -94,23 +134,23 @@ idRoute = Routes $ Just . toFilePath
94134--
95135-- > Just "posts/the-art-of-trolling.html"
96136setExtension :: String -> Routes
97−setExtension extension = Routes $
98− fmap (`replaceExtension` extension) . unRoutes idRoute
137+setExtension extension = customRoute $
138+ (`replaceExtension` extension) . toFilePath
99139
100140
101141--------------------------------------------------------------------------------
102142-- | Apply the route if the identifier matches the given pattern, fail
103143-- otherwise
104144matchRoute :: Pattern -> Routes -> Routes
105−matchRoute pattern (Routes route) = Routes $ \id' ->
106− if matches pattern id' then route id' else Nothing
145+matchRoute pattern (Routes route) = Routes $ \p id' ->
146+ if matches pattern id' then route p id' else return (Nothing, False)
107147
108148
109149--------------------------------------------------------------------------------
110150-- | Create a custom route. This should almost always be used with
111151-- 'matchRoute'
112152customRoute :: (Identifier -> FilePath) -> Routes
113−customRoute f = Routes $ Just . f
153+customRoute f = Routes $ const $ \id' -> return (Just (f id'), False)
114154
115155
116156--------------------------------------------------------------------------------
@@ -137,6 +177,14 @@ gsubRoute pattern replacement = customRoute $
137177 replaceAll pattern replacement . toFilePath
138178
139179
180+--------------------------------------------------------------------------------
181+-- | Get access to the metadata in order to determine the route
182+metadataRoute :: (Metadata -> Routes) -> Routes
183+metadataRoute f = Routes $ \r i -> do
184+ metadata <- resourceMetadata (routesProvider r) (routesUnderlying r)
185+ unRoutes (f metadata) r i
186+
187+
140188--------------------------------------------------------------------------------
141189-- | Compose routes so that @f \`composeRoutes\` g@ is more or less equivalent
142190-- with @g . f@.
@@ -154,6 +202,10 @@ gsubRoute pattern replacement = customRoute $
154202composeRoutes :: Routes -- ^ First route to apply
155203 -> Routes -- ^ Second route to apply
156204 -> Routes -- ^ Resulting route
157−composeRoutes (Routes f) (Routes g) = Routes $ \i -> do
158− p <- f i
159− g $ fromFilePath p
205+composeRoutes (Routes f) (Routes g) = Routes $ \p i -> do
206+ (mfp, um) <- f p i
207+ case mfp of
208+ Nothing -> return (Nothing, um)
209+ Just fp -> do
210+ (mfp', um') <- g p (fromFilePath fp)
211+ return (mfp', um || um')
src/Hakyll/Core/Rules.hs → lib/Hakyll/Core/Rules.hsrenamed+82−17
@@ -19,22 +19,25 @@
1919module Hakyll.Core.Rules
2020 ( Rules
2121 , match
22+ , matchMetadata
23+ , create
2224 , version
2325 , compile
2426 , route
2527
2628 -- * Advanced usage
29+ , preprocess
30+ , Dependency (..)
2731 , rulesExtraDependencies
2832 ) where
2933
3034
3135--------------------------------------------------------------------------------
32−import Control.Applicative ((<$>))
3336import Control.Monad.Reader (ask, local)
3437import Control.Monad.State (get, modify, put)
38+import Control.Monad.Trans (liftIO)
3539import Control.Monad.Writer (censor, tell)
3640import Data.Maybe (fromMaybe)
37−import Data.Monoid (mappend, mempty)
3841import qualified Data.Set as S
3942
4043
@@ -59,20 +62,26 @@ import Hakyll.Core.Writable
5962--------------------------------------------------------------------------------
6063-- | Add a route
6164tellRoute :: Routes -> Rules ()
62−tellRoute route' = Rules $ tell $ RuleSet route' mempty mempty
65+tellRoute route' = Rules $ tell $ RuleSet route' mempty mempty mempty
6366
6467
6568--------------------------------------------------------------------------------
6669-- | Add a number of compilers
6770tellCompilers :: [(Identifier, Compiler SomeItem)] -> Rules ()
68−tellCompilers compilers = Rules $ tell $ RuleSet mempty compilers mempty
71+tellCompilers compilers = Rules $ tell $ RuleSet mempty compilers mempty mempty
6972
7073
7174--------------------------------------------------------------------------------
7275-- | Add resources
7376tellResources :: [Identifier] -> Rules ()
7477tellResources resources' = Rules $ tell $
75− RuleSet mempty mempty $ S.fromList resources'
78+ RuleSet mempty mempty (S.fromList resources') mempty
79+
80+
81+--------------------------------------------------------------------------------
82+-- | Add a pattern
83+tellPattern :: Pattern -> Rules ()
84+tellPattern pattern = Rules $ tell $ RuleSet mempty mempty mempty pattern
7685
7786
7887--------------------------------------------------------------------------------
@@ -82,15 +91,21 @@ flush = Rules $ do
8291 case mcompiler of
8392 Nothing -> return ()
8493 Just compiler -> do
85− pattern <- rulesPattern <$> ask
94+ matches' <- rulesMatches <$> ask
8695 version' <- rulesVersion <$> ask
8796 route' <- fromMaybe mempty . rulesRoute <$> get
97+
98+ -- The version is possibly not set correctly at this point (yet)
99+ let ids = map (setVersion version') matches'
100+
101+ {-
88102 ids <- case fromLiteral pattern of
89103 Just id' -> return [setVersion version' id']
90104 Nothing -> do
91105 ids <- unRules $ getMatches pattern
92106 unRules $ tellResources ids
93107 return $ map (setVersion version') ids
108+ -}
94109
95110 -- Create a fast pattern for routing that matches exactly the
96111 -- compilers created in the block given to match
@@ -103,15 +118,36 @@ flush = Rules $ do
103118 put $ emptyRulesState
104119
105120
121+--------------------------------------------------------------------------------
122+matchInternal :: Pattern -> Rules [Identifier] -> Rules () -> Rules ()
123+matchInternal pattern getIDs rules = do
124+ tellPattern pattern
125+ flush
126+ ids <- getIDs
127+ tellResources ids
128+ Rules $ local (setMatches ids) $ unRules $ rules >> flush
129+ where
130+ setMatches ids env = env {rulesMatches = ids}
131+
106132--------------------------------------------------------------------------------
107133match :: Pattern -> Rules () -> Rules ()
108−match pattern rules = do
134+match pattern = matchInternal pattern $ getMatches pattern
135+
136+
137+--------------------------------------------------------------------------------
138+matchMetadata :: Pattern -> (Metadata -> Bool) -> Rules () -> Rules ()
139+matchMetadata pattern metadataPred = matchInternal pattern $
140+ map fst . filter (metadataPred . snd) <$> getAllMetadata pattern
141+
142+
143+--------------------------------------------------------------------------------
144+create :: [Identifier] -> Rules () -> Rules ()
145+create ids rules = do
109146 flush
110− Rules $ local addPattern $ unRules $ rules >> flush
147+ -- TODO Maybe check if the resources exist and call tellResources on that
148+ Rules $ local setMatches $ unRules $ rules >> flush
111149 where
112− addPattern env = env
113− { rulesPattern = rulesPattern env `mappend` pattern
114− }
150+ setMatches env = env {rulesMatches = ids}
115151
116152
117153--------------------------------------------------------------------------------
@@ -126,9 +162,7 @@ version v rules = do
126162--------------------------------------------------------------------------------
127163-- | Add a compilation rule to the rules.
128164--
129−-- This instructs all resources to be compiled using the given compiler. When
130−-- no resources match the current selection, nothing will happen. In this case,
131−-- you might want to have a look at 'create'.
165+-- This instructs all resources to be compiled using the given compiler.
132166compile :: (Binary a, Typeable a, Writable a) => Compiler (Item a) -> Rules ()
133167compile compiler = Rules $ modify $ \s ->
134168 s {rulesCompiler = Just (fmap SomeItem compiler)}
@@ -142,15 +176,46 @@ route :: Routes -> Rules ()
142176route route' = Rules $ modify $ \s -> s {rulesRoute = Just route'}
143177
144178
179+--------------------------------------------------------------------------------
180+-- | Execute an 'IO' action immediately while the rules are being evaluated.
181+-- This should be avoided if possible, but occasionally comes in useful.
182+preprocess :: IO a -> Rules a
183+preprocess = Rules . liftIO
184+
185+
145186--------------------------------------------------------------------------------
146187-- | Advanced usage: add extra dependencies to compilers. Basically this is
147188-- needed when you're doing unsafe tricky stuff in the rules monad, but you
148189-- still want correct builds.
190+--
191+-- A useful utility for this purpose is 'makePatternDependency'.
149192rulesExtraDependencies :: [Dependency] -> Rules a -> Rules a
150−rulesExtraDependencies deps = Rules . censor addDependencies . unRules
193+rulesExtraDependencies deps rules =
194+ -- Note that we add the dependencies seemingly twice here. However, this is
195+ -- done so that 'rulesExtraDependencies' works both if we have something
196+ -- like:
197+ --
198+ -- > match "*.css" $ rulesExtraDependencies [foo] $ ...
199+ --
200+ -- and something like:
201+ --
202+ -- > rulesExtraDependencies [foo] $ match "*.css" $ ...
203+ --
204+ -- (1) takes care of the latter and (2) of the former.
205+ Rules $ censor fixRuleSet $ do
206+ x <- unRules rules
207+ fixCompiler
208+ return x
151209 where
152− -- Adds the dependencies to the compilers in the ruleset
153− addDependencies ruleSet = ruleSet
210+ -- (1) Adds the dependencies to the compilers we are yet to create
211+ fixCompiler = modify $ \s -> case rulesCompiler s of
212+ Nothing -> s
213+ Just c -> s
214+ { rulesCompiler = Just $ compilerTellDependencies deps >> c
215+ }
216+
217+ -- (2) Adds the dependencies to the compilers that are already in the ruleset
218+ fixRuleSet ruleSet = ruleSet
154219 { rulesCompilers =
155220 [ (i, compilerTellDependencies deps >> c)
156221 | (i, c) <- rulesCompilers ruleSet
src/Hakyll/Core/Rules/Internal.hs → lib/Hakyll/Core/Rules/Internal.hsrenamed+30−17
@@ -1,4 +1,5 @@
11--------------------------------------------------------------------------------
2+{-# LANGUAGE CPP #-}
23{-# LANGUAGE GeneralizedNewtypeDeriving #-}
34{-# LANGUAGE Rank2Types #-}
45module Hakyll.Core.Rules.Internal
@@ -12,12 +13,13 @@ module Hakyll.Core.Rules.Internal
1213
1314
1415--------------------------------------------------------------------------------
15−import Control.Applicative (Applicative, (<$>))
1616import Control.Monad.Reader (ask)
1717import Control.Monad.RWS (RWST, runRWST)
1818import Control.Monad.Trans (liftIO)
1919import qualified Data.Map as M
20−import Data.Monoid (Monoid, mappend, mempty)
20+#if MIN_VERSION_base(4,9,0)
21+import Data.Semigroup (Semigroup (..))
22+#endif
2123import Data.Set (Set)
2224
2325
@@ -34,7 +36,7 @@ import Hakyll.Core.Routes
3436--------------------------------------------------------------------------------
3537data RulesRead = RulesRead
3638 { rulesProvider :: Provider
37− , rulesPattern :: Pattern
39+ , rulesMatches :: [Identifier]
3840 , rulesVersion :: Maybe String
3941 }
4042
@@ -47,14 +49,27 @@ data RuleSet = RuleSet
4749 rulesCompilers :: [(Identifier, Compiler SomeItem)]
4850 , -- | A set of the actually used files
4951 rulesResources :: Set Identifier
52+ , -- | A pattern we can use to check if a file *would* be used. This is
53+ -- needed for the preview server.
54+ rulesPattern :: Pattern
5055 }
5156
5257
5358--------------------------------------------------------------------------------
59+#if MIN_VERSION_base(4,9,0)
60+instance Semigroup RuleSet where
61+ (<>) (RuleSet r1 c1 s1 p1) (RuleSet r2 c2 s2 p2) =
62+ RuleSet (mappend r1 r2) (mappend c1 c2) (mappend s1 s2) (p1 .||. p2)
63+
64+instance Monoid RuleSet where
65+ mempty = RuleSet mempty mempty mempty mempty
66+ mappend = (<>)
67+#else
5468instance Monoid RuleSet where
55− mempty = RuleSet mempty mempty mempty
56− mappend (RuleSet r1 c1 s1) (RuleSet r2 c2 s2) =
57− RuleSet (mappend r1 r2) (mappend c1 c2) (mappend s1 s2)
69+ mempty = RuleSet mempty mempty mempty mempty
70+ mappend (RuleSet r1 c1 s1 p1) (RuleSet r2 c2 s2 p2) =
71+ RuleSet (mappend r1 r2) (mappend c1 c2) (mappend s1 s2) (p1 .||. p2)
72+#endif
5873
5974
6075--------------------------------------------------------------------------------
@@ -92,19 +107,17 @@ instance MonadMetadata Rules where
92107runRules :: Rules a -> Provider -> IO RuleSet
93108runRules rules provider = do
94109 (_, _, ruleSet) <- runRWST (unRules rules) env emptyRulesState
95− return $ nubCompilers ruleSet
110+
111+ -- Ensure compiler uniqueness
112+ let ruleSet' = ruleSet
113+ { rulesCompilers = M.toList $
114+ M.fromListWith (flip const) (rulesCompilers ruleSet)
115+ }
116+
117+ return ruleSet'
96118 where
97119 env = RulesRead
98120 { rulesProvider = provider
99− , rulesPattern = mempty
121+ , rulesMatches = []
100122 , rulesVersion = Nothing
101123 }
102−
103−
104−--------------------------------------------------------------------------------
105−-- | Remove duplicate compilers from the 'RuleSet'. When two compilers match an
106−-- item, we prefer the first one
107−nubCompilers :: RuleSet -> RuleSet
108−nubCompilers set = set {rulesCompilers = nubCompilers' (rulesCompilers set)}
109− where
110− nubCompilers' = M.toList . M.fromListWith (flip const)
src/Hakyll/Core/Runtime.hs → lib/Hakyll/Core/Runtime.hsrenamed+72−33
@@ -5,19 +5,18 @@ module Hakyll.Core.Runtime
55
66
77--------------------------------------------------------------------------------
8−import Control.Applicative ((<$>))
9−import Control.Monad (filterM)
10−import Control.Monad.Error (ErrorT, runErrorT, throwError)
8+import Control.Monad (unless)
9+import Control.Monad.Except (ExceptT, runExceptT, throwError)
1110import Control.Monad.Reader (ask)
1211import Control.Monad.RWS (RWST, runRWST)
1312import Control.Monad.State (get, modify)
1413import Control.Monad.Trans (liftIO)
14+import Data.List (intercalate)
1515import Data.Map (Map)
1616import qualified Data.Map as M
17−import Data.Monoid (mempty)
1817import Data.Set (Set)
1918import qualified Data.Set as S
20−import System.Exit (ExitCode (..), exitWith)
19+import System.Exit (ExitCode (..))
2120import System.FilePath ((</>))
2221
2322
@@ -27,6 +26,7 @@ import Hakyll.Core.Compiler.Require
2726import Hakyll.Core.Configuration
2827import Hakyll.Core.Dependencies
2928import Hakyll.Core.Identifier
29+import Hakyll.Core.Item
3030import Hakyll.Core.Item.SomeItem
3131import Hakyll.Core.Logger (Logger)
3232import qualified Hakyll.Core.Logger as Logger
@@ -40,10 +40,9 @@ import Hakyll.Core.Writable
4040
4141
4242--------------------------------------------------------------------------------
43−run :: Configuration -> Rules a -> IO RuleSet
44−run config rules = do
43+run :: Configuration -> Logger -> Rules a -> IO (ExitCode, RuleSet)
44+run config logger rules = do
4545 -- Initialization
46− logger <- Logger.new (verbosity config) putStrLn
4746 Logger.header logger "Initialising..."
4847 Logger.message logger "Creating store..."
4948 store <- Store.new (inMemoryCache config) $ storeDirectory config
@@ -69,23 +68,28 @@ run config rules = do
6968 , runtimeUniverse = M.fromList compilers
7069 }
7170 state = RuntimeState
72− { runtimeDone = S.empty
73− , runtimeTodo = M.empty
74− , runtimeFacts = oldFacts
71+ { runtimeDone = S.empty
72+ , runtimeSnapshots = S.empty
73+ , runtimeTodo = M.empty
74+ , runtimeFacts = oldFacts
7575 }
7676
7777 -- Run the program and fetch the resulting state
78− result <- runErrorT $ runRWST build read' state
78+ result <- runExceptT $ runRWST build read' state
7979 case result of
8080 Left e -> do
8181 Logger.error logger e
8282 Logger.flush logger
83− exitWith $ ExitFailure 1
83+ return (ExitFailure 1, ruleSet)
8484
8585 Right (_, s, _) -> do
8686 Store.set store factsKey $ runtimeFacts s
87+
88+ Logger.debug logger "Removing tmp directory..."
89+ removeDirectory $ tmpDirectory config
90+
8791 Logger.flush logger
88− return ruleSet
92+ return (ExitSuccess, ruleSet)
8993 where
9094 factsKey = ["Hakyll.Core.Runtime.run", "facts"]
9195
@@ -103,14 +107,15 @@ data RuntimeRead = RuntimeRead
103107
104108--------------------------------------------------------------------------------
105109data RuntimeState = RuntimeState
106− { runtimeDone :: Set Identifier
107− , runtimeTodo :: Map Identifier (Compiler SomeItem)
108− , runtimeFacts :: DependencyFacts
110+ { runtimeDone :: Set Identifier
111+ , runtimeSnapshots :: Set (Identifier, Snapshot)
112+ , runtimeTodo :: Map Identifier (Compiler SomeItem)
113+ , runtimeFacts :: DependencyFacts
109114 }
110115
111116
112117--------------------------------------------------------------------------------
113−type Runtime a = RWST RuntimeRead () RuntimeState (ErrorT String IO) a
118+type Runtime a = RWST RuntimeRead () RuntimeState (ExceptT String IO) a
114119
115120
116121--------------------------------------------------------------------------------
@@ -134,8 +139,9 @@ scheduleOutOfDate = do
134139 todo <- runtimeTodo <$> get
135140
136141 let identifiers = M.keys universe
137− modified <- fmap S.fromList $ flip filterM identifiers $
138− liftIO . resourceModified provider
142+ modified = S.fromList $ flip filter identifiers $
143+ resourceModified provider
144+
139145 let (ood, facts', msgs) = outOfDate identifiers modified facts
140146 todo' = M.filterWithKey
141147 (\id' _ -> id' `S.member` ood) universe
@@ -166,7 +172,9 @@ pickAndChase = do
166172--------------------------------------------------------------------------------
167173chase :: [Identifier] -> Identifier -> Runtime ()
168174chase trail id'
169− | id' `elem` trail = return () -- Cycle detected!
175+ | id' `elem` trail = throwError $ "Hakyll.Core.Runtime.chase: " ++
176+ "Dependency cycle detected: " ++ intercalate " depends on "
177+ (map show $ dropWhile (/= id') (reverse trail) ++ [id'])
170178 | otherwise = do
171179 logger <- runtimeLogger <$> ask
172180 todo <- runtimeTodo <$> get
@@ -179,7 +187,8 @@ chase trail id'
179187
180188 let compiler = todo M.! id'
181189 read' = CompilerRead
182− { compilerUnderlying = id'
190+ { compilerConfig = config
191+ , compilerUnderlying = id'
183192 , compilerProvider = provider
184193 , compilerUniverse = M.keysSet universe
185194 , compilerRoutes = routes
@@ -190,24 +199,42 @@ chase trail id'
190199 result <- liftIO $ runCompiler compiler read'
191200 case result of
192201 -- Rethrow error
193− CompilerError e -> throwError e
202+ CompilerError e -> throwError $ case compilerErrorMessages e of
203+ [] -> "Compiler failed but no info given, try running with -v?"
204+ es -> intercalate "; " es
205+
206+ -- Signal that a snapshot was saved ->
207+ CompilerSnapshot snapshot c -> do
208+ -- Update info. The next 'chase' will pick us again at some
209+ -- point so we can continue then.
210+ modify $ \s -> s
211+ { runtimeSnapshots =
212+ S.insert (id', snapshot) (runtimeSnapshots s)
213+ , runtimeTodo = M.insert id' c (runtimeTodo s)
214+ }
194215
195216 -- Huge success
196217 CompilerDone (SomeItem item) cwrite -> do
197− -- TODO: Sanity check on itemIdentifier?
198− let facts = compilerDependencies cwrite
218+ -- Print some info
219+ let facts = compilerDependencies cwrite
199220 cacheHits
200221 | compilerCacheHits cwrite <= 0 = "updated"
201222 | otherwise = "cached "
202−
203− -- Print some info
204223 Logger.message logger $ cacheHits ++ " " ++ show id'
205224
225+ -- Sanity check
226+ unless (itemIdentifier item == id') $ throwError $
227+ "The compiler yielded an Item with Identifier " ++
228+ show (itemIdentifier item) ++ ", but we were expecting " ++
229+ "an Item with Identifier " ++ show id' ++ " " ++
230+ "(you probably want to call makeItem to solve this problem)"
231+
206232 -- Write if necessary
207− case runRoutes routes id' of
208− Nothing -> return ()
209− Just url -> do
210− let path = destinationDirectory config </> url
233+ (mroute, _) <- liftIO $ runRoutes routes provider id'
234+ case mroute of
235+ Nothing -> return ()
236+ Just route -> do
237+ let path = destinationDirectory config </> route
211238 liftIO $ makeDirectories path
212239 liftIO $ write path item
213240 Logger.debug logger $ "Routed to " ++ path
@@ -225,7 +252,16 @@ chase trail id'
225252 -- Try something else first
226253 CompilerRequire dep c -> do
227254 -- Update the compiler so we don't execute it twice
228− depDone <- (dep `S.member`) . runtimeDone <$> get
255+ let (depId, depSnapshot) = dep
256+ done <- runtimeDone <$> get
257+ snapshots <- runtimeSnapshots <$> get
258+
259+ -- Done if we either completed the entire item (runtimeDone) or
260+ -- if we previously saved the snapshot (runtimeSnapshots).
261+ let depDone =
262+ depId `S.member` done ||
263+ (depId, depSnapshot) `S.member` snapshots
264+
229265 modify $ \s -> s
230266 { runtimeTodo = M.insert id'
231267 (if depDone then c else compilerResult result)
@@ -234,4 +270,7 @@ chase trail id'
234270
235271 -- If the required item is already compiled, continue, or, start
236272 -- chasing that
237− if depDone then chase trail id' else chase (id' : trail) dep
273+ Logger.debug logger $ "Require " ++ show depId ++
274+ " (snapshot " ++ depSnapshot ++ "): " ++
275+ (if depDone then "OK" else "chasing")
276+ if depDone then chase trail id' else chase (id' : trail) depId
src/Hakyll/Core/Store.hs → lib/Hakyll/Core/Store.hsrenamed+77−32
@@ -5,28 +5,34 @@
55module Hakyll.Core.Store
66 ( Store
77 , Result (..)
8+ , toMaybe
89 , new
910 , set
1011 , get
12+ , isMember
1113 , delete
14+ , hash
1215 ) where
1316
1417
1518--------------------------------------------------------------------------------
16−import Control.Applicative ((<$>))
17−import Control.Exception (IOException, handle)
18−import qualified Crypto.Hash.MD5 as MD5
19−import Data.Binary (Binary, decodeFile, encodeFile)
20−import qualified Data.ByteString as B
21−import qualified Data.Cache.LRU.IO as Lru
22−import Data.List (intercalate)
23−import qualified Data.Text as T
24−import qualified Data.Text.Encoding as T
25−import Data.Typeable (TypeRep, Typeable, cast, typeOf)
26−import System.Directory (createDirectoryIfMissing)
27−import System.Directory (doesFileExist, removeFile)
28−import System.FilePath ((</>))
29−import Text.Printf (printf)
19+import qualified Crypto.Hash.MD5 as MD5
20+import Data.Binary (Binary, decode, encodeFile)
21+import qualified Data.ByteString as B
22+import qualified Data.ByteString.Lazy as BL
23+import qualified Data.Cache.LRU.IO as Lru
24+import Data.List (intercalate)
25+import Data.Maybe (isJust)
26+import qualified Data.Text as T
27+import qualified Data.Text.Encoding as T
28+import Data.Typeable (TypeRep, Typeable, cast, typeOf)
29+import Numeric (showHex)
30+import System.Directory (createDirectoryIfMissing)
31+import System.Directory (doesFileExist, removeFile)
32+import System.FilePath ((</>))
33+import System.IO (IOMode (..), hClose, openFile)
34+import System.IO.Error (catchIOError, ioeSetFileName,
35+ ioeSetLocation, modifyIOError)
3036
3137
3238--------------------------------------------------------------------------------
@@ -43,6 +49,11 @@ data Store = Store
4349 }
4450
4551
52+--------------------------------------------------------------------------------
53+instance Show Store where
54+ show _ = "<Store>"
55+
56+
4657--------------------------------------------------------------------------------
4758-- | Result of a store query
4859data Result a
@@ -52,6 +63,13 @@ data Result a
5263 deriving (Show, Eq)
5364
5465
66+--------------------------------------------------------------------------------
67+-- | Convert result to 'Maybe'
68+toMaybe :: Result a -> Maybe a
69+toMaybe (Found x) = Just x
70+toMaybe _ = Nothing
71+
72+
5573--------------------------------------------------------------------------------
5674-- | Initialize the store
5775new :: Bool -- ^ Use in-memory caching
@@ -67,6 +85,14 @@ new inMemory directory = do
6785 where
6886 csize = Just 500
6987
88+--------------------------------------------------------------------------------
89+withStore :: Store -> String -> (String -> FilePath -> IO a) -> [String] -> IO a
90+withStore store loc run identifier = modifyIOError handle $ run key path
91+ where
92+ key = hash identifier
93+ path = storeDirectory store </> key
94+ handle e = e `ioeSetFileName` (path ++ " for " ++ intercalate "/" identifier)
95+ `ioeSetLocation` ("Store." ++ loc)
7096
7197--------------------------------------------------------------------------------
7298-- | Auxiliary: add an item to the in-memory cache
@@ -89,6 +115,12 @@ cacheLookup (Store _ (Just lru)) key = do
89115 Nothing -> WrongType (typeOf (undefined :: a)) (typeOf x)
90116
91117
118+--------------------------------------------------------------------------------
119+cacheIsMember :: Store -> String -> IO Bool
120+cacheIsMember (Store _ Nothing) _ = return False
121+cacheIsMember (Store _ (Just lru)) key = isJust <$> Lru.lookup key lru
122+
123+
92124--------------------------------------------------------------------------------
93125-- | Auxiliary: delete an item from the in-memory cache
94126cacheDelete :: Store -> String -> IO ()
@@ -101,17 +133,16 @@ cacheDelete (Store _ (Just lru)) key = do
101133--------------------------------------------------------------------------------
102134-- | Store an item
103135set :: (Binary a, Typeable a) => Store -> [String] -> a -> IO ()
104−set store identifier value = do
105− encodeFile (storeDirectory store </> key) value
136+set store identifier value = withStore store "set" (\key path -> do
137+ encodeFile path value
106138 cacheInsert store key value
107− where
108− key = hash identifier
139+ ) identifier
109140
110141
111142--------------------------------------------------------------------------------
112143-- | Load an item
113144get :: (Binary a, Typeable a) => Store -> [String] -> IO (Result a)
114−get store identifier = do
145+get store = withStore store "get" $ \key path -> do
115146 -- First check the in-memory map
116147 ref <- cacheLookup store key
117148 case ref of
@@ -123,33 +154,47 @@ get store identifier = do
123154 then return NotFound
124155 -- Found in the filesystem
125156 else do
126− v <- decodeFile path
157+ v <- decodeClose path
127158 cacheInsert store key v
128159 return $ Found v
129160 -- Found in the in-memory map (or wrong type), just return
130161 s -> return s
131162 where
132− key = hash identifier
133− path = storeDirectory store </> key
163+ -- 'decodeFile' from Data.Binary which closes the file ASAP
164+ decodeClose path = do
165+ h <- openFile path ReadMode
166+ lbs <- BL.hGetContents h
167+ BL.length lbs `seq` hClose h
168+ return $ decode lbs
169+
170+
171+--------------------------------------------------------------------------------
172+-- | Strict function
173+isMember :: Store -> [String] -> IO Bool
174+isMember store = withStore store "isMember" $ \key path -> do
175+ inCache <- cacheIsMember store key
176+ if inCache then return True else doesFileExist path
134177
135178
136179--------------------------------------------------------------------------------
137180-- | Delete an item
138181delete :: Store -> [String] -> IO ()
139−delete store identifier = do
182+delete store = withStore store "delete" $ \key path -> do
140183 cacheDelete store key
141− deleteFile $ storeDirectory store </> key
142− where
143− key = hash identifier
184+ deleteFile path
144185
145186
146187--------------------------------------------------------------------------------
147−hash :: [String] -> String
148−hash = concatMap (printf "%02x") . B.unpack .
149− MD5.hash . T.encodeUtf8 . T.pack . intercalate "/"
188+-- | Delete a file unless it doesn't exist...
189+deleteFile :: FilePath -> IO ()
190+deleteFile = (`catchIOError` \_ -> return ()) . removeFile
150191
151192
152193--------------------------------------------------------------------------------
153−-- | Delete a file unless it doesn't exist...
154−deleteFile :: FilePath -> IO ()
155−deleteFile = handle (\(_ :: IOException) -> return ()) . removeFile
194+-- | Mostly meant for internal usage
195+hash :: [String] -> String
196+hash = toHex . B.unpack . MD5.hash . T.encodeUtf8 . T.pack . intercalate "/"
197+ where
198+ toHex [] = ""
199+ toHex (x : xs) | x < 16 = '0' : showHex x (toHex xs)
200+ | otherwise = showHex x (toHex xs)
lib/Hakyll/Core/UnixFilter.hsnew file+160
@@ -0,0 +1,160 @@
1+{-# LANGUAGE CPP #-}
2+
3+--------------------------------------------------------------------------------
4+-- | A Compiler that supports unix filters.
5+module Hakyll.Core.UnixFilter
6+ ( unixFilter
7+ , unixFilterLBS
8+ ) where
9+
10+
11+--------------------------------------------------------------------------------
12+import Control.Concurrent (forkIO)
13+import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
14+import Control.DeepSeq (deepseq)
15+import Control.Monad (forM_)
16+import Data.ByteString.Lazy (ByteString)
17+import qualified Data.ByteString.Lazy as LB
18+import Data.IORef (newIORef, readIORef, writeIORef)
19+import System.Exit (ExitCode (..))
20+import System.IO (Handle, hClose, hFlush, hGetContents,
21+ hPutStr, hSetEncoding, localeEncoding)
22+import System.Process
23+
24+--------------------------------------------------------------------------------
25+import Hakyll.Core.Compiler
26+
27+
28+--------------------------------------------------------------------------------
29+-- | Use a unix filter as compiler. For example, we could use the 'rev' program
30+-- as a compiler.
31+--
32+-- > rev :: Compiler (Item String)
33+-- > rev = getResourceString >>= withItemBody (unixFilter "rev" [])
34+--
35+-- A more realistic example: one can use this to call, for example, the sass
36+-- compiler on CSS files. More information about sass can be found here:
37+--
38+-- <http://sass-lang.com/>
39+--
40+-- The code is fairly straightforward, given that we use @.scss@ for sass:
41+--
42+-- > match "style.scss" $ do
43+-- > route $ setExtension "css"
44+-- > compile $ getResourceString >>=
45+-- > withItemBody (unixFilter "sass" ["-s", "--scss"]) >>=
46+-- > return . fmap compressCss
47+unixFilter :: String -- ^ Program name
48+ -> [String] -- ^ Program args
49+ -> String -- ^ Program input
50+ -> Compiler String -- ^ Program output
51+unixFilter = unixFilterWith writer reader
52+ where
53+ writer handle input = do
54+ hSetEncoding handle localeEncoding
55+ hPutStr handle input
56+ reader handle = do
57+ hSetEncoding handle localeEncoding
58+ out <- hGetContents handle
59+ deepseq out (return out)
60+
61+
62+--------------------------------------------------------------------------------
63+-- | Variant of 'unixFilter' that should be used for binary files
64+--
65+-- > match "music.wav" $ do
66+-- > route $ setExtension "ogg"
67+-- > compile $ getResourceLBS >>= withItemBody (unixFilterLBS "oggenc" ["-"])
68+unixFilterLBS :: String -- ^ Program name
69+ -> [String] -- ^ Program args
70+ -> ByteString -- ^ Program input
71+ -> Compiler ByteString -- ^ Program output
72+unixFilterLBS = unixFilterWith LB.hPutStr $ \handle -> do
73+ out <- LB.hGetContents handle
74+ LB.length out `seq` return out
75+
76+
77+--------------------------------------------------------------------------------
78+-- | Overloaded compiler
79+unixFilterWith :: Monoid o
80+ => (Handle -> i -> IO ()) -- ^ Writer
81+ -> (Handle -> IO o) -- ^ Reader
82+ -> String -- ^ Program name
83+ -> [String] -- ^ Program args
84+ -> i -- ^ Program input
85+ -> Compiler o -- ^ Program output
86+unixFilterWith writer reader programName args input = do
87+ debugCompiler ("Executing external program " ++ programName)
88+ (output, err, exitCode) <- unsafeCompiler $
89+ unixFilterIO writer reader programName args input
90+ forM_ (lines err) debugCompiler
91+ case exitCode of
92+ ExitSuccess -> return output
93+ ExitFailure e -> fail $
94+ "Hakyll.Core.UnixFilter.unixFilterWith: " ++
95+ unwords (programName : args) ++ " gave exit code " ++ show e ++
96+ ". (Error: " ++ err ++ ")"
97+
98+
99+--------------------------------------------------------------------------------
100+-- | Internally used function
101+unixFilterIO :: Monoid o
102+ => (Handle -> i -> IO ())
103+ -> (Handle -> IO o)
104+ -> String
105+ -> [String]
106+ -> i
107+ -> IO (o, String, ExitCode)
108+unixFilterIO writer reader programName args input = do
109+ -- The problem on Windows is that `proc` is unable to execute
110+ -- batch stubs (eg. anything created using 'gem install ...') even if its in
111+ -- `$PATH`. A solution to this issue is to execute the batch file explicitly
112+ -- using `cmd /c batchfile` but there is no rational way to know where said
113+ -- batchfile is on the system. Hence, we detect windows using the
114+ -- CPP and instead of using `proc` to create the process, use `shell`
115+ -- which will be able to execute everything `proc` can
116+ -- as well as batch files.
117+#ifdef mingw32_HOST_OS
118+ let pr = shell $ unwords (programName : args)
119+#else
120+ let pr = proc programName args
121+#endif
122+
123+ (Just inh, Just outh, Just errh, pid) <-
124+ createProcess pr
125+ { std_in = CreatePipe
126+ , std_out = CreatePipe
127+ , std_err = CreatePipe
128+ }
129+
130+ -- Create boxes
131+ lock <- newEmptyMVar
132+ outRef <- newIORef mempty
133+ errRef <- newIORef ""
134+
135+ -- Write the input to the child pipe
136+ _ <- forkIO $ writer inh input >> hFlush inh >> hClose inh
137+
138+ -- Read from stdout
139+ _ <- forkIO $ do
140+ out <- reader outh
141+ hClose outh
142+ writeIORef outRef out
143+ putMVar lock ()
144+
145+ -- Read from stderr
146+ _ <- forkIO $ do
147+ hSetEncoding errh localeEncoding
148+ err <- hGetContents errh
149+ _ <- deepseq err (return err)
150+ hClose errh
151+ writeIORef errRef err
152+ putMVar lock ()
153+
154+ -- Get exit code & return
155+ takeMVar lock
156+ takeMVar lock
157+ exitCode <- waitForProcess pid
158+ out <- readIORef outRef
159+ err <- readIORef errRef
160+ return (out, err, exitCode)
src/Hakyll/Core/Util/File.hs → lib/Hakyll/Core/Util/File.hsrenamed+21−9
@@ -3,14 +3,15 @@
33module Hakyll.Core.Util.File
44 ( makeDirectories
55 , getRecursiveContents
6+ , removeDirectory
67 ) where
78
89
910--------------------------------------------------------------------------------
10−import Control.Applicative ((<$>))
11−import Control.Monad (forM)
11+import Control.Monad (filterM, forM, when)
1212import System.Directory (createDirectoryIfMissing,
13− doesDirectoryExist, getDirectoryContents)
13+ doesDirectoryExist, getDirectoryContents,
14+ removeDirectoryRecursive)
1415import System.FilePath (takeDirectory, (</>))
1516
1617
@@ -23,17 +24,21 @@ makeDirectories = createDirectoryIfMissing True . takeDirectory
2324
2425--------------------------------------------------------------------------------
2526-- | Get all contents of a directory.
26−getRecursiveContents :: FilePath -- ^ Directory to search
27− -> IO [FilePath] -- ^ List of files found
28−getRecursiveContents top = go ""
27+getRecursiveContents :: (FilePath -> IO Bool) -- ^ Ignore this file/directory
28+ -> FilePath -- ^ Directory to search
29+ -> IO [FilePath] -- ^ List of files found
30+getRecursiveContents ignore top = go ""
2931 where
30− isProper = (`notElem` [".", ".."])
31− go dir = do
32+ isProper x
33+ | x `elem` [".", ".."] = return False
34+ | otherwise = not <$> ignore x
35+
36+ go dir = do
3237 dirExists <- doesDirectoryExist (top </> dir)
3338 if not dirExists
3439 then return []
3540 else do
36− names <- filter isProper <$> getDirectoryContents (top </> dir)
41+ names <- filterM isProper =<< getDirectoryContents (top </> dir)
3742 paths <- forM names $ \name -> do
3843 let rel = dir </> name
3944 isDirectory <- doesDirectoryExist (top </> rel)
@@ -42,3 +47,10 @@ getRecursiveContents top = go ""
4247 else return [rel]
4348
4449 return $ concat paths
50+
51+
52+--------------------------------------------------------------------------------
53+removeDirectory :: FilePath -> IO ()
54+removeDirectory fp = do
55+ e <- doesDirectoryExist fp
56+ when e $ removeDirectoryRecursive fp
lib/Hakyll/Core/Util/Parser.hsnew file+31
@@ -0,0 +1,31 @@
1+--------------------------------------------------------------------------------
2+-- | Parser utilities
3+module Hakyll.Core.Util.Parser
4+ ( metadataKey
5+ ) where
6+
7+
8+--------------------------------------------------------------------------------
9+import Control.Applicative ((<|>))
10+import Control.Monad (guard, mzero, void)
11+import qualified Text.Parsec as P
12+import Text.Parsec.String (Parser)
13+
14+
15+--------------------------------------------------------------------------------
16+metadataKey :: Parser String
17+metadataKey = do
18+ -- Ensure trailing '-' binds to '$' if present.
19+ let hyphon = P.try $ do
20+ void $ P.char '-'
21+ x <- P.lookAhead P.anyChar
22+ guard $ x /= '$'
23+ pure '-'
24+
25+ i <- (:) <$> P.letter <*> P.many (P.alphaNum <|> P.oneOf "_." <|> hyphon)
26+ if i `elem` reservedKeys then mzero else return i
27+
28+
29+--------------------------------------------------------------------------------
30+reservedKeys :: [String]
31+reservedKeys = ["if", "else", "endif", "for", "sep", "endfor", "partial"]
src/Hakyll/Core/Util/String.hs → lib/Hakyll/Core/Util/String.hsrenamed+36−6
@@ -1,27 +1,33 @@
1+{-# LANGUAGE FlexibleContexts #-}
2+--------------------------------------------------------------------------------
13-- | Miscellaneous string manipulation functions.
2−--
34module Hakyll.Core.Util.String
45 ( trim
56 , replaceAll
67 , splitAll
8+ , needlePrefix
79 ) where
810
11+
12+--------------------------------------------------------------------------------
913import Data.Char (isSpace)
14+import Data.List (isPrefixOf)
1015import Data.Maybe (listToMaybe)
11−
1216import Text.Regex.TDFA ((=~~))
1317
18+
19+--------------------------------------------------------------------------------
1420-- | Trim a string (drop spaces, tabs and newlines at both sides).
15−--
1621trim :: String -> String
1722trim = reverse . trim' . reverse . trim'
1823 where
1924 trim' = dropWhile isSpace
2025
26+
27+--------------------------------------------------------------------------------
2128-- | A simple (but inefficient) regex replace funcion
22−--
2329replaceAll :: String -- ^ Pattern
24− -> (String -> String) -- ^ Replacement (called on capture)
30+ -> (String -> String) -- ^ Replacement (called on match)
2531 -> String -- ^ Source string
2632 -> String -- ^ Result
2733replaceAll pattern f source = replaceAll' source
@@ -33,9 +39,10 @@ replaceAll pattern f source = replaceAll' source
3339 (capture, after) = splitAt l tmp
3440 in before ++ f capture ++ replaceAll' after
3541
42+
43+--------------------------------------------------------------------------------
3644-- | A simple regex split function. The resulting list will contain no empty
3745-- strings.
38−--
3946splitAll :: String -- ^ Pattern
4047 -> String -- ^ String to split
4148 -> [String] -- ^ Result
@@ -46,3 +53,26 @@ splitAll pattern = filter (not . null) . splitAll'
4653 Just (o, l) ->
4754 let (before, tmp) = splitAt o src
4855 in before : splitAll' (drop l tmp)
56+
57+
58+
59+--------------------------------------------------------------------------------
60+-- | Find the first instance of needle (must be non-empty) in haystack. We
61+-- return the prefix of haystack before needle is matched.
62+--
63+-- Examples:
64+--
65+-- > needlePrefix "cd" "abcde" = "ab"
66+--
67+-- > needlePrefix "ab" "abc" = ""
68+--
69+-- > needlePrefix "ab" "xxab" = "xx"
70+--
71+-- > needlePrefix "a" "xx" = "xx"
72+needlePrefix :: String -> String -> Maybe String
73+needlePrefix needle haystack = go [] haystack
74+ where
75+ go _ [] = Nothing
76+ go acc xss@(x:xs)
77+ | needle `isPrefixOf` xss = Just $ reverse acc
78+ | otherwise = go (x : acc) xs
src/Hakyll/Core/Writable.hs → lib/Hakyll/Core/Writable.hsrenamed

The content is the same.

lib/Hakyll/Main.hsnew file+180
@@ -0,0 +1,180 @@
1+--------------------------------------------------------------------------------
2+-- | Module providing the main hakyll function and command-line argument parsing
3+{-# LANGUAGE CPP #-}
4+
5+module Hakyll.Main
6+ ( hakyll
7+ , hakyllWith
8+ , hakyllWithArgs
9+ , hakyllWithExitCode
10+ , hakyllWithExitCodeAndArgs
11+ , Options(..)
12+ , Command(..)
13+ ) where
14+
15+
16+--------------------------------------------------------------------------------
17+import System.Environment (getProgName)
18+import System.Exit (ExitCode (ExitSuccess), exitWith)
19+import System.IO.Unsafe (unsafePerformIO)
20+
21+
22+--------------------------------------------------------------------------------
23+import Data.Monoid ((<>))
24+import qualified Options.Applicative as OA
25+
26+
27+--------------------------------------------------------------------------------
28+import qualified Hakyll.Check as Check
29+import qualified Hakyll.Commands as Commands
30+import qualified Hakyll.Core.Configuration as Config
31+import qualified Hakyll.Core.Logger as Logger
32+import Hakyll.Core.Rules
33+
34+
35+--------------------------------------------------------------------------------
36+-- | This usually is the function with which the user runs the hakyll compiler
37+hakyll :: Rules a -> IO ()
38+hakyll = hakyllWith Config.defaultConfiguration
39+
40+--------------------------------------------------------------------------------
41+-- | A variant of 'hakyll' which allows the user to specify a custom
42+-- configuration
43+hakyllWith :: Config.Configuration -> Rules a -> IO ()
44+hakyllWith conf rules = hakyllWithExitCode conf rules >>= exitWith
45+
46+--------------------------------------------------------------------------------
47+-- | A variant of 'hakyll' which returns an 'ExitCode'
48+hakyllWithExitCode :: Config.Configuration -> Rules a -> IO ExitCode
49+hakyllWithExitCode conf rules = do
50+ args <- defaultParser conf
51+ hakyllWithExitCodeAndArgs conf args rules
52+
53+--------------------------------------------------------------------------------
54+-- | A variant of 'hakyll' which expects a 'Configuration' and command-line
55+-- 'Options'. This gives freedom to implement your own parsing.
56+hakyllWithArgs :: Config.Configuration -> Options -> Rules a -> IO ()
57+hakyllWithArgs conf args rules =
58+ hakyllWithExitCodeAndArgs conf args rules >>= exitWith
59+
60+--------------------------------------------------------------------------------
61+hakyllWithExitCodeAndArgs :: Config.Configuration ->
62+ Options -> Rules a -> IO ExitCode
63+hakyllWithExitCodeAndArgs conf args rules = do
64+ let args' = optCommand args
65+ verbosity' = if verbosity args then Logger.Debug else Logger.Message
66+ check =
67+ if internal_links args' then Check.InternalLinks else Check.All
68+
69+ logger <- Logger.new verbosity'
70+ invokeCommands args' conf check logger rules
71+
72+--------------------------------------------------------------------------------
73+defaultParser :: Config.Configuration -> IO Options
74+defaultParser conf =
75+ OA.customExecParser (OA.prefs OA.showHelpOnError)
76+ (OA.info (OA.helper <*> optionParser conf)
77+ (OA.fullDesc <> OA.progDesc
78+ (progName ++ " - Static site compiler created with Hakyll")))
79+
80+
81+--------------------------------------------------------------------------------
82+invokeCommands :: Command -> Config.Configuration ->
83+ Check.Check -> Logger.Logger -> Rules a -> IO ExitCode
84+invokeCommands args conf check logger rules =
85+ case args of
86+ Build -> Commands.build conf logger rules
87+ Check _ -> Commands.check conf logger check
88+ Clean -> Commands.clean conf logger >> ok
89+ Deploy -> Commands.deploy conf
90+ Preview p -> Commands.preview conf logger rules p >> ok
91+ Rebuild -> Commands.rebuild conf logger rules
92+ Server _ _ -> Commands.server conf logger (host args) (port args) >> ok
93+ Watch _ p s -> Commands.watch conf logger (host args) p (not s) rules >> ok
94+ where
95+ ok = return ExitSuccess
96+
97+
98+--------------------------------------------------------------------------------
99+
100+-- | The parsed command-line options.
101+data Options = Options {verbosity :: Bool, optCommand :: Command}
102+ deriving (Show)
103+
104+-- | The command to run.
105+data Command
106+ = Build
107+ -- ^ Generate the site.
108+ | Check {internal_links :: Bool}
109+ -- ^ Validate the site output.
110+ | Clean
111+ -- ^ Clean up and remove cache.
112+ | Deploy
113+ -- ^ Upload/deploy your site.
114+ | Preview {port :: Int}
115+ -- ^ [DEPRECATED] Please use the watch command.
116+ | Rebuild
117+ -- ^ Clean and build again.
118+ | Server {host :: String, port :: Int}
119+ -- ^ Start a preview server.
120+ | Watch {host :: String, port :: Int, no_server :: Bool }
121+ -- ^ Autocompile on changes and start a preview server.
122+ deriving (Show)
123+
124+{-# DEPRECATED Preview "Use Watch instead." #-}
125+
126+optionParser :: Config.Configuration -> OA.Parser Options
127+optionParser conf = Options <$> verboseParser <*> commandParser conf
128+ where
129+ verboseParser = OA.switch (OA.long "verbose" <> OA.short 'v' <> OA.help "Run in verbose mode")
130+
131+
132+commandParser :: Config.Configuration -> OA.Parser Command
133+commandParser conf = OA.subparser $ foldr ((<>) . produceCommand) mempty commands
134+ where
135+ portParser = OA.option OA.auto (OA.long "port" <> OA.help "Port to listen on" <> OA.value (Config.previewPort conf))
136+ hostParser = OA.strOption (OA.long "host" <> OA.help "Host to bind on" <> OA.value (Config.previewHost conf))
137+
138+ produceCommand (c,a,b) = OA.command c (OA.info (OA.helper <*> a) (b))
139+
140+ commands =
141+ [ ( "build"
142+ , pure Build
143+ , OA.fullDesc <> OA.progDesc "Generate the site"
144+ )
145+ , ( "check"
146+ , pure Check <*> OA.switch (OA.long "internal-links" <> OA.help "Check internal links only")
147+ , OA.fullDesc <> OA.progDesc "Validate the site output"
148+ )
149+ , ( "clean"
150+ , pure Clean
151+ , OA.fullDesc <> OA.progDesc "Clean up and remove cache"
152+ )
153+ , ( "deploy"
154+ , pure Deploy
155+ , OA.fullDesc <> OA.progDesc "Upload/deploy your site"
156+ )
157+ , ( "preview"
158+ , pure Preview <*> portParser
159+ , OA.fullDesc <> OA.progDesc "[DEPRECATED] Please use the watch command"
160+ )
161+ , ( "rebuild"
162+ , pure Rebuild
163+ , OA.fullDesc <> OA.progDesc "Clean and build again"
164+ )
165+ , ( "server"
166+ , pure Server <*> hostParser <*> portParser
167+ , OA.fullDesc <> OA.progDesc "Start a preview server"
168+ )
169+ , ( "watch"
170+ , pure Watch <*> hostParser <*> portParser <*> OA.switch (OA.long "no-server" <> OA.help "Disable the built-in web server")
171+ , OA.fullDesc <> OA.progDesc "Autocompile on changes and start a preview server. You can watch and recompile without running a server with --no-server."
172+ )
173+ ]
174+
175+
176+--------------------------------------------------------------------------------
177+-- | This is necessary because not everyone calls their program the same...
178+progName :: String
179+progName = unsafePerformIO getProgName
180+{-# NOINLINE progName #-}
lib/Hakyll/Preview/Poll.hsnew file+109
@@ -0,0 +1,109 @@
1+--------------------------------------------------------------------------------
2+{-# LANGUAGE CPP #-}
3+module Hakyll.Preview.Poll
4+ ( watchUpdates
5+ ) where
6+
7+
8+--------------------------------------------------------------------------------
9+import Control.Concurrent (forkIO)
10+import Control.Concurrent.MVar (newEmptyMVar, takeMVar,
11+ tryPutMVar)
12+import Control.Exception (AsyncException, fromException,
13+ handle, throw)
14+import Control.Monad (forever, void, when)
15+import System.Directory (canonicalizePath)
16+import System.FilePath (pathSeparators)
17+import qualified System.FSNotify as FSNotify
18+
19+#ifdef mingw32_HOST_OS
20+import Control.Concurrent (threadDelay)
21+import Control.Exception (IOException, throw, try)
22+import System.Directory (doesFileExist)
23+import System.Exit (exitFailure)
24+import System.FilePath ((</>))
25+import System.IO (Handle, IOMode (ReadMode),
26+ hClose, openFile)
27+import System.IO.Error (isPermissionError)
28+#endif
29+
30+
31+--------------------------------------------------------------------------------
32+import Hakyll.Core.Configuration
33+import Hakyll.Core.Identifier
34+import Hakyll.Core.Identifier.Pattern
35+
36+
37+--------------------------------------------------------------------------------
38+-- | A thread that watches for updates in a 'providerDirectory' and recompiles
39+-- a site as soon as any changes occur
40+watchUpdates :: Configuration -> IO Pattern -> IO ()
41+watchUpdates conf update = do
42+ let providerDir = providerDirectory conf
43+ shouldBuild <- newEmptyMVar
44+ pattern <- update
45+ fullProviderDir <- canonicalizePath $ providerDirectory conf
46+ manager <- FSNotify.startManager
47+
48+ let allowed event = do
49+ -- Absolute path of the changed file. This must be inside provider
50+ -- dir, since that's the only dir we're watching.
51+ let path = FSNotify.eventPath event
52+ relative = dropWhile (`elem` pathSeparators) $
53+ drop (length fullProviderDir) path
54+ identifier = fromFilePath relative
55+
56+ shouldIgnore <- shouldIgnoreFile conf path
57+ return $ not shouldIgnore && matches pattern identifier
58+
59+ -- This thread continually watches the `shouldBuild` MVar and builds
60+ -- whenever a value is present.
61+ _ <- forkIO $ forever $ do
62+ event <- takeMVar shouldBuild
63+ handle
64+ (\e -> case fromException e of
65+ Nothing -> putStrLn (show e)
66+ Just async -> throw (async :: AsyncException))
67+ (update' event providerDir)
68+
69+ -- Send an event whenever something occurs so that the thread described
70+ -- above will do a build.
71+ void $ FSNotify.watchTree manager providerDir (not . isRemove) $ \event -> do
72+ allowed' <- allowed event
73+ when allowed' $ void $ tryPutMVar shouldBuild event
74+ where
75+#ifndef mingw32_HOST_OS
76+ update' _ _ = void update
77+#else
78+ update' event provider = do
79+ let path = provider </> FSNotify.eventPath event
80+ -- on windows, a 'Modified' event is also sent on file deletion
81+ fileExists <- doesFileExist path
82+
83+ when fileExists . void $ waitOpen path ReadMode (\_ -> update) 10
84+
85+ -- continuously attempts to open the file in between sleep intervals
86+ -- handler is run only once it is able to open the file
87+ waitOpen :: FilePath -> IOMode -> (Handle -> IO r) -> Integer -> IO r
88+ waitOpen _ _ _ 0 = do
89+ putStrLn "[ERROR] Failed to retrieve modified file for regeneration"
90+ exitFailure
91+ waitOpen path mode handler retries = do
92+ res <- try $ openFile path mode :: IO (Either IOException Handle)
93+ case res of
94+ Left ex -> if isPermissionError ex
95+ then do
96+ threadDelay 100000
97+ waitOpen path mode handler (retries - 1)
98+ else throw ex
99+ Right h -> do
100+ handled <- handler h
101+ hClose h
102+ return handled
103+#endif
104+
105+
106+--------------------------------------------------------------------------------
107+isRemove :: FSNotify.Event -> Bool
108+isRemove (FSNotify.Removed {}) = True
109+isRemove _ = False
lib/Hakyll/Preview/Server.hsnew file+35
@@ -0,0 +1,35 @@
1+--------------------------------------------------------------------------------
2+-- | Implements a basic static file server for previewing options
3+{-# LANGUAGE OverloadedStrings #-}
4+module Hakyll.Preview.Server
5+ ( staticServer
6+ ) where
7+
8+
9+--------------------------------------------------------------------------------
10+import Data.String
11+import qualified Network.Wai.Handler.Warp as Warp
12+import qualified Network.Wai.Application.Static as Static
13+import qualified Network.Wai as Wai
14+import Network.HTTP.Types.Status (Status)
15+
16+--------------------------------------------------------------------------------
17+import Hakyll.Core.Logger (Logger)
18+import qualified Hakyll.Core.Logger as Logger
19+
20+staticServer :: Logger -- ^ Logger
21+ -> FilePath -- ^ Directory to serve
22+ -> String -- ^ Host to bind on
23+ -> Int -- ^ Port to listen on
24+ -> IO () -- ^ Blocks forever
25+staticServer logger directory host port = do
26+ Logger.header logger $ "Listening on http://" ++ host ++ ":" ++ show port
27+ Warp.runSettings warpSettings $
28+ Static.staticApp (Static.defaultFileServerSettings directory)
29+ where
30+ warpSettings = Warp.setLogger noLog
31+ $ Warp.setHost (fromString host)
32+ $ Warp.setPort port Warp.defaultSettings
33+
34+noLog :: Wai.Request -> Status -> Maybe Integer -> IO ()
35+noLog _ _ _ = return ()
lib/Hakyll/Web/CompressCss.hsnew file+101
@@ -0,0 +1,101 @@
1+--------------------------------------------------------------------------------
2+-- | Module used for CSS compression. The compression is currently in a simple
3+-- state, but would typically reduce the number of bytes by about 25%.
4+module Hakyll.Web.CompressCss
5+ ( compressCssCompiler
6+ , compressCss
7+ ) where
8+
9+
10+--------------------------------------------------------------------------------
11+import Data.Char (isSpace)
12+import Data.List (dropWhileEnd, isPrefixOf)
13+
14+
15+--------------------------------------------------------------------------------
16+import Hakyll.Core.Compiler
17+import Hakyll.Core.Item
18+import Hakyll.Core.Util.String
19+
20+
21+--------------------------------------------------------------------------------
22+-- | Compiler form of 'compressCss'
23+compressCssCompiler :: Compiler (Item String)
24+compressCssCompiler = fmap compressCss <$> getResourceString
25+
26+
27+--------------------------------------------------------------------------------
28+-- | Compress CSS to speed up your site.
29+compressCss :: String -> String
30+compressCss = withoutStrings (handleCalcExpressions compressSeparators . compressWhitespace)
31+ . dropWhileEnd isSpace
32+ . dropWhile isSpace
33+ . stripComments
34+
35+
36+--------------------------------------------------------------------------------
37+-- | Compresses certain forms of separators.
38+compressSeparators :: String -> String
39+compressSeparators =
40+ replaceAll "; *}" (const "}") .
41+ replaceAll ";+" (const ";") .
42+ replaceAll " *[{};,>+~!] *" (take 1 . dropWhile isSpace) .
43+ replaceAll ": *" (take 1) -- not destroying pseudo selectors (#323)
44+
45+-- | Uses `compressCalcExpression` on all parenthesised calc expressions
46+-- and applies `transform` to all parts outside of them
47+handleCalcExpressions :: (String -> String) -> String -> String
48+handleCalcExpressions transform = top transform
49+ where
50+ top f "" = f ""
51+ top f str | "calc(" `isPrefixOf` str = f "calc" ++ nested 0 compressCalcExpression (drop 4 str)
52+ top f (x:xs) = top (f . (x:)) xs
53+
54+ -- when called with depth=0, the first character must be a '('
55+ nested :: Int -> (String -> String) -> String -> String
56+ nested _ f "" = f "" -- shouldn't happen, mismatched nesting
57+ nested depth f str | "calc(" `isPrefixOf` str = nested depth f (drop 4 str)
58+ nested 1 f (')':xs) = f ")" ++ top transform xs
59+ nested depth f (x:xs) = nested (case x of
60+ '(' -> depth + 1
61+ ')' -> depth - 1 -- assert: depth > 1
62+ _ -> depth
63+ ) (f . (x:)) xs
64+
65+-- | does not remove whitespace around + and -, which is important in calc() expressions
66+compressCalcExpression :: String -> String
67+compressCalcExpression =
68+ replaceAll " *[*/] *| *\\)|\\( *" (take 1 . dropWhile isSpace)
69+
70+--------------------------------------------------------------------------------
71+-- | Compresses all whitespace.
72+compressWhitespace :: String -> String
73+compressWhitespace = replaceAll "[ \t\n\r]+" (const " ")
74+
75+--------------------------------------------------------------------------------
76+-- | Function that strips CSS comments away (outside of strings).
77+stripComments :: String -> String
78+stripComments "" = ""
79+stripComments ('/':'*':str) = stripComments $ eatComment str
80+stripComments (x:xs) | x `elem` "\"'" = retainString x xs stripComments
81+ | otherwise = x : stripComments xs
82+
83+eatComment :: String -> String
84+eatComment "" = ""
85+eatComment ('*':'/':str) = str
86+eatComment (_:str) = eatComment str
87+
88+
89+--------------------------------------------------------------------------------
90+-- | Helper functions to handle string tokens correctly.
91+
92+-- TODO: handle backslash escapes
93+withoutStrings :: (String -> String) -> String -> String
94+withoutStrings f str = case span (`notElem` "\"'") str of
95+ (text, "") -> f text
96+ (text, d:rest) -> f text ++ retainString d rest (withoutStrings f)
97+
98+retainString :: Char -> String -> (String -> String) -> String
99+retainString delim str cont = case span (/= delim) str of
100+ (val, "") -> delim : val
101+ (val, _:rest) -> delim : val ++ delim : cont rest
src/Hakyll/Web/Feed.hs → lib/Hakyll/Web/Feed.hsrenamed+71−27
@@ -1,3 +1,6 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE TemplateHaskell #-}
3+
14--------------------------------------------------------------------------------
25-- | A Module that allows easy rendering of RSS feeds.
36--
@@ -20,26 +23,40 @@ module Hakyll.Web.Feed
2023 ( FeedConfiguration (..)
2124 , renderRss
2225 , renderAtom
26+ , renderRssWithTemplates
27+ , renderAtomWithTemplates
2328 ) where
2429
2530
26−--------------------------------------------------------------------------------
27−import Control.Monad ((<=<))
28−import Data.Monoid (mconcat)
29−
30−
3131--------------------------------------------------------------------------------
3232import Hakyll.Core.Compiler
33−import Hakyll.Core.Compiler.Internal
3433import Hakyll.Core.Item
34+import Hakyll.Core.Util.String (replaceAll)
3535import Hakyll.Web.Template
3636import Hakyll.Web.Template.Context
3737import Hakyll.Web.Template.List
38−import Hakyll.Web.Template.Read
3938
4039
4140--------------------------------------------------------------------------------
42−import Paths_hakyll
41+import Data.FileEmbed (makeRelativeToProject)
42+
43+
44+--------------------------------------------------------------------------------
45+rssTemplate :: Template
46+rssTemplate =
47+ $(makeRelativeToProject "data/templates/rss.xml" >>= embedTemplate)
48+
49+rssItemTemplate :: Template
50+rssItemTemplate =
51+ $(makeRelativeToProject "data/templates/rss-item.xml" >>= embedTemplate)
52+
53+atomTemplate :: Template
54+atomTemplate =
55+ $(makeRelativeToProject "data/templates/atom.xml" >>= embedTemplate)
56+
57+atomItemTemplate :: Template
58+atomItemTemplate =
59+ $(makeRelativeToProject "data/templates/atom-item.xml" >>= embedTemplate)
4360
4461
4562--------------------------------------------------------------------------------
@@ -60,25 +77,27 @@ data FeedConfiguration = FeedConfiguration
6077
6178--------------------------------------------------------------------------------
6279-- | Abstract function to render any feed.
63−renderFeed :: FilePath -- ^ Feed template
64− -> FilePath -- ^ Item template
80+renderFeed :: Template -- ^ Default feed template
81+ -> Template -- ^ Default item template
6582 -> FeedConfiguration -- ^ Feed configuration
6683 -> Context String -- ^ Context for the items
6784 -> [Item String] -- ^ Input items
6885 -> Compiler (Item String) -- ^ Resulting item
69−renderFeed feedPath itemPath config itemContext items = do
70− feedTpl <- compilerUnsafeIO $ loadTemplate feedPath
71− itemTpl <- compilerUnsafeIO $ loadTemplate itemPath
72−
73− body <- makeItem =<< applyTemplateList itemTpl itemContext' items
86+renderFeed feedTpl itemTpl config itemContext items = do
87+ protectedItems <- mapM (applyFilter protectCDATA) items
88+ body <- makeItem =<< applyTemplateList itemTpl itemContext' protectedItems
7489 applyTemplate feedTpl feedContext body
7590 where
76− -- Auxiliary: load a template from a datafile
77− loadTemplate = fmap readTemplate . readFile <=< getDataFileName
91+ applyFilter :: (Monad m,Functor f) => (String -> String) -> f String -> m (f String)
92+ applyFilter tr str = return $ fmap tr str
93+ protectCDATA :: String -> String
94+ protectCDATA = replaceAll "]]>" (const "]]&gt;")
7895
7996 itemContext' = mconcat
80− [ constField "root" (feedRoot config)
81− , itemContext
97+ [ itemContext
98+ , constField "root" (feedRoot config)
99+ , constField "authorName" (feedAuthorName config)
100+ , constField "authorEmail" (feedAuthorEmail config)
82101 ]
83102
84103 feedContext = mconcat
@@ -97,7 +116,36 @@ renderFeed feedPath itemPath config itemContext items = do
97116 -- recent.
98117 updatedField = field "updated" $ \_ -> case items of
99118 [] -> return "Unknown"
100− (x : _) -> unContext itemContext' "updated" x
119+ (x : _) -> unContext itemContext' "updated" [] x >>= \cf -> case cf of
120+ StringField s -> return s
121+ _ -> fail "Hakyll.Web.Feed.renderFeed: Internal error"
122+
123+--------------------------------------------------------------------------------
124+-- | Render an RSS feed using given templates with a number of items.
125+renderRssWithTemplates ::
126+ Template -- ^ Feed template
127+ -> Template -- ^ Item template
128+ -> FeedConfiguration -- ^ Feed configuration
129+ -> Context String -- ^ Item context
130+ -> [Item String] -- ^ Feed items
131+ -> Compiler (Item String) -- ^ Resulting feed
132+renderRssWithTemplates feedTemplate itemTemplate config context = renderFeed
133+ feedTemplate itemTemplate config
134+ (makeItemContext "%a, %d %b %Y %H:%M:%S UT" context)
135+
136+
137+--------------------------------------------------------------------------------
138+-- | Render an Atom feed using given templates with a number of items.
139+renderAtomWithTemplates ::
140+ Template -- ^ Feed template
141+ -> Template -- ^ Item template
142+ -> FeedConfiguration -- ^ Feed configuration
143+ -> Context String -- ^ Item context
144+ -> [Item String] -- ^ Feed items
145+ -> Compiler (Item String) -- ^ Resulting feed
146+renderAtomWithTemplates feedTemplate itemTemplate config context = renderFeed
147+ feedTemplate itemTemplate config
148+ (makeItemContext "%Y-%m-%dT%H:%M:%SZ" context)
101149
102150
103151--------------------------------------------------------------------------------
@@ -106,9 +154,7 @@ renderRss :: FeedConfiguration -- ^ Feed configuration
106154 -> Context String -- ^ Item context
107155 -> [Item String] -- ^ Feed items
108156 -> Compiler (Item String) -- ^ Resulting feed
109−renderRss config context = renderFeed
110− "templates/rss.xml" "templates/rss-item.xml" config
111− (makeItemContext "%a, %d %b %Y %H:%M:%S UT" context)
157+renderRss = renderRssWithTemplates rssTemplate rssItemTemplate
112158
113159
114160--------------------------------------------------------------------------------
@@ -117,13 +163,11 @@ renderAtom :: FeedConfiguration -- ^ Feed configuration
117163 -> Context String -- ^ Item context
118164 -> [Item String] -- ^ Feed items
119165 -> Compiler (Item String) -- ^ Resulting feed
120−renderAtom config context = renderFeed
121− "templates/atom.xml" "templates/atom-item.xml" config
122− (makeItemContext "%Y-%m-%dT%H:%M:%SZ" context)
166+renderAtom = renderAtomWithTemplates atomTemplate atomItemTemplate
123167
124168
125169--------------------------------------------------------------------------------
126170-- | Copies @$updated$@ from @$published$@ if it is not already set.
127171makeItemContext :: String -> Context a -> Context a
128172makeItemContext fmt context = mconcat
129− [dateField "published" fmt, context, dateField "updated" fmt]
173+ [context, dateField "published" fmt, dateField "updated" fmt]
src/Hakyll/Web/Html.hs → lib/Hakyll/Web/Html.hsrenamed+61−20
@@ -3,11 +3,13 @@
33module Hakyll.Web.Html
44 ( -- * Generic
55 withTags
6+ , withTagList
67
78 -- * Headers
89 , demoteHeaders
910
1011 -- * Url manipulation
12+ , getUrls
1113 , withUrls
1214 , toUrl
1315 , toSiteRoot
@@ -20,23 +22,26 @@ module Hakyll.Web.Html
2022
2123
2224--------------------------------------------------------------------------------
23−import Data.Char (digitToInt, intToDigit, isDigit, toLower)
24−import Data.List (isPrefixOf)
25−import qualified Data.Set as S
26−import System.FilePath (joinPath, splitPath, takeDirectory)
27−import Text.Blaze.Html (toHtml)
28−import Text.Blaze.Html.Renderer.String (renderHtml)
29−
30−
31−--------------------------------------------------------------------------------
32−import qualified Text.HTML.TagSoup as TS
25+import Data.Char (digitToInt, intToDigit,
26+ isDigit, toLower)
27+import Data.List (isPrefixOf)
28+import qualified Data.Set as S
29+import System.FilePath.Posix (joinPath, splitPath,
30+ takeDirectory)
31+import Text.Blaze.Html (toHtml)
32+import Text.Blaze.Html.Renderer.String (renderHtml)
33+import qualified Text.HTML.TagSoup as TS
34+import Network.URI (isUnreserved, escapeURIString)
3335
3436
3537--------------------------------------------------------------------------------
3638-- | Map over all tags in the document
3739withTags :: (TS.Tag String -> TS.Tag String) -> String -> String
38−withTags f = renderTags' . map f . TS.parseTags
40+withTags = withTagList . map
3941
42+-- | Map over all tags (as list) in the document
43+withTagList :: ([TS.Tag String] -> [TS.Tag String]) -> String -> String
44+withTagList f = renderTags' . f . parseTags'
4045
4146--------------------------------------------------------------------------------
4247-- | Map every @h1@ to an @h2@, @h2@ to @h3@, etc.
@@ -52,6 +57,16 @@ demoteHeaders = withTags $ \tag -> case tag of
5257 demote t = t
5358
5459
60+--------------------------------------------------------------------------------
61+isUrlAttribute :: String -> Bool
62+isUrlAttribute = (`elem` ["src", "href", "data", "poster", "srcset"])
63+
64+
65+--------------------------------------------------------------------------------
66+getUrls :: [TS.Tag String] -> [String]
67+getUrls tags = [v | TS.TagOpen _ as <- tags, (k, v) <- as, isUrlAttribute k]
68+
69+
5570--------------------------------------------------------------------------------
5671-- | Apply a function to each URL on a webpage
5772withUrls :: (String -> String) -> String -> String
@@ -59,16 +74,32 @@ withUrls f = withTags tag
5974 where
6075 tag (TS.TagOpen s a) = TS.TagOpen s $ map attr a
6176 tag x = x
62− attr (k, v) = (k, if k `S.member` refs then f v else v)
63− refs = S.fromList ["src", "href"]
77+ attr (k, v) = (k, if isUrlAttribute k then f v else v)
6478
6579
66−-- | Customized TagSoup renderer. (The default TagSoup renderer escape CSS
67−-- within style tags.)
80+--------------------------------------------------------------------------------
81+-- | Customized TagSoup renderer. The default TagSoup renderer escape CSS
82+-- within style tags, and doesn't properly minimize.
6883renderTags' :: [TS.Tag String] -> String
69−renderTags' = TS.renderTagsOptions TS.renderOptions
84+renderTags' = TS.renderTagsOptions TS.RenderOptions
7085 { TS.optRawTag = (`elem` ["script", "style"]) . map toLower
71− , TS.optMinimize = (`elem` ["br", "img"])
86+ , TS.optMinimize = (`S.member` minimize) . map toLower
87+ , TS.optEscape = id
88+ }
89+ where
90+ -- A list of elements which must be minimized
91+ minimize = S.fromList
92+ [ "area", "br", "col", "embed", "hr", "img", "input", "meta", "link"
93+ , "param"
94+ ]
95+
96+
97+--------------------------------------------------------------------------------
98+-- | Customized TagSoup parser: do not decode any entities.
99+parseTags' :: String -> [TS.Tag String]
100+parseTags' = TS.parseTagsOptions (TS.parseOptions :: TS.ParseOptions String)
101+ { TS.optEntityData = \(str, b) -> [TS.TagText $ "&" ++ str ++ [';' | b]]
102+ , TS.optEntityAttrib = \(str, b) -> ("&" ++ str ++ [';' | b], [])
72103 }
73104
74105
@@ -82,9 +113,18 @@ renderTags' = TS.renderTagsOptions TS.renderOptions
82113-- Result:
83114--
84115-- > "/foo/bar.html"
116+--
117+-- This also sanitizes the URL, e.g. converting spaces into '%20'
85118toUrl :: FilePath -> String
86−toUrl ('/' : xs) = '/' : xs
87−toUrl url = '/' : url
119+toUrl url = case url of
120+ ('/' : xs) -> '/' : sanitize xs
121+ xs -> '/' : sanitize xs
122+ where
123+ -- Everything but unreserved characters should be escaped as we are
124+ -- sanitising the path therefore reserved characters which have a
125+ -- meaning in URI does not appear. Special casing for `/`, because it has
126+ -- a special meaning in FilePath as well as in URI.
127+ sanitize = escapeURIString (\c -> c == '/' || isUnreserved c)
88128
89129
90130--------------------------------------------------------------------------------
@@ -98,13 +138,14 @@ toSiteRoot = emptyException . joinPath . map parent
98138 emptyException x = x
99139 relevant "." = False
100140 relevant "/" = False
141+ relevant "./" = False
101142 relevant _ = True
102143
103144
104145--------------------------------------------------------------------------------
105146-- | Check if an URL links to an external HTTP(S) source
106147isExternal :: String -> Bool
107−isExternal url = any (flip isPrefixOf url) ["http://", "https://"]
148+isExternal url = any (flip isPrefixOf url) ["http://", "https://", "//"]
108149
109150
110151--------------------------------------------------------------------------------
src/Hakyll/Web/Html/RelativizeUrls.hs → lib/Hakyll/Web/Html/RelativizeUrls.hsrenamed

The content is the same.

lib/Hakyll/Web/Paginate.hsnew file+153
@@ -0,0 +1,153 @@
1+--------------------------------------------------------------------------------
2+{-# LANGUAGE OverloadedStrings #-}
3+module Hakyll.Web.Paginate
4+ ( PageNumber
5+ , Paginate (..)
6+ , buildPaginateWith
7+ , paginateEvery
8+ , paginateRules
9+ , paginateContext
10+ ) where
11+
12+
13+--------------------------------------------------------------------------------
14+import Control.Applicative (empty)
15+import Control.Monad (forM_, forM)
16+import qualified Data.Map as M
17+import qualified Data.Set as S
18+
19+
20+--------------------------------------------------------------------------------
21+import Hakyll.Core.Compiler
22+import Hakyll.Core.Identifier
23+import Hakyll.Core.Identifier.Pattern
24+import Hakyll.Core.Item
25+import Hakyll.Core.Metadata
26+import Hakyll.Core.Rules
27+import Hakyll.Web.Html
28+import Hakyll.Web.Template.Context
29+
30+
31+--------------------------------------------------------------------------------
32+type PageNumber = Int
33+
34+
35+--------------------------------------------------------------------------------
36+-- | Data about paginators
37+data Paginate = Paginate
38+ { paginateMap :: M.Map PageNumber [Identifier]
39+ , paginateMakeId :: PageNumber -> Identifier
40+ , paginateDependency :: Dependency
41+ }
42+
43+
44+--------------------------------------------------------------------------------
45+paginateNumPages :: Paginate -> Int
46+paginateNumPages = M.size . paginateMap
47+
48+
49+--------------------------------------------------------------------------------
50+paginateEvery :: Int -> [a] -> [[a]]
51+paginateEvery n = go
52+ where
53+ go [] = []
54+ go xs = let (y, ys) = splitAt n xs in y : go ys
55+
56+
57+--------------------------------------------------------------------------------
58+buildPaginateWith
59+ :: MonadMetadata m
60+ => ([Identifier] -> m [[Identifier]]) -- ^ Group items into pages
61+ -> Pattern -- ^ Select items to paginate
62+ -> (PageNumber -> Identifier) -- ^ Identifiers for the pages
63+ -> m Paginate
64+buildPaginateWith grouper pattern makeId = do
65+ ids <- getMatches pattern
66+ idGroups <- grouper ids
67+ let idsSet = S.fromList ids
68+ return Paginate
69+ { paginateMap = M.fromList (zip [1 ..] idGroups)
70+ , paginateMakeId = makeId
71+ , paginateDependency = PatternDependency pattern idsSet
72+ }
73+
74+
75+--------------------------------------------------------------------------------
76+paginateRules :: Paginate -> (PageNumber -> Pattern -> Rules ()) -> Rules ()
77+paginateRules paginator rules =
78+ forM_ (M.toList $ paginateMap paginator) $ \(idx, identifiers) ->
79+ rulesExtraDependencies [paginateDependency paginator] $
80+ create [paginateMakeId paginator idx] $
81+ rules idx $ fromList identifiers
82+
83+
84+--------------------------------------------------------------------------------
85+-- | Get the identifier for a certain page by passing in the page number.
86+paginatePage :: Paginate -> PageNumber -> Maybe Identifier
87+paginatePage pag pageNumber
88+ | pageNumber < 1 = Nothing
89+ | pageNumber > (paginateNumPages pag) = Nothing
90+ | otherwise = Just $ paginateMakeId pag pageNumber
91+
92+
93+--------------------------------------------------------------------------------
94+-- | A default paginate context which provides the following keys:
95+--
96+--
97+-- * @firstPageNum@
98+-- * @firstPageUrl@
99+-- * @previousPageNum@
100+-- * @previousPageUrl@
101+-- * @nextPageNum@
102+-- * @nextPageUrl@
103+-- * @lastPageNum@
104+-- * @lastPageUrl@
105+-- * @currentPageNum@
106+-- * @currentPageUrl@
107+-- * @numPages@
108+-- * @allPages@
109+paginateContext :: Paginate -> PageNumber -> Context a
110+paginateContext pag currentPage = mconcat
111+ [ field "firstPageNum" $ \_ -> otherPage 1 >>= num
112+ , field "firstPageUrl" $ \_ -> otherPage 1 >>= url
113+ , field "previousPageNum" $ \_ -> otherPage (currentPage - 1) >>= num
114+ , field "previousPageUrl" $ \_ -> otherPage (currentPage - 1) >>= url
115+ , field "nextPageNum" $ \_ -> otherPage (currentPage + 1) >>= num
116+ , field "nextPageUrl" $ \_ -> otherPage (currentPage + 1) >>= url
117+ , field "lastPageNum" $ \_ -> otherPage lastPage >>= num
118+ , field "lastPageUrl" $ \_ -> otherPage lastPage >>= url
119+ , field "currentPageNum" $ \i -> thisPage i >>= num
120+ , field "currentPageUrl" $ \i -> thisPage i >>= url
121+ , constField "numPages" $ show $ paginateNumPages pag
122+ , Context $ \k _ i -> case k of
123+ "allPages" -> do
124+ let ctx =
125+ field "isCurrent" (\n -> if fst (itemBody n) == currentPage then return "true" else empty) `mappend`
126+ field "num" (num . itemBody) `mappend`
127+ field "url" (url . itemBody)
128+
129+ list <- forM [1 .. lastPage] $
130+ \n -> if n == currentPage then thisPage i else otherPage n
131+ items <- mapM makeItem list
132+ return $ ListField ctx items
133+ _ -> do
134+ empty
135+
136+ ]
137+ where
138+ lastPage = paginateNumPages pag
139+
140+ thisPage i = return (currentPage, itemIdentifier i)
141+ otherPage n
142+ | n == currentPage = fail $ "This is the current page: " ++ show n
143+ | otherwise = case paginatePage pag n of
144+ Nothing -> fail $ "No such page: " ++ show n
145+ Just i -> return (n, i)
146+
147+ num :: (Int, Identifier) -> Compiler String
148+ num = return . show . fst
149+
150+ url :: (Int, Identifier) -> Compiler String
151+ url (n, i) = getRoute i >>= \mbR -> case mbR of
152+ Just r -> return $ toUrl r
153+ Nothing -> fail $ "No URL for page: " ++ show n
lib/Hakyll/Web/Pandoc.hsnew file+165
@@ -0,0 +1,165 @@
1+--------------------------------------------------------------------------------
2+-- | Module exporting convenient pandoc bindings
3+module Hakyll.Web.Pandoc
4+ ( -- * The basic building blocks
5+ readPandoc
6+ , readPandocWith
7+ , writePandoc
8+ , writePandocWith
9+ , renderPandoc
10+ , renderPandocWith
11+
12+ -- * Derived compilers
13+ , pandocCompiler
14+ , pandocCompilerWith
15+ , pandocCompilerWithTransform
16+ , pandocCompilerWithTransformM
17+
18+ -- * Default options
19+ , defaultHakyllReaderOptions
20+ , defaultHakyllWriterOptions
21+ ) where
22+
23+
24+--------------------------------------------------------------------------------
25+import qualified Data.Text as T
26+import Text.Pandoc
27+import Text.Pandoc.Highlighting (pygments)
28+
29+
30+--------------------------------------------------------------------------------
31+import Hakyll.Core.Compiler
32+import Hakyll.Core.Item
33+import Hakyll.Web.Pandoc.FileType
34+
35+
36+--------------------------------------------------------------------------------
37+-- | Read a string using pandoc, with the default options
38+readPandoc
39+ :: Item String -- ^ String to read
40+ -> Compiler (Item Pandoc) -- ^ Resulting document
41+readPandoc = readPandocWith defaultHakyllReaderOptions
42+
43+
44+--------------------------------------------------------------------------------
45+-- | Read a string using pandoc, with the supplied options
46+readPandocWith
47+ :: ReaderOptions -- ^ Parser options
48+ -> Item String -- ^ String to read
49+ -> Compiler (Item Pandoc) -- ^ Resulting document
50+readPandocWith ropt item =
51+ case runPure $ traverse (reader ropt (itemFileType item)) (fmap T.pack item) of
52+ Left err -> fail $
53+ "Hakyll.Web.Pandoc.readPandocWith: parse failed: " ++ show err
54+ Right item' -> return item'
55+ where
56+ reader ro t = case t of
57+ DocBook -> readDocBook ro
58+ Html -> readHtml ro
59+ LaTeX -> readLaTeX ro
60+ LiterateHaskell t' -> reader (addExt ro Ext_literate_haskell) t'
61+ Markdown -> readMarkdown ro
62+ MediaWiki -> readMediaWiki ro
63+ OrgMode -> readOrg ro
64+ Rst -> readRST ro
65+ Textile -> readTextile ro
66+ _ -> error $
67+ "Hakyll.Web.readPandocWith: I don't know how to read a file of " ++
68+ "the type " ++ show t ++ " for: " ++ show (itemIdentifier item)
69+
70+ addExt ro e = ro {readerExtensions = enableExtension e $ readerExtensions ro}
71+
72+
73+--------------------------------------------------------------------------------
74+-- | Write a document (as HTML) using pandoc, with the default options
75+writePandoc :: Item Pandoc -- ^ Document to write
76+ -> Item String -- ^ Resulting HTML
77+writePandoc = writePandocWith defaultHakyllWriterOptions
78+
79+
80+--------------------------------------------------------------------------------
81+-- | Write a document (as HTML) using pandoc, with the supplied options
82+writePandocWith :: WriterOptions -- ^ Writer options for pandoc
83+ -> Item Pandoc -- ^ Document to write
84+ -> Item String -- ^ Resulting HTML
85+writePandocWith wopt (Item itemi doc) =
86+ case runPure $ writeHtml5String wopt doc of
87+ Left err -> error $ "Hakyll.Web.Pandoc.writePandocWith: " ++ show err
88+ Right item' -> Item itemi $ T.unpack item'
89+
90+
91+--------------------------------------------------------------------------------
92+-- | Render the resource using pandoc
93+renderPandoc :: Item String -> Compiler (Item String)
94+renderPandoc =
95+ renderPandocWith defaultHakyllReaderOptions defaultHakyllWriterOptions
96+
97+
98+--------------------------------------------------------------------------------
99+-- | Render the resource using pandoc
100+renderPandocWith
101+ :: ReaderOptions -> WriterOptions -> Item String -> Compiler (Item String)
102+renderPandocWith ropt wopt item =
103+ writePandocWith wopt <$> readPandocWith ropt item
104+
105+
106+--------------------------------------------------------------------------------
107+-- | Read a page render using pandoc
108+pandocCompiler :: Compiler (Item String)
109+pandocCompiler =
110+ pandocCompilerWith defaultHakyllReaderOptions defaultHakyllWriterOptions
111+
112+
113+--------------------------------------------------------------------------------
114+-- | A version of 'pandocCompiler' which allows you to specify your own pandoc
115+-- options
116+pandocCompilerWith :: ReaderOptions -> WriterOptions -> Compiler (Item String)
117+pandocCompilerWith ropt wopt =
118+ cached "Hakyll.Web.Pandoc.pandocCompilerWith" $
119+ pandocCompilerWithTransform ropt wopt id
120+
121+
122+--------------------------------------------------------------------------------
123+-- | An extension of 'pandocCompilerWith' which allows you to specify a custom
124+-- pandoc transformation for the content
125+pandocCompilerWithTransform :: ReaderOptions -> WriterOptions
126+ -> (Pandoc -> Pandoc)
127+ -> Compiler (Item String)
128+pandocCompilerWithTransform ropt wopt f =
129+ pandocCompilerWithTransformM ropt wopt (return . f)
130+
131+
132+--------------------------------------------------------------------------------
133+-- | Similar to 'pandocCompilerWithTransform', but the transformation
134+-- function is monadic. This is useful when you want the pandoc
135+-- transformation to use the 'Compiler' information such as routes,
136+-- metadata, etc
137+pandocCompilerWithTransformM :: ReaderOptions -> WriterOptions
138+ -> (Pandoc -> Compiler Pandoc)
139+ -> Compiler (Item String)
140+pandocCompilerWithTransformM ropt wopt f =
141+ writePandocWith wopt <$>
142+ (traverse f =<< readPandocWith ropt =<< getResourceBody)
143+
144+
145+--------------------------------------------------------------------------------
146+-- | The default reader options for pandoc parsing in hakyll
147+defaultHakyllReaderOptions :: ReaderOptions
148+defaultHakyllReaderOptions = def
149+ { -- The following option causes pandoc to read smart typography, a nice
150+ -- and free bonus.
151+ readerExtensions = enableExtension Ext_smart pandocExtensions
152+ }
153+
154+
155+--------------------------------------------------------------------------------
156+-- | The default writer options for pandoc rendering in hakyll
157+defaultHakyllWriterOptions :: WriterOptions
158+defaultHakyllWriterOptions = def
159+ { -- This option causes literate haskell to be written using '>' marks in
160+ -- html, which I think is a good default.
161+ writerExtensions = enableExtension Ext_smart pandocExtensions
162+ , -- We want to have hightlighting by default, to be compatible with earlier
163+ -- Hakyll releases
164+ writerHighlightStyle = Just pygments
165+ }
src/Hakyll/Web/Pandoc/Biblio.hs → lib/Hakyll/Web/Pandoc/Biblio.hsrenamed+47−23
@@ -4,9 +4,11 @@
44-- In order to add a bibliography, you will need a bibliography file (e.g.
55-- @.bib@) and a CSL file (@.csl@). Both need to be compiled with their
66-- respective compilers ('biblioCompiler' and 'cslCompiler'). Then, you can
7−-- refer to these files when you use 'pageReadPandocBiblio'. This function also
8−-- takes a parser state for completeness -- you can use
9−-- 'defaultHakyllParserState' if you're unsure.
7+-- refer to these files when you use 'readPandocBiblio'. This function also
8+-- takes the reader options for completeness -- you can use
9+-- 'defaultHakyllReaderOptions' if you're unsure.
10+-- 'pandocBiblioCompiler' is a convenience wrapper which works like 'pandocCompiler',
11+-- but also takes paths to compiled bibliography and csl files.
1012{-# LANGUAGE Arrows #-}
1113{-# LANGUAGE DeriveDataTypeable #-}
1214{-# LANGUAGE GeneralizedNewtypeDeriving #-}
@@ -16,24 +18,26 @@ module Hakyll.Web.Pandoc.Biblio
1618 , Biblio (..)
1719 , biblioCompiler
1820 , readPandocBiblio
21+ , pandocBiblioCompiler
1922 ) where
2023
2124
2225--------------------------------------------------------------------------------
23−import Control.Applicative ((<$>))
24−import Data.Binary (Binary (..))
25−import Data.Typeable (Typeable)
26−import qualified Text.CSL as CSL
27−import Text.Pandoc (Pandoc, ParserState (..))
28−import Text.Pandoc.Biblio (processBiblio)
29−
30−
31−--------------------------------------------------------------------------------
26+import Control.Monad (liftM, replicateM)
27+import Data.Binary (Binary (..))
28+import Data.Typeable (Typeable)
3229import Hakyll.Core.Compiler
30+import Hakyll.Core.Compiler.Internal
3331import Hakyll.Core.Identifier
3432import Hakyll.Core.Item
33+import Hakyll.Core.Provider
3534import Hakyll.Core.Writable
3635import Hakyll.Web.Pandoc
36+import Hakyll.Web.Pandoc.Binary ()
37+import qualified Text.CSL as CSL
38+import Text.CSL.Pandoc (processCites)
39+import Text.Pandoc (Pandoc, ReaderOptions (..),
40+ enableExtension, Extension (..))
3741
3842
3943--------------------------------------------------------------------------------
@@ -66,8 +70,10 @@ newtype Biblio = Biblio [CSL.Reference]
6670--------------------------------------------------------------------------------
6771instance Binary Biblio where
6872 -- Ugly.
69− get = Biblio . read <$> get
70− put (Biblio rs) = put $ show rs
73+ get = do
74+ len <- get
75+ Biblio <$> replicateM len get
76+ put (Biblio rs) = put (length rs) >> mapM_ put rs
7177
7278
7379--------------------------------------------------------------------------------
@@ -79,24 +85,42 @@ instance Writable Biblio where
7985--------------------------------------------------------------------------------
8086biblioCompiler :: Compiler (Item Biblio)
8187biblioCompiler = do
82− filePath <- toFilePath <$> getUnderlying
83− makeItem =<< unsafeCompiler (Biblio <$> CSL.readBiblioFile filePath)
88+ filePath <- getResourceFilePath
89+ makeItem =<< unsafeCompiler (Biblio <$> CSL.readBiblioFile idpred filePath)
90+ where
91+ -- This is a filter on citations. We include all citations.
92+ idpred = const True
8493
8594
8695--------------------------------------------------------------------------------
87−readPandocBiblio :: ParserState
96+readPandocBiblio :: ReaderOptions
8897 -> Item CSL
8998 -> Item Biblio
9099 -> (Item String)
91100 -> Compiler (Item Pandoc)
92−readPandocBiblio state csl biblio item = do
101+readPandocBiblio ropt csl biblio item = do
102+ -- Parse CSL file, if given
103+ provider <- compilerProvider <$> compilerAsk
104+ style <- unsafeCompiler $
105+ CSL.readCSLFile Nothing . (resourceFilePath provider) . itemIdentifier $ csl
106+
93107 -- We need to know the citation keys, add then *before* actually parsing the
94108 -- actual page. If we don't do this, pandoc won't even consider them
95109 -- citations!
96110 let Biblio refs = itemBody biblio
97− cits = map CSL.refId refs
98− state' = state {stateCitations = stateCitations state ++ cits}
99− pandoc = itemBody $ readPandocWith state' item
100− cslPath = toFilePath $ itemIdentifier csl
101− pandoc' <- unsafeCompiler $ processBiblio cslPath Nothing refs pandoc
111+ pandoc <- itemBody <$> readPandocWith ropt item
112+ let pandoc' = processCites style refs pandoc
113+
102114 return $ fmap (const pandoc') item
115+
116+--------------------------------------------------------------------------------
117+pandocBiblioCompiler :: String -> String -> Compiler (Item String)
118+pandocBiblioCompiler cslFileName bibFileName = do
119+ csl <- load $ fromFilePath cslFileName
120+ bib <- load $ fromFilePath bibFileName
121+ liftM writePandoc
122+ (getResourceBody >>= readPandocBiblio ropt csl bib)
123+ where ropt = defaultHakyllReaderOptions
124+ { -- The following option enables citation rendering
125+ readerExtensions = enableExtension Ext_citations $ readerExtensions defaultHakyllReaderOptions
126+ }
lib/Hakyll/Web/Pandoc/Binary.hsnew file+33
@@ -0,0 +1,33 @@
1+{-# OPTIONS_GHC -fno-warn-orphans #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+module Hakyll.Web.Pandoc.Binary where
4+
5+import Data.Binary (Binary (..))
6+
7+import qualified Text.CSL as CSL
8+import qualified Text.CSL.Reference as REF
9+import qualified Text.CSL.Style as STY
10+import Text.Pandoc
11+
12+--------------------------------------------------------------------------------
13+-- orphans
14+
15+instance Binary Alignment
16+instance Binary Block
17+instance Binary CSL.Reference
18+instance Binary Citation
19+instance Binary CitationMode
20+instance Binary Format
21+instance Binary Inline
22+instance Binary ListNumberDelim
23+instance Binary ListNumberStyle
24+instance Binary MathType
25+instance Binary QuoteType
26+instance Binary REF.CLabel
27+instance Binary REF.CNum
28+instance Binary REF.Literal
29+instance Binary REF.RefDate
30+instance Binary REF.RefType
31+instance Binary REF.Season
32+instance Binary STY.Agent
33+instance Binary STY.Formatted
src/Hakyll/Web/Pandoc/FileType.hs → lib/Hakyll/Web/Pandoc/FileType.hsrenamed+30−21
@@ -8,7 +8,7 @@ module Hakyll.Web.Pandoc.FileType
88
99
1010--------------------------------------------------------------------------------
11−import System.FilePath (takeExtension)
11+import System.FilePath (splitExtension)
1212
1313
1414--------------------------------------------------------------------------------
@@ -22,10 +22,12 @@ import Hakyll.Core.Item
2222data FileType
2323 = Binary
2424 | Css
25+ | DocBook
2526 | Html
2627 | LaTeX
2728 | LiterateHaskell FileType
2829 | Markdown
30+ | MediaWiki
2931 | OrgMode
3032 | PlainText
3133 | Rst
@@ -36,27 +38,34 @@ data FileType
3638--------------------------------------------------------------------------------
3739-- | Get the file type for a certain file. The type is determined by extension.
3840fileType :: FilePath -> FileType
39−fileType = fileType' . takeExtension
41+fileType = uncurry fileType' . splitExtension
4042 where
41− fileType' ".css" = Css
42− fileType' ".htm" = Html
43− fileType' ".html" = Html
44− fileType' ".lhs" = LiterateHaskell Markdown
45− fileType' ".markdown" = Markdown
46− fileType' ".md" = Markdown
47− fileType' ".mdn" = Markdown
48− fileType' ".mdown" = Markdown
49− fileType' ".mdwn" = Markdown
50− fileType' ".mkd" = Markdown
51− fileType' ".mkdwn" = Markdown
52− fileType' ".org" = OrgMode
53− fileType' ".page" = Markdown
54− fileType' ".rst" = Rst
55− fileType' ".tex" = LaTeX
56− fileType' ".text" = PlainText
57− fileType' ".textile" = Textile
58− fileType' ".txt" = PlainText
59− fileType' _ = Binary -- Treat unknown files as binary
43+ fileType' _ ".css" = Css
44+ fileType' _ ".dbk" = DocBook
45+ fileType' _ ".htm" = Html
46+ fileType' _ ".html" = Html
47+ fileType' f ".lhs" = LiterateHaskell $ case fileType f of
48+ -- If no extension is given, default to Markdown + LiterateHaskell
49+ Binary -> Markdown
50+ -- Otherwise, LaTeX + LiterateHaskell or whatever the user specified
51+ x -> x
52+ fileType' _ ".markdown" = Markdown
53+ fileType' _ ".mediawiki" = MediaWiki
54+ fileType' _ ".md" = Markdown
55+ fileType' _ ".mdn" = Markdown
56+ fileType' _ ".mdown" = Markdown
57+ fileType' _ ".mdwn" = Markdown
58+ fileType' _ ".mkd" = Markdown
59+ fileType' _ ".mkdwn" = Markdown
60+ fileType' _ ".org" = OrgMode
61+ fileType' _ ".page" = Markdown
62+ fileType' _ ".rst" = Rst
63+ fileType' _ ".tex" = LaTeX
64+ fileType' _ ".text" = PlainText
65+ fileType' _ ".textile" = Textile
66+ fileType' _ ".txt" = PlainText
67+ fileType' _ ".wiki" = MediaWiki
68+ fileType' _ _ = Binary -- Treat unknown files as binary
6069
6170
6271--------------------------------------------------------------------------------
lib/Hakyll/Web/Redirect.hsnew file+100
@@ -0,0 +1,100 @@
1+-- | Module used for generating HTML redirect pages. This allows renaming pages
2+-- to avoid breaking existing links without requiring server-side support for
3+-- formal 301 Redirect error codes
4+module Hakyll.Web.Redirect
5+ ( Redirect (..)
6+ , createRedirects
7+ ) where
8+
9+import Control.Applicative ((<$>))
10+import Control.Monad (forM_, when)
11+import Data.Binary (Binary (..))
12+import Data.List (sort, group)
13+import Hakyll.Core.Compiler
14+import Hakyll.Core.Identifier
15+import Hakyll.Core.Routes
16+import Hakyll.Core.Rules
17+import Hakyll.Core.Writable (Writable (..))
18+
19+-- | This function exposes a higher-level interface compared to using the
20+-- 'Redirect' type manually.
21+--
22+-- This creates, using a database mapping broken URLs to working ones, HTML
23+-- files which will do HTML META tag redirect pages (since, as a static site, we
24+-- can't use web-server-level 301 redirects, and using JS is gross).
25+--
26+-- This is useful for sending people using old URLs to renamed versions, dealing
27+-- with common typos etc, and will increase site traffic. Such broken URLs can
28+-- be found by looking at server logs or by using Google Webmaster Tools.
29+-- Broken URLs must be valid Haskell strings, non-URL-escaped valid POSIX
30+-- filenames, and relative links, since they will be defined in a @hakyll.hs@
31+-- and during generation, written to disk with the filename corresponding to the
32+-- broken URLs. (Target URLs can be absolute or relative, but should be
33+-- URL-escaped.) So broken incoming links like <http://www.gwern.net/foo/> which
34+-- should be <http://www.gwern.net/foobar> cannot be fixed (since you cannot
35+-- create a HTML file named @"foo/"@ on disk, as that would be a directory).
36+--
37+-- An example of a valid association list would be:
38+--
39+-- > brokenLinks =
40+-- > [ ("projects.html", "http://github.com/gwern")
41+-- > , ("/Black-market archive", "Black-market%20archives")
42+-- > ]
43+--
44+-- In which case the functionality can then be used in `main` with a line like:
45+--
46+-- > version "redirects" $ createRedirects brokenLinks
47+--
48+-- The 'version' is recommended to separate these items from your other pages.
49+--
50+-- The on-disk files can then be uploaded with HTML mimetypes
51+-- (either explicitly by generating and uploading them separately, by
52+-- auto-detection of the filetype, or an upload tool defaulting to HTML
53+-- mimetype, such as calling @s3cmd@ with @--default-mime-type=text/html@) and
54+-- will redirect browsers and search engines going to the old/broken URLs.
55+--
56+-- See also <https://groups.google.com/d/msg/hakyll/sWc6zxfh-uM/fUpZPsFNDgAJ>.
57+createRedirects :: [(Identifier, String)] -> Rules ()
58+createRedirects redirects =
59+ do -- redirects are many-to-fewer; keys must be unique, and must point somewhere else:
60+ let gkeys = group $ sort $ map fst redirects
61+ forM_ gkeys $ \gkey -> case gkey of
62+ (k : _ : _) -> fail $
63+ "Duplicate 301 redirects; " ++ show k ++ " is ambiguous."
64+ _ -> return ()
65+
66+ forM_ redirects $ \(r, t) ->
67+ when (toFilePath r == t) $ fail $
68+ "Self-redirect detected: " ++ show r ++ " points to itself."
69+
70+ forM_ redirects $ \(ident, to) ->
71+ create [ident] $ do
72+ route idRoute
73+ compile $ makeItem $! Redirect to
74+
75+-- | This datatype can be used directly if you want a lower-level interface to
76+-- generate redirects. For example, if you want to redirect @foo.html@ to
77+-- @bar.jpg@, you can use:
78+--
79+-- > create ["foo.html"] $ do
80+-- > route idRoute
81+-- > compile $ makeItem $ Redirect "bar.jpg"
82+data Redirect = Redirect
83+ { redirectTo :: String
84+ } deriving (Eq, Ord, Show)
85+
86+instance Binary Redirect where
87+ put (Redirect to) = put to
88+ get = Redirect <$> get
89+
90+instance Writable Redirect where
91+ write path = write path . fmap redirectToHtml
92+
93+redirectToHtml :: Redirect -> String
94+redirectToHtml (Redirect working) =
95+ "<!DOCTYPE html><html><head><meta charset=\"utf-8\"/><meta name=\"generator\" content=\"hakyll\"/>" ++
96+ "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" ++
97+ "<meta http-equiv=\"refresh\" content=\"0; url=" ++ working ++
98+ "\"><link rel=\"canonical\" href=\"" ++ working ++
99+ "\"><title>Permanent Redirect</title></head><body><p>The page has moved to: <a href=\"" ++ working ++
100+ "\">this page</a></p></body></html>"
src/Hakyll/Web/Tags.hs → lib/Hakyll/Web/Tags.hsrenamed+116−43
@@ -16,7 +16,7 @@
1616-- > its populations have been damaged greatly by Nuclear...
1717--
1818-- To use categories, use the 'buildCategories' function. Categories are
19−-- determined by the direcetory a page is in, for example, the post
19+-- determined by the directory a page is in, for example, the post
2020--
2121-- > posts/coding/2010-01-28-hakyll-categories.markdown
2222--
@@ -41,15 +41,21 @@
4141{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4242{-# LANGUAGE OverloadedStrings #-}
4343module Hakyll.Web.Tags
44− ( Tags
44+ ( Tags (..)
4545 , getTags
46+ , getCategory
4647 , buildTagsWith
4748 , buildTags
4849 , buildCategories
4950 , tagsRules
51+ , renderTags
5052 , renderTagCloud
53+ , renderTagCloudWith
54+ , tagCloudField
55+ , tagCloudFieldWith
5156 , renderTagList
5257 , tagsField
58+ , tagsFieldWith
5359 , categoryField
5460 , sortTagsBy
5561 , caseInsensitiveTags
@@ -58,14 +64,14 @@ module Hakyll.Web.Tags
5864
5965--------------------------------------------------------------------------------
6066import Control.Arrow ((&&&))
61−import Control.Monad (foldM, forM, forM_)
67+import Control.Monad (foldM, forM, forM_, mplus)
6268import Data.Char (toLower)
6369import Data.List (intercalate, intersperse,
6470 sortBy)
6571import qualified Data.Map as M
6672import Data.Maybe (catMaybes, fromMaybe)
67−import Data.Monoid (mconcat)
6873import Data.Ord (comparing)
74+import qualified Data.Set as S
6975import System.FilePath (takeBaseName, takeDirectory)
7076import Text.Blaze.Html (toHtml, toValue, (!))
7177import Text.Blaze.Html.Renderer.String (renderHtml)
@@ -82,8 +88,8 @@ import Hakyll.Core.Item
8288import Hakyll.Core.Metadata
8389import Hakyll.Core.Rules
8490import Hakyll.Core.Util.String
85−import Hakyll.Web.Template.Context
8691import Hakyll.Web.Html
92+import Hakyll.Web.Template.Context
8793
8894
8995--------------------------------------------------------------------------------
@@ -92,19 +98,22 @@ data Tags = Tags
9298 { tagsMap :: [(String, [Identifier])]
9399 , tagsMakeId :: String -> Identifier
94100 , tagsDependency :: Dependency
95− } deriving (Show)
101+ }
96102
97103
98104--------------------------------------------------------------------------------
99105-- | Obtain tags from a page in the default way: parse them from the @tags@
100−-- metadata field.
106+-- metadata field. This can either be a list or a comma-separated string.
101107getTags :: MonadMetadata m => Identifier -> m [String]
102108getTags identifier = do
103109 metadata <- getMetadata identifier
104− return $ maybe [] (map trim . splitAll ",") $ M.lookup "tags" metadata
110+ return $ fromMaybe [] $
111+ (lookupStringList "tags" metadata) `mplus`
112+ (map trim . splitAll "," <$> lookupString "tags" metadata)
113+
105114
106115--------------------------------------------------------------------------------
107−-- | Obtain categories from a page.
116+-- | Obtain category from a page.
108117getCategory :: MonadMetadata m => Identifier -> m [String]
109118getCategory = return . return . takeBaseName . takeDirectory . toFilePath
110119
@@ -119,7 +128,8 @@ buildTagsWith :: MonadMetadata m
119128buildTagsWith f pattern makeId = do
120129 ids <- getMatches pattern
121130 tagMap <- foldM addTags M.empty ids
122− return $ Tags (M.toList tagMap) makeId (PatternDependency pattern ids)
131+ let set' = S.fromList ids
132+ return $ Tags (M.toList tagMap) makeId (PatternDependency pattern set')
123133 where
124134 -- Create a tag map for one page
125135 addTags tagMap id' = do
@@ -143,13 +153,13 @@ buildCategories = buildTagsWith getCategory
143153tagsRules :: Tags -> (String -> Pattern -> Rules ()) -> Rules ()
144154tagsRules tags rules =
145155 forM_ (tagsMap tags) $ \(tag, identifiers) ->
146− match (fromGlob $ toFilePath $ tagsMakeId tags tag) $
147− rulesExtraDependencies [tagsDependency tags] $
156+ rulesExtraDependencies [tagsDependency tags] $
157+ create [tagsMakeId tags tag] $
148158 rules tag $ fromList identifiers
149159
150160
151161--------------------------------------------------------------------------------
152−-- | Render tags in HTML
162+-- | Render tags in HTML (the flexible higher-order function)
153163renderTags :: (String -> String -> Int -> Int -> Int -> String)
154164 -- ^ Produce a tag item: tag, url, count, min count, max count
155165 -> ([String] -> String)
@@ -183,7 +193,6 @@ renderTags makeHtml concatHtml tags = do
183193
184194--------------------------------------------------------------------------------
185195-- | Render a tag cloud in HTML
186−-- TODO: Maybe produce a Context here
187196renderTagCloud :: Double
188197 -- ^ Smallest font size, in percent
189198 -> Double
@@ -192,19 +201,73 @@ renderTagCloud :: Double
192201 -- ^ Input tags
193202 -> Compiler String
194203 -- ^ Rendered cloud
195−renderTagCloud minSize maxSize = renderTags makeLink (intercalate " ")
204+renderTagCloud = renderTagCloudWith makeLink (intercalate " ")
196205 where
197− makeLink tag url count min' max' = renderHtml $
198− H.a ! A.style (toValue $ "font-size: " ++ size count min' max')
199− ! A.href (toValue url)
200− $ toHtml tag
201−
202− -- Show the relative size of one 'count' in percent
203− size count min' max' =
204− let diff = 1 + fromIntegral max' - fromIntegral min'
206+ makeLink minSize maxSize tag url count min' max' =
207+ -- Show the relative size of one 'count' in percent
208+ let diff = 1 + fromIntegral max' - fromIntegral min'
205209 relative = (fromIntegral count - fromIntegral min') / diff
206− size' = floor $ minSize + relative * (maxSize - minSize)
207− in show (size' :: Int) ++ "%"
210+ size = floor $ minSize + relative * (maxSize - minSize) :: Int
211+ in renderHtml $
212+ H.a ! A.style (toValue $ "font-size: " ++ show size ++ "%")
213+ ! A.href (toValue url)
214+ $ toHtml tag
215+
216+
217+--------------------------------------------------------------------------------
218+-- | Render a tag cloud in HTML
219+renderTagCloudWith :: (Double -> Double ->
220+ String -> String -> Int -> Int -> Int -> String)
221+ -- ^ Render a single tag link
222+ -> ([String] -> String)
223+ -- ^ Concatenate links
224+ -> Double
225+ -- ^ Smallest font size, in percent
226+ -> Double
227+ -- ^ Biggest font size, in percent
228+ -> Tags
229+ -- ^ Input tags
230+ -> Compiler String
231+ -- ^ Rendered cloud
232+renderTagCloudWith makeLink cat minSize maxSize =
233+ renderTags (makeLink minSize maxSize) cat
234+
235+
236+--------------------------------------------------------------------------------
237+-- | Render a tag cloud in HTML as a context
238+tagCloudField :: String
239+ -- ^ Destination key
240+ -> Double
241+ -- ^ Smallest font size, in percent
242+ -> Double
243+ -- ^ Biggest font size, in percent
244+ -> Tags
245+ -- ^ Input tags
246+ -> Context a
247+ -- ^ Context
248+tagCloudField key minSize maxSize tags =
249+ field key $ \_ -> renderTagCloud minSize maxSize tags
250+
251+
252+--------------------------------------------------------------------------------
253+-- | Render a tag cloud in HTML as a context
254+tagCloudFieldWith :: String
255+ -- ^ Destination key
256+ -> (Double -> Double ->
257+ String -> String -> Int -> Int -> Int -> String)
258+ -- ^ Render a single tag link
259+ -> ([String] -> String)
260+ -- ^ Concatenate links
261+ -> Double
262+ -- ^ Smallest font size, in percent
263+ -> Double
264+ -- ^ Biggest font size, in percent
265+ -> Tags
266+ -- ^ Input tags
267+ -> Context a
268+ -- ^ Context
269+tagCloudFieldWith key makeLink cat minSize maxSize tags =
270+ field key $ \_ -> renderTagCloudWith makeLink cat minSize maxSize tags
208271
209272
210273--------------------------------------------------------------------------------
@@ -218,27 +281,27 @@ renderTagList = renderTags makeLink (intercalate ", ")
218281
219282
220283--------------------------------------------------------------------------------
221−-- | Render tags with links with custom function to get tags. It is typically
222−-- together with 'getTags' like this:
223−--
224−-- > renderTagsFieldWith (customFunction . getTags)
225−-- > "tags" (fromCapture "tags/*")
226−tagsFieldWith :: (Identifier -> Compiler [String]) -- ^ Get the tags
227− -> String -- ^ Destination key
228− -> Tags -- ^ Tags structure
229− -> Context a -- ^ Resulting context
230−tagsFieldWith getTags' key tags = field key $ \item -> do
284+-- | Render tags with links with custom functions to get tags and to
285+-- render links
286+tagsFieldWith :: (Identifier -> Compiler [String])
287+ -- ^ Get the tags
288+ -> (String -> (Maybe FilePath) -> Maybe H.Html)
289+ -- ^ Render link for one tag
290+ -> ([H.Html] -> H.Html)
291+ -- ^ Concatenate tag links
292+ -> String
293+ -- ^ Destination field
294+ -> Tags
295+ -- ^ Tags structure
296+ -> Context a
297+ -- ^ Resulting context
298+tagsFieldWith getTags' renderLink cat key tags = field key $ \item -> do
231299 tags' <- getTags' $ itemIdentifier item
232300 links <- forM tags' $ \tag -> do
233301 route' <- getRoute $ tagsMakeId tags tag
234302 return $ renderLink tag route'
235303
236− return $ renderHtml $ mconcat $ intersperse ", " $ catMaybes $ links
237− where
238− -- Render one tag link
239− renderLink _ Nothing = Nothing
240− renderLink tag (Just filePath) = Just $
241− H.a ! A.href (toValue $ toUrl filePath) $ toHtml tag
304+ return $ renderHtml $ cat $ catMaybes $ links
242305
243306
244307--------------------------------------------------------------------------------
@@ -246,7 +309,8 @@ tagsFieldWith getTags' key tags = field key $ \item -> do
246309tagsField :: String -- ^ Destination key
247310 -> Tags -- ^ Tags
248311 -> Context a -- ^ Context
249−tagsField = tagsFieldWith getTags
312+tagsField =
313+ tagsFieldWith getTags simpleRenderLink (mconcat . intersperse ", ")
250314
251315
252316--------------------------------------------------------------------------------
@@ -254,7 +318,16 @@ tagsField = tagsFieldWith getTags
254318categoryField :: String -- ^ Destination key
255319 -> Tags -- ^ Tags
256320 -> Context a -- ^ Context
257−categoryField = tagsFieldWith getCategory
321+categoryField =
322+ tagsFieldWith getCategory simpleRenderLink (mconcat . intersperse ", ")
323+
324+
325+--------------------------------------------------------------------------------
326+-- | Render one tag link
327+simpleRenderLink :: String -> (Maybe FilePath) -> Maybe H.Html
328+simpleRenderLink _ Nothing = Nothing
329+simpleRenderLink tag (Just filePath) =
330+ Just $ H.a ! A.href (toValue $ toUrl filePath) $ toHtml tag
258331
259332
260333--------------------------------------------------------------------------------
lib/Hakyll/Web/Template.hsnew file+178
@@ -0,0 +1,178 @@
1+-- | This module provides means for reading and applying 'Template's.
2+--
3+-- Templates are tools to convert items into a string. They are perfectly suited
4+-- for laying out your site.
5+--
6+-- Let's look at an example template:
7+--
8+-- > <html>
9+-- > <head>
10+-- > <title>My crazy homepage - $title$</title>
11+-- > </head>
12+-- > <body>
13+-- > <div id="header">
14+-- > <h1>My crazy homepage - $title$</h1>
15+-- > </div>
16+-- > <div id="content">
17+-- > $body$
18+-- > </div>
19+-- > <div id="footer">
20+-- > By reading this you agree that I now own your soul
21+-- > </div>
22+-- > </body>
23+-- > </html>
24+--
25+-- As you can see, the format is very simple -- @$key$@ is used to render the
26+-- @$key$@ field from the page, everything else is literally copied. If you want
27+-- to literally insert @\"$key$\"@ into your page (for example, when you're
28+-- writing a Hakyll tutorial) you can use
29+--
30+-- > <p>
31+-- > A literal $$key$$.
32+-- > </p>
33+--
34+-- Because of it's simplicity, these templates can be used for more than HTML:
35+-- you could make, for example, CSS or JS templates as well.
36+--
37+-- Apart from interpolating @$key$@s from the 'Context' you can also
38+-- use the following macros:
39+--
40+-- * @$if(key)$@
41+--
42+-- > $if(key)$
43+-- > <b> Defined </b>
44+-- > $else$
45+-- > <b> Non-defined </b>
46+-- > $endif$
47+--
48+-- This example will print @Defined@ if @key@ is defined in the
49+-- context and @Non-defined@ otherwise. The @$else$@ clause is
50+-- optional.
51+--
52+-- * @$for(key)$@
53+--
54+-- The @for@ macro is used for enumerating 'Context' elements that are
55+-- lists, i.e. constructed using the 'listField' function. Assume that
56+-- in a context we have an element @listField \"key\" c itms@. Then
57+-- the snippet
58+--
59+-- > $for(key)$
60+-- > $x$
61+-- > $sep$,
62+-- > $endfor$
63+--
64+-- would, for each item @i@ in 'itms', lookup @$x$@ in the context @c@
65+-- with item @i@, interpolate it, and join the resulting list with
66+-- @,@.
67+--
68+-- Another concrete example one may consider is the following. Given the
69+-- context
70+--
71+-- > listField "things" (field "thing" (return . itemBody))
72+-- > (sequence [makeItem "fruits", makeItem "vegetables"])
73+--
74+-- and a template
75+--
76+-- > I like
77+-- > $for(things)$
78+-- > fresh $thing$$sep$, and
79+-- > $endfor$
80+--
81+-- the resulting page would look like
82+--
83+-- > <p>
84+-- > I like
85+-- >
86+-- > fresh fruits, and
87+-- >
88+-- > fresh vegetables
89+-- > </p>
90+--
91+-- The @$sep$@ part can be omitted. Usually, you can get by using the
92+-- 'applyListTemplate' and 'applyJoinListTemplate' functions.
93+--
94+-- * @$partial(path)$@
95+--
96+-- Loads a template located in a separate file and interpolates it
97+-- under the current context.
98+--
99+-- Assuming that the file @test.html@ contains
100+--
101+-- > <b>$key$</b>
102+--
103+-- The result of rendering
104+--
105+-- > <p>
106+-- > $partial("test.html")$
107+-- > </p>
108+--
109+-- is the same as the result of rendering
110+--
111+-- > <p>
112+-- > <b>$key$</b>
113+-- > </p>
114+--
115+-- That is, calling @$partial$@ is equivalent to just copying and pasting
116+-- template code.
117+--
118+-- In the examples above you can see that the outputs contain a lot of leftover
119+-- whitespace that you may wish to remove. Using @'$-'@ or @'-$'@ instead of
120+-- @'$'@ in a macro strips all whitespace to the left or right of that clause
121+-- respectively. Given the context
122+--
123+-- > listField "counts" (field "count" (return . itemBody))
124+-- > (sequence [makeItem "3", makeItem "2", makeItem "1"])
125+--
126+-- and a template
127+--
128+-- > <p>
129+-- > $for(counts)-$
130+-- > $count$
131+-- > $-sep$...
132+-- > $-endfor$
133+-- > </p>
134+--
135+-- the resulting page would look like
136+--
137+-- > <p>
138+-- > 3...2...1
139+-- > </p>
140+--
141+{-# LANGUAGE TemplateHaskell #-}
142+module Hakyll.Web.Template
143+ ( Template
144+ , templateBodyCompiler
145+ , templateCompiler
146+ , applyTemplate
147+ , loadAndApplyTemplate
148+ , applyAsTemplate
149+ , readTemplate
150+ , compileTemplateItem
151+ , unsafeReadTemplateFile
152+ , embedTemplate
153+ ) where
154+
155+
156+--------------------------------------------------------------------------------
157+import Hakyll.Web.Template.Internal
158+
159+
160+--------------------------------------------------------------------------------
161+import Data.FileEmbed (embedFile)
162+import qualified Data.Text as T
163+import qualified Data.Text.Encoding as T
164+import Language.Haskell.TH (Exp, Q)
165+
166+
167+--------------------------------------------------------------------------------
168+-- | Embed template allows you embed a template within the Haskell binary.
169+-- Example:
170+--
171+-- > myTemplate :: Template
172+-- > myTemplate = $(embedTemplate "test.html")
173+embedTemplate :: FilePath -> Q Exp
174+embedTemplate filePath = [|
175+ let source = T.unpack $ T.decodeUtf8 $(embedFile filePath) in
176+ case parseTemplateElemsFile filePath source of
177+ Left err -> error err
178+ Right tpl -> template filePath tpl |]
lib/Hakyll/Web/Template/Context.hsnew file+481
@@ -0,0 +1,481 @@
1+-- | This module provides 'Context's which are used to expand expressions in
2+-- templates and allow for arbitrary customisation.
3+--
4+-- 'Template's define a small expression DSL which consists of strings,
5+-- identifiers and function application. There is no type system, every value is
6+-- a string and on the top level they get substituted verbatim into the page.
7+--
8+-- For example, you can build a context that contains
9+--
10+-- > … <> functionField "concat" (const . concat) <> …
11+--
12+-- which will allow you to use the @concat@ identifier as a function that takes
13+-- arbitrarily many stings and concatenates them to a new string:
14+--
15+-- > $partial(concat("templates/categories/", category))$
16+--
17+-- This will evaluate the @category@ field in the context, then prepend he path,
18+-- and include the referenced file as a template.
19+
20+
21+--------------------------------------------------------------------------------
22+{-# LANGUAGE CPP #-}
23+{-# LANGUAGE ExistentialQuantification #-}
24+{-# LANGUAGE RankNTypes #-}
25+module Hakyll.Web.Template.Context
26+ ( ContextField (..)
27+ , Context (..)
28+ , field
29+ , boolField
30+ , constField
31+ , listField
32+ , listFieldWith
33+ , functionField
34+ , mapContext
35+
36+ , defaultContext
37+ , bodyField
38+ , metadataField
39+ , urlField
40+ , pathField
41+ , titleField
42+ , snippetField
43+ , dateField
44+ , dateFieldWith
45+ , getItemUTC
46+ , getItemModificationTime
47+ , modificationTimeField
48+ , modificationTimeFieldWith
49+ , teaserField
50+ , teaserFieldWithSeparator
51+ , missingField
52+ , bindItem
53+ ) where
54+
55+
56+--------------------------------------------------------------------------------
57+import Control.Applicative (Alternative (..))
58+import Control.Monad (msum)
59+import Data.List (intercalate, tails)
60+#if MIN_VERSION_base(4,9,0)
61+import Data.Semigroup (Semigroup (..))
62+#endif
63+import Data.Time.Clock (UTCTime (..))
64+import Data.Time.Format (formatTime)
65+import qualified Data.Time.Format as TF
66+import Data.Time.Locale.Compat (TimeLocale, defaultTimeLocale)
67+import Hakyll.Core.Compiler
68+import Hakyll.Core.Compiler.Internal
69+import Hakyll.Core.Identifier
70+import Hakyll.Core.Item
71+import Hakyll.Core.Metadata
72+import Hakyll.Core.Provider
73+import Hakyll.Core.Util.String (needlePrefix, splitAll)
74+import Hakyll.Web.Html
75+import Prelude hiding (id)
76+import System.FilePath (dropExtension, splitDirectories,
77+ takeBaseName)
78+
79+
80+--------------------------------------------------------------------------------
81+-- | Mostly for internal usage
82+data ContextField
83+ = EmptyField
84+ | StringField String
85+ | forall a. ListField (Context a) [Item a]
86+
87+
88+--------------------------------------------------------------------------------
89+-- | The 'Context' monoid. Please note that the order in which you
90+-- compose the items is important. For example in
91+--
92+-- > field "A" f1 <> field "A" f2
93+--
94+-- the first context will overwrite the second. This is especially
95+-- important when something is being composed with
96+-- 'metadataField' (or 'defaultContext'). If you want your context to be
97+-- overwritten by the metadata fields, compose it from the right:
98+--
99+-- @
100+-- 'metadataField' \<\> field \"date\" fDate
101+-- @
102+--
103+newtype Context a = Context
104+ { unContext :: String -> [String] -> Item a -> Compiler ContextField
105+ }
106+
107+
108+--------------------------------------------------------------------------------
109+-- | Tries to find a key in the left context,
110+-- or when that fails in the right context.
111+#if MIN_VERSION_base(4,9,0)
112+instance Semigroup (Context a) where
113+ (<>) (Context f) (Context g) = Context $ \k a i -> f k a i <|> g k a i
114+
115+instance Monoid (Context a) where
116+ mempty = missingField
117+ mappend = (<>)
118+#else
119+instance Monoid (Context a) where
120+ mempty = missingField
121+ mappend (Context f) (Context g) = Context $ \k a i -> f k a i <|> g k a i
122+#endif
123+
124+
125+--------------------------------------------------------------------------------
126+field' :: String -> (Item a -> Compiler ContextField) -> Context a
127+field' key value = Context $ \k _ i ->
128+ if k == key
129+ then value i
130+ else noResult $ "Tried field " ++ key
131+
132+
133+--------------------------------------------------------------------------------
134+-- | Constructs a new field for a 'Context'.
135+-- If the key matches, the compiler is run and its result is substituted in the
136+-- template.
137+--
138+-- If the compiler fails, the field will be considered non-existent
139+-- in an @$if()$@ macro or ultimately break the template application
140+-- (unless the key is found in another context when using '<>').
141+-- Use 'empty' or 'noResult' for intentional failures of fields used in
142+-- @$if()$@, to distinguish them from exceptions thrown with 'fail'.
143+field
144+ :: String -- ^ Key
145+ -> (Item a -> Compiler String) -- ^ Function that constructs a value based
146+ -- on the item (e.g. accessing metadata)
147+ -> Context a
148+field key value = field' key (fmap StringField . value)
149+
150+
151+--------------------------------------------------------------------------------
152+-- | Creates a 'field' to use with the @$if()$@ template macro.
153+-- Attempting to substitute the field into the template will cause an error.
154+boolField
155+ :: String
156+ -> (Item a -> Bool)
157+ -> Context a
158+boolField name f = field' name (\i -> if f i
159+ then return EmptyField
160+ else noResult $ "Field " ++ name ++ " is false")
161+
162+
163+--------------------------------------------------------------------------------
164+-- | Creates a 'field' that does not depend on the 'Item' but always yields
165+-- the same string
166+constField :: String -- ^ Key
167+ -> String -- ^ Value
168+ -> Context a
169+constField key = field key . const . return
170+
171+
172+--------------------------------------------------------------------------------
173+-- | Creates a list field to be consumed by a @$for(…)$@ expression.
174+-- The compiler returns multiple items which are rendered in the loop body
175+-- with the supplied context.
176+listField :: String -> Context a -> Compiler [Item a] -> Context b
177+listField key c xs = listFieldWith key c (const xs)
178+
179+
180+--------------------------------------------------------------------------------
181+-- | Produces a new 'Context' which has list field 'key'. All fields from
182+-- 'c' are also accessible from the produced context.
183+-- Be careful when doing @listFieldWith k ca f <> cb@ as any fields in @ca@
184+-- will override fields in @cb@ with the same name.
185+-- Creates a list field like 'listField', but supplies the current page
186+-- to the compiler.
187+listFieldWith
188+ :: String -> Context a -> (Item b -> Compiler [Item a]) -> Context b
189+listFieldWith key c f = field' key $ fmap (ListField c) . f
190+
191+
192+--------------------------------------------------------------------------------
193+-- | Creates a variadic function field.
194+--
195+-- The function will be called with the dynamically evaluated string arguments
196+-- from the template as well as the page that is currently rendered.
197+functionField :: String -- ^ Key
198+ -> ([String] -> Item a -> Compiler String) -- ^ Function
199+ -> Context a
200+functionField name value = Context $ \k args i ->
201+ if k == name
202+ then StringField <$> value args i
203+ else noResult $ "Tried function field " ++ name
204+
205+
206+--------------------------------------------------------------------------------
207+-- | Transform the respective string results of all fields in a context.
208+-- For example,
209+--
210+-- > mapContext (++"c") (constField "x" "a" <> constField "y" "b")
211+--
212+-- is equivalent to
213+--
214+-- > constField "x" "ac" <> constField "y" "bc"
215+--
216+mapContext :: (String -> String) -> Context a -> Context a
217+mapContext f (Context c) = Context $ \k a i -> do
218+ fld <- c k a i
219+ case fld of
220+ EmptyField -> wrongType "boolField"
221+ StringField str -> return $ StringField (f str)
222+ _ -> wrongType "ListField"
223+ where
224+ wrongType typ = fail $ "Hakyll.Web.Template.Context.mapContext: " ++
225+ "can't map over a " ++ typ ++ "!"
226+
227+--------------------------------------------------------------------------------
228+-- | A context that allows snippet inclusion. In processed file, use as:
229+--
230+-- > ...
231+-- > $snippet("path/to/snippet/")$
232+-- > ...
233+--
234+-- The contents of the included file will not be interpolated like @partial@
235+-- does it.
236+--
237+snippetField :: Context String
238+snippetField = functionField "snippet" f
239+ where
240+ f [contentsPath] _ = loadBody (fromFilePath contentsPath)
241+ f [] _ = fail "No argument to function 'snippet()'"
242+ f _ _ = fail "Too many arguments to function 'snippet()'"
243+
244+--------------------------------------------------------------------------------
245+-- | A context that contains (in that order)
246+--
247+-- 1. A @$body$@ field
248+--
249+-- 2. Metadata fields
250+--
251+-- 3. A @$url$@ 'urlField'
252+--
253+-- 4. A @$path$@ 'pathField'
254+--
255+-- 5. A @$title$@ 'titleField'
256+defaultContext :: Context String
257+defaultContext =
258+ bodyField "body" `mappend`
259+ metadataField `mappend`
260+ urlField "url" `mappend`
261+ pathField "path" `mappend`
262+ titleField "title"
263+
264+
265+--------------------------------------------------------------------------------
266+teaserSeparator :: String
267+teaserSeparator = "<!--more-->"
268+
269+
270+--------------------------------------------------------------------------------
271+-- | Constructs a 'field' that contains the body of the item.
272+bodyField :: String -> Context String
273+bodyField key = field key $ return . itemBody
274+
275+
276+--------------------------------------------------------------------------------
277+-- | Map any field to its metadata value, if present
278+metadataField :: Context a
279+metadataField = Context $ \k _ i -> do
280+ let id = itemIdentifier i
281+ empty' = noResult $ "No '" ++ k ++ "' field in metadata " ++
282+ "of item " ++ show id
283+ value <- getMetadataField id k
284+ maybe empty' (return . StringField) value
285+
286+
287+--------------------------------------------------------------------------------
288+-- | Absolute url to the resulting item
289+urlField :: String -> Context a
290+urlField key = field key $ \i -> do
291+ let id = itemIdentifier i
292+ empty' = fail $ "No route url found for item " ++ show id
293+ fmap (maybe empty' toUrl) $ getRoute id
294+
295+
296+--------------------------------------------------------------------------------
297+-- | Filepath of the underlying file of the item
298+pathField :: String -> Context a
299+pathField key = field key $ return . toFilePath . itemIdentifier
300+
301+
302+--------------------------------------------------------------------------------
303+-- | This title 'field' takes the basename of the underlying file by default
304+titleField :: String -> Context a
305+titleField = mapContext takeBaseName . pathField
306+
307+
308+--------------------------------------------------------------------------------
309+-- | When the metadata has a field called @published@ in one of the
310+-- following formats then this function can render the date.
311+--
312+-- * @Mon, 06 Sep 2010 00:01:00 +0000@
313+--
314+-- * @Mon, 06 Sep 2010 00:01:00 UTC@
315+--
316+-- * @Mon, 06 Sep 2010 00:01:00@
317+--
318+-- * @2010-09-06T00:01:00+0000@
319+--
320+-- * @2010-09-06T00:01:00Z@
321+--
322+-- * @2010-09-06T00:01:00@
323+--
324+-- * @2010-09-06 00:01:00+0000@
325+--
326+-- * @2010-09-06 00:01:00@
327+--
328+-- * @September 06, 2010 00:01 AM@
329+--
330+-- Following date-only formats are supported too (@00:00:00@ for time is
331+-- assumed)
332+--
333+-- * @2010-09-06@
334+--
335+-- * @September 06, 2010@
336+--
337+-- Alternatively, when the metadata has a field called @path@ in a
338+-- @folder/yyyy-mm-dd-title.extension@ format (the convention for pages)
339+-- and no @published@ metadata field set, this function can render
340+-- the date. This pattern matches the file name or directory names
341+-- that begins with @yyyy-mm-dd@ . For example:
342+-- @folder//yyyy-mm-dd-title//dist//main.extension@ .
343+-- In case of multiple matches, the rightmost one is used.
344+--
345+-- As another alternative, if none of the above matches, and the file has a
346+-- path which contains nested directories specifying a date, then that date
347+-- will be used. In other words, if the path is of the form
348+-- @**//yyyy//mm//dd//**//main.extension@ .
349+-- As above, in case of multiple matches, the rightmost one is used.
350+
351+dateField :: String -- ^ Key in which the rendered date should be placed
352+ -> String -- ^ Format to use on the date
353+ -> Context a -- ^ Resulting context
354+dateField = dateFieldWith defaultTimeLocale
355+
356+
357+--------------------------------------------------------------------------------
358+-- | This is an extended version of 'dateField' that allows you to
359+-- specify a time locale that is used for outputting the date. For more
360+-- details, see 'dateField' and 'formatTime'.
361+dateFieldWith :: TimeLocale -- ^ Output time locale
362+ -> String -- ^ Destination key
363+ -> String -- ^ Format to use on the date
364+ -> Context a -- ^ Resulting context
365+dateFieldWith locale key format = field key $ \i -> do
366+ time <- getItemUTC locale $ itemIdentifier i
367+ return $ formatTime locale format time
368+
369+
370+--------------------------------------------------------------------------------
371+-- | Parser to try to extract and parse the time from the @published@
372+-- field or from the filename. See 'dateField' for more information.
373+-- Exported for user convenience.
374+getItemUTC :: MonadMetadata m
375+ => TimeLocale -- ^ Output time locale
376+ -> Identifier -- ^ Input page
377+ -> m UTCTime -- ^ Parsed UTCTime
378+getItemUTC locale id' = do
379+ metadata <- getMetadata id'
380+ let tryField k fmt = lookupString k metadata >>= parseTime' fmt
381+ paths = splitDirectories $ (dropExtension . toFilePath) id'
382+
383+ maybe empty' return $ msum $
384+ [tryField "published" fmt | fmt <- formats] ++
385+ [tryField "date" fmt | fmt <- formats] ++
386+ [parseTime' "%Y-%m-%d" $ intercalate "-" $ take 3 $ splitAll "-" fnCand | fnCand <- reverse paths] ++
387+ [parseTime' "%Y-%m-%d" $ intercalate "-" $ fnCand | fnCand <- map (take 3) $ reverse . tails $ paths]
388+ where
389+ empty' = fail $ "Hakyll.Web.Template.Context.getItemUTC: " ++
390+ "could not parse time for " ++ show id'
391+ parseTime' = parseTimeM True locale
392+ formats =
393+ [ "%a, %d %b %Y %H:%M:%S %Z"
394+ , "%Y-%m-%dT%H:%M:%S%Z"
395+ , "%Y-%m-%d %H:%M:%S%Z"
396+ , "%Y-%m-%d"
397+ , "%B %e, %Y %l:%M %p"
398+ , "%B %e, %Y"
399+ , "%b %d, %Y"
400+ ]
401+
402+
403+--------------------------------------------------------------------------------
404+-- | Get the time on which the actual file was last modified. This only works if
405+-- there actually is an underlying file, of couse.
406+getItemModificationTime
407+ :: Identifier
408+ -> Compiler UTCTime
409+getItemModificationTime identifier = do
410+ provider <- compilerProvider <$> compilerAsk
411+ return $ resourceModificationTime provider identifier
412+
413+
414+--------------------------------------------------------------------------------
415+-- | Creates a field with the last modification date of the underlying item.
416+modificationTimeField :: String -- ^ Key
417+ -> String -- ^ Format
418+ -> Context a -- ^ Resulting context
419+modificationTimeField = modificationTimeFieldWith defaultTimeLocale
420+
421+
422+--------------------------------------------------------------------------------
423+-- | Creates a field with the last modification date of the underlying item
424+-- in a custom localisation format (see 'formatTime').
425+modificationTimeFieldWith :: TimeLocale -- ^ Time output locale
426+ -> String -- ^ Key
427+ -> String -- ^ Format
428+ -> Context a -- ^ Resulting context
429+modificationTimeFieldWith locale key fmt = field key $ \i -> do
430+ mtime <- getItemModificationTime $ itemIdentifier i
431+ return $ formatTime locale fmt mtime
432+
433+
434+--------------------------------------------------------------------------------
435+-- | A context with "teaser" key which contain a teaser of the item.
436+-- The item is loaded from the given snapshot (which should be saved
437+-- in the user code before any templates are applied).
438+teaserField :: String -- ^ Key to use
439+ -> Snapshot -- ^ Snapshot to load
440+ -> Context String -- ^ Resulting context
441+teaserField = teaserFieldWithSeparator teaserSeparator
442+
443+
444+--------------------------------------------------------------------------------
445+-- | A context with "teaser" key which contain a teaser of the item, defined as
446+-- the snapshot content before the teaser separator. The item is loaded from the
447+-- given snapshot (which should be saved in the user code before any templates
448+-- are applied).
449+teaserFieldWithSeparator :: String -- ^ Separator to use
450+ -> String -- ^ Key to use
451+ -> Snapshot -- ^ Snapshot to load
452+ -> Context String -- ^ Resulting context
453+teaserFieldWithSeparator separator key snapshot = field key $ \item -> do
454+ body <- itemBody <$> loadSnapshot (itemIdentifier item) snapshot
455+ case needlePrefix separator body of
456+ Nothing -> fail $
457+ "Hakyll.Web.Template.Context: no teaser defined for " ++
458+ show (itemIdentifier item)
459+ Just t -> return t
460+
461+
462+--------------------------------------------------------------------------------
463+-- | Constantly reports any field as missing. Mostly for internal usage,
464+-- it is the last choice in every context used in a template application.
465+missingField :: Context a
466+missingField = Context $ \k _ _ -> noResult $
467+ "Missing field '" ++ k ++ "' in context"
468+
469+parseTimeM :: Bool -> TimeLocale -> String -> String -> Maybe UTCTime
470+#if MIN_VERSION_time(1,5,0)
471+parseTimeM = TF.parseTimeM
472+#else
473+parseTimeM _ = TF.parseTime
474+#endif
475+
476+--------------------------------------------------------------------------------
477+
478+-- | Binds an 'Item' to a given 'Context', allowing it to be combined with any
479+-- other 'Context' of any type.
480+bindItem :: Context a -> Item a -> forall b. Context b
481+bindItem (Context ctx) ia = Context $ \k args _ -> ctx k args ia
lib/Hakyll/Web/Template/Internal.hsnew file+261
@@ -0,0 +1,261 @@
1+{-# LANGUAGE DeriveAnyClass #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+{-# LANGUAGE ScopedTypeVariables #-}
5+
6+module Hakyll.Web.Template.Internal
7+ ( Template (..)
8+ , template
9+ , templateBodyCompiler
10+ , templateCompiler
11+ , applyTemplate
12+ , loadAndApplyTemplate
13+ , applyAsTemplate
14+ , readTemplate
15+ , compileTemplateItem
16+ , unsafeReadTemplateFile
17+
18+ , module Hakyll.Web.Template.Internal.Element
19+ , module Hakyll.Web.Template.Internal.Trim
20+ ) where
21+
22+
23+--------------------------------------------------------------------------------
24+import Control.Monad (forM)
25+import Control.Monad.Except (MonadError (..), catchError)
26+import Data.Binary (Binary)
27+import Data.List (intercalate)
28+import qualified Data.List.NonEmpty as NonEmpty
29+import Data.Typeable (Typeable)
30+import GHC.Exts (IsString (..))
31+import GHC.Generics (Generic)
32+
33+
34+--------------------------------------------------------------------------------
35+import Hakyll.Core.Compiler
36+import Hakyll.Core.Compiler.Internal
37+import Hakyll.Core.Identifier
38+import Hakyll.Core.Item
39+import Hakyll.Core.Writable
40+import Hakyll.Web.Template.Context
41+import Hakyll.Web.Template.Internal.Element
42+import Hakyll.Web.Template.Internal.Trim
43+
44+
45+--------------------------------------------------------------------------------
46+-- | Datatype used for template substitutions.
47+data Template = Template
48+ { tplElements :: [TemplateElement]
49+ , tplOrigin :: FilePath -- Only for error messages.
50+ } deriving (Show, Eq, Generic, Binary, Typeable)
51+
52+
53+--------------------------------------------------------------------------------
54+instance Writable Template where
55+ -- Writing a template is impossible
56+ write _ _ = return ()
57+
58+
59+--------------------------------------------------------------------------------
60+instance IsString Template where
61+ fromString = readTemplate
62+
63+
64+--------------------------------------------------------------------------------
65+-- | Wrap the constructor to ensure trim is called.
66+template :: FilePath -> [TemplateElement] -> Template
67+template p = flip Template p . trim
68+
69+
70+--------------------------------------------------------------------------------
71+-- | Parse a string into a template.
72+-- You should prefer 'compileTemplateItem' over this.
73+readTemplate :: String -> Template
74+readTemplate = either error (template origin) . parseTemplateElemsFile origin
75+ where
76+ origin = "{literal}"
77+{-# DEPRECATED readTemplate "Use templateCompiler instead" #-}
78+
79+--------------------------------------------------------------------------------
80+-- | Parse an item body into a template.
81+-- Provides useful error messages in the 'Compiler' monad.
82+compileTemplateItem :: Item String -> Compiler Template
83+compileTemplateItem item = let file = itemIdentifier item
84+ in compileTemplateFile file (itemBody item)
85+
86+--------------------------------------------------------------------------------
87+compileTemplateFile :: Identifier -> String -> Compiler Template
88+compileTemplateFile file = either fail (return . template origin)
89+ . parseTemplateElemsFile origin
90+ where
91+ origin = show file
92+
93+--------------------------------------------------------------------------------
94+-- | Read a template, without metadata header
95+templateBodyCompiler :: Compiler (Item Template)
96+templateBodyCompiler = cached "Hakyll.Web.Template.templateBodyCompiler" $ do
97+ item <- getResourceBody
98+ file <- getUnderlying
99+ withItemBody (compileTemplateFile file) item
100+
101+--------------------------------------------------------------------------------
102+-- | Read complete file contents as a template
103+templateCompiler :: Compiler (Item Template)
104+templateCompiler = cached "Hakyll.Web.Template.templateCompiler" $ do
105+ item <- getResourceString
106+ file <- getUnderlying
107+ withItemBody (compileTemplateFile file) item
108+
109+
110+--------------------------------------------------------------------------------
111+-- | Interpolate template expressions from context values in a page
112+applyTemplate :: Template -- ^ Template
113+ -> Context a -- ^ Context
114+ -> Item a -- ^ Page
115+ -> Compiler (Item String) -- ^ Resulting item
116+applyTemplate tpl context item = do
117+ body <- applyTemplate' (tplElements tpl) context item `catchError` handler
118+ return $ itemSetBody body item
119+ where
120+ tplName = tplOrigin tpl
121+ itemName = show $ itemIdentifier item
122+ handler es = fail $ "Hakyll.Web.Template.applyTemplate: Failed to " ++
123+ (if tplName == itemName
124+ then "interpolate template in item " ++ itemName
125+ else "apply template " ++ tplName ++ " to item " ++ itemName) ++
126+ ":\n" ++ intercalate ",\n" es
127+
128+
129+
130+--------------------------------------------------------------------------------
131+applyTemplate'
132+ :: forall a.
133+ [TemplateElement] -- ^ Unwrapped Template
134+ -> Context a -- ^ Context
135+ -> Item a -- ^ Page
136+ -> Compiler String -- ^ Resulting item
137+applyTemplate' tes context x = go tes
138+ where
139+ context' :: String -> [String] -> Item a -> Compiler ContextField
140+ context' = unContext (context `mappend` missingField)
141+
142+ go = fmap concat . mapM applyElem
143+
144+ ---------------------------------------------------------------------------
145+
146+ applyElem :: TemplateElement -> Compiler String
147+
148+ applyElem TrimL = trimError
149+
150+ applyElem TrimR = trimError
151+
152+ applyElem (Chunk c) = return c
153+
154+ applyElem (Expr e) = withErrorMessage evalMsg (applyStringExpr typeMsg e)
155+ where
156+ evalMsg = "In expr '$" ++ show e ++ "$'"
157+ typeMsg = "expr '$" ++ show e ++ "$'"
158+
159+ applyElem Escaped = return "$"
160+
161+ applyElem (If e t mf) = compilerTry (applyExpr e) >>= handle
162+ where
163+ f = maybe (return "") go mf
164+ handle (Right _) = go t
165+ handle (Left (CompilationNoResult _)) = f
166+ handle (Left (CompilationFailure es)) = debug (NonEmpty.toList es) >> f
167+ debug = compilerDebugEntries ("Hakyll.Web.Template.applyTemplate: " ++
168+ "[ERROR] in 'if' condition on expr '" ++ show e ++ "':")
169+
170+ applyElem (For e b s) = withErrorMessage headMsg (applyExpr e) >>= \cf -> case cf of
171+ EmptyField -> expected "list" "boolean" typeMsg
172+ StringField _ -> expected "list" "string" typeMsg
173+ ListField c xs -> withErrorMessage bodyMsg $ do
174+ sep <- maybe (return "") go s
175+ bs <- forM xs $ applyTemplate' b $ c <> bindItem context x
176+ return $ intercalate sep bs
177+ where
178+ headMsg = "In expr '$for(" ++ show e ++ ")$'"
179+ typeMsg = "loop expr '" ++ show e ++ "'"
180+ bodyMsg = "In loop context of '$for(" ++ show e ++ ")$'"
181+
182+ applyElem (Partial e) = withErrorMessage headMsg $
183+ applyStringExpr typeMsg e >>= \p ->
184+ withErrorMessage inclMsg $ do
185+ tpl' <- loadBody (fromFilePath p)
186+ itemBody <$> applyTemplate tpl' context x
187+ where
188+ headMsg = "In expr '$partial(" ++ show e ++ ")$'"
189+ typeMsg = "partial expr '" ++ show e ++ "'"
190+ inclMsg = "In inclusion of '$partial(" ++ show e ++ ")$'"
191+
192+ ---------------------------------------------------------------------------
193+
194+ applyExpr :: TemplateExpr -> Compiler ContextField
195+
196+ applyExpr (Ident (TemplateKey k)) = context' k [] x
197+
198+ applyExpr (Call (TemplateKey k) args) = do
199+ args' <- mapM (\e -> applyStringExpr (typeMsg e) e) args
200+ context' k args' x
201+ where
202+ typeMsg e = "argument '" ++ show e ++ "'"
203+
204+ applyExpr (StringLiteral s) = return (StringField s)
205+
206+ ----------------------------------------------------------------------------
207+
208+ applyStringExpr :: String -> TemplateExpr -> Compiler String
209+ applyStringExpr msg expr =
210+ applyExpr expr >>= getString
211+ where
212+ getString EmptyField = expected "string" "boolean" msg
213+ getString (StringField s) = return s
214+ getString (ListField _ _) = expected "string" "list" msg
215+
216+ expected typ act expr = fail $ unwords ["Hakyll.Web.Template.applyTemplate:",
217+ "expected", typ, "but got", act, "for", expr]
218+
219+ -- expected to never happen with all templates constructed by 'template'
220+ trimError = fail $
221+ "Hakyll.Web.Template.applyTemplate: template not fully trimmed."
222+
223+
224+--------------------------------------------------------------------------------
225+-- | The following pattern is so common:
226+--
227+-- > tpl <- loadBody "templates/foo.html"
228+-- > someCompiler
229+-- > >>= applyTemplate tpl context
230+--
231+-- That we have a single function which does this:
232+--
233+-- > someCompiler
234+-- > >>= loadAndApplyTemplate "templates/foo.html" context
235+loadAndApplyTemplate :: Identifier -- ^ Template identifier
236+ -> Context a -- ^ Context
237+ -> Item a -- ^ Page
238+ -> Compiler (Item String) -- ^ Resulting item
239+loadAndApplyTemplate identifier context item = do
240+ tpl <- loadBody identifier
241+ applyTemplate tpl context item
242+
243+
244+--------------------------------------------------------------------------------
245+-- | It is also possible that you want to substitute @$key$@s within the body of
246+-- an item. This function does that by interpreting the item body as a template,
247+-- and then applying it to itself.
248+applyAsTemplate :: Context String -- ^ Context
249+ -> Item String -- ^ Item and template
250+ -> Compiler (Item String) -- ^ Resulting item
251+applyAsTemplate context item = do
252+ tpl <- compileTemplateItem item
253+ applyTemplate tpl context item
254+
255+
256+--------------------------------------------------------------------------------
257+unsafeReadTemplateFile :: FilePath -> Compiler Template
258+unsafeReadTemplateFile file = do
259+ tpl <- unsafeCompiler $ readFile file
260+ compileTemplateFile (fromFilePath file) tpl
261+{-# DEPRECATED unsafeReadTemplateFile "Use templateCompiler" #-}
lib/Hakyll/Web/Template/Internal/Element.hsnew file+291
@@ -0,0 +1,291 @@
1+--------------------------------------------------------------------------------
2+-- | Module containing the elements used in a template. A template is generally
3+-- just a list of these elements.
4+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5+module Hakyll.Web.Template.Internal.Element
6+ ( TemplateKey (..)
7+ , TemplateExpr (..)
8+ , TemplateElement (..)
9+ , templateElems
10+ , parseTemplateElemsFile
11+ ) where
12+
13+
14+--------------------------------------------------------------------------------
15+import Control.Applicative ((<|>), (<*))
16+import Control.Monad (void)
17+import Control.Arrow (left)
18+import Data.Binary (Binary, get, getWord8, put, putWord8)
19+import Data.List (intercalate)
20+import Data.Maybe (isJust)
21+import Data.Typeable (Typeable)
22+import GHC.Exts (IsString (..))
23+import qualified Text.Parsec as P
24+import qualified Text.Parsec.String as P
25+
26+
27+--------------------------------------------------------------------------------
28+import Hakyll.Core.Util.Parser
29+
30+
31+--------------------------------------------------------------------------------
32+newtype TemplateKey = TemplateKey String
33+ deriving (Binary, Show, Eq, Typeable)
34+
35+
36+--------------------------------------------------------------------------------
37+instance IsString TemplateKey where
38+ fromString = TemplateKey
39+
40+
41+--------------------------------------------------------------------------------
42+-- | Elements of a template.
43+data TemplateElement
44+ = Chunk String
45+ | Expr TemplateExpr
46+ | Escaped
47+ -- expr, then, else
48+ | If TemplateExpr [TemplateElement] (Maybe [TemplateElement])
49+ -- expr, body, separator
50+ | For TemplateExpr [TemplateElement] (Maybe [TemplateElement])
51+ -- filename
52+ | Partial TemplateExpr
53+ | TrimL
54+ | TrimR
55+ deriving (Show, Eq, Typeable)
56+
57+
58+--------------------------------------------------------------------------------
59+instance Binary TemplateElement where
60+ put (Chunk string) = putWord8 0 >> put string
61+ put (Expr e) = putWord8 1 >> put e
62+ put Escaped = putWord8 2
63+ put (If e t f) = putWord8 3 >> put e >> put t >> put f
64+ put (For e b s) = putWord8 4 >> put e >> put b >> put s
65+ put (Partial e) = putWord8 5 >> put e
66+ put TrimL = putWord8 6
67+ put TrimR = putWord8 7
68+
69+ get = getWord8 >>= \tag -> case tag of
70+ 0 -> Chunk <$> get
71+ 1 -> Expr <$> get
72+ 2 -> pure Escaped
73+ 3 -> If <$> get <*> get <*> get
74+ 4 -> For <$> get <*> get <*> get
75+ 5 -> Partial <$> get
76+ 6 -> pure TrimL
77+ 7 -> pure TrimR
78+ _ -> error "Hakyll.Web.Template.Internal: Error reading cached template"
79+
80+
81+--------------------------------------------------------------------------------
82+-- | Expression in a template
83+data TemplateExpr
84+ = Ident TemplateKey
85+ | Call TemplateKey [TemplateExpr]
86+ | StringLiteral String
87+ deriving (Eq, Typeable)
88+
89+
90+--------------------------------------------------------------------------------
91+instance Show TemplateExpr where
92+ show (Ident (TemplateKey k)) = k
93+ show (Call (TemplateKey k) as) =
94+ k ++ "(" ++ intercalate ", " (map show as) ++ ")"
95+ show (StringLiteral s) = show s
96+
97+
98+--------------------------------------------------------------------------------
99+instance Binary TemplateExpr where
100+ put (Ident k) = putWord8 0 >> put k
101+ put (Call k as) = putWord8 1 >> put k >> put as
102+ put (StringLiteral s) = putWord8 2 >> put s
103+
104+ get = getWord8 >>= \tag -> case tag of
105+ 0 -> Ident <$> get
106+ 1 -> Call <$> get <*> get
107+ 2 -> StringLiteral <$> get
108+ _ -> error "Hakyll.Web.Template.Internal: Error reading cached template"
109+
110+--------------------------------------------------------------------------------
111+parseTemplateElemsFile :: FilePath -> String -> Either String [TemplateElement]
112+parseTemplateElemsFile file = left (\e -> "Cannot parse template " ++ show e)
113+ . P.parse (templateElems <* P.eof) file
114+
115+
116+--------------------------------------------------------------------------------
117+templateElems :: P.Parser [TemplateElement]
118+templateElems = mconcat <$> P.many (P.choice [ lift chunk
119+ , lift escaped
120+ , conditional
121+ , for
122+ , partial
123+ , expr
124+ ])
125+ where lift = fmap (:[])
126+
127+
128+--------------------------------------------------------------------------------
129+chunk :: P.Parser TemplateElement
130+chunk = Chunk <$> P.many1 (P.noneOf "$")
131+
132+
133+--------------------------------------------------------------------------------
134+expr :: P.Parser [TemplateElement]
135+expr = P.try $ do
136+ trimLExpr <- trimOpen
137+ e <- expr'
138+ trimRExpr <- trimClose
139+ return $ [TrimL | trimLExpr] ++ [Expr e] ++ [TrimR | trimRExpr]
140+
141+
142+--------------------------------------------------------------------------------
143+expr' :: P.Parser TemplateExpr
144+expr' = stringLiteral <|> call <|> ident
145+
146+
147+--------------------------------------------------------------------------------
148+escaped :: P.Parser TemplateElement
149+escaped = Escaped <$ P.try (P.string "$$")
150+
151+
152+--------------------------------------------------------------------------------
153+trimOpen :: P.Parser Bool
154+trimOpen = do
155+ void $ P.char '$'
156+ trimLIf <- P.optionMaybe $ P.try (P.char '-')
157+ pure $ isJust trimLIf
158+
159+
160+--------------------------------------------------------------------------------
161+trimClose :: P.Parser Bool
162+trimClose = do
163+ trimIfR <- P.optionMaybe $ (P.char '-')
164+ void $ P.char '$'
165+ pure $ isJust trimIfR
166+
167+
168+--------------------------------------------------------------------------------
169+conditional :: P.Parser [TemplateElement]
170+conditional = P.try $ do
171+ -- if
172+ trimLIf <- trimOpen
173+ void $ P.string "if("
174+ e <- expr'
175+ void $ P.char ')'
176+ trimRIf <- trimClose
177+ -- then
178+ thenBranch <- templateElems
179+ -- else
180+ elseParse <- opt "else"
181+ -- endif
182+ trimLEnd <- trimOpen
183+ void $ P.string "endif"
184+ trimREnd <- trimClose
185+
186+ -- As else is optional we need to sort out where any Trim_s need to go.
187+ let (thenBody, elseBody) = maybe (thenNoElse, Nothing) thenElse elseParse
188+ where thenNoElse =
189+ [TrimR | trimRIf] ++ thenBranch ++ [TrimL | trimLEnd]
190+
191+ thenElse (trimLElse, elseBranch, trimRElse) = (thenB, elseB)
192+ where thenB = [TrimR | trimRIf]
193+ ++ thenBranch
194+ ++ [TrimL | trimLElse]
195+
196+ elseB = Just $ [TrimR | trimRElse]
197+ ++ elseBranch
198+ ++ [TrimL | trimLEnd]
199+
200+ pure $ [TrimL | trimLIf] ++ [If e thenBody elseBody] ++ [TrimR | trimREnd]
201+
202+
203+--------------------------------------------------------------------------------
204+for :: P.Parser [TemplateElement]
205+for = P.try $ do
206+ -- for
207+ trimLFor <- trimOpen
208+ void $ P.string "for("
209+ e <- expr'
210+ void $ P.char ')'
211+ trimRFor <- trimClose
212+ -- body
213+ bodyBranch <- templateElems
214+ -- sep
215+ sepParse <- opt "sep"
216+ -- endfor
217+ trimLEnd <- trimOpen
218+ void $ P.string "endfor"
219+ trimREnd <- trimClose
220+
221+ -- As sep is optional we need to sort out where any Trim_s need to go.
222+ let (forBody, sepBody) = maybe (forNoSep, Nothing) forSep sepParse
223+ where forNoSep =
224+ [TrimR | trimRFor] ++ bodyBranch ++ [TrimL | trimLEnd]
225+
226+ forSep (trimLSep, sepBranch, trimRSep) = (forB, sepB)
227+ where forB = [TrimR | trimRFor]
228+ ++ bodyBranch
229+ ++ [TrimL | trimLSep]
230+
231+ sepB = Just $ [TrimR | trimRSep]
232+ ++ sepBranch
233+ ++ [TrimL | trimLEnd]
234+
235+ pure $ [TrimL | trimLFor] ++ [For e forBody sepBody] ++ [TrimR | trimREnd]
236+
237+
238+--------------------------------------------------------------------------------
239+partial :: P.Parser [TemplateElement]
240+partial = P.try $ do
241+ trimLPart <- trimOpen
242+ void $ P.string "partial("
243+ e <- expr'
244+ void $ P.char ')'
245+ trimRPart <- trimClose
246+
247+ pure $ [TrimL | trimLPart] ++ [Partial e] ++ [TrimR | trimRPart]
248+
249+
250+--------------------------------------------------------------------------------
251+ident :: P.Parser TemplateExpr
252+ident = P.try $ Ident <$> key
253+
254+
255+--------------------------------------------------------------------------------
256+call :: P.Parser TemplateExpr
257+call = P.try $ do
258+ f <- key
259+ void $ P.char '('
260+ P.spaces
261+ as <- P.sepBy expr' (P.spaces >> P.char ',' >> P.spaces)
262+ P.spaces
263+ void $ P.char ')'
264+ return $ Call f as
265+
266+
267+--------------------------------------------------------------------------------
268+stringLiteral :: P.Parser TemplateExpr
269+stringLiteral = do
270+ void $ P.char '\"'
271+ str <- P.many $ do
272+ x <- P.noneOf "\""
273+ if x == '\\' then P.anyChar else return x
274+ void $ P.char '\"'
275+ return $ StringLiteral str
276+
277+
278+--------------------------------------------------------------------------------
279+key :: P.Parser TemplateKey
280+key = TemplateKey <$> metadataKey
281+
282+
283+--------------------------------------------------------------------------------
284+opt :: String -> P.Parser (Maybe (Bool, [TemplateElement], Bool))
285+opt clause = P.optionMaybe $ P.try $ do
286+ trimL <- trimOpen
287+ void $ P.string clause
288+ trimR <- trimClose
289+ branch <- templateElems
290+ pure (trimL, branch, trimR)
291+
lib/Hakyll/Web/Template/Internal/Trim.hsnew file+95
@@ -0,0 +1,95 @@
1+--------------------------------------------------------------------------------
2+-- | Module for trimming whitespace from tempaltes.
3+module Hakyll.Web.Template.Internal.Trim
4+ ( trim
5+ ) where
6+
7+
8+--------------------------------------------------------------------------------
9+import Data.Char (isSpace)
10+import Data.List (dropWhileEnd)
11+
12+
13+--------------------------------------------------------------------------------
14+import Hakyll.Web.Template.Internal.Element
15+
16+
17+--------------------------------------------------------------------------------
18+trim :: [TemplateElement] -> [TemplateElement]
19+trim = cleanse . canonicalize
20+
21+
22+--------------------------------------------------------------------------------
23+-- | Apply the Trim nodes to the Chunks.
24+cleanse :: [TemplateElement] -> [TemplateElement]
25+cleanse = recurse cleanse . process
26+ where process [] = []
27+ process (TrimR:Chunk str:ts) = let str' = dropWhile isSpace str
28+ in if null str'
29+ then process ts
30+ -- Might need to TrimL.
31+ else process $ Chunk str':ts
32+
33+ process (Chunk str:TrimL:ts) = let str' = dropWhileEnd isSpace str
34+ in if null str'
35+ then process ts
36+ else Chunk str':process ts
37+
38+ process (t:ts) = t:process ts
39+
40+--------------------------------------------------------------------------------
41+-- | Enforce the invariant that:
42+--
43+-- * Every 'TrimL' has a 'Chunk' to its left.
44+-- * Every 'TrimR' has a 'Chunk' to its right.
45+--
46+canonicalize :: [TemplateElement] -> [TemplateElement]
47+canonicalize = go
48+ where go t = let t' = redundant . swap $ dedupe t
49+ in if t == t' then t else go t'
50+
51+
52+--------------------------------------------------------------------------------
53+-- | Remove the 'TrimR' and 'TrimL's that are no-ops.
54+redundant :: [TemplateElement] -> [TemplateElement]
55+redundant = recurse redundant . process
56+ where -- Remove the leading 'TrimL's.
57+ process (TrimL:ts) = process ts
58+ -- Remove trailing 'TrimR's.
59+ process ts = foldr trailing [] ts
60+ where trailing TrimR [] = []
61+ trailing x xs = x:xs
62+
63+
64+--------------------------------------------------------------------------------
65+-- >>> swap $ [TrimR, TrimL]
66+-- [TrimL, TrimR]
67+swap :: [TemplateElement] -> [TemplateElement]
68+swap = recurse swap . process
69+ where process [] = []
70+ process (TrimR:TrimL:ts) = TrimL:process (TrimR:ts)
71+ process (t:ts) = t:process ts
72+
73+
74+--------------------------------------------------------------------------------
75+-- | Remove 'TrimR' and 'TrimL' duplication.
76+dedupe :: [TemplateElement] -> [TemplateElement]
77+dedupe = recurse dedupe . process
78+ where process [] = []
79+ process (TrimR:TrimR:ts) = process (TrimR:ts)
80+ process (TrimL:TrimL:ts) = process (TrimL:ts)
81+ process (t:ts) = t:process ts
82+
83+
84+--------------------------------------------------------------------------------
85+-- | @'recurse' f t@ applies f to every '[TemplateElement]' in t.
86+recurse :: ([TemplateElement] -> [TemplateElement])
87+ -> [TemplateElement]
88+ -> [TemplateElement]
89+recurse _ [] = []
90+recurse f (x:xs) = process x:recurse f xs
91+ where process y = case y of
92+ If e tb eb -> If e (f tb) (f <$> eb)
93+ For e t s -> For e (f t) (f <$> s)
94+ _ -> y
95+
lib/Hakyll/Web/Template/List.hsnew file+91
@@ -0,0 +1,91 @@
1+--------------------------------------------------------------------------------
2+-- | Provides an easy way to combine several items in a list. The applications
3+-- are obvious:
4+--
5+-- * A post list on a blog
6+--
7+-- * An image list in a gallery
8+--
9+-- * A sitemap
10+{-# LANGUAGE TupleSections #-}
11+module Hakyll.Web.Template.List
12+ ( applyTemplateList
13+ , applyJoinTemplateList
14+ , chronological
15+ , recentFirst
16+ , sortChronological
17+ , sortRecentFirst
18+ ) where
19+
20+
21+--------------------------------------------------------------------------------
22+import Control.Monad (liftM)
23+import Data.List (intersperse, sortBy)
24+import Data.Ord (comparing)
25+import Data.Time.Locale.Compat (defaultTimeLocale)
26+
27+
28+--------------------------------------------------------------------------------
29+import Hakyll.Core.Compiler
30+import Hakyll.Core.Identifier
31+import Hakyll.Core.Item
32+import Hakyll.Core.Metadata
33+import Hakyll.Web.Template
34+import Hakyll.Web.Template.Context
35+
36+
37+--------------------------------------------------------------------------------
38+-- | Generate a string of a listing of pages, after applying a template to each
39+-- page.
40+applyTemplateList :: Template
41+ -> Context a
42+ -> [Item a]
43+ -> Compiler String
44+applyTemplateList = applyJoinTemplateList ""
45+
46+
47+--------------------------------------------------------------------------------
48+-- | Join a listing of pages with a string in between, after applying a template
49+-- to each page.
50+applyJoinTemplateList :: String
51+ -> Template
52+ -> Context a
53+ -> [Item a]
54+ -> Compiler String
55+applyJoinTemplateList delimiter tpl context items = do
56+ items' <- mapM (applyTemplate tpl context) items
57+ return $ concat $ intersperse delimiter $ map itemBody items'
58+
59+
60+--------------------------------------------------------------------------------
61+-- | Sort pages chronologically. Uses the same method as 'dateField' for
62+-- extracting the date.
63+chronological :: MonadMetadata m => [Item a] -> m [Item a]
64+chronological =
65+ sortByM $ getItemUTC defaultTimeLocale . itemIdentifier
66+ where
67+ sortByM :: (Monad m, Ord k) => (a -> m k) -> [a] -> m [a]
68+ sortByM f xs = liftM (map fst . sortBy (comparing snd)) $
69+ mapM (\x -> liftM (x,) (f x)) xs
70+
71+
72+--------------------------------------------------------------------------------
73+-- | The reverse of 'chronological'
74+recentFirst :: MonadMetadata m => [Item a] -> m [Item a]
75+recentFirst = liftM reverse . chronological
76+
77+
78+--------------------------------------------------------------------------------
79+-- | Version of 'chronological' which doesn't need the actual items.
80+sortChronological
81+ :: MonadMetadata m => [Identifier] -> m [Identifier]
82+sortChronological ids =
83+ liftM (map itemIdentifier) $ chronological [Item i () | i <- ids]
84+
85+
86+--------------------------------------------------------------------------------
87+-- | Version of 'recentFirst' which doesn't need the actual items.
88+sortRecentFirst
89+ :: MonadMetadata m => [Identifier] -> m [Identifier]
90+sortRecentFirst ids =
91+ liftM (map itemIdentifier) $ recentFirst [Item i () | i <- ids]
src/Hakyll/Core/Compiler/Internal.hsdeleted−226
@@ -1,226 +0,0 @@
1−--------------------------------------------------------------------------------
2−-- | Internally used compiler module
3−{-# LANGUAGE GADTs #-}
4−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5−module Hakyll.Core.Compiler.Internal
6− ( -- * Types
7− CompilerRead (..)
8− , CompilerWrite (..)
9− , CompilerResult (..)
10− , Compiler (..)
11− , runCompiler
12−
13− -- * Core operations
14− , compilerTell
15− , compilerAsk
16− , compilerThrow
17− , compilerCatch
18− , compilerResult
19− , compilerUnsafeIO
20−
21− -- * Utilities
22− , compilerTellDependencies
23− , compilerTellCacheHits
24− ) where
25−
26−
27−--------------------------------------------------------------------------------
28−import Control.Applicative (Alternative (..),
29− Applicative (..), (<$>))
30−import Control.Exception (SomeException, handle)
31−import Data.Monoid (Monoid (..))
32−import Data.Set (Set)
33−import qualified Data.Set as S
34−
35−
36−--------------------------------------------------------------------------------
37−import Hakyll.Core.Dependencies
38−import Hakyll.Core.Identifier
39−import Hakyll.Core.Identifier.Pattern
40−import Hakyll.Core.Logger
41−import Hakyll.Core.Metadata
42−import Hakyll.Core.Provider
43−import Hakyll.Core.Routes
44−import Hakyll.Core.Store
45−
46−
47−--------------------------------------------------------------------------------
48−-- | Environment in which a compiler runs
49−data CompilerRead = CompilerRead
50− { -- | Underlying identifier
51− compilerUnderlying :: Identifier
52− , -- | Resource provider
53− compilerProvider :: Provider
54− , -- | List of all known identifiers
55− compilerUniverse :: Set Identifier
56− , -- | Site routes
57− compilerRoutes :: Routes
58− , -- | Compiler store
59− compilerStore :: Store
60− , -- | Logger
61− compilerLogger :: Logger
62− }
63−
64−
65−--------------------------------------------------------------------------------
66−data CompilerWrite = CompilerWrite
67− { compilerDependencies :: [Dependency]
68− , compilerCacheHits :: Int
69− } deriving (Show)
70−
71−
72−--------------------------------------------------------------------------------
73−instance Monoid CompilerWrite where
74− mempty = CompilerWrite [] 0
75− mappend (CompilerWrite d1 h1) (CompilerWrite d2 h2) =
76− CompilerWrite (d1 ++ d2) (h1 + h2)
77−
78−
79−--------------------------------------------------------------------------------
80−data CompilerResult a where
81− CompilerDone :: a -> CompilerWrite -> CompilerResult a
82− CompilerError :: String -> CompilerResult a
83− CompilerRequire :: Identifier -> Compiler a -> CompilerResult a
84−
85−
86−--------------------------------------------------------------------------------
87−-- | A monad which lets you compile items and takes care of dependency tracking
88−-- for you.
89−newtype Compiler a = Compiler
90− { unCompiler :: CompilerRead -> IO (CompilerResult a)
91− }
92−
93−
94−--------------------------------------------------------------------------------
95−instance Functor Compiler where
96− fmap f (Compiler c) = Compiler $ \r -> do
97− res <- c r
98− return $ case res of
99− CompilerDone x w -> CompilerDone (f x) w
100− CompilerError e -> CompilerError e
101− CompilerRequire i c' -> CompilerRequire i (fmap f c')
102− {-# INLINE fmap #-}
103−
104−
105−--------------------------------------------------------------------------------
106−instance Monad Compiler where
107− return x = Compiler $ \_ -> return $ CompilerDone x mempty
108− {-# INLINE return #-}
109−
110− Compiler c >>= f = Compiler $ \r -> do
111− res <- c r
112− case res of
113− CompilerDone x w -> do
114− res' <- unCompiler (f x) r
115− return $ case res' of
116− CompilerDone y w' -> CompilerDone y (w `mappend` w')
117− CompilerError e -> CompilerError e
118− CompilerRequire i c' -> CompilerRequire i $ do
119− compilerTell w -- Save dependencies!
120− c'
121−
122− CompilerError e -> return $ CompilerError e
123− CompilerRequire i c' -> return $ CompilerRequire i $ c' >>= f
124− {-# INLINE (>>=) #-}
125−
126−
127−--------------------------------------------------------------------------------
128−instance Applicative Compiler where
129− pure x = return x
130− {-# INLINE pure #-}
131−
132− f <*> x = f >>= \f' -> fmap f' x
133− {-# INLINE (<*>) #-}
134−
135−
136−--------------------------------------------------------------------------------
137−instance MonadMetadata Compiler where
138− getMetadata = compilerGetMetadata
139− getMatches = compilerGetMatches
140−
141−
142−--------------------------------------------------------------------------------
143−runCompiler :: Compiler a -> CompilerRead -> IO (CompilerResult a)
144−runCompiler compiler read' = handle handler $ unCompiler compiler read'
145− where
146− handler :: SomeException -> IO (CompilerResult a)
147− handler e = return $ CompilerError $ show e
148−
149−
150−--------------------------------------------------------------------------------
151−instance Alternative Compiler where
152− empty = compilerThrow "Hakyll.Core.Compiler.Internal: empty alternative"
153− x <|> y = compilerCatch x (\_ -> y)
154− {-# INLINE (<|>) #-}
155−
156−
157−--------------------------------------------------------------------------------
158−compilerAsk :: Compiler CompilerRead
159−compilerAsk = Compiler $ \r -> return $ CompilerDone r mempty
160−{-# INLINE compilerAsk #-}
161−
162−
163−--------------------------------------------------------------------------------
164−compilerTell :: CompilerWrite -> Compiler ()
165−compilerTell deps = Compiler $ \_ -> return $ CompilerDone () deps
166−{-# INLINE compilerTell #-}
167−
168−
169−--------------------------------------------------------------------------------
170−compilerThrow :: String -> Compiler a
171−compilerThrow e = Compiler $ \_ -> return $ CompilerError e
172−{-# INLINE compilerThrow #-}
173−
174−
175−--------------------------------------------------------------------------------
176−compilerCatch :: Compiler a -> (String -> Compiler a) -> Compiler a
177−compilerCatch (Compiler x) f = Compiler $ \r -> do
178− res <- x r
179− case res of
180− CompilerError e -> unCompiler (f e) r
181− _ -> return res
182−{-# INLINE compilerCatch #-}
183−
184−
185−--------------------------------------------------------------------------------
186−-- | Put the result back in a compiler
187−compilerResult :: CompilerResult a -> Compiler a
188−compilerResult x = Compiler $ \_ -> return x
189−{-# INLINE compilerResult #-}
190−
191−
192−--------------------------------------------------------------------------------
193−compilerUnsafeIO :: IO a -> Compiler a
194−compilerUnsafeIO io = Compiler $ \_ -> do
195− x <- io
196− return $ CompilerDone x mempty
197−{-# INLINE compilerUnsafeIO #-}
198−
199−
200−--------------------------------------------------------------------------------
201−compilerTellDependencies :: [Dependency] -> Compiler ()
202−compilerTellDependencies ds = compilerTell mempty {compilerDependencies = ds}
203−{-# INLINE compilerTellDependencies #-}
204−
205−
206−--------------------------------------------------------------------------------
207−compilerTellCacheHits :: Int -> Compiler ()
208−compilerTellCacheHits ch = compilerTell mempty {compilerCacheHits = ch}
209−{-# INLINE compilerTellCacheHits #-}
210−
211−
212−--------------------------------------------------------------------------------
213−compilerGetMetadata :: Identifier -> Compiler Metadata
214−compilerGetMetadata identifier = do
215− provider <- compilerProvider <$> compilerAsk
216− compilerTellDependencies [IdentifierDependency identifier]
217− compilerUnsafeIO $ resourceMetadata provider identifier
218−
219−
220−--------------------------------------------------------------------------------
221−compilerGetMatches :: Pattern -> Compiler [Identifier]
222−compilerGetMatches pattern = do
223− universe <- compilerUniverse <$> compilerAsk
224− let matching = filterMatches pattern $ S.toList universe
225− compilerTellDependencies [PatternDependency pattern matching]
226− return matching
src/Hakyll/Core/Metadata.hsdeleted−32
@@ -1,32 +0,0 @@
1−--------------------------------------------------------------------------------
2−module Hakyll.Core.Metadata
3− ( Metadata
4− , MonadMetadata (..)
5− ) where
6−
7−
8−--------------------------------------------------------------------------------
9−import Control.Monad (forM)
10−import Data.Map (Map)
11−
12−
13−--------------------------------------------------------------------------------
14−import Hakyll.Core.Identifier
15−import Hakyll.Core.Identifier.Pattern
16−
17−
18−--------------------------------------------------------------------------------
19−type Metadata = Map String String
20−
21−
22−--------------------------------------------------------------------------------
23−class Monad m => MonadMetadata m where
24− getMetadata :: Identifier -> m Metadata
25− getMatches :: Pattern -> m [Identifier]
26−
27− getAllMetadata :: Pattern -> m [(Identifier, Metadata)]
28− getAllMetadata pattern = do
29− matches' <- getMatches pattern
30− forM matches' $ \id' -> do
31− metadata <- getMetadata id'
32− return (id', metadata)
src/Hakyll/Core/Provider.hsdeleted−46
@@ -1,46 +0,0 @@
1−--------------------------------------------------------------------------------
2−-- | This module provides an wrapper API around the file system which does some
3−-- caching.
4−module Hakyll.Core.Provider
5− ( -- * Constructing resource providers
6− Provider
7− , newProvider
8−
9− -- * Querying resource properties
10− , resourceList
11− , resourceExists
12− , resourceModified
13− , resourceModificationTime
14−
15− -- * Access to raw resource content
16− , resourceString
17− , resourceLBS
18−
19− -- * Access to metadata and body content
20− , resourceMetadata
21− , resourceBody
22− ) where
23−
24−
25−--------------------------------------------------------------------------------
26−import Hakyll.Core.Identifier
27−import Hakyll.Core.Metadata
28−import Hakyll.Core.Provider.Internal
29−import qualified Hakyll.Core.Provider.MetadataCache as Internal
30−import Hakyll.Core.Provider.Modified
31−
32−
33−--------------------------------------------------------------------------------
34−-- | Wrapper to ensure metadata cache is invalidated if necessary
35−resourceMetadata :: Provider -> Identifier -> IO Metadata
36−resourceMetadata rp r = do
37− _ <- resourceModified rp r
38− Internal.resourceMetadata rp r
39−
40−
41−--------------------------------------------------------------------------------
42−-- | Wrapper to ensure metadata cache is invalidated if necessary
43−resourceBody :: Provider -> Identifier -> IO String
44−resourceBody rp r = do
45− _ <- resourceModified rp r
46− Internal.resourceBody rp r
src/Hakyll/Core/Provider/Internal.hsdeleted−94
@@ -1,94 +0,0 @@
1−--------------------------------------------------------------------------------
2−module Hakyll.Core.Provider.Internal
3− ( Provider (..)
4− , newProvider
5−
6− , resourceList
7− , resourceExists
8− , resourceMetadataResource
9−
10− , resourceFilePath
11− , resourceString
12− , resourceLBS
13− ) where
14−
15−
16−--------------------------------------------------------------------------------
17−import Control.Applicative ((<$>))
18−import qualified Data.ByteString.Lazy as BL
19−import Data.IORef
20−import Data.Map (Map)
21−import qualified Data.Map as M
22−import Data.Set (Set)
23−import qualified Data.Set as S
24−import System.FilePath (addExtension, (</>))
25−
26−
27−--------------------------------------------------------------------------------
28−import Hakyll.Core.Identifier
29−import Hakyll.Core.Store
30−import Hakyll.Core.Util.File
31−
32−
33−--------------------------------------------------------------------------------
34−-- | Responsible for retrieving and listing resources
35−data Provider = Provider
36− { -- Top of the provided directory
37− providerDirectory :: FilePath
38− , -- | A list of all files found
39− providerSet :: Set Identifier
40− , -- | Cache keeping track of modified files
41− providerModifiedCache :: IORef (Map Identifier Bool)
42− , -- | Underlying persistent store for caching
43− providerStore :: Store
44− }
45−
46−
47−--------------------------------------------------------------------------------
48−-- | Create a resource provider
49−newProvider :: Store -- ^ Store to use
50− -> (FilePath -> Bool) -- ^ Should we ignore this file?
51− -> FilePath -- ^ Search directory
52− -> IO Provider -- ^ Resulting provider
53−newProvider store ignore directory = do
54− list <- map fromFilePath . filter (not . ignore) <$>
55− getRecursiveContents directory
56− cache <- newIORef M.empty
57− return $ Provider directory (S.fromList list) cache store
58−
59−
60−--------------------------------------------------------------------------------
61−resourceList :: Provider -> [Identifier]
62−resourceList = S.toList . providerSet
63−
64−
65−--------------------------------------------------------------------------------
66−-- | Check if a given resource exists
67−resourceExists :: Provider -> Identifier -> Bool
68−resourceExists provider =
69− (`S.member` providerSet provider) . setVersion Nothing
70−
71−
72−--------------------------------------------------------------------------------
73−-- | Each resource may have an associated metadata resource (with a @.metadata@
74−-- filename)
75−resourceMetadataResource :: Identifier -> Identifier
76−resourceMetadataResource =
77− fromFilePath . flip addExtension "metadata" . toFilePath
78−
79−
80−--------------------------------------------------------------------------------
81−resourceFilePath :: Provider -> Identifier -> FilePath
82−resourceFilePath p i = providerDirectory p </> toFilePath i
83−
84−
85−--------------------------------------------------------------------------------
86−-- | Get the raw body of a resource as string
87−resourceString :: Provider -> Identifier -> IO String
88−resourceString p i = readFile $ resourceFilePath p i
89−
90−
91−--------------------------------------------------------------------------------
92−-- | Get the raw body of a resource of a lazy bytestring
93−resourceLBS :: Provider -> Identifier -> IO BL.ByteString
94−resourceLBS p i = BL.readFile $ resourceFilePath p i
src/Hakyll/Core/Provider/Metadata.hsdeleted−125
@@ -1,125 +0,0 @@
1−--------------------------------------------------------------------------------
2−-- | Internal module to parse metadata
3−module Hakyll.Core.Provider.Metadata
4− ( loadMetadata
5− ) where
6−
7−
8−--------------------------------------------------------------------------------
9−import Control.Applicative
10−import Control.Arrow (second)
11−import qualified Data.ByteString.Char8 as BC
12−import qualified Data.Map as M
13−import System.IO as IO
14−import Text.Parsec ((<?>))
15−import qualified Text.Parsec as P
16−import Text.Parsec.String (Parser)
17−
18−
19−--------------------------------------------------------------------------------
20−import Hakyll.Core.Identifier
21−import Hakyll.Core.Metadata
22−import Hakyll.Core.Provider.Internal
23−import Hakyll.Core.Util.String
24−
25−
26−--------------------------------------------------------------------------------
27−loadMetadata :: Provider -> Identifier -> IO (Metadata, Maybe String)
28−loadMetadata p identifier = do
29− hasHeader <- probablyHasMetadataHeader fp
30− (md, body) <- if hasHeader
31− then second Just <$> loadMetadataHeader fp
32− else return (M.empty, Nothing)
33−
34− emd <- if resourceExists p mi then loadMetadataFile mfp else return M.empty
35−
36− return (M.union md emd, body)
37− where
38− fp = resourceFilePath p identifier
39− mi = resourceMetadataResource identifier
40− mfp = resourceFilePath p mi
41−
42−
43−--------------------------------------------------------------------------------
44−loadMetadataHeader :: FilePath -> IO (Metadata, String)
45−loadMetadataHeader fp = do
46− contents <- readFile fp
47− case P.parse page fp contents of
48− Left err -> error (show err)
49− Right (md, b) -> return (M.fromList md, b)
50−
51−
52−--------------------------------------------------------------------------------
53−loadMetadataFile :: FilePath -> IO Metadata
54−loadMetadataFile fp = do
55− contents <- readFile fp
56− case P.parse metadata fp contents of
57− Left err -> error (show err)
58− Right md -> return $ M.fromList md
59−
60−
61−--------------------------------------------------------------------------------
62−-- | Check if a file "probably" has a metadata header. The main goal of this is
63−-- to exclude binary files (which are unlikely to start with "---").
64−probablyHasMetadataHeader :: FilePath -> IO Bool
65−probablyHasMetadataHeader fp = do
66− handle <- IO.openFile fp IO.ReadMode
67− bs <- BC.hGet handle 1024
68− IO.hClose handle
69− return $ isMetadataHeader bs
70− where
71− isMetadataHeader bs =
72− let pre = BC.takeWhile (\x -> x /= '\n' && x /= '\r') bs
73− in BC.length pre >= 3 && BC.all (== '-') pre
74−
75−
76−--------------------------------------------------------------------------------
77−-- | Space or tab, no newline
78−inlineSpace :: Parser Char
79−inlineSpace = P.oneOf ['\t', ' '] <?> "space"
80−
81−
82−--------------------------------------------------------------------------------
83−-- | Parse Windows newlines as well (i.e. "\n" or "\r\n")
84−newline :: Parser String
85−newline = P.string "\n" <|> P.string "\r\n"
86−
87−
88−--------------------------------------------------------------------------------
89−-- | Parse a single metadata field
90−metadataField :: Parser (String, String)
91−metadataField = do
92− key <- P.manyTill P.alphaNum $ P.char ':'
93− P.skipMany1 inlineSpace <?> "space followed by metadata for: " ++ key
94− value <- P.manyTill P.anyChar newline
95− trailing' <- P.many trailing
96− return (key, trim $ value ++ concat trailing')
97− where
98− trailing = (++) <$> P.many1 inlineSpace <*> P.manyTill P.anyChar newline
99−
100−
101−--------------------------------------------------------------------------------
102−-- | Parse a metadata block
103−metadata :: Parser [(String, String)]
104−metadata = P.many metadataField
105−
106−
107−--------------------------------------------------------------------------------
108−-- | Parse a metadata block, including delimiters and trailing newlines
109−metadataBlock :: Parser [(String, String)]
110−metadataBlock = do
111− open <- P.many1 (P.char '-') <* P.many inlineSpace <* newline
112− metadata' <- metadata
113− _ <- P.choice $ map (P.string . replicate (length open)) ['-', '.']
114− P.skipMany inlineSpace
115− P.skipMany1 newline
116− return metadata'
117−
118−
119−--------------------------------------------------------------------------------
120−-- | Parse a page consisting of a metadata header and a body
121−page :: Parser ([(String, String)], String)
122−page = do
123− metadata' <- P.option [] metadataBlock
124− body <- P.many P.anyChar
125− return (metadata', body)
src/Hakyll/Core/Provider/Modified.hsdeleted−101
@@ -1,101 +0,0 @@
1−--------------------------------------------------------------------------------
2−{-# LANGUAGE CPP #-}
3−module Hakyll.Core.Provider.Modified
4− ( resourceModified
5− , resourceModificationTime
6− ) where
7−
8−
9−--------------------------------------------------------------------------------
10−import Control.Applicative ((<$>), (<*>))
11−import Control.Monad (when)
12−import qualified Crypto.Hash.MD5 as MD5
13−import qualified Data.ByteString as B
14−import qualified Data.ByteString.Lazy as BL
15−import Data.IORef
16−import qualified Data.Map as M
17−import Data.Time (UTCTime)
18−import System.Directory (getModificationTime)
19−
20−
21−--------------------------------------------------------------------------------
22−#if !MIN_VERSION_directory(1,2,0)
23−import Data.Time (readTime)
24−import System.Locale (defaultTimeLocale)
25−import System.Time (formatCalendarTime,
26− toCalendarTime)
27−#endif
28−
29−
30−--------------------------------------------------------------------------------
31−import Hakyll.Core.Identifier
32−import Hakyll.Core.Provider.Internal
33−import Hakyll.Core.Provider.MetadataCache
34−import Hakyll.Core.Store (Store)
35−import qualified Hakyll.Core.Store as Store
36−
37−
38−--------------------------------------------------------------------------------
39−-- | A resource is modified if it or its metadata has changed
40−resourceModified :: Provider -> Identifier -> IO Bool
41−resourceModified p r
42− | not exists = return False
43− | otherwise = do
44− cache <- readIORef cacheRef
45− case M.lookup normalized cache of
46− Just m -> return m
47− Nothing -> do
48− -- Check if the actual file was modified, and do a recursive
49− -- call to check if the metadata file was modified
50− m <- (||)
51− <$> fileDigestModified store filePath
52− <*> resourceModified p (resourceMetadataResource r)
53− modifyIORef cacheRef (M.insert normalized m)
54−
55− -- Important! (But ugly)
56− when m $ resourceInvalidateMetadataCache p r
57−
58− return m
59− where
60− normalized = setVersion Nothing r
61− exists = resourceExists p r
62− store = providerStore p
63− cacheRef = providerModifiedCache p
64− filePath = resourceFilePath p r
65−
66−
67−--------------------------------------------------------------------------------
68−-- | Utility: Check if a the digest of a file was modified
69−fileDigestModified :: Store -> FilePath -> IO Bool
70−fileDigestModified store fp = do
71− -- Get the latest seen digest from the store, and calculate the current
72− -- digest for the
73− lastDigest <- Store.get store key
74− newDigest <- fileDigest fp
75− if Store.Found newDigest == lastDigest
76− -- All is fine, not modified
77− then return False
78− -- Resource modified; store new digest
79− else do
80− Store.set store key newDigest
81− return True
82− where
83− key = ["Hakyll.Core.Resource.Provider.fileModified", fp]
84−
85−
86−--------------------------------------------------------------------------------
87−-- | Utility: Retrieve a digest for a given file
88−fileDigest :: FilePath -> IO B.ByteString
89−fileDigest = fmap MD5.hashlazy . BL.readFile
90−
91−
92−--------------------------------------------------------------------------------
93−resourceModificationTime :: Provider -> Identifier -> IO UTCTime
94−resourceModificationTime p i = do
95−#if MIN_VERSION_directory(1,2,0)
96− getModificationTime $ resourceFilePath p i
97−#else
98− ct <- toCalendarTime =<< getModificationTime (resourceFilePath p i)
99− let str = formatCalendarTime defaultTimeLocale "%s" ct
100− return $ readTime defaultTimeLocale "%s" str
101−#endif
src/Hakyll/Core/UnixFilter.hsdeleted−120
@@ -1,120 +0,0 @@
1−--------------------------------------------------------------------------------
2−-- | A Compiler that supports unix filters.
3−module Hakyll.Core.UnixFilter
4− ( unixFilter
5− , unixFilterLBS
6− ) where
7−
8−
9−--------------------------------------------------------------------------------
10−import Control.Concurrent (forkIO)
11−import Data.ByteString.Lazy (ByteString)
12−import qualified Data.ByteString.Lazy as LB
13−import System.IO (Handle, hClose, hGetContents, hPutStr,
14− hSetEncoding, localeEncoding)
15−import System.Posix.IO (closeFd, createPipe, dupTo, fdToHandle,
16− stdInput, stdOutput)
17−import System.Posix.Process (executeFile, forkProcess)
18−
19−
20−--------------------------------------------------------------------------------
21−import Hakyll.Core.Compiler
22−
23−
24−--------------------------------------------------------------------------------
25−-- | Use a unix filter as compiler. For example, we could use the 'rev' program
26−-- as a compiler.
27−--
28−-- > rev :: Compiler String
29−-- > rev = getResourceString >>= withItemBody (unixFilter "rev" [])
30−--
31−-- A more realistic example: one can use this to call, for example, the sass
32−-- compiler on CSS files. More information about sass can be found here:
33−--
34−-- <http://sass-lang.com/>
35−--
36−-- The code is fairly straightforward, given that we use @.scss@ for sass:
37−--
38−-- > match "style.scss" $ do
39−-- > route $ setExtension "css"
40−-- > compile $ getResourceString >>=
41−-- > withItemBody (unixFilter "sass" ["-s", "--scss"]) >>=
42−-- > compressCssCompiler
43−unixFilter :: String -- ^ Program name
44− -> [String] -- ^ Program args
45− -> String -- ^ Program input
46− -> Compiler String -- ^ Program output
47−unixFilter = unixFilterWith writer reader
48− where
49− writer handle input = do
50− hSetEncoding handle localeEncoding
51− hPutStr handle input
52− reader handle = do
53− hSetEncoding handle localeEncoding
54− hGetContents handle
55−
56−
57−--------------------------------------------------------------------------------
58−-- | Variant of 'unixFilter' that should be used for binary files
59−--
60−-- > match "music.wav" $ do
61−-- > route $ setExtension "ogg"
62−-- > compile $ getResourceLBS >>= withItemBody (unixFilter "oggenc" ["-"])
63−unixFilterLBS :: String -- ^ Program name
64− -> [String] -- ^ Program args
65− -> ByteString -- ^ Program input
66− -> Compiler ByteString -- ^ Program output
67−unixFilterLBS = unixFilterWith LB.hPutStr LB.hGetContents
68−
69−
70−--------------------------------------------------------------------------------
71−-- | Overloaded compiler
72−unixFilterWith :: (Handle -> i -> IO ()) -- ^ Writer
73− -> (Handle -> IO o) -- ^ Reader
74− -> String -- ^ Program name
75− -> [String] -- ^ Program args
76− -> i -- ^ Program input
77− -> Compiler o -- ^ Program output
78−unixFilterWith writer reader programName args input = do
79− debugCompiler ("Executing external program " ++ programName)
80− unsafeCompiler $ unixFilterIO writer reader programName args input
81−
82−
83−--------------------------------------------------------------------------------
84−-- | Internally used function
85−unixFilterIO :: (Handle -> i -> IO ())
86− -> (Handle -> IO o)
87− -> String
88− -> [String]
89− -> i
90− -> IO o
91−unixFilterIO writer reader programName args input = do
92− -- Create pipes
93− (stdinRead, stdinWrite) <- createPipe
94− (stdoutRead, stdoutWrite) <- createPipe
95−
96− -- Fork the child
97− _ <- forkProcess $ do
98− -- Copy our pipes over the regular stdin/stdout
99− _ <- dupTo stdinRead stdInput
100− _ <- dupTo stdoutWrite stdOutput
101−
102− -- Close the now unneeded file descriptors in the child
103− mapM_ closeFd [stdinWrite, stdoutRead, stdinRead, stdoutWrite]
104−
105− -- Execute the program
106− _ <- executeFile programName True args Nothing
107− return ()
108−
109− -- On the parent side, close the client-side FDs.
110− mapM_ closeFd [stdinRead, stdoutWrite]
111−
112− -- Write the input to the child pipe
113− _ <- forkIO $ do
114− stdinWriteHandle <- fdToHandle stdinWrite
115− writer stdinWriteHandle input
116− hClose stdinWriteHandle
117−
118− -- Receive the output from the child
119− stdoutReadHandle <- fdToHandle stdoutRead
120− reader stdoutReadHandle
src/Hakyll/Core/Writable/CopyFile.hsdeleted−43
@@ -1,43 +0,0 @@
1−--------------------------------------------------------------------------------
2−-- | Exports simple compilers to just copy files
3−{-# LANGUAGE DeriveDataTypeable #-}
4−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5−module Hakyll.Core.Writable.CopyFile
6− ( CopyFile (..)
7− , copyFileCompiler
8− ) where
9−
10−
11−--------------------------------------------------------------------------------
12−import Data.Binary (Binary (..))
13−import Data.Typeable (Typeable)
14−import System.Directory (copyFile)
15−
16−
17−--------------------------------------------------------------------------------
18−import Hakyll.Core.Compiler
19−import Hakyll.Core.Identifier
20−import Hakyll.Core.Item
21−import Hakyll.Core.Writable
22−
23−
24−--------------------------------------------------------------------------------
25−-- | This will copy any file directly by using a system call
26−data CopyFile = CopyFile
27− deriving (Show, Eq, Ord, Typeable)
28−
29−
30−--------------------------------------------------------------------------------
31−instance Binary CopyFile where
32− put CopyFile = return ()
33− get = return CopyFile
34−
35−
36−--------------------------------------------------------------------------------
37−instance Writable CopyFile where
38− write dst item = copyFile (toFilePath $ itemIdentifier item) dst
39−
40−
41−--------------------------------------------------------------------------------
42−copyFileCompiler :: Compiler (Item CopyFile)
43−copyFileCompiler = makeItem CopyFile
src/Hakyll/Init.hsdeleted−37
@@ -1,37 +0,0 @@
1−--------------------------------------------------------------------------------
2−module Main
3− ( main
4− ) where
5−
6−
7−--------------------------------------------------------------------------------
8−import Control.Monad (forM_)
9−import System.Directory (copyFile)
10−import System.Environment (getArgs, getProgName)
11−import System.Exit (exitFailure)
12−import System.FilePath ((</>))
13−
14−
15−--------------------------------------------------------------------------------
16−import Hakyll.Core.Util.File
17−import Paths_hakyll
18−
19−
20−--------------------------------------------------------------------------------
21−main :: IO ()
22−main = do
23− progName <- getProgName
24− args <- getArgs
25− srcDir <- getDataFileName "example"
26− files <- getRecursiveContents srcDir
27−
28− case args of
29− [dstDir] -> forM_ files $ \file -> do
30− let dst = dstDir </> file
31− src = srcDir </> file
32− putStrLn $ "Creating " ++ dst
33− makeDirectories dst
34− copyFile src dst
35− _ -> do
36− putStrLn $ "Usage: " ++ progName ++ " <directory>"
37− exitFailure
src/Hakyll/Main.hsdeleted−169
@@ -1,169 +0,0 @@
1−--------------------------------------------------------------------------------
2−-- | Module providing the main hakyll function and command-line argument parsing
3−{-# LANGUAGE CPP #-}
4−module Hakyll.Main
5− ( hakyll
6− , hakyllWith
7− ) where
8−
9−
10−--------------------------------------------------------------------------------
11−import Control.Monad (when)
12−import System.Directory (doesDirectoryExist,
13− removeDirectoryRecursive)
14−import System.Environment (getArgs, getProgName)
15−import System.Process (system)
16−
17−
18−--------------------------------------------------------------------------------
19−import Hakyll.Core.Configuration
20−import Hakyll.Core.Rules
21−import Hakyll.Core.Runtime
22−
23−
24−--------------------------------------------------------------------------------
25−#ifdef PREVIEW_SERVER
26−import Control.Applicative ((<$>))
27−import Control.Concurrent (forkIO)
28−import qualified Data.Set as S
29−import Hakyll.Core.Identifier
30−import Hakyll.Core.Rules.Internal
31−import Hakyll.Web.Preview.Poll
32−import Hakyll.Web.Preview.Server
33−#endif
34−
35−
36−--------------------------------------------------------------------------------
37−-- | This usualy is the function with which the user runs the hakyll compiler
38−hakyll :: Rules a -> IO ()
39−hakyll = hakyllWith defaultConfiguration
40−
41−
42−--------------------------------------------------------------------------------
43−-- | A variant of 'hakyll' which allows the user to specify a custom
44−-- configuration
45−hakyllWith :: Configuration -> Rules a -> IO ()
46−hakyllWith conf rules = do
47− args <- getArgs
48− case args of
49− ["build"] -> build conf rules
50− ["clean"] -> clean conf
51− ["help"] -> help
52− ["preview"] -> preview conf rules 8000
53− ["preview", p] -> preview conf rules (read p)
54− ["rebuild"] -> rebuild conf rules
55− ["server"] -> server conf 8000
56− ["server", p] -> server conf (read p)
57− ["deploy"] -> deploy conf
58− _ -> help
59−
60−
61−--------------------------------------------------------------------------------
62−-- | Build the site
63−build :: Configuration -> Rules a -> IO ()
64−build conf rules = do
65− _ <- run conf rules
66− return ()
67−
68−
69−--------------------------------------------------------------------------------
70−-- | Remove the output directories
71−clean :: Configuration -> IO ()
72−clean conf = do
73− remove $ destinationDirectory conf
74− remove $ storeDirectory conf
75− where
76− remove dir = do
77− putStrLn $ "Removing " ++ dir ++ "..."
78− exists <- doesDirectoryExist dir
79− when exists $ removeDirectoryRecursive dir
80−
81−
82−--------------------------------------------------------------------------------
83−-- | Show usage information.
84−help :: IO ()
85−help = do
86− name <- getProgName
87− mapM_ putStrLn
88− [ "ABOUT"
89− , ""
90− , "This is a Hakyll site generator program. You should always"
91− , "run it from the project root directory."
92− , ""
93− , "USAGE"
94− , ""
95− , name ++ " build Generate the site"
96− , name ++ " clean Clean up and remove cache"
97− , name ++ " help Show this message"
98− , name ++ " preview [port] Run a server and autocompile"
99− , name ++ " rebuild Clean up and build again"
100− , name ++ " server [port] Run a local test server"
101− , name ++ " deploy Upload/deploy your site"
102− , ""
103− ]
104−
105−#ifndef PREVIEW_SERVER
106− previewServerDisabled
107−#endif
108−
109−
110−--------------------------------------------------------------------------------
111−-- | Preview the site
112−preview :: Configuration -> Rules a -> Int -> IO ()
113−#ifdef PREVIEW_SERVER
114−preview conf rules port = do
115− -- Fork a thread polling for changes
116− _ <- forkIO $ previewPoll conf update
117−
118− -- Run the server in the main thread
119− server conf port
120− where
121− update = map toFilePath . S.toList . rulesResources <$> run conf rules
122−#else
123−preview _ _ _ = previewServerDisabled
124−#endif
125−
126−
127−--------------------------------------------------------------------------------
128−-- | Rebuild the site
129−rebuild :: Configuration -> Rules a -> IO ()
130−rebuild conf rules = do
131− clean conf
132− build conf rules
133−
134−
135−--------------------------------------------------------------------------------
136−-- | Start a server
137−server :: Configuration -> Int -> IO ()
138−#ifdef PREVIEW_SERVER
139−server conf port = do
140− let destination = destinationDirectory conf
141− staticServer destination preServeHook port
142− where
143− preServeHook _ = return ()
144−#else
145−server _ _ = previewServerDisabled
146−#endif
147−
148−
149−--------------------------------------------------------------------------------
150−-- | Upload the site
151−deploy :: Configuration -> IO ()
152−deploy conf = do
153− _ <- system $ deployCommand conf
154− return ()
155−
156−
157−--------------------------------------------------------------------------------
158−-- | Print a warning message about the preview serving not being enabled
159−#ifndef PREVIEW_SERVER
160−previewServerDisabled :: IO ()
161−previewServerDisabled =
162− mapM_ putStrLn
163− [ "PREVIEW SERVER"
164− , ""
165− , "The preview server is not enabled in the version of Hakyll. To"
166− , "enable it, set the flag to True and recompile Hakyll."
167− , "Alternatively, use an external tool to serve your site directory."
168− ]
169−#endif
src/Hakyll/Web/CompressCss.hsdeleted−60
@@ -1,60 +0,0 @@
1−--------------------------------------------------------------------------------
2−-- | Module used for CSS compression. The compression is currently in a simple
3−-- state, but would typically reduce the number of bytes by about 25%.
4−module Hakyll.Web.CompressCss
5− ( compressCssCompiler
6− , compressCss
7− ) where
8−
9−
10−--------------------------------------------------------------------------------
11−import Control.Applicative ((<$>))
12−import Data.Char (isSpace)
13−import Data.List (isPrefixOf)
14−
15−
16−--------------------------------------------------------------------------------
17−import Hakyll.Core.Compiler
18−import Hakyll.Core.Item
19−import Hakyll.Core.Util.String
20−
21−
22−--------------------------------------------------------------------------------
23−-- | Compiler form of 'compressCss'
24−compressCssCompiler :: Compiler (Item String)
25−compressCssCompiler = fmap compressCss <$> getResourceString
26−
27−
28−--------------------------------------------------------------------------------
29−-- | Compress CSS to speed up your site.
30−compressCss :: String -> String
31−compressCss = compressSeparators . stripComments . compressWhitespace
32−
33−
34−--------------------------------------------------------------------------------
35−-- | Compresses certain forms of separators.
36−compressSeparators :: String -> String
37−compressSeparators =
38− replaceAll "; *}" (const "}") .
39− replaceAll " *([{};:]) *" (take 1 . dropWhile isSpace) .
40− replaceAll ";+" (const ";")
41−
42−
43−--------------------------------------------------------------------------------
44−-- | Compresses all whitespace.
45−compressWhitespace :: String -> String
46−compressWhitespace = replaceAll "[ \t\n\r]+" (const " ")
47−
48−
49−--------------------------------------------------------------------------------
50−-- | Function that strips CSS comments away.
51−stripComments :: String -> String
52−stripComments [] = []
53−stripComments str
54− | isPrefixOf "/*" str = stripComments $ eatComments $ drop 2 str
55− | otherwise = head str : stripComments (drop 1 str)
56− where
57− eatComments str'
58− | null str' = []
59− | isPrefixOf "*/" str' = drop 2 str'
60− | otherwise = eatComments $ drop 1 str'
src/Hakyll/Web/Pandoc.hsdeleted−131
@@ -1,131 +0,0 @@
1−--------------------------------------------------------------------------------
2−-- | Module exporting convenient pandoc bindings
3−module Hakyll.Web.Pandoc
4− ( -- * The basic building blocks
5− readPandoc
6− , readPandocWith
7− , writePandoc
8− , writePandocWith
9− , renderPandoc
10− , renderPandocWith
11−
12− -- * Derived compilers
13− , pandocCompiler
14− , pandocCompilerWith
15− , pandocCompilerWithTransform
16−
17− -- * Default options
18− , defaultHakyllParserState
19− , defaultHakyllWriterOptions
20− ) where
21−
22−
23−--------------------------------------------------------------------------------
24−import Control.Applicative ((<$>))
25−import Text.Pandoc
26−
27−
28−--------------------------------------------------------------------------------
29−import Hakyll.Core.Compiler
30−import Hakyll.Core.Item
31−import Hakyll.Web.Pandoc.FileType
32−
33−
34−--------------------------------------------------------------------------------
35−-- | Read a string using pandoc, with the default options
36−readPandoc :: Item String -- ^ String to read
37− -> Item Pandoc -- ^ Resulting document
38−readPandoc = readPandocWith defaultHakyllParserState
39−
40−
41−--------------------------------------------------------------------------------
42−-- | Read a string using pandoc, with the supplied options
43−readPandocWith :: ParserState -- ^ Parser options
44− -> Item String -- ^ String to read
45− -> Item Pandoc -- ^ Resulting document
46−readPandocWith state item = fmap (reader state (itemFileType item)) item
47− where
48− reader s t = case t of
49− Html -> readHtml s
50− LaTeX -> readLaTeX s
51− LiterateHaskell t' -> reader s {stateLiterateHaskell = True} t'
52− Markdown -> readMarkdown s
53− Rst -> readRST s
54− Textile -> readTextile s
55− _ -> error $
56− "Hakyll.Web.readPandocWith: I don't know how to read a file of the " ++
57− "type " ++ show t ++ " for: " ++ show (itemIdentifier item)
58−
59−
60−--------------------------------------------------------------------------------
61−-- | Write a document (as HTML) using pandoc, with the default options
62−writePandoc :: Item Pandoc -- ^ Document to write
63− -> Item String -- ^ Resulting HTML
64−writePandoc = writePandocWith defaultHakyllWriterOptions
65−
66−
67−--------------------------------------------------------------------------------
68−-- | Write a document (as HTML) using pandoc, with the supplied options
69−writePandocWith :: WriterOptions -- ^ Writer options for pandoc
70− -> Item Pandoc -- ^ Document to write
71− -> Item String -- ^ Resulting HTML
72−writePandocWith options = fmap $ writeHtmlString options
73−
74−
75−--------------------------------------------------------------------------------
76−-- | Render the resource using pandoc
77−renderPandoc :: Item String -> Item String
78−renderPandoc =
79− renderPandocWith defaultHakyllParserState defaultHakyllWriterOptions
80−
81−
82−--------------------------------------------------------------------------------
83−-- | Render the resource using pandoc
84−renderPandocWith :: ParserState -> WriterOptions -> Item String -> Item String
85−renderPandocWith state options = writePandocWith options . readPandocWith state
86−
87−
88−--------------------------------------------------------------------------------
89−-- | Read a page render using pandoc
90−pandocCompiler :: Compiler (Item String)
91−pandocCompiler =
92− pandocCompilerWith defaultHakyllParserState defaultHakyllWriterOptions
93−
94−
95−--------------------------------------------------------------------------------
96−-- | A version of 'pandocCompiler' which allows you to specify your own pandoc
97−-- options
98−pandocCompilerWith :: ParserState -> WriterOptions -> Compiler (Item String)
99−pandocCompilerWith state options = pandocCompilerWithTransform state options id
100−
101−
102−--------------------------------------------------------------------------------
103−-- | An extension of 'pandocCompilerWith' which allows you to specify a custom
104−-- pandoc transformation for the content
105−pandocCompilerWithTransform :: ParserState -> WriterOptions
106− -> (Pandoc -> Pandoc)
107− -> Compiler (Item String)
108−pandocCompilerWithTransform state options f = cached cacheName $
109− writePandocWith options . fmap f . readPandocWith state <$> getResourceBody
110− where
111− cacheName = "Hakyll.Web.Page.pageCompilerWithPandoc"
112−
113−
114−--------------------------------------------------------------------------------
115−-- | The default reader options for pandoc parsing in hakyll
116−defaultHakyllParserState :: ParserState
117−defaultHakyllParserState = defaultParserState
118− { -- The following option causes pandoc to read smart typography, a nice
119− -- and free bonus.
120− stateSmart = True
121− }
122−
123−
124−--------------------------------------------------------------------------------
125−-- | The default writer options for pandoc rendering in hakyll
126−defaultHakyllWriterOptions :: WriterOptions
127−defaultHakyllWriterOptions = defaultWriterOptions
128− { -- This option causes literate haskell to be written using '>' marks in
129− -- html, which I think is a good default.
130− writerLiterateHaskell = True
131− }
src/Hakyll/Web/Preview/Poll.hsdeleted−43
@@ -1,43 +0,0 @@
1−-- | Interval-based implementation of preview polling
2−--
3−{-# LANGUAGE CPP #-}
4−module Hakyll.Web.Preview.Poll
5− ( previewPoll
6− ) where
7−
8−import Control.Applicative ((<$>))
9−import Control.Concurrent (threadDelay)
10−import Control.Monad (filterM)
11−#if MIN_VERSION_directory(1,2,0)
12−import Data.Time (getCurrentTime)
13−#else
14−import System.Time (getClockTime)
15−#endif
16−import System.Directory (getModificationTime, doesFileExist)
17−
18−import Hakyll.Core.Configuration
19−
20−-- | A preview thread that periodically recompiles the site.
21−--
22−previewPoll :: Configuration -- ^ Configuration
23− -> IO [FilePath] -- ^ Updating action
24− -> IO () -- ^ Can block forever
25−previewPoll _ update = do
26−#if MIN_VERSION_directory(1,2,0)
27− time <- getCurrentTime
28−#else
29− time <- getClockTime
30−#endif
31− loop time =<< update
32− where
33− delay = 1000000
34− loop time files = do
35− threadDelay delay
36− files' <- filterM doesFileExist files
37− filesTime <- case files' of
38− [] -> return time
39− _ -> maximum <$> mapM getModificationTime files'
40−
41− if filesTime > time || files' /= files
42− then loop filesTime =<< update
43− else loop time files'
src/Hakyll/Web/Preview/Server.hsdeleted−40
@@ -1,40 +0,0 @@
1−-- | Implements a basic static file server for previewing options
2−--
3−{-# LANGUAGE OverloadedStrings #-}
4−module Hakyll.Web.Preview.Server
5− ( staticServer
6− ) where
7−
8−import Control.Monad.Trans (liftIO)
9−
10−import qualified Snap.Core as Snap
11−import qualified Snap.Http.Server as Snap
12−import qualified Snap.Util.FileServe as Snap
13−
14−-- | Serve a given directory
15−--
16−static :: FilePath -- ^ Directory to serve
17− -> (FilePath -> IO ()) -- ^ Pre-serve hook
18− -> Snap.Snap ()
19−static directory preServe =
20− Snap.serveDirectoryWith directoryConfig directory
21− where
22− directoryConfig :: Snap.DirectoryConfig Snap.Snap
23− directoryConfig = Snap.fancyDirectoryConfig
24− { Snap.preServeHook = liftIO . preServe
25− }
26−
27−-- | Main method, runs a static server in the given directory
28−--
29−staticServer :: FilePath -- ^ Directory to serve
30− -> (FilePath -> IO ()) -- ^ Pre-serve hook
31− -> Int -- ^ Port to listen on
32− -> IO () -- ^ Blocks forever
33−staticServer directory preServe port =
34− Snap.httpServe config $ static directory preServe
35− where
36− -- Snap server config
37− config = Snap.setPort port
38− $ Snap.setAccessLog Snap.ConfigNoLog
39− $ Snap.setErrorLog Snap.ConfigNoLog
40− $ Snap.emptyConfig
src/Hakyll/Web/Template.hsdeleted−123
@@ -1,123 +0,0 @@
1−-- | This module provides means for reading and applying 'Template's.
2−--
3−-- Templates are tools to convert data (pages) into a string. They are
4−-- perfectly suited for laying out your site.
5−--
6−-- Let's look at an example template:
7−--
8−-- > <html>
9−-- > <head>
10−-- > <title>My crazy homepage - $title$</title>
11−-- > </head>
12−-- > <body>
13−-- > <div id="header">
14−-- > <h1>My crazy homepage - $title$</h1>
15−-- > </div>
16−-- > <div id="content">
17−-- > $body$
18−-- > </div>
19−-- > <div id="footer">
20−-- > By reading this you agree that I now own your soul
21−-- > </div>
22−-- > </body>
23−-- > </html>
24−--
25−-- We can use this template to render a 'Page' which has a body and a @$title$@
26−-- metadata field.
27−--
28−-- As you can see, the format is very simple -- @$key$@ is used to render the
29−-- @$key$@ field from the page, everything else is literally copied. If you want
30−-- to literally insert @\"$key$\"@ into your page (for example, when you're
31−-- writing a Hakyll tutorial) you can use
32−--
33−-- > <p>
34−-- > A literal $$key$$.
35−-- > </p>
36−--
37−-- Because of it's simplicity, these templates can be used for more than HTML:
38−-- you could make, for example, CSS or JS templates as well.
39−module Hakyll.Web.Template
40− ( Template
41− , templateCompiler
42− , applyTemplate
43− , loadAndApplyTemplate
44− , applyAsTemplate
45− , applyTemplateWith
46− ) where
47−
48−
49−--------------------------------------------------------------------------------
50−import Control.Monad (forM, liftM)
51−import Prelude hiding (id)
52−
53−
54−--------------------------------------------------------------------------------
55−import Hakyll.Core.Compiler
56−import Hakyll.Core.Identifier
57−import Hakyll.Core.Item
58−import Hakyll.Web.Template.Context
59−import Hakyll.Web.Template.Internal
60−import Hakyll.Web.Template.Read
61−
62−
63−--------------------------------------------------------------------------------
64−-- | Read a template.
65−templateCompiler :: Compiler (Item Template)
66−templateCompiler = cached "Hakyll.Web.Template.templateCompiler" $ do
67− item <- getResourceString
68− return $ fmap readTemplate item
69−
70−
71−--------------------------------------------------------------------------------
72−applyTemplate :: Template -- ^ Template
73− -> Context a -- ^ Context
74− -> Item a -- ^ Page
75− -> Compiler (Item String) -- ^ Resulting item
76−applyTemplate tpl context item = do
77− let context' k x = unContext context k x
78− body <- applyTemplateWith context' tpl item
79− return $ itemSetBody body item
80−
81−
82−--------------------------------------------------------------------------------
83−-- | The following pattern is so common:
84−--
85−-- > tpl <- loadBody "templates/foo.html"
86−-- > someCompiler
87−-- > >>= applyTemplate tpl context
88−--
89−-- That we have a single function which does this:
90−--
91−-- > someCompiler
92−-- > >>= loadAndApplyTemplate "templates/foo.html" context
93−loadAndApplyTemplate :: Identifier -- ^ Template identifier
94− -> Context a -- ^ Context
95− -> Item a -- ^ Page
96− -> Compiler (Item String) -- ^ Resulting item
97−loadAndApplyTemplate identifier context item = do
98− tpl <- loadBody identifier
99− applyTemplate tpl context item
100−
101−
102−--------------------------------------------------------------------------------
103−-- | It is also possible that you want to substitute @$key$@s within the body of
104−-- an item. This function does that by interpreting the item body as a template,
105−-- and then applying it to itself.
106−applyAsTemplate :: Context String -- ^ Context
107− -> Item String -- ^ Item and template
108− -> Compiler (Item String) -- ^ Resulting item
109−applyAsTemplate context item =
110− let tpl = readTemplate $ itemBody item
111− in applyTemplate tpl context item
112−
113−
114−--------------------------------------------------------------------------------
115−-- | Overloaded apply template function to work in an arbitrary Monad.
116−applyTemplateWith :: Monad m
117− => (String -> a -> m String)
118− -> Template -> a -> m String
119−applyTemplateWith context tpl x = liftM concat $
120− forM (unTemplate tpl) $ \e -> case e of
121− Chunk c -> return c
122− Escaped -> return "$"
123− Key k -> context k x
src/Hakyll/Web/Template/Context.hsdeleted−215
@@ -1,215 +0,0 @@
1−--------------------------------------------------------------------------------
2−module Hakyll.Web.Template.Context
3− ( Context (..)
4− , mapContext
5− , field
6− , constField
7− , functionField
8−
9− , defaultContext
10− , bodyField
11− , metadataField
12− , urlField
13− , pathField
14− , titleField
15− , dateField
16− , dateFieldWith
17− , modificationTimeField
18− , modificationTimeFieldWith
19− , missingField
20− ) where
21−
22−
23−--------------------------------------------------------------------------------
24−import Control.Applicative (Alternative (..), (<$>))
25−import Control.Monad (msum)
26−import Data.List (intercalate)
27−import qualified Data.Map as M
28−import Data.Monoid (Monoid (..))
29−import Data.Time.Clock (UTCTime (..))
30−import Data.Time.Format (formatTime, parseTime)
31−import System.FilePath (takeBaseName, takeFileName)
32−import System.Locale (TimeLocale, defaultTimeLocale)
33−
34−
35−--------------------------------------------------------------------------------
36−import Hakyll.Core.Compiler
37−import Hakyll.Core.Compiler.Internal
38−import Hakyll.Core.Identifier
39−import Hakyll.Core.Item
40−import Hakyll.Core.Metadata
41−import Hakyll.Core.Provider
42−import Hakyll.Core.Util.String (splitAll)
43−import Hakyll.Web.Html
44−
45−
46−--------------------------------------------------------------------------------
47−newtype Context a = Context
48− { unContext :: String -> Item a -> Compiler String
49− }
50−
51−
52−--------------------------------------------------------------------------------
53−instance Monoid (Context a) where
54− mempty = missingField
55− mappend (Context f) (Context g) = Context $ \k i -> f k i <|> g k i
56−
57−
58−--------------------------------------------------------------------------------
59−mapContext :: (String -> String) -> Context a -> Context a
60−mapContext f (Context g) = Context $ \k i -> f <$> g k i
61−
62−
63−--------------------------------------------------------------------------------
64−field :: String -> (Item a -> Compiler String) -> Context a
65−field key value = Context $ \k i -> if k == key then value i else empty
66−
67−
68−--------------------------------------------------------------------------------
69−constField :: String -> String -> Context a
70−constField key = field key . const . return
71−
72−
73−--------------------------------------------------------------------------------
74−functionField :: String -> ([String] -> Item a -> Compiler String) -> Context a
75−functionField name value = Context $ \k i -> case words k of
76− [] -> empty
77− (n : args)
78− | n == name -> value args i
79− | otherwise -> empty
80−
81−
82−--------------------------------------------------------------------------------
83−defaultContext :: Context String
84−defaultContext =
85− bodyField "body" `mappend`
86− metadataField `mappend`
87− urlField "url" `mappend`
88− pathField "path" `mappend`
89− titleField "title" `mappend`
90− missingField
91−
92−
93−--------------------------------------------------------------------------------
94−bodyField :: String -> Context String
95−bodyField key = field key $ return . itemBody
96−
97−
98−--------------------------------------------------------------------------------
99−-- | Map any field to its metadata value, if present
100−metadataField :: Context String
101−metadataField = Context $ \k i -> do
102− metadata <- getMetadata $ itemIdentifier i
103− maybe empty return $ M.lookup k metadata
104−
105−
106−--------------------------------------------------------------------------------
107−-- | Absolute url to the resulting item
108−urlField :: String -> Context a
109−urlField key = field key $
110− fmap (maybe empty toUrl) . getRoute . itemIdentifier
111−
112−
113−--------------------------------------------------------------------------------
114−-- | Filepath of the underlying file of the item
115−pathField :: String -> Context a
116−pathField key = field key $ return . toFilePath . itemIdentifier
117−
118−
119−--------------------------------------------------------------------------------
120−-- | This title field takes the basename of the underlying file by default
121−titleField :: String -> Context a
122−titleField key = mapContext takeBaseName $ pathField key
123−
124−
125−--------------------------------------------------------------------------------
126−-- | When the metadata has a field called @published@ in one of the
127−-- following formats then this function can render the date.
128−--
129−-- * @Sun, 01 Feb 2000 13:00:00 UT@ (RSS date format)
130−--
131−-- * @2000-02-01T13:00:00Z@ (Atom date format)
132−--
133−-- * @February 1, 2000 1:00 PM@ (PM is usually uppercase)
134−--
135−-- * @February 1, 2000@ (assumes 12:00 AM for the time)
136−--
137−-- Alternatively, when the metadata has a field called @path@ in a
138−-- @folder/yyyy-mm-dd-title.extension@ format (the convention for pages)
139−-- and no @published@ metadata field set, this function can render
140−-- the date.
141−--
142−-- > renderDateField "date" "%B %e, %Y" "Date unknown"
143−--
144−-- Will render something like @January 32, 2010@.
145−--
146−dateField :: String -- ^ Key in which the rendered date should be placed
147− -> String -- ^ Format to use on the date
148− -> Context a -- ^ Resulting context
149−dateField = dateFieldWith defaultTimeLocale
150−
151−
152−--------------------------------------------------------------------------------
153−-- | This is an extended version of 'dateField' that allows you to
154−-- specify a time locale that is used for outputting the date. For more
155−-- details, see 'dateField'.
156−dateFieldWith :: TimeLocale -- ^ Output time locale
157− -> String -- ^ Destination key
158− -> String -- ^ Format to use on the date
159− -> Context a -- ^ Resulting context
160−dateFieldWith locale key format = field key $ \i -> do
161− time <- getUTC locale $ itemIdentifier i
162− return $ formatTime locale format time
163−
164−
165−--------------------------------------------------------------------------------
166−-- | Parser to try to extract and parse the time from the @published@
167−-- field or from the filename. See 'renderDateField' for more information.
168−getUTC :: TimeLocale -- ^ Output time locale
169− -> Identifier -- ^ Input page
170− -> Compiler UTCTime -- ^ Parsed UTCTime
171−getUTC locale id' = do
172− metadata <- getMetadata id'
173− let tryField k fmt = M.lookup k metadata >>= parseTime' fmt
174− fn = takeFileName $ toFilePath id'
175−
176− maybe empty return $ msum $
177− [tryField "published" fmt | fmt <- formats] ++
178− [tryField "date" fmt | fmt <- formats] ++
179− [parseTime' "%Y-%m-%d" $ intercalate "-" $ take 3 $ splitAll "-" fn]
180− where
181− parseTime' = parseTime locale
182− formats =
183− [ "%a, %d %b %Y %H:%M:%S UT"
184− , "%Y-%m-%dT%H:%M:%SZ"
185− , "%Y-%m-%d %H:%M:%S"
186− , "%Y-%m-%d"
187− , "%B %e, %Y %l:%M %p"
188− , "%B %e, %Y"
189− ]
190−
191−
192−--------------------------------------------------------------------------------
193−modificationTimeField :: String -- ^ Key
194− -> String -- ^ Format
195− -> Context a -- ^ Resuting context
196−modificationTimeField = modificationTimeFieldWith defaultTimeLocale
197−
198−
199−--------------------------------------------------------------------------------
200−modificationTimeFieldWith :: TimeLocale -- ^ Time output locale
201− -> String -- ^ Key
202− -> String -- ^ Format
203− -> Context a -- ^ Resulting context
204−modificationTimeFieldWith locale key fmt = field key $ \i -> do
205− provider <- compilerProvider <$> compilerAsk
206− mtime <- compilerUnsafeIO $
207− resourceModificationTime provider $ itemIdentifier i
208− return $ formatTime locale fmt mtime
209−
210−
211−--------------------------------------------------------------------------------
212−missingField :: Context a
213−missingField = Context $ \k i -> compilerThrow $
214− "Missing field $" ++ k ++ "$ in context for item " ++
215− show (itemIdentifier i)
src/Hakyll/Web/Template/Internal.hsdeleted−45
@@ -1,45 +0,0 @@
1−-- | Module containing the template data structure
2−--
3−{-# LANGUAGE GeneralizedNewtypeDeriving, DeriveDataTypeable #-}
4−module Hakyll.Web.Template.Internal
5− ( Template (..)
6− , TemplateElement (..)
7− ) where
8−
9−import Control.Applicative ((<$>))
10−
11−import Data.Binary (Binary, get, getWord8, put, putWord8)
12−import Data.Typeable (Typeable)
13−
14−import Hakyll.Core.Writable
15−
16−-- | Datatype used for template substitutions.
17−--
18−newtype Template = Template
19− { unTemplate :: [TemplateElement]
20− }
21− deriving (Show, Eq, Binary, Typeable)
22−
23−instance Writable Template where
24− -- Writing a template is impossible
25− write _ _ = return ()
26−
27−-- | Elements of a template.
28−--
29−data TemplateElement
30− = Chunk String
31− | Key String
32− | Escaped
33− deriving (Show, Eq, Typeable)
34−
35−instance Binary TemplateElement where
36− put (Chunk string) = putWord8 0 >> put string
37− put (Key key) = putWord8 1 >> put key
38− put (Escaped) = putWord8 2
39−
40− get = getWord8 >>= \tag -> case tag of
41− 0 -> Chunk <$> get
42− 1 -> Key <$> get
43− 2 -> return Escaped
44− _ -> error $ "Hakyll.Web.Template.Internal: "
45− ++ "Error reading cached template"
src/Hakyll/Web/Template/List.hsdeleted−53
@@ -1,53 +0,0 @@
1−--------------------------------------------------------------------------------
2−-- | Provides an easy way to combine several items in a list. The applications
3−-- are obvious:
4−--
5−-- * A post list on a blog
6−--
7−-- * An image list in a gallery
8−--
9−-- * A sitemap
10−module Hakyll.Web.Template.List
11− ( applyTemplateList
12− , chronological
13− , recentFirst
14− ) where
15−
16−
17−--------------------------------------------------------------------------------
18−import Data.List (sortBy)
19−import Data.Ord (comparing)
20−import System.FilePath (takeBaseName)
21−
22−
23−--------------------------------------------------------------------------------
24−import Hakyll.Core.Compiler
25−import Hakyll.Core.Identifier
26−import Hakyll.Core.Item
27−import Hakyll.Web.Template
28−import Hakyll.Web.Template.Context
29−
30−
31−--------------------------------------------------------------------------------
32−-- | Set a field of a page to a listing of pages
33−applyTemplateList :: Template
34− -> Context a
35− -> [Item a]
36− -> Compiler String
37−applyTemplateList tpl context items = do
38− items' <- mapM (applyTemplate tpl context) items
39− return $ concat $ map itemBody items'
40−
41−
42−--------------------------------------------------------------------------------
43−-- | Sort pages chronologically. This function assumes that the pages have a
44−-- @year-month-day-title.extension@ naming scheme -- as is the convention in
45−-- Hakyll.
46−chronological :: [Item a] -> [Item a]
47−chronological = sortBy $ comparing $ takeBaseName . toFilePath . itemIdentifier
48−
49−
50−--------------------------------------------------------------------------------
51−-- | The reverse of 'chronological'
52−recentFirst :: [Item a] -> [Item a]
53−recentFirst = reverse . chronological
src/Hakyll/Web/Template/Read.hsdeleted−41
@@ -1,41 +0,0 @@
1−--------------------------------------------------------------------------------
2−-- | Read templates in Hakyll's native format
3−module Hakyll.Web.Template.Read
4− ( readTemplate
5− ) where
6−
7−
8−--------------------------------------------------------------------------------
9−import Data.List (isPrefixOf)
10−
11−
12−--------------------------------------------------------------------------------
13−import Hakyll.Web.Template.Internal
14−
15−
16−--------------------------------------------------------------------------------
17−-- | Construct a @Template@ from a string.
18−readTemplate :: String -> Template
19−readTemplate = Template . readTemplate'
20− where
21− readTemplate' [] = []
22− readTemplate' string
23− | "$$" `isPrefixOf` string =
24− Escaped : readTemplate' (drop 2 string)
25− | "$" `isPrefixOf` string =
26− case readKey (drop 1 string) of
27− Just (key, rest) -> Key key : readTemplate' rest
28− Nothing -> Chunk "$" : readTemplate' (drop 1 string)
29− | otherwise =
30− let (chunk, rest) = break (== '$') string
31− in Chunk chunk : readTemplate' rest
32−
33− -- Parse an key into (key, rest) if it's valid, and return
34− -- Nothing otherwise
35− readKey string =
36− let (key, rest) = span validKeyChar string
37− in if not (null key) && "$" `isPrefixOf` rest
38− then Just (key, drop 1 rest)
39− else Nothing
40−
41− validKeyChar x = x `notElem` ['$', '\n', '\r']
src/Init.hsnew file+123
@@ -0,0 +1,123 @@
1+--------------------------------------------------------------------------------
2+module Main
3+ ( main
4+ ) where
5+
6+
7+--------------------------------------------------------------------------------
8+import Control.Arrow (first)
9+import Control.Monad (forM, forM_)
10+import Data.Char (isAlphaNum, isNumber)
11+import Data.List (foldl', intercalate, isPrefixOf)
12+import Data.Version (Version (..))
13+import System.Directory (canonicalizePath, copyFile,
14+ doesFileExist)
15+import System.Environment (getArgs, getProgName)
16+import System.Exit (exitFailure)
17+import System.FilePath (splitDirectories, (</>))
18+
19+
20+--------------------------------------------------------------------------------
21+import Hakyll.Core.Util.File
22+import Paths_hakyll
23+
24+
25+--------------------------------------------------------------------------------
26+import Prelude
27+
28+
29+--------------------------------------------------------------------------------
30+main :: IO ()
31+main = do
32+ progName <- getProgName
33+ args <- getArgs
34+ srcDir <- getDataFileName "example"
35+ files <- getRecursiveContents (const $ return False) srcDir
36+
37+ case args of
38+ -- When the argument begins with hyphens, it's more likely that the user
39+ -- intends to attempt some arguments like ("--help", "-h", "--version", etc.)
40+ -- rather than create directory with that name.
41+ -- If dstDir begins with hyphens, the guard will prevent it from creating
42+ -- directory with that name so we can fall to the second alternative
43+ -- which prints a usage info for user.
44+ [dstDir] | not ("-" `isPrefixOf` dstDir) ->
45+ createFiles False srcDir files dstDir
46+ ["-f", dstDir] ->
47+ createFiles True srcDir files dstDir
48+ _ -> do
49+ putStrLn $ "Usage: " ++ progName ++ "[-f] <directory>"
50+ exitFailure
51+
52+ where
53+ createFiles force srcDir files dstDir = do
54+ name <- makeName dstDir
55+ let cabalPath = dstDir </> name ++ ".cabal"
56+
57+ diff <- if force then return []
58+ else existingFiles dstDir (cabalPath : files)
59+
60+ case diff of
61+ [] -> do
62+ forM_ files $ \file -> do
63+ let dst = dstDir </> file
64+ src = srcDir </> file
65+ putStrLn $ "Creating " ++ dst
66+ makeDirectories dst
67+ copyFile src dst
68+
69+ putStrLn $ "Creating " ++ cabalPath
70+ createCabal cabalPath name
71+ fs -> do
72+ putStrLn $ "The following files will be overwritten:"
73+ mapM_ putStrLn fs
74+ putStrLn $ "Use -f to overwrite them"
75+ exitFailure
76+
77+existingFiles :: FilePath -> [FilePath] -> IO [FilePath]
78+existingFiles dstDir files = fmap concat $ forM files $ \file -> do
79+ let dst = dstDir </> file
80+ exists <- doesFileExist dst
81+ return $ if exists then [dst] else []
82+
83+-- | Figure out a good cabal package name from the given (existing) directory
84+-- name
85+makeName :: FilePath -> IO String
86+makeName dstDir = do
87+ canonical <- canonicalizePath dstDir
88+ return $ case safeLast (splitDirectories canonical) of
89+ Nothing -> fallbackName
90+ Just "/" -> fallbackName
91+ Just x -> repair (fallbackName ++) id x
92+ where
93+ -- Package name repair code comes from
94+ -- cabal-install.Distribution.Client.Init.Heuristics
95+ repair invalid valid x = case dropWhile (not . isAlphaNum) x of
96+ "" -> repairComponent ""
97+ x' -> let (c, r) = first repairComponent $ break (not . isAlphaNum) x'
98+ in c ++ repairRest r
99+ where repairComponent c | all isNumber c = invalid c
100+ | otherwise = valid c
101+ repairRest = repair id ('-' :)
102+ fallbackName = "site"
103+
104+ safeLast = foldl' (\_ x -> Just x) Nothing
105+
106+createCabal :: FilePath -> String -> IO ()
107+createCabal path name =
108+ writeFile path $ unlines [
109+ "name: " ++ name
110+ , "version: 0.1.0.0"
111+ , "build-type: Simple"
112+ , "cabal-version: >= 1.10"
113+ , ""
114+ , "executable site"
115+ , " main-is: site.hs"
116+ , " build-depends: base == 4.*"
117+ , " , hakyll == " ++ version' ++ ".*"
118+ , " ghc-options: -threaded"
119+ , " default-language: Haskell2010"
120+ ]
121+ where
122+ -- Major hakyll version
123+ version' = intercalate "." . take 2 . map show $ versionBranch version
stack.yamlnew file+33
@@ -0,0 +1,33 @@
1+resolver: lts-13.29
2+save-hackage-creds: false
3+
4+flags:
5+ hakyll:
6+ previewServer: true
7+ watchServer: true
8+ checkExternal: true
9+ usePandoc: true
10+ buildWebsite: true
11+
12+packages:
13+ - '.'
14+
15+nix:
16+ enable: false
17+ packages:
18+ - zlib
19+
20+build:
21+ haddock: true
22+ haddock-hyperlink-source: true
23+ haddock-deps: false
24+
25+extra-deps:
26+- 'cmark-gfm-0.2.0'
27+- 'hslua-module-system-0.2.1'
28+- 'ipynb-0.1'
29+- 'lrucache-1.2.0.1'
30+- 'pandoc-2.7.3'
31+- 'pandoc-citeproc-0.16.2'
32+- 'skylighting-0.8.2'
33+- 'skylighting-core-0.8.2'
test.hsnew file+30
@@ -0,0 +1,30 @@
1+{-# LANGUAGE BangPatterns #-}
2+import Control.Monad (forM)
3+import qualified Crypto.Hash.SHA256 as SHA256
4+import qualified Data.ByteString.Base16 as Base16
5+import qualified Data.ByteString.Char8 as BS8
6+import qualified Data.ByteString.Lazy as BSL
7+import Data.Map (Map)
8+import qualified Data.Map as Map
9+import Hakyll
10+import System.FilePath ((</>))
11+
12+type FileHashes = Map Identifier String
13+
14+mkFileHashes :: FilePath -> IO FileHashes
15+mkFileHashes dir = do
16+ allFiles <- getRecursiveContents (\_ -> return False) dir
17+ fmap Map.fromList $ forM allFiles $ \path0 -> do
18+ let path1 = dir </> path0
19+ !h <- hash path1
20+ return (fromFilePath path1, h)
21+ where
22+ hash :: FilePath -> IO String
23+ hash fp = do
24+ !h <- SHA256.hashlazy <$> BSL.readFile fp
25+ return $! BS8.unpack $! Base16.encode h
26+
27+main :: IO ()
28+main = hakyll $ do
29+ fileHashes <- preprocess (mkFileHashes "images")
30+ undefined
tests/Hakyll/Core/Dependencies/Tests.hs+5−4
@@ -9,8 +9,8 @@ module Hakyll.Core.Dependencies.Tests
99import Data.List (delete)
1010import qualified Data.Map as M
1111import qualified Data.Set as S
12−import Test.Framework (Test, testGroup)
13−import Test.HUnit (Assertion, (@=?))
12+import Test.Tasty (TestTree, testGroup)
13+import Test.Tasty.HUnit (Assertion, (@=?))
1414
1515
1616--------------------------------------------------------------------------------
@@ -20,7 +20,7 @@ import TestSuite.Util
2020
2121
2222--------------------------------------------------------------------------------
23−tests :: Test
23+tests :: TestTree
2424tests = testGroup "Hakyll.Core.Dependencies.Tests" $
2525 fromAssertions "analyze" [case01, case02, case03]
2626
@@ -38,7 +38,8 @@ oldFacts = M.fromList
3838 , ("posts/02.md",
3939 [])
4040 , ("index.md",
41− [ PatternDependency "posts/*" ["posts/01.md", "posts/02.md"]
41+ [ PatternDependency "posts/*"
42+ (S.fromList ["posts/01.md", "posts/02.md"])
4243 , IdentifierDependency "posts/01.md"
4344 , IdentifierDependency "posts/02.md"
4445 ])
tests/Hakyll/Core/Identifier/Tests.hs+9−17
@@ -6,9 +6,8 @@ module Hakyll.Core.Identifier.Tests
66
77
88--------------------------------------------------------------------------------
9−import Data.Monoid (mappend, mempty)
10−import Test.Framework (Test, testGroup)
11−import Test.HUnit ((@=?))
9+import Test.Tasty (TestTree, testGroup)
10+import Test.Tasty.HUnit ((@=?))
1211
1312
1413--------------------------------------------------------------------------------
@@ -18,25 +17,15 @@ import TestSuite.Util
1817
1918
2019--------------------------------------------------------------------------------
21−tests :: Test
20+tests :: TestTree
2221tests = testGroup "Hakyll.Core.Identifier.Tests" $ concat
23− [ isLiteralTests
24− , captureTests
22+ [ captureTests
2523 , matchesTests
2624 ]
2725
2826
2927--------------------------------------------------------------------------------
30−isLiteralTests :: [Test]
31−isLiteralTests = fromAssertions "isLiteral"
32− [ Just "index.html" @=? fromLiteral "index.html"
33− , Nothing @=? fromLiteral "posts/*.markdown"
34− , Just "test.txt" @=? fromLiteral ("test.txt" `mappend` mempty)
35− ]
36−
37−
38−--------------------------------------------------------------------------------
39−captureTests :: [Test]
28+captureTests :: [TestTree]
4029captureTests = fromAssertions "capture"
4130 [ Just ["bar"] @=? capture "foo/**" "foo/bar"
4231 , Just ["foo/bar"] @=? capture "**" "foo/bar"
@@ -53,11 +42,12 @@ captureTests = fromAssertions "capture"
5342 , Just ["lol", "fun/large"] @=? capture "*cat/**.jpg" "lolcat/fun/large.jpg"
5443 , Just [] @=? capture "\\*.jpg" "*.jpg"
5544 , Nothing @=? capture "\\*.jpg" "foo.jpg"
45+ , Just ["xyz","42"] @=? capture (fromRegex "cat-([a-z]+)/foo([0-9]+).jpg") "cat-xyz/foo42.jpg"
5646 ]
5747
5848
5949--------------------------------------------------------------------------------
60−matchesTests :: [Test]
50+matchesTests :: [TestTree]
6151matchesTests = fromAssertions "matches"
6252 [ True @=? matches (fromList ["foo.markdown"]) "foo.markdown"
6353 , False @=? matches (fromList ["foo"]) (setVersion (Just "x") "foo")
@@ -66,4 +56,6 @@ matchesTests = fromAssertions "matches"
6656 , False @=? matches (fromRegex "^foo/[^x]*$") "foo/barx"
6757 , True @=? matches (complement "foo.markdown") "bar.markdown"
6858 , False @=? matches (complement "foo.markdown") "foo.markdown"
59+ , True @=? matches ("foo" .||. "bar") "bar"
60+ , False @=? matches ("bar" .&&. hasNoVersion) (setVersion (Just "xz") "bar")
6961 ]
tests/Hakyll/Core/Provider/Metadata/Tests.hsnew file+62
@@ -0,0 +1,62 @@
1+--------------------------------------------------------------------------------
2+module Hakyll.Core.Provider.Metadata.Tests
3+ ( tests
4+ ) where
5+
6+
7+--------------------------------------------------------------------------------
8+import qualified Data.HashMap.Strict as HMS
9+import qualified Data.Text as T
10+import qualified Data.Yaml as Yaml
11+import Hakyll.Core.Metadata
12+import Hakyll.Core.Provider.Metadata
13+import Test.Tasty (TestTree, testGroup)
14+import Test.Tasty.HUnit (Assertion, assertFailure, (@=?))
15+import TestSuite.Util
16+
17+
18+--------------------------------------------------------------------------------
19+tests :: TestTree
20+tests = testGroup "Hakyll.Core.Provider.Metadata.Tests" $
21+ fromAssertions "page" [testPage01, testPage02]
22+
23+
24+
25+--------------------------------------------------------------------------------
26+testPage01 :: Assertion
27+testPage01 =
28+ (meta [("foo", "bar")], "qux\n") `expectRight` parsePage
29+ "---\n\
30+ \foo: bar\n\
31+ \---\n\
32+ \qux\n"
33+
34+
35+--------------------------------------------------------------------------------
36+testPage02 :: Assertion
37+testPage02 =
38+ (meta [("description", descr)], "Hello I am dog\n") `expectRight`
39+ parsePage
40+ "---\n\
41+ \description: A long description that would look better if it\n\
42+ \ spanned multiple lines and was indented\n\
43+ \---\n\
44+ \Hello I am dog\n"
45+ where
46+ descr :: String
47+ descr =
48+ "A long description that would look better if it \
49+ \spanned multiple lines and was indented"
50+
51+
52+--------------------------------------------------------------------------------
53+meta :: Yaml.ToJSON a => [(String, a)] -> Metadata
54+meta pairs = HMS.fromList [(T.pack k, Yaml.toJSON v) | (k, v) <- pairs]
55+
56+
57+--------------------------------------------------------------------------------
58+-- | This is useful when the 'Left' side of 'Either' doesn't have an 'Eq'
59+-- instance.
60+expectRight :: (Eq b, Show a, Show b) => b -> Either a b -> Assertion
61+expectRight _ (Left err) = assertFailure (show err)
62+expectRight expected (Right res) = expected @=? res
tests/Hakyll/Core/Provider/Tests.hs+11−13
@@ -6,19 +6,15 @@ module Hakyll.Core.Provider.Tests
66
77
88--------------------------------------------------------------------------------
9−import qualified Data.Map as M
10−import Test.Framework (Test, testGroup)
11−import Test.Framework.Providers.HUnit (testCase)
12−import Test.HUnit (Assertion, assert, (@=?))
13−
14−
15−--------------------------------------------------------------------------------
9+import Hakyll.Core.Metadata
1610import Hakyll.Core.Provider
11+import Test.Tasty (TestTree, testGroup)
12+import Test.Tasty.HUnit (Assertion, testCase, (@=?))
1713import TestSuite.Util
1814
1915
2016--------------------------------------------------------------------------------
21−tests :: Test
17+tests :: TestTree
2218tests = testGroup "Hakyll.Core.Provider.Tests"
2319 [ testCase "case01" case01
2420 ]
@@ -26,13 +22,15 @@ tests = testGroup "Hakyll.Core.Provider.Tests"
2622
2723--------------------------------------------------------------------------------
2824case01 :: Assertion
29−case01 = withTestStore $ \store -> do
25+case01 = do
26+ store <- newTestStore
3027 provider <- newTestProvider store
31− assert $ resourceExists provider "example.md"
28+ True @=? resourceExists provider "example.md"
3229
3330 metadata <- resourceMetadata provider "example.md"
34− Just "An example" @=? M.lookup "title" metadata
35− Just "External data" @=? M.lookup "external" metadata
31+ Just "An example" @=? lookupString "title" metadata
32+ Just "External data" @=? lookupString "external" metadata
3633
3734 doesntExist <- resourceMetadata provider "doesntexist.md"
38− M.empty @=? doesntExist
35+ mempty @=? doesntExist
36+ cleanTestEnv
tests/Hakyll/Core/Routes/Tests.hs+31−18
@@ -6,32 +6,45 @@ module Hakyll.Core.Routes.Tests
66
77
88--------------------------------------------------------------------------------
9−import Test.Framework (Test, testGroup)
10−import Test.HUnit ((@=?))
11−
12−
13−--------------------------------------------------------------------------------
9+import Data.Maybe (fromMaybe)
1410import Hakyll.Core.Identifier
11+import Hakyll.Core.Metadata
1512import Hakyll.Core.Routes
13+import System.FilePath ((</>))
14+import Test.Tasty (TestTree, testGroup)
15+import Test.Tasty.HUnit (Assertion, (@=?))
1616import TestSuite.Util
1717
1818
1919--------------------------------------------------------------------------------
20−tests :: Test
20+tests :: TestTree
2121tests = testGroup "Hakyll.Core.Routes.Tests" $ fromAssertions "runRoutes"
22− [ Just "foo.html" @=? runRoutes (setExtension "html") "foo"
23− , Just "foo.html" @=? runRoutes (setExtension ".html") "foo"
24− , Just "foo.html" @=? runRoutes (setExtension "html") "foo.markdown"
25− , Just "foo.html" @=? runRoutes (setExtension ".html") "foo.markdown"
22+ [ testRoutes "foo.html" (setExtension "html") "foo"
23+ , testRoutes "foo.html" (setExtension ".html") "foo"
24+ , testRoutes "foo.html" (setExtension "html") "foo.markdown"
25+ , testRoutes "foo.html" (setExtension ".html") "foo.markdown"
26+
27+ , testRoutes "neve ro ddo reven"
28+ (customRoute (reverse . toFilePath )) "never odd or even"
2629
27− , Just "neve ro ddo reven" @=?
28− runRoutes (customRoute (reverse . toFilePath )) "never odd or even"
30+ , testRoutes "foo" (constRoute "foo") "bar"
2931
30− , Just "foo" @=? runRoutes (constRoute "foo") "bar"
32+ , testRoutes "tags/bar.xml" (gsubRoute "rss/" (const "")) "tags/rss/bar.xml"
33+ , testRoutes "tags/bar.xml"
34+ (gsubRoute "rss/" (const "") `composeRoutes` setExtension "xml")
35+ "tags/rss/bar"
3136
32− , Just "tags/bar.xml" @=?
33− runRoutes (gsubRoute "rss/" (const "")) "tags/rss/bar.xml"
34− , Just "tags/bar.xml" @=?
35− runRoutes (gsubRoute "rss/" (const "") `composeRoutes`
36− setExtension "xml") "tags/rss/bar"
37+ , testRoutes "food/example.md" (metadataRoute $ \md -> customRoute $ \id' ->
38+ fromMaybe "?" (lookupString "subblog" md) </> toFilePath id')
39+ "example.md"
3740 ]
41+
42+
43+--------------------------------------------------------------------------------
44+testRoutes :: FilePath -> Routes -> Identifier -> Assertion
45+testRoutes expected r id' = do
46+ store <- newTestStore
47+ provider <- newTestProvider store
48+ (route, _) <- runRoutes r provider id'
49+ Just expected @=? route
50+ cleanTestEnv
tests/Hakyll/Core/Rules/Tests.hs+49−30
@@ -6,70 +6,89 @@ module Hakyll.Core.Rules.Tests
66
77
88--------------------------------------------------------------------------------
9+import Data.IORef (IORef, newIORef, readIORef,
10+ writeIORef)
911import qualified Data.Set as S
10−import Test.Framework (Test, testGroup)
11−import Test.Framework.Providers.HUnit (testCase)
12−import Test.HUnit (Assertion, assert, (@=?))
13−
14−
15−--------------------------------------------------------------------------------
1612import Hakyll.Core.Compiler
13+import Hakyll.Core.File
1714import Hakyll.Core.Identifier
1815import Hakyll.Core.Identifier.Pattern
16+import Hakyll.Core.Metadata
1917import Hakyll.Core.Routes
2018import Hakyll.Core.Rules
2119import Hakyll.Core.Rules.Internal
22−import Hakyll.Core.Writable.CopyFile
23−import Hakyll.Web.Pandoc
20+import System.FilePath ((</>))
21+import Test.Tasty (TestTree, testGroup)
22+import Test.Tasty.HUnit (Assertion, (@=?))
2423import TestSuite.Util
2524
2625
2726--------------------------------------------------------------------------------
28−tests :: Test
29−tests = testGroup "Hakyll.Core.Rules.Tests"
30− [ testCase "runRules" rulesTest
31− ]
27+tests :: TestTree
28+tests = testGroup "Hakyll.Core.Rules.Tests" $ fromAssertions "runRules"
29+ [case01]
3230
3331
3432--------------------------------------------------------------------------------
35−rulesTest :: Assertion
36−rulesTest = withTestStore $ \store -> do
33+case01 :: Assertion
34+case01 = do
35+ ioref <- newIORef False
36+ store <- newTestStore
3737 provider <- newTestProvider store
38− ruleSet <- runRules rules provider
39− let identifiers = S.fromList $ map fst $ rulesCompilers ruleSet
40− routes = rulesRoutes ruleSet
38+ ruleSet <- runRules (rules01 ioref) provider
39+ let identifiers = S.fromList $ map fst $ rulesCompilers ruleSet
40+ routes = rulesRoutes ruleSet
41+ checkRoute ex i =
42+ runRoutes routes provider i >>= \(r, _) -> Just ex @=? r
4143
4244 -- Test that we have some identifiers and that the routes work out
43− assert $ all (`S.member` identifiers) expected
44− Just "example.html" @=? runRoutes routes "example.md"
45− Just "example.md" @=? runRoutes routes (sv "raw" "example.md")
46− Just "example.md" @=? runRoutes routes (sv "nav" "example.md")
47− Just "example.mv1" @=? runRoutes routes (sv "mv1" "example.md")
48− Just "example.mv2" @=? runRoutes routes (sv "mv2" "example.md")
45+ S.fromList expected @=? identifiers
46+ checkRoute "example.html" "example.md"
47+ checkRoute "example.md" (sv "raw" "example.md")
48+ checkRoute "example.md" (sv "nav" "example.md")
49+ checkRoute "example.mv1" (sv "mv1" "example.md")
50+ checkRoute "example.mv2" (sv "mv2" "example.md")
51+ checkRoute "food/example.md" (sv "metadataMatch" "example.md")
52+ readIORef ioref >>= (True @=?)
53+ cleanTestEnv
4954 where
5055 sv g = setVersion (Just g)
5156 expected =
52− [ "example.md"
53− , "russian.md"
54− , sv "raw" "example.md"
57+ [ "example.md"
58+ , sv "raw" "example.md"
59+ , sv "metadataMatch" "example.md"
60+ , sv "nav" "example.md"
61+ , sv "mv1" "example.md"
62+ , sv "mv2" "example.md"
63+
64+ , "russian.md"
5565 , sv "raw" "russian.md"
56− , sv "nav" "example.md"
66+ , sv "mv1" "russian.md"
67+ , sv "mv2" "russian.md"
5768 ]
5869
5970
6071--------------------------------------------------------------------------------
61−rules :: Rules ()
62−rules = do
72+rules01 :: IORef Bool -> Rules ()
73+rules01 ioref = do
6374 -- Compile some posts
6475 match "*.md" $ do
6576 route $ setExtension "html"
66− compile pandocCompiler
77+ compile copyFileCompiler
78+
79+ -- Yeah. I don't know how else to test this stuff?
80+ preprocess $ writeIORef ioref True
6781
6882 -- Compile them, raw
6983 match "*.md" $ version "raw" $ do
7084 route idRoute
7185 compile getResourceString
7286
87+ version "metadataMatch" $
88+ matchMetadata "*.md" (\md -> lookupString "subblog" md == Just "food") $ do
89+ route $ customRoute $ \id' -> "food" </> toFilePath id'
90+ compile getResourceString
91+
7392 -- Regression test
7493 version "nav" $ match (fromList ["example.md"]) $ do
7594 route idRoute
tests/Hakyll/Core/Runtime/Tests.hs+62−12
@@ -6,41 +6,91 @@ module Hakyll.Core.Runtime.Tests
66
77
88--------------------------------------------------------------------------------
9+import qualified Data.ByteString as B
910import System.FilePath ((</>))
10−import Test.Framework (Test, testGroup)
11−import Test.HUnit (Assertion, (@?=))
11+import Test.Tasty (TestTree, testGroup)
12+import Test.Tasty.HUnit (Assertion, (@?=))
1213
1314
1415--------------------------------------------------------------------------------
1516import Hakyll
17+import qualified Hakyll.Core.Logger as Logger
1618import Hakyll.Core.Runtime
1719import TestSuite.Util
1820
1921
2022--------------------------------------------------------------------------------
21−tests :: Test
22−tests = testGroup "Hakyll.Core.Runtime.Tests" $ fromAssertions "run" [case01]
23+tests :: TestTree
24+tests = testGroup "Hakyll.Core.Runtime.Tests" $
25+ fromAssertions "run" [case01, case02]
2326
2427
2528--------------------------------------------------------------------------------
2629case01 :: Assertion
27−case01 = withTestConfiguration $ \config -> do
28− _ <- run config $ do
30+case01 = do
31+ logger <- Logger.new Logger.Error
32+ _ <- run testConfiguration logger $ do
33+ match "images/*" $ do
34+ route idRoute
35+ compile copyFileCompiler
36+
2937 match "*.md" $ do
3038 route $ setExtension "html"
3139 compile $ do
32− body <- getResourceBody
33− saveSnapshot "raw" body
34− return $ renderPandoc body
40+ getResourceBody
41+ >>= saveSnapshot "raw"
42+ >>= renderParagraphs
3543
36− match "bodies.txt" $ do
44+ match (fromList ["partial.html", "partial-helper.html"]) $
45+ compile templateCompiler
46+ create ["partial.html.out"] $ do
47+ route idRoute
48+ compile $ do
49+ example <- loadSnapshotBody "example.md" "raw"
50+ makeItem example
51+ >>= loadAndApplyTemplate "partial.html" defaultContext
52+
53+ create ["bodies.txt"] $ do
3754 route idRoute
3855 compile $ do
3956 items <- loadAllSnapshots "*.md" "raw"
4057 makeItem $ concat $ map itemBody (items :: [Item String])
4158
42− example <- readFile $ destinationDirectory config </> "example.html"
59+ favicon <- B.readFile $
60+ providerDirectory testConfiguration </> "images/favicon.ico"
61+ favicon' <- B.readFile $
62+ destinationDirectory testConfiguration </> "images/favicon.ico"
63+ favicon @?= favicon'
64+
65+ example <- readFile $
66+ destinationDirectory testConfiguration </> "example.html"
4367 lines example @?= ["<p>This is an example.</p>"]
4468
45− bodies <- readFile $ destinationDirectory config </> "bodies.txt"
69+ bodies <- readFile $ destinationDirectory testConfiguration </> "bodies.txt"
4670 head (lines bodies) @?= "This is an example."
71+
72+ partial <- readFile $ providerDirectory testConfiguration </> "partial.html.out"
73+ partial' <- readFile $ destinationDirectory testConfiguration </> "partial.html.out"
74+ partial @?= partial'
75+
76+ cleanTestEnv
77+
78+
79+--------------------------------------------------------------------------------
80+case02 :: Assertion
81+case02 = do
82+ logger <- Logger.new Logger.Error
83+ _ <- run testConfiguration logger $ do
84+ match "images/favicon.ico" $ do
85+ route $ gsubRoute "images/" (const "")
86+ compile $ makeItem ("Test" :: String)
87+
88+ match "images/**" $ do
89+ route idRoute
90+ compile copyFileCompiler
91+
92+ favicon <- readFile $
93+ destinationDirectory testConfiguration </> "favicon.ico"
94+ favicon @?= "Test"
95+
96+ cleanTestEnv
tests/Hakyll/Core/Store/Tests.hs+32−17
@@ -6,26 +6,27 @@ module Hakyll.Core.Store.Tests
66
77
88--------------------------------------------------------------------------------
9−import Data.Typeable (typeOf)
10−import Test.Framework (Test, testGroup)
11−import Test.Framework.Providers.HUnit (testCase)
12−import Test.Framework.Providers.QuickCheck2 (testProperty)
13−import qualified Test.HUnit as H
14−import qualified Test.QuickCheck as Q
15−import qualified Test.QuickCheck.Monadic as Q
9+import Data.Typeable (typeOf)
10+import qualified Test.QuickCheck as Q
11+import qualified Test.QuickCheck.Monadic as Q
12+import Test.Tasty (TestTree, testGroup)
13+import Test.Tasty.HUnit (testCase)
14+import qualified Test.Tasty.HUnit as H
15+import Test.Tasty.QuickCheck (testProperty)
1616
1717
1818--------------------------------------------------------------------------------
19−import qualified Hakyll.Core.Store as Store
19+import qualified Hakyll.Core.Store as Store
2020import TestSuite.Util
2121
2222
2323--------------------------------------------------------------------------------
24−tests :: Test
24+tests :: TestTree
2525tests = testGroup "Hakyll.Core.Store.Tests"
2626 [ testProperty "simple get . set" simpleSetGet
2727 , testProperty "persistent get . set" persistentSetGet
2828 , testCase "WrongType get . set" wrongType
29+ , testCase "isMembertest . set" isMembertest
2930 ]
3031
3132
@@ -38,7 +39,7 @@ simpleSetGet = Q.monadicIO $ do
3839 Q.run $ Store.set store key (value :: String)
3940 value' <- Q.run $ Store.get store key
4041 Q.assert $ Store.Found value == value'
41− Q.run cleanTestStore
42+ Q.run cleanTestEnv
4243
4344
4445--------------------------------------------------------------------------------
@@ -52,17 +53,31 @@ persistentSetGet = Q.monadicIO $ do
5253 store2 <- Q.run newTestStore
5354 value' <- Q.run $ Store.get store2 key
5455 Q.assert $ Store.Found value == value'
55− Q.run cleanTestStore
56+ Q.run cleanTestEnv
5657
5758
5859--------------------------------------------------------------------------------
5960wrongType :: H.Assertion
60−wrongType = withTestStore $ \store -> do
61+wrongType = do
62+ store <- newTestStore
6163 -- Store a string and try to fetch an int
6264 Store.set store ["foo", "bar"] ("qux" :: String)
6365 value <- Store.get store ["foo", "bar"] :: IO (Store.Result Int)
64− H.assert $ case value of
65− Store.WrongType e t ->
66− e == typeOf (undefined :: Int) &&
67− t == typeOf (undefined :: String)
68− _ -> False
66+ case value of
67+ Store.WrongType e t -> do
68+ typeOf (undefined :: Int) H.@=? e
69+ typeOf (undefined :: String) H.@=? t
70+ _ -> H.assertFailure "Expecting WrongType"
71+ cleanTestEnv
72+
73+
74+--------------------------------------------------------------------------------
75+isMembertest :: H.Assertion
76+isMembertest = do
77+ store <- newTestStore
78+ Store.set store ["foo", "bar"] ("qux" :: String)
79+ good <- Store.isMember store ["foo", "bar"]
80+ bad <- Store.isMember store ["foo", "baz"]
81+ True H.@=? good
82+ False H.@=? bad
83+ cleanTestEnv
tests/Hakyll/Core/UnixFilter/Tests.hs+38−9
@@ -6,32 +6,61 @@ module Hakyll.Core.UnixFilter.Tests
66
77
88--------------------------------------------------------------------------------
9−import Test.Framework (Test, testGroup)
10−import Test.Framework.Providers.HUnit (testCase)
11−import qualified Test.HUnit as H
9+import Test.Tasty (TestTree, testGroup)
10+import Test.Tasty.HUnit (testCase)
11+import qualified Test.Tasty.HUnit as H
1212
1313
1414--------------------------------------------------------------------------------
1515import Hakyll.Core.Compiler
16+import Hakyll.Core.Identifier
1617import Hakyll.Core.Item
1718import Hakyll.Core.UnixFilter
1819import TestSuite.Util
1920
2021
2122--------------------------------------------------------------------------------
22−tests :: Test
23+tests :: TestTree
2324tests = testGroup "Hakyll.Core.UnixFilter.Tests"
24− [ testCase "unixFilter rev" unixFilterRev
25+ [ testCase "unixFilter rev" unixFilterRev
26+ , testCase "unixFilter false" unixFilterFalse
27+ , testCase "unixFilter error" unixFilterError
2528 ]
2629
30+testMarkdown :: Identifier
31+testMarkdown = "russian.md"
2732
2833--------------------------------------------------------------------------------
2934unixFilterRev :: H.Assertion
30−unixFilterRev = withTestStore $ \store -> do
35+unixFilterRev = do
36+ store <- newTestStore
3137 provider <- newTestProvider store
32− output <- testCompilerDone store provider "russian.md" compiler
33− expected <- testCompilerDone store provider "russian.md" getResourceString
34− H.assert $ rev (itemBody expected) == lines (itemBody output)
38+ output <- testCompilerDone store provider testMarkdown compiler
39+ expected <- testCompilerDone store provider testMarkdown getResourceString
40+ rev (itemBody expected) H.@=? lines (itemBody output)
41+ cleanTestEnv
3542 where
3643 compiler = getResourceString >>= withItemBody (unixFilter "rev" [])
3744 rev = map reverse . lines
45+
46+
47+--------------------------------------------------------------------------------
48+unixFilterFalse :: H.Assertion
49+unixFilterFalse = do
50+ store <- newTestStore
51+ provider <- newTestProvider store
52+ testCompilerError store provider testMarkdown compiler "exit code"
53+ cleanTestEnv
54+ where
55+ compiler = getResourceString >>= withItemBody (unixFilter "false" [])
56+
57+
58+--------------------------------------------------------------------------------
59+unixFilterError :: H.Assertion
60+unixFilterError = do
61+ store <- newTestStore
62+ provider <- newTestProvider store
63+ testCompilerError store provider testMarkdown compiler "option"
64+ cleanTestEnv
65+ where
66+ compiler = getResourceString >>= withItemBody (unixFilter "head" ["-#"])
tests/Hakyll/Core/Util/String/Tests.hs+13−3
@@ -5,8 +5,8 @@ module Hakyll.Core.Util.String.Tests
55
66
77--------------------------------------------------------------------------------
8−import Test.Framework (Test, testGroup)
9−import Test.HUnit ((@=?))
8+import Test.Tasty (TestTree, testGroup)
9+import Test.Tasty.HUnit ((@=?))
1010
1111
1212--------------------------------------------------------------------------------
@@ -15,7 +15,7 @@ import TestSuite.Util
1515
1616
1717--------------------------------------------------------------------------------
18−tests :: Test
18+tests :: TestTree
1919tests = testGroup "Hakyll.Core.Util.String.Tests" $ concat
2020 [ fromAssertions "trim"
2121 [ "foo" @=? trim " foo\n\t "
@@ -28,6 +28,16 @@ tests = testGroup "Hakyll.Core.Util.String.Tests" $ concat
2828 , fromAssertions "splitAll"
2929 [ ["λ", "∀x.x", "hi"] @=? splitAll ", *" "λ, ∀x.x, hi"
3030 ]
31+
32+ , fromAssertions "needlePrefix"
33+ [ Just "ab" @=? needlePrefix "cd" "abcde"
34+ , Just "xx" @=? needlePrefix "ab" "xxab"
35+ , Nothing @=? needlePrefix "a" "xx"
36+ , Just "x" @=? needlePrefix "ab" "xabxab"
37+ , Just "" @=? needlePrefix "ab" "abc"
38+ , Just "" @=? needlePrefix "ab" "abab"
39+ , Nothing @=? needlePrefix "" ""
40+ ]
3141 ]
3242
3343 where
tests/Hakyll/Web/CompressCss/Tests.hsnew file+64
@@ -0,0 +1,64 @@
1+--------------------------------------------------------------------------------
2+module Hakyll.Web.CompressCss.Tests
3+ ( tests
4+ ) where
5+
6+
7+--------------------------------------------------------------------------------
8+import Test.Tasty (TestTree, testGroup)
9+import Test.Tasty.HUnit ((@=?))
10+
11+
12+--------------------------------------------------------------------------------
13+import Hakyll.Web.CompressCss
14+import TestSuite.Util
15+
16+
17+--------------------------------------------------------------------------------
18+tests :: TestTree
19+tests = testGroup "Hakyll.Web.CompressCss.Tests" $ concat
20+ [ fromAssertions "compressCss"
21+ [
22+ -- compress whitespace
23+ "something something" @=?
24+ compressCss " something \n\t\r something "
25+ -- do not compress whitespace in string tokens
26+ , "abc \" \t\n\r \" xyz" @=?
27+ compressCss "abc \" \t\n\r \" xyz"
28+ , "abc ' \t\n\r ' xyz" @=?
29+ compressCss "abc ' \t\n\r ' xyz"
30+
31+ -- strip comments
32+ , "before after" @=? compressCss "before /* abc { } ;; \n\t\r */ after"
33+ -- don't strip comments inside string tokens
34+ , "before \"/* abc { } ;; \n\t\r */\" after"
35+ @=? compressCss "before \"/* abc { } ;; \n\t\r */\" after"
36+
37+ -- compress separators
38+ , "}" @=? compressCss "; }"
39+ , ";{};" @=? compressCss " ; { } ; "
40+ , "text," @=? compressCss "text , "
41+ , "a>b" @=? compressCss "a > b"
42+ , "a+b" @=? compressCss "a + b"
43+ , "a!b" @=? compressCss "a ! b"
44+ -- compress calc()
45+ , "calc(1px + 100%/(5 + 3) - (3px + 2px)*5)" @=? compressCss "calc( 1px + 100% / ( 5 + 3) - calc( 3px + 2px ) * 5 )"
46+ -- compress whitespace even after this curly brace
47+ , "}" @=? compressCss "; } "
48+ -- but do not compress separators inside string tokens
49+ , "\" { } ; , \"" @=? compressCss "\" { } ; , \""
50+ -- don't compress separators at the start or end of string tokens
51+ , "\" }\"" @=? compressCss "\" }\""
52+ , "\"{ \"" @=? compressCss "\"{ \""
53+ -- don't get irritated by the wrong token delimiter
54+ , "\" ' \"" @=? compressCss "\" ' \""
55+ , "' \" '" @=? compressCss "' \" '"
56+ -- don't compress whitespace in the middle of a string
57+ , "abc '{ '" @=? compressCss "abc '{ '"
58+ , "abc \"{ \"" @=? compressCss "abc \"{ \""
59+ -- compress whitespace after colons (but not before)
60+ , "abc :xyz" @=? compressCss "abc : xyz"
61+ -- compress multiple semicolons
62+ , ";" @=? compressCss ";;;;;;;"
63+ ]
64+ ]
tests/Hakyll/Web/Html/RelativizeUrls/Tests.hs+7−3
@@ -6,8 +6,9 @@ module Hakyll.Web.Html.RelativizeUrls.Tests
66
77
88--------------------------------------------------------------------------------
9−import Test.Framework (Test, testGroup)
10−import Test.HUnit ((@=?))
9+import Test.Tasty (TestTree, testGroup)
10+import Test.Tasty.HUnit ((@=?))
11+
1112
1213--------------------------------------------------------------------------------
1314import Hakyll.Web.Html.RelativizeUrls
@@ -15,13 +16,16 @@ import TestSuite.Util
1516
1617
1718--------------------------------------------------------------------------------
18−tests :: Test
19+tests :: TestTree
1920tests = testGroup "Hakyll.Web.Html.RelativizeUrls.Tests" $
2021 fromAssertions "relativizeUrls"
2122 [ "<a href=\"../foo\">bar</a>" @=?
2223 relativizeUrlsWith ".." "<a href=\"/foo\">bar</a>"
2324 , "<img src=\"../../images/lolcat.png\" />" @=?
2425 relativizeUrlsWith "../.." "<img src=\"/images/lolcat.png\" />"
26+ , "<video poster=\"../../images/lolcat.png\"></video>" @=?
27+ relativizeUrlsWith "../.."
28+ "<video poster=\"/images/lolcat.png\"></video>"
2529 , "<a href=\"http://haskell.org\">Haskell</a>" @=?
2630 relativizeUrlsWith "../.."
2731 "<a href=\"http://haskell.org\">Haskell</a>"
tests/Hakyll/Web/Html/Tests.hs+24−11
@@ -5,9 +5,9 @@ module Hakyll.Web.Html.Tests
55
66
77--------------------------------------------------------------------------------
8−import Data.Char (toUpper)
9−import Test.Framework (Test, testGroup)
10−import Test.HUnit (assert, (@=?))
8+import Data.Char (toUpper)
9+import Test.Tasty (TestTree, testGroup)
10+import Test.Tasty.HUnit ((@=?))
1111
1212
1313--------------------------------------------------------------------------------
@@ -16,7 +16,7 @@ import TestSuite.Util
1616
1717
1818--------------------------------------------------------------------------------
19−tests :: Test
19+tests :: TestTree
2020tests = testGroup "Hakyll.Web.Html.Tests" $ concat
2121 [ fromAssertions "demoteHeaders"
2222 [ "<h2>A h1 title</h2>" @=?
@@ -36,12 +36,22 @@ tests = testGroup "Hakyll.Web.Html.Tests" $ concat
3636 withUrls id "<code>&lt;stdio&gt;</code>"
3737 , "<style>body > p { line-height: 1.3 }</style>" @=?
3838 withUrls id "<style>body > p { line-height: 1.3 }</style>"
39+
40+ -- Test minimizing elements
41+ , "<meta bar=\"foo\" />" @=?
42+ withUrls id "<meta bar=\"foo\" />"
3943 ]
4044
4145 , fromAssertions "toUrl"
42− [ "/foo/bar.html" @=? toUrl "foo/bar.html"
43− , "/" @=? toUrl "/"
44− , "/funny-pics.html" @=? toUrl "/funny-pics.html"
46+ [ "/foo/bar.html" @=? toUrl "foo/bar.html"
47+ , "/" @=? toUrl "/"
48+ , "/funny-pics.html" @=? toUrl "/funny-pics.html"
49+ , "/funny%20pics.html" @=? toUrl "funny pics.html"
50+ -- Test various reserved characters (RFC 3986, section 2.2)
51+ , "/%21%2A%27%28%29%3B%3A%40%26.html" @=? toUrl "/!*'();:@&.html"
52+ , "/%3D%2B%24%2C/%3F%23%5B%5D.html" @=? toUrl "=+$,/?#[].html"
53+ -- Test various characters that are nor reserved, nor unreserved.
54+ , "/%E3%81%82%F0%9D%90%87%E2%88%80" @=? toUrl "\12354\119815\8704"
4555 ]
4656
4757 , fromAssertions "toSiteRoot"
@@ -49,13 +59,16 @@ tests = testGroup "Hakyll.Web.Html.Tests" $ concat
4959 , "." @=? toSiteRoot "index.html"
5060 , "." @=? toSiteRoot "/index.html"
5161 , "../.." @=? toSiteRoot "foo/bar/qux"
62+ , ".." @=? toSiteRoot "./foo/bar.html"
63+ , ".." @=? toSiteRoot "/foo/./bar.html"
5264 ]
5365
5466 , fromAssertions "isExternal"
55− [ assert (isExternal "http://reddit.com")
56− , assert (isExternal "https://mail.google.com")
57− , assert (not (isExternal "../header.png"))
58− , assert (not (isExternal "/foo/index.html"))
67+ [ True @=? isExternal "http://reddit.com"
68+ , True @=? isExternal "https://mail.google.com"
69+ , True @=? isExternal "//ajax.googleapis.com"
70+ , False @=? isExternal "../header.png"
71+ , False @=? isExternal "/foo/index.html"
5972 ]
6073
6174 , fromAssertions "stripTags"
tests/Hakyll/Web/Pandoc/FileType/Tests.hsnew file+26
@@ -0,0 +1,26 @@
1+--------------------------------------------------------------------------------
2+{-# LANGUAGE OverloadedStrings #-}
3+module Hakyll.Web.Pandoc.FileType.Tests
4+ ( tests
5+ ) where
6+
7+
8+--------------------------------------------------------------------------------
9+import Test.Tasty (TestTree, testGroup)
10+import Test.Tasty.HUnit ((@=?))
11+
12+
13+--------------------------------------------------------------------------------
14+import Hakyll.Web.Pandoc.FileType
15+import TestSuite.Util
16+
17+
18+--------------------------------------------------------------------------------
19+tests :: TestTree
20+tests = testGroup "Hakyll.Web.Pandoc.FileType.Tests" $
21+ fromAssertions "fileType"
22+ [ Markdown @=? fileType "index.md"
23+ , Rst @=? fileType "about/foo.rst"
24+ , LiterateHaskell Markdown @=? fileType "posts/bananas.lhs"
25+ , LiterateHaskell LaTeX @=? fileType "posts/bananas.tex.lhs"
26+ ]
tests/Hakyll/Web/Template/Context/Tests.hs+52−8
@@ -6,30 +6,33 @@ module Hakyll.Web.Template.Context.Tests
66
77
88--------------------------------------------------------------------------------
9−import Test.Framework (Test, testGroup)
10−import Test.Framework.Providers.HUnit (testCase)
11−import Test.HUnit (Assertion, (@=?))
9+import Test.Tasty (TestTree, testGroup)
10+import Test.Tasty.HUnit (Assertion, testCase, (@=?))
1211
1312
1413--------------------------------------------------------------------------------
1514import Hakyll.Core.Compiler
1615import Hakyll.Core.Identifier
16+import Hakyll.Core.Item
1717import Hakyll.Core.Provider
18−import Hakyll.Core.Store (Store)
18+import Hakyll.Core.Store (Store)
1919import Hakyll.Web.Template.Context
20+import Hakyll.Web.Template.Internal
2021import TestSuite.Util
2122
2223
2324--------------------------------------------------------------------------------
24−tests :: Test
25−tests = testGroup "Hakyll.Core.Template.Context.Tests"
25+tests :: TestTree
26+tests = testGroup "Hakyll.Web.Template.Context.Tests"
2627 [ testCase "testDateField" testDateField
28+ , testCase "testOuerLoopContextAccess" testOuerLoopContextAccess
2729 ]
2830
2931
3032--------------------------------------------------------------------------------
3133testDateField :: Assertion
32−testDateField = withTestStore $ \store -> do
34+testDateField = do
35+ store <- newTestStore
3336 provider <- newTestProvider store
3437
3538 date1 <- testContextDone store provider "example.md" "date" $
@@ -41,6 +44,17 @@ testDateField = withTestStore $ \store -> do
4144 dateField "date" "%B %e, %Y"
4245 date2 @=? "August 26, 2010"
4346
47+ date3 <- testContextDone store provider
48+ "posts/2018-09-26.md" "date" $
49+ dateField "date" "%B %e, %Y"
50+ date3 @=? "September 26, 2018"
51+
52+ date4 <- testContextDone store provider
53+ "posts/2019/05/10/tomorrow.md" "date" $
54+ dateField "date" "%B %e, %Y"
55+ date4 @=? "May 10, 2019"
56+ cleanTestEnv
57+
4458
4559--------------------------------------------------------------------------------
4660testContextDone :: Store -> Provider -> Identifier -> String
@@ -48,4 +62,34 @@ testContextDone :: Store -> Provider -> Identifier -> String
4862testContextDone store provider identifier key context =
4963 testCompilerDone store provider identifier $ do
5064 item <- getResourceBody
51− unContext context key item
65+ cf <- unContext context key [] item
66+ case cf of
67+ StringField str -> return str
68+ _ -> error $
69+ "Hakyll.Web.Template.Context.Tests.testContextDone: " ++
70+ "expected StringField"
71+
72+--------------------------------------------------------------------------------
73+
74+testOuerLoopContextAccess :: Assertion
75+testOuerLoopContextAccess = do
76+ store <- newTestStore
77+ provider <- newTestProvider store
78+ test store provider ctx "baz"
79+ test store provider (ctx' <> ctx) "not baz"
80+ test store provider (ctx <> ctx') "baz"
81+
82+ cleanTestEnv
83+ where
84+ tpl = readTemplate "$for(foo)$$for(bar)$$qux$$endfor$$endfor$"
85+ ctx = mconcat [
86+ field "qux" $ const $ return "baz"
87+ , listField "foo" (listField "bar" mempty $ return [mockItem])
88+ $ return [mockItem]
89+ ]
90+ ctx' = field "qux" $ const $ return "not baz"
91+ mockItem = Item "" ()
92+ test store provider context str = do
93+ str' <- testCompilerDone store provider ""
94+ $ applyTemplate tpl context mockItem
95+ str @=? itemBody str'
tests/Hakyll/Web/Template/Tests.hs+123−16
@@ -1,49 +1,156 @@
11--------------------------------------------------------------------------------
22{-# LANGUAGE OverloadedStrings #-}
3+{-# LANGUAGE TemplateHaskell #-}
34module Hakyll.Web.Template.Tests
45 ( tests
56 ) where
67
78
89--------------------------------------------------------------------------------
9−import Data.Monoid (mconcat)
10−import Test.Framework (Test, testGroup)
11−import Test.Framework.Providers.HUnit (testCase)
12−import Test.HUnit (Assertion, (@=?))
10+import Test.Tasty (TestTree, testGroup)
11+import Test.Tasty.HUnit (Assertion, assertBool, testCase,
12+ (@=?), (@?=))
1313
14+import Data.Either (isLeft)
1415
1516--------------------------------------------------------------------------------
17+import Hakyll.Core.Compiler
18+import Hakyll.Core.Identifier
1619import Hakyll.Core.Item
1720import Hakyll.Core.Provider
18−import Hakyll.Web.Pandoc
1921import Hakyll.Web.Template
2022import Hakyll.Web.Template.Context
23+import Hakyll.Web.Template.Internal
24+import Hakyll.Web.Template.List
2125import TestSuite.Util
2226
2327
2428--------------------------------------------------------------------------------
25−tests :: Test
26−tests = testGroup "Hakyll.Core.Template.Tests"
27− [ testCase "case01" case01
29+tests :: TestTree
30+tests = testGroup "Hakyll.Web.Template.Tests" $ concat
31+ [ [ testCase "case01" $ test ("template.html.out", "template.html", "example.md")
32+ , testCase "case02" $ test ("strip.html.out", "strip.html", "example.md")
33+ , testCase "case03" $ test ("just-meta.html.out", "just-meta.html", "example.md")
34+ , testCase "applyJoinTemplateList" testApplyJoinTemplateList
35+ ]
36+
37+ , fromAssertions "parseTemplate"
38+ [ Right [Chunk "Hello ", Expr (Call "guest" [])]
39+ @=? parse "Hello $guest()$"
40+ , Right [If (Call "a" [StringLiteral "bar"]) [Chunk "foo"] Nothing]
41+ @=? parse "$if(a(\"bar\"))$foo$endif$"
42+ -- 'If' trim check.
43+ , Right [ TrimL
44+ , If (Ident (TemplateKey "body"))
45+ [ TrimR
46+ , Chunk "\n"
47+ , Expr (Ident (TemplateKey "body"))
48+ , Chunk "\n"
49+ , TrimL
50+ ]
51+ (Just [ TrimR
52+ , Chunk "\n"
53+ , Expr (Ident (TemplateKey "body"))
54+ , Chunk "\n"
55+ , TrimL
56+ ])
57+ , TrimR
58+ ]
59+ @=? parse "$-if(body)-$\n$body$\n$-else-$\n$body$\n$-endif-$"
60+ -- 'For' trim check.
61+ , Right [ TrimL
62+ , For (Ident (TemplateKey "authors"))
63+ [TrimR, Chunk "\n body \n", TrimL]
64+ Nothing
65+ , TrimR
66+ ]
67+ @=? parse "$-for(authors)-$\n body \n$-endfor-$"
68+ -- 'Partial' trim check.
69+ , Right [ TrimL
70+ , Partial (StringLiteral "path")
71+ , TrimR
72+ ]
73+ @=? parse "$-partial(\"path\")-$"
74+ -- 'Expr' trim check.
75+ , Right [ TrimL
76+ , Expr (Ident (TemplateKey "foo"))
77+ , TrimR
78+ ]
79+ @=? parse "$-foo-$"
80+ -- fail on incomplete template.
81+ , assertBool "did not yield error" $ isLeft $
82+ parse "a$b"
83+ -- fail on mismatched template syntax.
84+ , assertBool "did not fail to parse" $ isLeft $
85+ parse "$for(xs)$\n <p>foo</p>\n$endif$"
86+ ]
87+
88+ , [testCase "embeddedTemplate" testEmbeddedTemplate]
2889 ]
90+ where
91+ parse = parseTemplateElemsFile ""
2992
3093
3194--------------------------------------------------------------------------------
32−case01 :: Assertion
33−case01 = withTestStore $ \store -> do
95+test :: (Identifier, Identifier, Identifier) -> Assertion
96+test (outf, tplf, itemf) = do
97+ store <- newTestStore
3498 provider <- newTestProvider store
3599
36− out <- resourceString provider "template.html.out"
37− tpl <- testCompilerDone store provider "template.html" $ templateCompiler
38− item <- testCompilerDone store provider "example.md" $
39− pandocCompiler >>= applyTemplate (itemBody tpl) testContext
100+ out <- resourceString provider outf
101+ tpl <- testCompilerDone store provider tplf templateBodyCompiler
102+ item <- testCompilerDone store provider itemf $
103+ getResourceBody
104+ >>= renderParagraphs
105+ >>= applyTemplate (itemBody tpl) testContext
40106
41107 out @=? itemBody item
108+ cleanTestEnv
42109
43110
44111--------------------------------------------------------------------------------
45112testContext :: Context String
46113testContext = mconcat
47− [ functionField "echo" (\args _ -> return $ unwords args)
48− , defaultContext
114+ [ defaultContext
115+ , listField "authors" (bodyField "name") $ do
116+ n1 <- makeItem "Jan"
117+ n2 <- makeItem "Piet"
118+ return [n1, n2]
119+ , functionField "rev" $ \args _ -> return $ unwords $ map reverse args
49120 ]
121+
122+
123+--------------------------------------------------------------------------------
124+testApplyJoinTemplateList :: Assertion
125+testApplyJoinTemplateList = do
126+ store <- newTestStore
127+ provider <- newTestProvider store
128+ tpl <- testCompilerDone store provider "tpl" $
129+ compileTemplateItem (Item "tpl" "<b>$body$</b>")
130+ str <- testCompilerDone store provider "item3" $
131+ applyJoinTemplateList ", " tpl defaultContext [i1, i2]
132+
133+ str @?= "<b>Hello</b>, <b>World</b>"
134+ cleanTestEnv
135+ where
136+ i1 = Item "item1" "Hello"
137+ i2 = Item "item2" "World"
138+
139+
140+--------------------------------------------------------------------------------
141+embeddedTemplate :: Template
142+embeddedTemplate = $(embedTemplate "tests/data/embed.html")
143+
144+--------------------------------------------------------------------------------
145+testEmbeddedTemplate :: Assertion
146+testEmbeddedTemplate = do
147+ store <- newTestStore
148+ provider <- newTestProvider store
149+ str <- testCompilerDone store provider "item3" $
150+ applyTemplate embeddedTemplate defaultContext item
151+
152+ itemBody str @?= "<p>Hello, world</p>\n"
153+ cleanTestEnv
154+ where
155+ item = Item "item1" "Hello, world"
156+
tests/TestSuite.hs+15−2
@@ -1,16 +1,18 @@
11--------------------------------------------------------------------------------
2+{-# LANGUAGE CPP #-}
23module Main
34 ( main
45 ) where
56
67
78--------------------------------------------------------------------------------
8−import Test.Framework (defaultMain)
9+import Test.Tasty (defaultMain, testGroup)
910
1011
1112--------------------------------------------------------------------------------
1213import qualified Hakyll.Core.Dependencies.Tests
1314import qualified Hakyll.Core.Identifier.Tests
15+import qualified Hakyll.Core.Provider.Metadata.Tests
1416import qualified Hakyll.Core.Provider.Tests
1517import qualified Hakyll.Core.Routes.Tests
1618import qualified Hakyll.Core.Rules.Tests
@@ -18,17 +20,23 @@ import qualified Hakyll.Core.Runtime.Tests
1820import qualified Hakyll.Core.Store.Tests
1921import qualified Hakyll.Core.UnixFilter.Tests
2022import qualified Hakyll.Core.Util.String.Tests
23+import qualified Hakyll.Web.CompressCss.Tests
2124import qualified Hakyll.Web.Html.RelativizeUrls.Tests
2225import qualified Hakyll.Web.Html.Tests
26+#ifdef USE_PANDOC
27+import qualified Hakyll.Web.Pandoc.FileType.Tests
28+#endif
2329import qualified Hakyll.Web.Template.Context.Tests
2430import qualified Hakyll.Web.Template.Tests
31+import qualified Hakyll.Web.Tags.Tests
2532
2633
2734--------------------------------------------------------------------------------
2835main :: IO ()
29−main = defaultMain
36+main = defaultMain $ testGroup "Hakyll"
3037 [ Hakyll.Core.Dependencies.Tests.tests
3138 , Hakyll.Core.Identifier.Tests.tests
39+ , Hakyll.Core.Provider.Metadata.Tests.tests
3240 , Hakyll.Core.Provider.Tests.tests
3341 , Hakyll.Core.Routes.Tests.tests
3442 , Hakyll.Core.Rules.Tests.tests
@@ -36,8 +44,13 @@ main = defaultMain
3644 , Hakyll.Core.Store.Tests.tests
3745 , Hakyll.Core.UnixFilter.Tests.tests
3846 , Hakyll.Core.Util.String.Tests.tests
47+ , Hakyll.Web.CompressCss.Tests.tests
3948 , Hakyll.Web.Html.RelativizeUrls.Tests.tests
4049 , Hakyll.Web.Html.Tests.tests
50+#ifdef USE_PANDOC
51+ , Hakyll.Web.Pandoc.FileType.Tests.tests
52+#endif
53+ , Hakyll.Web.Tags.Tests.tests
4154 , Hakyll.Web.Template.Context.Tests.tests
4255 , Hakyll.Web.Template.Tests.tests
4356 ]
tests/TestSuite/Util.hs+59−48
@@ -3,74 +3,64 @@
33module TestSuite.Util
44 ( fromAssertions
55 , newTestStore
6− , cleanTestStore
7− , withTestStore
86 , newTestProvider
97 , testCompiler
108 , testCompilerDone
11− , withTestConfiguration
9+ , testCompilerError
10+ , testConfiguration
11+ , cleanTestEnv
12+ , renderParagraphs
1213 ) where
1314
1415
1516--------------------------------------------------------------------------------
16−import Data.Monoid (mempty)
17−import qualified Data.Set as S
18−import System.Directory (removeDirectoryRecursive)
19−import Test.Framework
20−import Test.Framework.Providers.HUnit
21−import Test.HUnit hiding (Test)
22−import Text.Printf (printf)
17+import Data.List (intercalate, isInfixOf)
18+import Data.Monoid (mempty)
19+import qualified Data.Set as S
20+import Test.Tasty
21+import Test.Tasty.HUnit
22+import Text.Printf (printf)
2323
2424
2525--------------------------------------------------------------------------------
2626import Hakyll.Core.Compiler.Internal
2727import Hakyll.Core.Configuration
2828import Hakyll.Core.Identifier
29−import qualified Hakyll.Core.Logger as Logger
29+import qualified Hakyll.Core.Logger as Logger
3030import Hakyll.Core.Provider
31−import Hakyll.Core.Store (Store)
32−import qualified Hakyll.Core.Store as Store
31+import Hakyll.Core.Store (Store)
32+import qualified Hakyll.Core.Store as Store
33+import Hakyll.Core.Util.File
34+import Hakyll.Core.Item
3335
3436
3537--------------------------------------------------------------------------------
3638fromAssertions :: String -- ^ Name
3739 -> [Assertion] -- ^ Cases
38− -> [Test] -- ^ Result tests
40+ -> [TestTree] -- ^ Result tests
3941fromAssertions name =
4042 zipWith testCase [printf "[%2d] %s" n name | n <- [1 :: Int ..]]
4143
4244
4345--------------------------------------------------------------------------------
4446newTestStore :: IO Store
45−newTestStore = Store.new True "_teststore"
46−
47−
48−--------------------------------------------------------------------------------
49−cleanTestStore :: IO ()
50−cleanTestStore = removeDirectoryRecursive "_teststore"
51−
52−
53−--------------------------------------------------------------------------------
54−withTestStore :: (Store -> IO a) -> IO a
55−withTestStore f = do
56− store <- newTestStore
57− result <- f store
58− cleanTestStore
59− return result
47+newTestStore = Store.new True $ storeDirectory testConfiguration
6048
6149
6250--------------------------------------------------------------------------------
6351newTestProvider :: Store -> IO Provider
64−newTestProvider store = newProvider store (const False) "tests/data"
52+newTestProvider store = newProvider store (const $ return False) $
53+ providerDirectory testConfiguration
6554
6655
6756--------------------------------------------------------------------------------
6857testCompiler :: Store -> Provider -> Identifier -> Compiler a
6958 -> IO (CompilerResult a)
7059testCompiler store provider underlying compiler = do
71− logger <- Logger.new Logger.Debug (\_ -> return ())
60+ logger <- Logger.new Logger.Error
7261 let read' = CompilerRead
73− { compilerUnderlying = underlying
62+ { compilerConfig = testConfiguration
63+ , compilerUnderlying = underlying
7464 , compilerProvider = provider
7565 , compilerUniverse = S.empty
7666 , compilerRoutes = mempty
@@ -89,26 +79,47 @@ testCompilerDone store provider underlying compiler = do
8979 result <- testCompiler store provider underlying compiler
9080 case result of
9181 CompilerDone x _ -> return x
92− CompilerError e -> error $
82+ CompilerError e -> fail $
9383 "TestSuite.Util.testCompilerDone: compiler " ++ show underlying ++
94− " threw: " ++ e
95− CompilerRequire i _ -> error $
84+ " threw: " ++ intercalate "; " (compilerErrorMessages e)
85+ CompilerRequire i _ -> fail $
9686 "TestSuite.Util.testCompilerDone: compiler " ++ show underlying ++
9787 " requires: " ++ show i
88+ CompilerSnapshot _ _ -> fail
89+ "TestSuite.Util.testCompilerDone: unexpected CompilerSnapshot"
9890
91+testCompilerError :: Store -> Provider -> Identifier -> Compiler a -> String -> IO ()
92+testCompilerError store provider underlying compiler expectedMessage = do
93+ result <- testCompiler store provider underlying compiler
94+ case result of
95+ CompilerError e ->
96+ any (expectedMessage `isInfixOf`) (compilerErrorMessages e) @?
97+ "Expecting '" ++ expectedMessage ++ "' error"
98+ _ -> assertFailure "Expecting CompilerError"
99+
100+--------------------------------------------------------------------------------
101+testConfiguration :: Configuration
102+testConfiguration = defaultConfiguration
103+ { destinationDirectory = "_testsite"
104+ , storeDirectory = "_teststore"
105+ , tmpDirectory = "_testtmp"
106+ , providerDirectory = "tests/data"
107+ }
108+
109+
110+--------------------------------------------------------------------------------
111+cleanTestEnv :: IO ()
112+cleanTestEnv = do
113+ removeDirectory $ destinationDirectory testConfiguration
114+ removeDirectory $ storeDirectory testConfiguration
115+ removeDirectory $ tmpDirectory testConfiguration
99116
100117
101118--------------------------------------------------------------------------------
102−withTestConfiguration :: (Configuration -> IO a) -> IO a
103−withTestConfiguration f = do
104− x <- f config
105− removeDirectoryRecursive $ destinationDirectory config
106− removeDirectoryRecursive $ storeDirectory config
107− return x
108− where
109− config = defaultConfiguration
110− { destinationDirectory = "_testsite"
111− , storeDirectory = "_teststore"
112− , providerDirectory = "tests/data"
113− , verbosity = Logger.Error
114− }
119+-- | like 'Hakyll.Web.Pandoc.renderPandoc'
120+-- | but allowing to test without the @usePandoc@ flag
121+renderParagraphs :: Item String -> Compiler (Item String)
122+renderParagraphs = withItemBody (return
123+ . intercalate "\n" -- no trailing line
124+ . map (("<p>"++) . (++"</p>"))
125+ . lines)
tests/data/embed.htmlnew file+1
@@ -0,0 +1 @@
1+<p>$body$</p>
tests/data/example.md.metadata+3
@@ -1,2 +1,5 @@
11external: External data
22date: 2012-10-22 14:35:24
3+subblog: food
4+intfield: 42
5+numfield: 3.14
tests/data/images/favicon.iconew file

Binary file not shown.

tests/data/just-meta.htmlnew file+7
@@ -0,0 +1,7 @@
1+<pre>
2+external: {$external$}
3+date: {$date$}
4+subblog: {$subblog$}
5+intfield: {$intfield$}
6+numfield: {$numfield$}
7+</pre>
tests/data/just-meta.html.outnew file+7
@@ -0,0 +1,7 @@
1+<pre>
2+external: {External data}
3+date: {2012-10-22 14:35:24}
4+subblog: {food}
5+intfield: {42}
6+numfield: {3.14}
7+</pre>
tests/data/partial-helper.htmlnew file+3
@@ -0,0 +1,3 @@
1+<p>This is an included partial.</p>
2+
3+$body$
tests/data/partial.htmlnew file+3
@@ -0,0 +1,3 @@
1+<p>This is a file that includes a partial.</p>
2+
3+$partial("partial-helper.html")$
tests/data/partial.html.outnew file+7
@@ -0,0 +1,7 @@
1+<p>This is a file that includes a partial.</p>
2+
3+<p>This is an included partial.</p>
4+
5+This is an example.
6+
7+
tests/data/posts/2018-09-26.mdnew file+1
@@ -0,0 +1 @@
1+Something happened today.
tests/data/posts/2019/05/10/tomorrow.mdnew file+1
@@ -0,0 +1 @@
1+This day hasn't happened yet (as of writing this).
tests/data/strip.htmlnew file+34
@@ -0,0 +1,34 @@
1+<div>
2+ I'm so rich I have $$3.
3+
4+ $rev("foo")$
5+ $-rev(rev("foo"))$
6+
7+ $if(body)-$
8+ I have body
9+ $else-$
10+ or no
11+ $-endif-$
12+
13+ $if(unbound)$
14+ should not be printed
15+ $endif$
16+
17+ $-if(body)-$
18+ should be printed
19+ $-endif$
20+
21+ <ul>
22+ $for(authors)-$
23+ <li>$name$</li>
24+ $endfor-$
25+ </ul>
26+
27+ $for(authors)-$
28+ $name-$
29+ $sep$,
30+ $-endfor$
31+
32+ $body$
33+</div>
34+
tests/data/strip.html.outnew file+18
@@ -0,0 +1,18 @@
1+<div>
2+ I'm so rich I have $3.
3+
4+ ooffoo
5+
6+ I have body
7+ should be printed
8+
9+ <ul>
10+ <li>Jan</li>
11+ <li>Piet</li>
12+ </ul>
13+
14+ Jan,Piet
15+
16+ <p>This is an example.</p>
17+</div>
18+
tests/data/template.html+26−1
@@ -1,5 +1,30 @@
11<div>
22 I'm so rich I have $$3.
3− $echo test!$
3+
4+ $rev("foo")$
5+ $rev(rev("foo"))$
6+
7+ $if(body)$
8+ I have body
9+ $else$
10+ or no
11+ $endif$
12+
13+ $if(unbound)$
14+ should not be printed
15+ $endif$
16+
17+ $if(body)$
18+ should be printed
19+ $endif$
20+
21+ <ul>
22+ $for(authors)$
23+ <li>$name$</li>
24+ $endfor$
25+ </ul>
26+
27+ $for(authors)$$name$$sep$, $endfor$
28+
429 $body$
530</div>
tests/data/template.html.out+24−1
@@ -1,5 +1,28 @@
11<div>
22 I'm so rich I have $3.
3− test!
3+
4+ oof
5+ foo
6+
7+
8+ I have body
9+
10+
11+
12+
13+
14+ should be printed
15+
16+
17+ <ul>
18+
19+ <li>Jan</li>
20+
21+ <li>Piet</li>
22+
23+ </ul>
24+
25+ Jan, Piet
26+
427 <p>This is an example.</p>
528</div>
web/about.markdown+3
@@ -37,3 +37,6 @@ who still maintains the package. Contributors:
3737- [sargon](https://github.com/sargon)
3838- [seschwar](https://github.com/seschwar)
3939- [favonia](https://github.com/favonia)
40+- [Robin Windels](https://github.com/rwindelz)
41+- [Miikka Koskinen](http://miikka.me/)
42+- [Simonas Kazlauskas](http://kazlauskas.me/)
web/changelog.markdowndeleted−150
@@ -1,150 +0,0 @@
1−---
2−title: Changelog
3−---
4−
5−## Hakyll 3.2.2
6−
7−- Add `applyTemplateWith`, `applyTemplateCompilerWith` to allows missing keys
8−
9−## Hakyll 3.2.1
10−
11−- Add `Pandoc.Biblio` module for reference handling
12−
13−## Hakyll 3.2
14−
15−- Switch to type-safe identifiers: e.g. `Identifier` becomes
16− `Identifier (Page String)`
17−
18−## Hakyll 3.1.1
19−
20−- Allow `group` in rules DSL
21−
22−## Hakyll 3.1
23−
24−- New `match` function in rules DSL
25−- More expressive `Pattern`s
26−
27−## Hakyll 3
28−
29−- Complete rewrite
30−
31−## Hakyll 2.4.1
32−
33−- Add a number of utility functions
34−- Fix bug in `enableIndexUrl` mode
35−
36−## Hakyll 2.4
37−
38−- Arrow based interface to pandoc
39−- Easier custom fields
40−
41−## Hakyll 2.3
42−
43−- Ability to choose between preview modes.
44−- Simple static configuration available.
45−- Support hamlet templates.
46−
47−## Hakyll 2.2.2
48−
49−- Cabal dependency fixes.
50−
51−## Hakyll 2.2.1
52−
53−- Allow custom time locale for `renderDate`.
54−- Render RSS feeds with `CDATA` sections.
55−
56−## Hakyll 2.2
57−
58−- Allow markup languages in templates.
59−
60−## Hakyll 2.1.1
61−
62−- Fix issues in autocompilation/preview mode.
63−
64−## Hakyll 2.1
65−
66−May 21, 2010
67−
68−- Expose pandoc options to HakyllConfiguration.
69−- Allow dashes in pages.
70−- Some typo's and bugs fixed.
71−
72−## Hakyll 2.0
73−
74−March 31, 2010
75−
76−- Rewrite of the API to a clean, Arrow based API.
77−- Added built-in support for RSS and Atom.
78−- Added more documentation.
79−- Added pagination.
80−- Many bugfixes.
81−
82−## Hakyll 1.4
83−
84−February 17, 2010
85−
86−- Added an autocompilation feature.
87−- Support for index URL's (`enableIndexUrl`).
88−
89−## Hakyll 1.3
90−
91−January 30, 2010
92−
93−- Added categories in addition to tags.
94−- Added `createListing` and `createListingWith` function for a more high-level
95− way to create listings.
96−
97−## Hakyll 1.2
98−
99−January 27, 2010
100−
101−- `Data.Binary` is now used for serialization.
102−- Rewrite of the caching system.
103−- Specialized data structure for templates.
104−- Caching of pages and templates.
105−
106−## Hakyll 1.1
107−
108−January 19, 2010
109−
110−- Switched to a custom `Hakyll` monad stack instead of the `IO` monad.
111−- Page sections.
112−- Combining renderables.
113−- `renderAndConcat` can now use multiple templates.
114−
115−## Hakyll 1.0
116−
117−January 14, 2009
118−
119−- First stable release.
120−- Custom templating system.
121−- Added `$root` key for relative URL's.
122−
123−## Hakyll 0.4
124−
125−January 8, 2010
126−
127−- Added examples.
128−- Added `ContextManipulation` type.
129−
130−## Hakyll 0.3
131−
132−December 28, 2009
133−
134−- Added a general `directory` function.
135−- Added CSS compression.
136−- Added tag support.
137−- Added a simple HTTP server for testing purposes.
138−
139−## Hakyll 0.2
140−
141−December 16, 2010
142−
143−- Abstracted `Renderable` type.
144−- Added simple caching and dependency checking.
145−
146−## Hakyll 0.1
147−
148−December 5, 2009
149−
150−- Initial release.
web/css/default.css+13−7
@@ -13,16 +13,21 @@ body {
1313}
1414
1515div#header {
16+ font-size: 28px;
17+ font-weight: bold;
1618 height: 32px;
17− margin: 20px 0px 20px 60px;
19+ line-height: 32px;
20+ margin: 24px 0px 12px 128px;
1821 text-transform: uppercase;
19− font-weight: bold;
20− font-size: 28px;
2122}
2223
2324div#header img {
24− display: inline;
25− margin: 0px 0px 0px 0px;
25+ float: left;
26+ margin: 0px 30px 0px 0px;
27+}
28+
29+div#header a {
30+ text-decoration: none;
2631}
2732
2833div#main {
@@ -77,12 +82,12 @@ a {
7782
7883h1 {
7984 font-size: 20px;
80− margin: 24px 0px 16px 0px;
85+ margin: 12px 0px 12px 0px;
8186}
8287
8388h2 {
8489 font-size: 18px;
85− margin: 24px 0px 16px 0px;
90+ margin: 12px 0px 12px 0px;
8691}
8792
8893h3 {
@@ -91,6 +96,7 @@ h3 {
9196
9297h1, h2, h3 {
9398 text-transform: uppercase;
99+ font-weight: bold;
94100}
95101
96102h1 a, h2 a, h3 a {
web/css/syntax.css+61−18
@@ -1,18 +1,61 @@
1−/* Generated by pandoc. */
2−table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode, table.sourceCode pre
3− { margin: 0; padding: 0; border: 0; vertical-align: baseline; border: none; }
4−td.lineNumbers { border-right: 1px solid #AAAAAA; text-align: right; color: #AAAAAA; padding-right: 5px; padding-left: 5px; }
5−td.sourceCode { padding-left: 5px; }
6−pre.sourceCode span.kw { color: #007020; font-weight: bold; }
7−pre.sourceCode span.dt { color: #902000; }
8−pre.sourceCode span.dv { color: #40a070; }
9−pre.sourceCode span.bn { color: #40a070; }
10−pre.sourceCode span.fl { color: #40a070; }
11−pre.sourceCode span.ch { color: #4070a0; }
12−pre.sourceCode span.st { color: #4070a0; }
13−pre.sourceCode span.co { color: #60a0b0; font-style: italic; }
14−pre.sourceCode span.ot { color: #007020; }
15−pre.sourceCode span.al { color: red; font-weight: bold; }
16−pre.sourceCode span.fu { color: #06287e; }
17−pre.sourceCode span.re { }
18−pre.sourceCode span.er { color: red; font-weight: bold; }
1+a.sourceLine { display: inline-block; line-height: 1.25; }
2+a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
3+a.sourceLine:empty { height: 1.2em; }
4+.sourceCode { overflow: visible; }
5+code.sourceCode { white-space: pre; position: relative; }
6+div.sourceCode { margin: 1em 0; }
7+pre.sourceCode { margin: 0; }
8+@media screen {
9+div.sourceCode { overflow: auto; }
10+}
11+@media print {
12+code.sourceCode { white-space: pre-wrap; }
13+a.sourceLine { text-indent: -1em; padding-left: 1em; }
14+}
15+pre.numberSource a.sourceLine
16+ { position: relative; left: -4em; }
17+pre.numberSource a.sourceLine::before
18+ { content: attr(title);
19+ position: relative; left: -1em; text-align: right; vertical-align: baseline;
20+ border: none; pointer-events: all; display: inline-block;
21+ -webkit-touch-callout: none; -webkit-user-select: none;
22+ -khtml-user-select: none; -moz-user-select: none;
23+ -ms-user-select: none; user-select: none;
24+ padding: 0 4px; width: 4em;
25+ color: #aaaaaa;
26+ }
27+pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
28+div.sourceCode
29+ { }
30+@media screen {
31+a.sourceLine::before { text-decoration: underline; }
32+}
33+code span.al { color: #ff0000; font-weight: bold; } /* Alert */
34+code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
35+code span.at { color: #7d9029; } /* Attribute */
36+code span.bn { color: #40a070; } /* BaseN */
37+code span.bu { } /* BuiltIn */
38+code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
39+code span.ch { color: #4070a0; } /* Char */
40+code span.cn { color: #880000; } /* Constant */
41+code span.co { color: #60a0b0; font-style: italic; } /* Comment */
42+code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
43+code span.do { color: #ba2121; font-style: italic; } /* Documentation */
44+code span.dt { color: #902000; } /* DataType */
45+code span.dv { color: #40a070; } /* DecVal */
46+code span.er { color: #ff0000; font-weight: bold; } /* Error */
47+code span.ex { } /* Extension */
48+code span.fl { color: #40a070; } /* Float */
49+code span.fu { color: #06287e; } /* Function */
50+code span.im { } /* Import */
51+code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
52+code span.kw { color: #007020; font-weight: bold; } /* Keyword */
53+code span.op { color: #666666; } /* Operator */
54+code span.ot { color: #007020; } /* Other */
55+code span.pp { color: #bc7a00; } /* Preprocessor */
56+code span.sc { color: #4070a0; } /* SpecialChar */
57+code span.ss { color: #bb6688; } /* SpecialString */
58+code span.st { color: #4070a0; } /* String */
59+code span.va { color: #19177c; } /* Variable */
60+code span.vs { color: #4070a0; } /* VerbatimString */
61+code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
web/examples.markdown+176−21
@@ -2,15 +2,6 @@
22title: Examples
33---
44
5−# Simple examples
6−
7−A number of simple examples are available in the GitHub repository. They can be
8−found [here](https://github.com/jaspervdj/hakyll-examples). The README located
9−in that directory explains which example does what. You can fetch the entire
10−directory using
11−
12− git clone git://github.com/jaspervdj/hakyll-examples.git
13−
145# People using Hakyll
156
167A lot of sites running Hakyll also publish the source code. This is a very
@@ -18,8 +9,184 @@ interesting resource to learn from as well. If you're using Hakyll for your
189site, and the source code is available, please notify me so I can add you to
1910this list. This list has no particular ordering.
2011
12+Note that there is also a [Hakyll CSS garden] which has some themes you can use
13+directly with the default Hakyll site.
14+
15+[Hakyll CSS garden]: http://katychuang.com/hakyll-cssgarden/gallery/
16+
17+## Hakyll 4
18+
2119- <http://jaspervdj.be/>,
2220 [source](https://github.com/jaspervdj/jaspervdj)
21+- <http://www.imagination-land.org/>,
22+ [source](https://github.com/Keruspe/blog/)
23+- <https://bananasandlenses.net/>,
24+ [source](https://github.com/denisshevchenko/bananasandlenses)
25+- <http://blog.clement.delafargue.name/>,
26+ [source](https://github.com/divarvel/blog)
27+- <http://bneijt.nl>,
28+ [source](https://github.com/bneijt/bneijt.nl)
29+- <http://brianshourd.com/>,
30+ [source](https://github.com/brianshourd/brianshourd.com)
31+- <http://variadic.me>,
32+ [source](https://github.com/eakron/variadic.me)
33+- <http://yannesposito.com/>,
34+ [source](https://github.com/yogsototh/yblog)
35+- <http://dannysu.com/>,
36+ [source](https://github.com/dannysu/hakyll-blog)
37+- <http://meta.plasm.us/>,
38+ [source](https://github.com/travisbrown/metaplasm)
39+- <http://workforpizza.com/>,
40+ [source](https://github.com/irneh/workforpizza)
41+- <http://poleiro.info/>,
42+ [source](https://github.com/arthuraa/poleiro)
43+- <http://www.austinrochford.com/>,
44+ [source](https://github.com/AustinRochford/blog)
45+- <http://www.regisfoucault.com/>,
46+ [source](https://github.com/regisfoucault/blog)
47+- <http://www.chaoxuprime.com/>,
48+ [source](https://github.com/chaoxu/chaoxu.github.io/tree/develop)
49+- <http://www.skybluetrades.net/>,
50+ [source](https://github.com/ian-ross/blog)
51+- <http://chromaticleaves.com/>,
52+ [source](https://github.com/ericrasmussen/chromaticleaves)
53+- <http://www.clever-cloud.com/en/>,
54+ [source](https://github.com/CleverCloud/clever-cloud.com)
55+- <http://blaenkdenum.com/>,
56+ [source](https://github.com/blaenk/blaenk.github.io)
57+- <https://xinitrc.de/>,
58+ [source](https://github.com/xinitrc/xinitrc.de)
59+- <http://nickcharlton.net/>,
60+ [source](https://github.com/nickcharlton/nickcharlton.net)
61+- <http://scr.stunts.hu/>,
62+ [literate source](http://scr.stunts.hu/hakyll.html)
63+- <http://www.eanalytica.com/>,
64+ [literate source](http://www.eanalytica.com/site/)
65+- <http://cse.iitk.ac.in/users/ppk>,
66+ [source](https://github.com/piyush-kurur-pages/website)
67+- <http://web.engr.oregonstate.edu/~walkiner/>,
68+ [source](https://github.com/walkie/WebPage)
69+- <http://techblog.rosedu.org/>,
70+ [source](https://github.com/rosedu/techblog)
71+- <http://freizl.github.io>,
72+ [source](https://github.com/freizl/freizl.github.com/tree/master/build)
73+- <http://www.gwern.net/>,
74+ [source](https://github.com/gwern/gwern.net)
75+- <http://www.corentindupont.info/>,
76+ [source](https://github.com/cdupont/CorentinDupont-WebPage)
77+- <http://jelv.is>,
78+ [source](https://github.com/TikhonJelvis/website)
79+- <https://secure.plaimi.net/>,
80+ [source](https://github.com/plaimi/www)
81+- <http://duplode.github.io/>,
82+ [source](https://github.com/duplode/duplode.github.io/tree/sources)
83+- <http://tzemanovic.github.io/>,
84+ [source](https://github.com/tzemanovic/tzemanovic)
85+- <http://listx.github.io>,
86+ [source](https://github.com/listx/listx_blog)
87+- <http://kyle.marek-spartz.org>,
88+ [source](https://github.com/zeckalpha/kyle.marek-spartz.org)
89+- <http://ladatura-corsets.com/>,
90+ [source](https://github.com/dsferruzza/datura-corsets)
91+- <http://reichertbrothers.com/>,
92+ [source](https://github.com/rbros/reichertbrothers.com)
93+- <http://alemedeiros.sdf.org>,
94+ [source](https://github.com/alemedeiros/homepage)
95+- <http://www.haskell.mn>,
96+ [source](https://github.com/HaskellMN/www.haskell.mn)
97+- <https://axiomatic.neophilus.net/>,
98+ [source](https://github.com/Libbum/AxiomaticSemantics)
99+- [https://tesser.org/](http://web.archive.org/web/20161022221713/https://tesser.org/),
100+ [source](https://github.com/fractalcat/tesser.org)
101+- <https://weien.io/>,
102+  [source](https://github.com/wei2912/blog-src)
103+- <http://www.metasepi.org/>,
104+ [source](https://github.com/metasepi/metasepi-website)
105+- <http://gisli.hamstur.is/>,
106+ [source](https://github.com/gislik/gisli.hamstur.is)
107+- <http://blog.ssanj.net/>,
108+ [source](https://github.com/ssanj/babyloncandle)
109+- <http://ruhaskell.org/>,
110+ [source](https://github.com/ruHaskell/ruhaskell)
111+- <http://www.haskell.od.ua/>,
112+ [source](https://github.com/odhug/odhug.github.com)
113+- <http://blog.rekahsoft.ca/>,
114+ [source](http://git.rekahsoft.ca/blog-rekahsoft-ca)
115+- <http://www.andreascarpino.it>,
116+ [source](https://gitlab.com/ilpianista/website)
117+- <http://cascadeofinsights.com/>,
118+ [source](https://github.com/agbell/agbell.github.io/tree/hakyll)
119+- <http://www.xoltar.org/>,
120+ [source](https://github.com/xoltar/xoltar.org)
121+- <http://sigkill.dk/>
122+ [literate source](http://sigkill.dk/programs/sigkill.html)
123+- <http://curry-club-augsburg.de/>,
124+ [source](https://github.com/curry-club-aux/curry-club-augsburg.de)
125+- <http://ismailmustafa.com/>,
126+ [source](https://github.com/ismailmustafa/ismailmustafa.github.io/tree/hakyll)
127+- <http://lettier.github.io/>,
128+ [source](https://github.com/lettier/lettier.github.io)
129+- <http://raindev.io>,
130+ [source](https://github.com/raindev/raindev.github.io)
131+- <http://hal2016.haskell.org/>,
132+ [source](https://github.com/nomeata/hal2016-website/)
133+- <http://acehack.org/>,
134+ [source](https://github.com/sakshamsharma/acehack/)
135+- <https://nek0.eu/>,
136+ [source](https://github.com/nek0/nek0.eu)
137+- <http://static-dust.klpn.se/>,
138+ [source](https://github.com/klpn/static-dust)
139+- <http://www.kuznero.com/>,
140+ [source](https://github.com/kuznero/kuznero.github.io)
141+- <http://nicolerauch.de/>,
142+ [source](https://github.com/NicoleRauch/BusinessWebsite)
143+- <http://johnduhamel.io/>,
144+ [source](https://github.com/jjduhamel/blog)
145+- <http://alexanderlobov.ru/>,
146+ [source](https://github.com/alexanderlobov/homepage)
147+- <http://www.rosanbo.com/>,
148+ [source](https://github.com/polo2ro/blog)
149+- <http://limansky.me/>,
150+ [source](https://github.com/limansky/limansky_me)
151+- <http://rahul.nu/>,
152+ [source](https://github.com/rp/rp.github.io/tree/dev)
153+- <http://haskanything.com/>,
154+ [source](https://github.com/beerendlauwers/HaskAnything)
155+- <https://anler.me/>,
156+ [source](https://github.com/anler/anler.me)
157+- <http://argumatronic.com/>,
158+ [source](https://gitlab.com/GinBaby/argumatronic/tree/master)
159+- <http://sillybytes.net/>,
160+ [source](https://github.com/sillybytes/sillybytes)
161+- <https://falconpilot.github.io/>,
162+ [source](https://github.com/FalconPilot/falconpilot.github.io)
163+- <http://spyhop.cafe/>,
164+ [source](https://github.com/thunky-monk/spyhop-cafe)
165+- <https://bendo.github.io/>,
166+ [source](https://github.com/bendo/bendo.github.io)
167+- <http://adarsh.cc/>,
168+ [source](https://github.com/adarshp/adarsh.cc)
169+- <http://0a.io>,
170+ [source](https://github.com/arrchyy/0a.io)
171+- <https://nasy.moe/>,
172+ [source](https://github.com/nasyxx/NasyMoe)
173+- <http://uniprocess.org/>,
174+ [source](https://gitlab.com/uniprocess/uniprocess.org)
175+- <https://regularflolloping.com/>,
176+ [source](https://github.com/techieAgnostic/rf)
177+- <http://enter-haken.github.io/>,
178+ [source](https://github.com/enter-haken/blog)
179+- <https://modular.blind.guru/>,
180+ [source](https://github.com/mlang/eurorack-metadata)
181+- <http://www.jonashietala.se/>,
182+ [source](https://github.com/treeman/jonashietala)
183+- <https://abhinavsarkar.net/>,
184+ [source](https://github.com/abhin4v/abhin4v.github.io)
185+- <https://dailyreadingnotes.com/>,
186+ [source](https://github.com/hackeryarn/daily-reading-notes)
187+
188+## Hakyll 3.X
189+
23190- <http://ethanschoonover.com/>,
24191 [source](https://github.com/altercation/ethanschoonover.com)
25192- <http://extralogical.net/>,
@@ -32,18 +199,10 @@ this list. This list has no particular ordering.
32199 [source](https://github.com/wunki/wunki.org)
33200- <http://rs.io>,
34201 [source](https://github.com/robertseaton/rs.io/)
35−- <http://www.gwern.net/>,
36− source: `darcs get http://www.gwern.net`
37−- <http://www.skybluetrades.net/>,
38− [source](https://github.com/ian-ross/blog)
39202- <http://blog.coldflake.com/>,
40203 [source](https://github.com/marcmo/blog.coldflake)
41204- <http://deepak.jois.name>,
42205 [source](https://github.com/deepakjois/website)
43−- <http://sigkill.dk/>,
44− [literate source](http://sigkill.dk/programs/sigkill.html)
45−- <http://variadic.me>,
46− [source](https://github.com/eakron/variadic.me)
47206- <http://orftz.com>,
48207 [source](https://github.com/orftz/orftz.com)
49208- <http://citationneeded.me/>,
@@ -52,9 +211,5 @@ this list. This list has no particular ordering.
52211 [source](http://gitorious.org/shakthimaan-blog)
53212- <http://www.alfredodinapoli.com/>,
54213 [source](https://github.com/CharlesStain/alfredodinapoli.com)
55−- <http://www.rohanjain.in/>,
56− [source](https://github.com/crodjer/rohanjain.in)
57−- <http://datahackermd.com>,
58− [source](http://github.com/akshayjshah/datahackermd)
59214- <http://michaelxavier.net>,
60215 [source](https://github.com/michaelxavier/michaelxavier.net)
web/images/hakyll-system-2.png

Binary file not shown.

web/index.markdown+4−2
@@ -31,8 +31,10 @@ and TeX support, including syntax highlighting and other goodies.
3131
3232# Getting Started
3333
34−You can get the latest version from hackage using `cabal install hakyll`. Then,
35−you can:
34+You can get the latest version from hackage using `cabal install hakyll`, or
35+using [stack] by using `stack install hakyll`. Then, you can:
36+
37+[stack]: http://www.haskellstack.org/
3638
3739- read the [tutorials](/tutorials.html);
3840- mail the [google discussion group](http://groups.google.com/group/hakyll);
web/libraries.markdownnew file+21
@@ -0,0 +1,21 @@
1+---
2+title: Libraries
3+---
4+
5+# Libraries
6+
7+Since some functionality is too specific to be included in the main Hakyll
8+library, people have generously written utility libraries and tools for Hakyll.
9+Here is a list of the ones I know of (I would be happy to hear about others).
10+
11+- [hakyll-contrib-links](http://hackage.haskell.org/package/hakyll-contrib-links),
12+ a Hakyll library that helps maintain a separate links database.
13+- [hakyll-agda](https://hackage.haskell.org/package/hakyll-agda),
14+ a simple module useful to generate blog posts from literate Agda files.
15+- [hakyll-contrib-hyphenation](http://hackage.haskell.org/package/hakyll-contrib-hyphenation),
16+ automatic hyphenation for Hakyll.
17+- [hakyll-convert](https://hackage.haskell.org/package/hakyll-convert),
18+ a conversion tool from other blog engines to Hakyll.
19+- [hakyll-elm](https://hackage.haskell.org/package/hakyll-elm),
20+ Hakyll wrapper for the Elm compiler.
21+- [hakyll-sass](http://hackage.haskell.org/package/hakyll-sass), a compiler for SASS templates.
web/site.hs+68−22
@@ -1,20 +1,39 @@
11--------------------------------------------------------------------------------
22{-# LANGUAGE OverloadedStrings #-}
3−import Control.Monad (forM_)
4−import Data.Monoid (mappend)
3+import Control.Arrow (second)
4+import Control.Monad (filterM, forM_)
5+import Data.List (isPrefixOf, sortBy)
6+import Data.Monoid ((<>))
7+import Data.Ord (comparing)
58import Hakyll
9+import System.Directory (copyFile)
10+import System.FilePath (dropTrailingPathSeparator, splitPath)
611import Text.Pandoc
712
813
914--------------------------------------------------------------------------------
1015main :: IO ()
1116main = hakyllWith config $ do
17+ -- Copy CHANGELOG.md here.
18+ preprocess $ copyFile "../CHANGELOG.md" "releases.markdown"
19+
20+ -- CSS
1221 match "css/*" $ do
1322 route idRoute
1423 compile compressCssCompiler
1524
1625 -- Static directories
17− forM_ ["images/*", "examples/*", "reference/**"] $ \f -> match f $ do
26+ forM_ ["images/*", "examples/*"] $ \f -> match f $ do
27+ route idRoute
28+ compile copyFileCompiler
29+
30+ -- Haddock stuff
31+ match "reference/**.html" $ do
32+ route idRoute
33+ compile $ fmap (withUrls hackage) <$> getResourceString
34+
35+ -- Haddock stuff
36+ match ("reference/**" `mappend` complement "**.html") $ do
1837 route idRoute
1938 compile copyFileCompiler
2039
@@ -28,28 +47,20 @@ main = hakyllWith config $ do
2847 -- Tutorials
2948 match "tutorials/*" $ do
3049 route $ setExtension "html"
31− compile $ pandocCompilerWith defaultHakyllParserState withToc
50+ compile $ pandocCompilerWith defaultHakyllReaderOptions withToc
3251 >>= loadAndApplyTemplate "templates/tutorial.html" defaultContext
3352 >>= loadAndApplyTemplate "templates/default.html" defaultContext
3453 >>= relativizeUrls
3554
3655 -- Tutorial list
37− match "tutorials.html" $ do
56+ create ["tutorials.html"] $ do
3857 route idRoute
3958 compile $ do
40− tutorials <- loadAll "tutorials/*"
41− itemTpl <- loadBody "templates/tutorial-item.html"
42− list <- applyTemplateList itemTpl defaultContext $
43− chronological tutorials
44−
45− let tutorialsCtx =
46− constField "title" "Tutorials" `mappend`
47− constField "tutorials" list `mappend`
48− defaultContext
49−
59+ ctx <- tutorialsCtx <$>
60+ sortBy (comparing itemIdentifier) <$> loadAll "tutorials/*"
5061 makeItem ""
51− >>= loadAndApplyTemplate "templates/tutorials.html" tutorialsCtx
52− >>= loadAndApplyTemplate "templates/default.html" tutorialsCtx
62+ >>= loadAndApplyTemplate "templates/tutorials.html" ctx
63+ >>= loadAndApplyTemplate "templates/default.html" ctx
5364 >>= relativizeUrls
5465
5566 -- Templates
@@ -57,15 +68,50 @@ main = hakyllWith config $ do
5768 where
5869 withToc = defaultHakyllWriterOptions
5970 { writerTableOfContents = True
60− , writerTemplate = "$toc$\n$body$"
61− , writerStandalone = True
71+ , writerTemplate = Just "$toc$\n$body$"
6272 }
6373
6474
6575--------------------------------------------------------------------------------
6676config :: Configuration
6777config = defaultConfiguration
68− { verbosity = Debug
69− , deployCommand = "rsync --checksum -ave 'ssh -p 2222' \
70− \_site/* jaspervdj@jaspervdj.be:jaspervdj.be/tmp/hakyll4"
78+ { deployCommand = "rsync --checksum -ave 'ssh -p 2222' \
79+ \_site/* \
80+ \jaspervdj@jaspervdj.be:jaspervdj.be/hakyll/"
7181 }
82+
83+
84+--------------------------------------------------------------------------------
85+-- | Turns
86+--
87+-- > /usr/share/doc/ghc/html/libraries/base-4.6.0.0/Data-String.html
88+--
89+-- into
90+--
91+-- > http://hackage.haskell.org/packages/archive/base/4.6.0.0/doc/html/Data-String.html
92+hackage :: String -> String
93+hackage url
94+ | "/usr" `isPrefixOf` url =
95+ "http://hackage.haskell.org/packages/archive/" ++
96+ packageName ++ "/" ++ version' ++ "/doc/html/" ++ baseName
97+ | otherwise = url
98+ where
99+ (packageName, version') = second (drop 1) $ break (== '-') package
100+ (baseName : package : _) = map dropTrailingPathSeparator $
101+ reverse $ splitPath url
102+
103+
104+--------------------------------------------------------------------------------
105+-- | Partition tutorials into tutorial series, other articles, external articles
106+tutorialsCtx :: [Item String] -> Context String
107+tutorialsCtx tuts =
108+ constField "title" "Tutorials" <>
109+ listField "main" defaultContext (ofType "main") <>
110+ listField "articles" defaultContext (ofType "article") <>
111+ listField "externals" defaultContext (ofType "external") <>
112+ listField "robertwpearce" defaultContext (ofType "robertwpearce") <>
113+ defaultContext
114+ where
115+ ofType ty = filterM (\item -> do
116+ mbType <- getMetadataField (itemIdentifier item) "type"
117+ return $ Just ty == mbType) tuts
web/templates/default.html+9−9
@@ -15,27 +15,27 @@
1515 <meta name="description" content="Hakyll - A Static Site Generator in Haskell."/>
1616 </head>
1717 <body>
18+ <!-- Fork me on Github -->
19+ <a href="https://github.com/jaspervdj/hakyll"><img style="position: absolute; top: 0; right: 0; border: 0; margin: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
1820 <div id="main">
1921 <div id="header">
20− <img src="/images/lambda.png" alt="lambda" />
21− Hakyll - $title$
22+ <a href="/">
23+ <img src="/images/lambda.png" alt="lambda" />
24+ Hakyll
25+ </a>
2226 </div>
2327 <!-- Sidebar. -->
2428 <div id="sidebar">
2529 <h1>Navigation</h1>
2630 <a href="/index.html">home</a>
2731 <a href="/tutorials.html">tutorials</a>
32+ <a href="/libraries.html">libraries</a>
2833 <a href="/reference.html">reference</a>
2934 <a href="/about.html">about</a>
3035 <a href="/tutorials/faq.html">faq</a>
3136 <a href="/examples.html">examples</a>
32− <a href="/changelog.html">changelog</a></p>
33−
34− <!-- Flattr button -->
35− <h1>Donate</h1>
36− <a href="http://flattr.com/thing/291889/Hakyll" target="_blank">
37− <img src="http://api.flattr.com/button/flattr-badge-large.png"
38− alt="Flattr this" title="Flattr this" border="0" /></a>
37+ <a href="/releases.html">releases</a>
38+ <a href="https://jaspervdj.be/contact.html#donate" target="_blank">donate</a>
3939 </div>
4040
4141 <div id="content">
web/templates/tutorial-item.htmldeleted−1
@@ -1 +0,0 @@
1−<li><a href="$url$">$title$</a> by <em>$author$</em></li>
web/templates/tutorials.html+47−5
@@ -1,14 +1,56 @@
11<h1>Tutorials about Hakyll</h1>
2+<h2 id="series">Tutorial series</h2>
3+<ul>
4+ $for(main)$
5+ <li>
6+ <a href="$url$">$title$</a>
7+ </li>
8+ $endfor$
9+</ul>
10+<h2 id="articles">Other articles</h2>
11+<p>In no particular order:</p>
12+<ul>
13+ $for(articles)$
14+ <li>
15+ <a href="$url$">$title$</a> by <em>$author$</em>
16+ </li>
17+ $endfor$
18+</ul>
19+<h2 id="external-articles">External articles</h2>
20+<p>
21+ These are articles on external blogs. If you wrote a similar article, feel
22+ free to <a href="http://jaspervdj.be/contact.html">shoot me an email</a> so
23+ I can add it to the list.
24+</p>
25+<p>In no particular order:</p>
26+<ul>
27+ $for(externals)$
28+ <li>
29+ <a href="$url$">$title$</a> by <em>$author$</em>
30+ </li>
31+ $endfor$
32+</ul>
33+<h2 id="robertwpearce">Robert Pearce's series</h2>
234<p>
3− Here is a list of tutorials I've written about Hakyll:
35+ <a href="https://robertwpearce.com/">Robert Pearce</a> has been writing an
36+ excellent tutorial-style series on Hakyll as well. You can find those here:
437</p>
538<ul>
6− $tutorials$
39+ $for(robertwpearce)$
40+ <li>
41+ <a href="$url$">$title$</a>
42+ </li>
43+ $endfor$
744</ul>
845<p>
946 All these tutorials assume you are using the latest stable version of
1047 Hakyll. If this is not the case, you might want to update using:
11− <pre><code>$ ghc-pkg unregister hakyll
12−$ cabal update
13−$ cabal install hakyll</code></pre>
1448</p>
49+<pre><code>$$ ghc-pkg unregister hakyll
50+$$ cabal update
51+$$ cabal install hakyll</code></pre>
52+
53+<p>
54+ Or using stack:
55+</p>
56+<pre><code>$$ stack install hakyll</code></pre>
web/tutorials/01-installation.markdown+20−17
@@ -1,51 +1,54 @@
11---
22title: Installation
33author: Jasper Van der Jeugt
4+type: main
45---
56
67Installation
78------------
89
9−Installation is provided using [cabal], and some packages are available for
10−different distributions.
10+Installation is provided via Hackage, and some packages are available for
11+different distributions. For installation from source (i.e. via Hackage),
12+[stack] is recommended:
1113
12− $ cabal install hakyll
14+ $ stack install hakyll
1315
14−[cabal]: http://www.haskell.org/cabal/
15−
16−If you have a recent installation of `cabal` and your time is somewhat valuable,
17−use:
18−
19− $ cabal install -j hakyll
16+[stack]: http://www.haskellstack.org/
2017
2118Linux distro packages:
2219
23−- [Archlinux (AUR)](https://aur.archlinux.org/packages/haskell-hakyll/)
2420- [Debian unstable](http://packages.debian.org/source/sid/haskell-hakyll)
21+- [Fedora](https://apps.fedoraproject.org/packages/ghc-hakyll)
22+- [Nix](https://nixos.org/nixos/packages.html#hakyll): `$ nix-env -i hakyll`
2523
2624Building the example site
2725-------------------------
2826
29−Apart from the main Hakyll library, the cabal package also provided you with an
27+Apart from the main Hakyll library, the cabal package also provides you with an
3028executable `hakyll-init` to create an example site. This is an easy way to get
3129started:
3230
33− $ hakyll-init my-site
31+ $ stack exec hakyll-init my-site
32+
33+This creates a folder `my-site` in the current directory, with some example
34+content and a generic configuration.
3435
35−If `hakyll-init` is not found, you should make sure `$HOME/.cabal/bin` is in
36−your `$PATH`.
36+If `hakyll-init` is not found, you should make sure your stack bin path
37+(usually `$HOME/.local/bin`) is in your `$PATH`. You can check your stack local
38+bin path by running `stack path --local-bin`.
3739
3840The file `site.hs` holds the configuration of your site, as an executable
3941haskell program. We can compile and run it like this:
4042
4143 $ cd my-site
42− $ ghc --make site.hs
43− $ ./site build
44+ $ stack init # creates stack.yaml file based on my-site.cabal
45+ $ stack build
46+ $ stack exec site build
4447
4548If you installed `hakyll` with a preview server (this is the default), you can
4649now use
4750
48− $ ./site preview
51+ $ stack exec site watch
4952
5053and have a look at your site at
5154[http://localhost:8000/](http://localhost:8000/).
web/tutorials/02-basics.markdown+13−8
@@ -1,26 +1,31 @@
11---
22title: The basics
33author: Jasper Van der Jeugt
4+type: main
45---
56
67Building and cleaning
78---------------------
89
910If you followed along with the previous tutorial, you should now have the
10−example site up and running. By running `./site build`, you created two
11−directories:
11+example site up and running. By running `stack exec site build`, you created
12+two directories:
1213
1314- `_site`, with your site as HTML files, ready to be deployed;
1415- `_cache`, which Hakyll uses internally.
1516
16−`./site clean` removes these directories, and `./site rebuild` performs a
17−`clean` and then a `build`.
17+`stack exec site clean` removes these directories, and `stack exec site
18+rebuild` performs a `clean` and then a `build`.
1819
19−In general, it's only necessary to use `rebuild` when you made changes to your
20−`site.hs`, and not when you just made changes to the contents of your website.
20+In general, you want to use `stack exec site build` when you just made changes
21+to the contents of your website. If you made changes to `site.hs`, you need to
22+recompile `site.hs` followed by a rebuild:
2123
22−At this point, feel free to change some files, `./site build` and see what
23−happens!
24+ stack build
25+ stack exec site rebuild
26+
27+At this point, feel free to change some files, `stack exec site build` and see
28+what happens!
2429
2530Pages and metadata
2631------------------
web/tutorials/03-rules-routes-compilers.markdown+21−1
@@ -1,6 +1,7 @@
11---
22title: Rules, routes and compilers
33author: Jasper Van der Jeugt
4+type: main
45---
56
67Basic rules
@@ -36,7 +37,7 @@ match "css/*" $ do
3637
3738```
3839
39−This is a declarative DSL: the order in which you write the rules make little
40+This is a declarative DSL: the order in which you write the rules makes little
4041difference, Hakyll will use dependency tracking to determine the correct order.
4142
4243We group the different rules using `match`. The first argument for `match` is a
@@ -46,6 +47,25 @@ and all files in the `css/` directory.
4647
4748[Pattern]: /reference/Hakyll-Core-Identifier-Pattern.html
4849
50+However, we can see that one item makes no use of `match`, but uses `create`
51+instead.
52+
53+```haskell
54+create ["archive.html"] $ do
55+ route idRoute
56+ compile $ do
57+ ...
58+```
59+
60+Don't pay attention to the somewhat complicated-looking stuff in `compile` --
61+this will become clear soon. The real question here is why we use `create`
62+instead of `match`.
63+
64+The answer is simple: there is no `archive.html` file in our project directory!
65+So if we were to use `match`, no file would be matched, and hence, nothing
66+would appear in the output directory. `create`, however, ensures the items
67+listed are always produced.
68+
4969Basic routes
5070------------
5171
web/tutorials/04-compilers.markdown+149−26
@@ -1,6 +1,7 @@
11---
2−title: More on compilers: load, and templates
2+title: 'More on compilers: load, and templates'
33author: Jasper Van der Jeugt
4+type: main
45---
56
67Loading items
@@ -41,9 +42,11 @@ Templates
4142
4243Let's have a look at a simple template:
4344
44− <h1>$title$</h1>
45− <div class="info">Posted on $date$</div>
46− $body$
45+```html
46+<h1>$title$</h1>
47+<div class="info">Posted on $date$</div>
48+$body$
49+```
4750
4851As you can probably guess, template files just contain text and only the `$`
4952character has special meaning: text between dollar signs ("fields") is replaced
@@ -51,9 +54,11 @@ when the template is applied. If you want an actual dollar sign in the output,
5154use `$$`.
5255
5356You usually compile the templates from disk using the aptly named
54−`templateCompiler`:
57+`templateBodyCompiler`:
5558
56− match "templates/*" $ compile templateCompiler
59+```haskell
60+match "templates/*" $ compile templateBodyCompiler
61+```
5762
5863Notice the lack of `route` here: this is because we don't need to write the
5964templates to your `_site` folder, we just want to use them elsewhere.
@@ -86,9 +91,9 @@ And `$title$` like this:
8691
8792```haskell
8893titleContext :: Context a
89−titleContext :: field "title" $ \item -> do
94+titleContext = field "title" $ \item -> do
9095 metadata <- getMetadata (itemIdentifier item)
91− return $ fromMaybe "No title" $ M.lookup "title" metadata
96+ return $ fromMaybe "No title" $ lookupString "title" metadata
9297```
9398
9499And compose them using the `Monoid` instance:
@@ -111,8 +116,10 @@ you to use:
111116- `$path$` for the original filepath of the page;
112117- `$foo$` where foo is specified in the metadata.
113118
114−`$date$` is not provided by default, you can see how we add it in the definition
115−of `postCtx` in `site.hs`:
119+`$date$` is not provided by default. In the scaffold, we use the convenience
120+context function `dateField`, which will parse an `Item`'s filename to check if
121+it begins with a date. You can see how we add it in the definition of `postCtx`
122+in `site.hs`:
116123
117124```haskell
118125postCtx :: Context String
@@ -124,7 +131,7 @@ postCtx =
124131### Loading and applying templates
125132
126133Now we know about templates, context and how to load arbitrary items. This gives
127−us enough background information in order to understand you can apply a
134+us enough background information in order to understand how you can apply a
128135template:
129136
130137```haskell
@@ -141,32 +148,148 @@ compile $
141148 pandocCompiler >>= loadAndApplyTemplate "templates/post.html" postCtx
142149```
143150
144−## Producing a list of items
151+Control flow in templates
152+-------------------------
153+
154+Sometimes string interpolation does not suffice, and you want a little more
155+control over how your templates are laid out. Hakyll provides a few control
156+structures for this. The syntax for these structures was based on the syntax
157+used in pandoc templates, since Hakyll already has tight integration with
158+pandoc.
159+
160+### Conditionals
161+
162+In `templates/post.html` of the example site we generated using `hakyll-init`,
163+we see an example of a conditional:
164+
165+```html
166+<div class="info">
167+ Posted on $date$
168+ $if(author)$
169+ by $author$
170+ $endif$
171+</div>
172+```
173+
174+This example should be pretty straightforward. One important thing to notice is
175+that `$if(foo)$` **does not** check the truth value of `$foo$`: it merely checks
176+if such a key is present.
177+
178+Note that an if-else form is supported as well:
179+
180+```html
181+<div class="info">
182+ Posted on $date$
183+ $if(author)$
184+ by $author$
185+ $else$
186+ by some unknown author
187+ $endif$
188+</div>
189+```
190+
191+### Partials
192+
193+Partials allow you to [DRY] up your templates by putting repetitive actions into
194+separate template files. You can then include them using
195+`$partial("filename.html")$`.
196+
197+[DRY]: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself
198+
199+An example can be found in `templates/archive.html`:
200+
201+```html
202+Here you can find all my previous posts:
203+$partial("templates/post-list.html")$
204+```
205+
206+This partial is just another template and uses the same syntax. Note that in
207+order to use something like this, we also need to load the partial template in
208+our `site.hs`:
209+
210+```haskell
211+match "templates/post-list.html" $ compile templateCompiler
212+```
213+
214+Fortunately, we usually don't need to add this since we already have:
215+
216+```haskell
217+match "templates/*" $ compile templateCompiler
218+```
219+
220+### Producing a list of items: for
145221
146222At this point, everything in the example site we generated should be clear to
147223you, except for how we produce the list of posts in `archive.html` and
148−`index.html`.
224+`index.html`. Let's look at the `templates/post-list.html` template:
225+
226+```html
227+<ul>
228+ $for(posts)$
229+ <li>
230+ <a href="$url$">$title$</a> - $date$
231+ </li>
232+ $endfor$
233+</ul>
234+```
149235
150−However, this really isn't hard and just uses the things we saw before: loading
151−other items and applying templates.
236+This uses the `$for(foo)$` construct. This construct allows you loop over a
237+list, in this case, `$posts$`. Inside the body of this for loop, all fields
238+refer to the current post, e.g.: `$url$`, `$title$` and `$date$`.
239+
240+You can also add a simple separator with the special `$sep$` field.
241+Everything between `$sep$` and `$endfor$` will be regarded as a separator
242+that will only be shown if there is more than one item in the list.
243+
244+```html
245+<ul>
246+ $for(posts)$
247+ $x$
248+ $sep$,
249+ $endfor$
250+</ul>
251+```
152252
153−We can reproduce a list of items in the archive using the following code:
253+
254+Of course, $posts$ does not magically appear. We have to specify this in
255+`site.hs`. Let's look at how `archive.html` is generated:
154256
155257```haskell
156−compile $ do
157− posts <- recentFirst <$> loadAll "posts/*"
158− itemTpl <- loadBody "templates/post-item.html"
159− applyTemplateList itemTpl postCtx posts
258+posts <- recentFirst =<< loadAll "posts/*"
259+let archiveCtx =
260+ listField "posts" postCtx (return posts) `mappend`
261+ constField "title" "Archives" `mappend`
262+ defaultContext
160263```
161264
265+We discussed `loadAll` earlier in this tutorial.
266+
162267`recentFirst` sorts items by date. This relies on the convention that posts are
163−always named `YYYY-MM-DD-title.extension` in Hakyll -- if you use some other
164−format, you'll have to write some other sorting method.
268+always named `YYYY-MM-DD-title.extension` in Hakyll -- or that the date must be
269+present in the metadata.
270+
271+```haskell
272+recentFirst :: [Item a] -> Compiler [Item a]
273+```
274+
275+After loading and sorting the items, we use `listField` to create the `$posts$`
276+key.
165277
166278```haskell
167−recentFirst :: [Item a] -> [Item a]
279+listField :: String -> Context a -> Compiler [Item a] -> Context b
168280```
169281
170−After loading and sorting the items, we load a template for the posts.
171−`applyTemplateList` applies this template to every post and concatenates the
172−result, which is a simple `String`.
282+The first parameter is simply the name of the key (`"posts"`). Secondly we have
283+a `Context` with which all items should be rendered -- for our example site, we
284+already wrote such a `Context` for posts: `postCtx`. Lastly, we have a
285+`Compiler` which loads the items. We already loaded the items so we can just use
286+`return posts`.
287+
288+The following snippet would produce the same result:
289+
290+```haskell
291+let archiveCtx =
292+ listField "posts" postCtx (recentFirst =<< loadAll "posts/*") `mappend`
293+ constField "title" "Archives" `mappend`
294+ defaultContext
295+```
web/tutorials/05-snapshots-feeds.markdownnew file+129
@@ -0,0 +1,129 @@
1+---
2+title: Snapshots, and how to produce an RSS/Atom feed
3+author: Jasper Van der Jeugt
4+type: main
5+---
6+
7+Basic feed configuration
8+------------------------
9+
10+Hakyll has built-in support for two types of feeds: RSS and Atom. This tutorial
11+explains how you can add these to your blog or website. The first step is to
12+define a `FeedConfiguration` to set some basic options. For example, a cooking
13+blog may have the following declaration:
14+
15+```haskell
16+myFeedConfiguration :: FeedConfiguration
17+myFeedConfiguration = FeedConfiguration
18+ { feedTitle = "Healthy cooking: latest recipes"
19+ , feedDescription = "This feed provides fresh recipes for fresh food!"
20+ , feedAuthorName = "John Doe"
21+ , feedAuthorEmail = "test@example.com"
22+ , feedRoot = "http://healthycooking.example.com"
23+ }
24+```
25+
26+Simple feed rendering
27+---------------------
28+
29+Now, let's look at how we actually create a feed. Two functions are available:
30+
31+```haskell
32+renderAtom :: FeedConfiguration
33+ -> Context String
34+ -> [Item String]
35+ -> Compiler (Item String)
36+```
37+
38+```haskell
39+renderRss :: FeedConfiguration
40+ -> Context String
41+ -> [Item String]
42+ -> Compiler (Item String)
43+```
44+
45+As you can see, they have exactly the same signature: we're going to use
46+`renderAtom` in this tutorial, but it's trivial to change this to an RSS feed.
47+
48+```haskell
49+create ["atom.xml"] $ do
50+ route idRoute
51+ compile $ do
52+ let feedCtx = postCtx `mappend`
53+ constField "description" "This is the post description"
54+
55+ posts <- fmap (take 10) . recentFirst =<< loadAll "posts/*"
56+ renderAtom myFeedConfiguration feedCtx posts
57+```
58+
59+There we go! We simply take the 10 last posts and pass them to `renderAtom`,
60+with our configuration and a `Context`.
61+
62+It's a bit of a problem that we don't have a description for our posts, and the
63+Atom/RSS feed renderer requires this. One option is to add a `description: Foo`
64+header to our all posts. However, the description is the body text as it appears
65+in most RSS readers, so we would prefer to include the entire content of the
66+posts here.
67+
68+Snapshots
69+---------
70+
71+This poses a problem: if we just `load` all posts and take their content: we get
72+the *finished, processed* content. This means all templates have been applied at
73+that point (including `templates/default.html`). We don't want to include our
74+entire site with navigation in the RSS feed, but rather just the post HTML.
75+
76+Snapshots provide a solution to this problem. They allow you to save an `Item`
77+at any point during its compilation, so you can `load` it later. Let's apply
78+this to our concrete problem.
79+
80+```haskell
81+match "posts/*" $ do
82+ route $ setExtension "html"
83+ compile $ pandocCompiler
84+ >>= loadAndApplyTemplate "templates/post.html" postCtx
85+ >>= loadAndApplyTemplate "templates/default.html" postCtx
86+ >>= relativizeUrls
87+```
88+
89+now becomes:
90+
91+```haskell
92+match "posts/*" $ do
93+ route $ setExtension "html"
94+ compile $ pandocCompiler
95+ >>= loadAndApplyTemplate "templates/post.html" postCtx
96+ >>= saveSnapshot "content"
97+ >>= loadAndApplyTemplate "templates/default.html" postCtx
98+ >>= relativizeUrls
99+```
100+
101+The `saveSnapshot` function is really simple: it takes an item and returns the
102+same item, after saving it. This return value makes it easier to use
103+`saveSnapshot` in a chain of compilers as we did in the above example, but you
104+can discard it if you want.
105+
106+```haskell
107+type Snapshot = String
108+
109+saveSnapshot :: (Typeable a, Binary a)
110+ => Snapshot -> Item a -> Compiler (Item a)
111+```
112+
113+Including the post body
114+-----------------------
115+
116+With this modification, we can update our Atom code. Instead of loading the
117+compiled posts, we just load their content (i.e. the snapshot we just took).
118+
119+We update the `Context` to map `$description$` to the post body, and we're done!
120+
121+```haskell
122+create ["atom.xml"] $ do
123+ route idRoute
124+ compile $ do
125+ let feedCtx = postCtx `mappend` bodyField "description"
126+ posts <- fmap (take 10) . recentFirst =<<
127+ loadAllSnapshots "posts/*" "content"
128+ renderAtom myFeedConfiguration feedCtx posts
129+```
web/tutorials/06-versions.markdownnew file+68
@@ -0,0 +1,68 @@
1+---
2+title: Producing multiple versions of a single file
3+author: Jasper Van der Jeugt
4+type: main
5+---
6+
7+Basics
8+------
9+
10+Suppose that you want to make your writings available in raw markdown format, in
11+addition to the HTML available on your website. Is this possible using Hakyll?
12+
13+In the [previous tutorial], we explained how you can use snapshots to store an
14+item while it's being processed. However, this does not allow you to route it to
15+a different location.
16+
17+[previous tutorial]: /tutorials/05-snapshots-feeds.html
18+
19+Instead, we must use `version` in order to do this. The type signature of
20+`version` does not reveal much:
21+
22+```haskell
23+version :: String -> Rules () -> Rules ()
24+```
25+
26+So let's look at an example:
27+
28+```haskell
29+match "posts/*" $ do
30+ route $ setExtension "html"
31+ compile $ pandocCompiler
32+ >>= loadAndApplyTemplate "templates/post.html" postCtx
33+ >>= loadAndApplyTemplate "templates/default.html" postCtx
34+ >>= relativizeUrls
35+
36+match "posts/*" $ version "raw" $ do
37+ route idRoute
38+ compile getResourceBody
39+```
40+
41+Here, you can see how we produce two versions of each item in `posts/*`: one
42+`"raw"` version, and the default version.
43+
44+Attention: patterns and versions
45+--------------------------------
46+
47+However, there is one important thing to note: suppose you use `Pattern`s for a
48+function such as `loadAll`, e.g. to create an index page with all blogposts.
49+
50+```haskell
51+loadAll "posts/*" :: Compiler [Item String]
52+```
53+
54+is valid code, but **probably not** what you want to do: this will select all
55+`posts/*` items, meaning, both your HTML posts, and the raw versions. In order
56+to fix this, use any of the following:
57+
58+```haskell
59+loadAll ("posts/*" .&&. hasNoVersion) :: Compiler [Item String]
60+```
61+
62+for the default versions, i.e. the HTML pages, and:
63+
64+```haskell
65+loadAll ("posts/*" .&&. hasVersion "raw") :: Compiler [Item String]
66+```
67+
68+for the raw versions.
web/tutorials/guide.markdown → web/tutorials/a-guide-to-the-hakyll-module-zoo.markdownrenamed+2−5
@@ -1,6 +1,7 @@
11---
22title: A Guide to the Hakyll Module Zoo
33author: Brent Yorgey
4+type: article
45---
56
67The hakyll package [contains a bewildering array](/reference/) of modules, and
@@ -62,7 +63,7 @@ These modules contain some "pre-packaged" solutions for common situations. They
6263can be used as-is, or their source can be used as inspiration for your own
6364tools.
6465
65−* [Hakyll.Web.Page.List](/reference/Hakyll-Web-Page-List.html)
66+* [Hakyll.Web.Template.List](/reference/Hakyll-Web-Template-List.html)
6667
6768 Combine several pages into a list, such as a list of posts, images in a
6869 gallery, etc.
@@ -81,10 +82,6 @@ tools.
8182
8283 Use any unix utility as a compiler.
8384
84−* [Hakyll.Core.Util.File](/reference/Hakyll-Core-Util-File.html)
85−
86− Utility functions for working with the file system.
87−
8885* [Hakyll.Core.Util.String](/reference/Hakyll-Core-Util-String.html)
8986
9087 A few utility functions for working with `String`s (trim spaces, find and
web/tutorials/external-add-tags-to-your-hakyll-blog.mdnew file+6
@@ -0,0 +1,6 @@
1+---
2+title: Add tags to your Hakyll blog
3+author: Javran Cheng
4+url: https://javran.github.io/posts/2014-03-01-add-tags-to-your-hakyll-blog.html
5+type: external
6+---
web/tutorials/external-blogger-port.mdnew file+6
@@ -0,0 +1,6 @@
1+---
2+title: Port an existing Blogger blog to Hakyll
3+author: Daniel Campoverde
4+url: 'http://www.sillybytes.net/posts/from_blogger_to_hakyll.html'
5+type: external
6+---
web/tutorials/external-cassius-template.mdnew file+6
@@ -0,0 +1,6 @@
1+---
2+title: Using Cassius templating with Hakyll
3+author: Daniel Campoverde
4+url: 'http://www.sillybytes.net/posts/using_hakyll_with_cassius.html'
5+type: external
6+---
web/tutorials/external-clean-urls-with-hakyll.mdnew file+6
@@ -0,0 +1,6 @@
1+---
2+title: Clean URLs with Hakyll
3+author: Rohan Jain
4+url: http://www.rohanjain.in/hakyll-clean-urls/
5+type: external
6+---
web/tutorials/external-deploying-with-widely.mdnew file+6
@@ -0,0 +1,6 @@
1+---
2+title: Deploying with Widely
3+author: Kyle Marek-Spartz
4+url: http://kyle.marek-spartz.org/posts/2013-12-09-widely-and-hakyll.html
5+type: external
6+---
web/tutorials/external-external-code-inclusion.mdnew file+6
@@ -0,0 +1,6 @@
1+---
2+title: 'External code inclusion with Hakyll'
3+author: 'Tobias Pleyer'
4+url: 'http://blog.tpleyer.de/posts/2019-04-21-external-code-inclusion-with-hakyll.html'
5+type: external
6+---
web/tutorials/external-functionfield.mdnew file+6
@@ -0,0 +1,6 @@
1+---
2+title: Hakyll's functionField
3+author: Beerend Lauwers
4+url: http://beerendlauwers.be/posts/2015-09-21-hakylls-functionfield.html
5+type: external
6+---
web/tutorials/external-github-pages-circle-ci.mdnew file+6
@@ -0,0 +1,6 @@
1+---
2+title: Create a GitHub page with Hakyll and CircleCI
3+author: Juan Pedro Villa
4+url: https://www.stackbuilders.com/news/dr-hakyll-create-a-github-page-with-hakyll-and-circleci
5+type: external
6+---
web/tutorials/external-inlining-and-compressing-css.mdnew file+6
@@ -0,0 +1,6 @@
1+---
2+title: Inlining and compressing CSS
3+author: Kyle Marek-Spartz
4+url: http://kyle.marek-spartz.org/posts/2014-12-09-hakyll-css-template-compiler.html
5+type: external
6+---
web/tutorials/faq.markdown+59−32
@@ -1,8 +1,45 @@
11---
22title: FAQ
33author: Jasper Van der Jeugt
4+type: article
45---
56
7+## "hGetContents: invalid argument" or "commitBuffer: invalid argument"
8+
9+If you get any of the errors:
10+
11+ commitBuffer: invalid argument (invalid character)
12+
13+or:
14+
15+ hGetContents: invalid argument (Invalid or incomplete multibyte or wide
16+ character)
17+
18+It means that your Hakyll executable couldn't write to (in the former case) or
19+read (in the latter) from an UTF-8 encoded file.
20+
21+On most linux distros, you can solve this by setting your `LANG` to use UTF-8,
22+using something like:
23+
24+ LANG=nl_BE.UTF-8 ./site build
25+
26+You should also add this to your `.profile`, or whatever config file you use.
27+
28+On Windows, running `chcp 65001` before running your Hakyll executable has been
29+reported to work.
30+
31+Alternatively, you can specify this in your `site.hs`:
32+
33+```haskell
34+import qualified GHC.IO.Encoding as E
35+
36+main :: IO ()
37+main = do
38+ E.setLocaleEncoding E.utf8
39+ hakyll $ do
40+ ...
41+```
42+
643## "File name does not match module name" on Mac OS
744
845 Hakyll.hs:1:1:
@@ -10,38 +47,19 @@ author: Jasper Van der Jeugt
1047 Saw: `Main'
1148 Expected: `Hakyll'
1249
13−Is an error encountered on Mac OS when `hakyll.hs` is located on a
14−case-insensitive filesystem. A workaround is to rename it to something that
15−isn't the name of the module, for example, `site.hs`.
50+Is an error encountered on Mac OS when your configuration is named `hakyll.hs`
51+and located on a case-insensitive filesystem. A workaround is to rename it to
52+something that isn't the name of the module, for example, `site.hs`.
1653
17−## `pageCompiler`/Hakyll/Pandoc eats my HTML!
54+## pandocCompiler/Hakyll/Pandoc eats my HTML!
1855
1956Sometimes, it can seem that HTML pages are stripped of some arbitrary tags, e.g.
20−`<div>`'s. The issue here is that, when using the default `pageCompiler`, your
57+`<div>`'s. The issue here is that, when using the default `pandocCompiler`, your
2158page passes through Pandoc. Pandoc unfortunately strips away this information,
2259giving you the "wrong" HTML.
2360
24−The solution is not to use `pageCompiler` -- it is very common to write custom
25−page processing compiler. The definition of `pageCompiler` is, put simply:
26−
27−~~~~~{.haskell}
28−pageCompiler =
29− readPageCompiler >>>
30− addDefaultFields >>> -- Sets some things like $path$
31− arr applySelf >>> -- Used to fill in $var$s in the page
32− pageRenderPandoc -- Pass through pandoc
33−~~~~~
34−
35−You can add your own version in your `hakyll.hs` file:
36−
37−~~~~~{.haskell}
38−myPageCompiler =
39− readPageCompiler >>>
40− addDefaultFields >>> -- Sets some things like $path$
41− arr applySelf -- Used to fill in $var$s in the page
42−~~~~~
43−
44−And using this instead of `pageCompiler` should solve the issue.
61+The solution is not to use `pandocCompiler`, but something simpler like
62+`getResourceBody`. This way, your HTML is not touched.
4563
4664## Does Hakyll support syntax highlighting?
4765
@@ -50,16 +68,25 @@ recent version of Pandoc (1.9 and onwards). Note that you also need to include
5068some CSS in order for this to work! This site, for example, uses the [default
5169Pandoc syntax CSS file][syntax-css].
5270
71+To highlight a code block, you need to use Pandoc's fenced code block syntax to
72+set the block's language. For example, here's how you highlight Haskell code:
73+
74+ ``` haskell
75+ fac n = foldr (*) 1 [1..n]
76+ ```
77+
78+For details, see Pandoc's user guide on [fenced code
79+blocks][pandoc-code-blocks] and [inline code][pandoc-inline-code].
80+
5381[syntax-css]: https://github.com/jaspervdj/hakyll/blob/master/web/css/syntax.css
82+[pandoc-code-blocks]: http://johnmacfarlane.net/pandoc/README.html#fenced-code-blocks
83+[pandoc-inline-code]: http://johnmacfarlane.net/pandoc/README.html#verbatim
5484
5585## When should I rebuild and when should I build?
5686
57−If you execute a `./hakyll build`, Hakyll will build your site incrementally.
58−This means it will be very fast, but it will not pick up _all_ changes.
59−
60−- In case you edited `hakyll.hs`, you first want to compile it again.
61−- It is generally recommended to do a `./hakyll rebuild` before you deploy your
62− site.
87+If you execute a `./site build`, Hakyll will build your site incrementally.
88+However, we can not detect if you edited `site.hs`. In this case, you first want
89+to compile `site.hs` again, and then do a `./site rebuild`.
6390
6491After rebuilding your site, all files will look as "modified" to the filesystem.
6592This means that when you upload your site, it will usually transfer all files --
web/tutorials/github-pages-tutorial.mdnew file+181
@@ -0,0 +1,181 @@
1+---
2+title: Using Hakyll with GitHub Pages
3+author: Erik Stevenson
4+type: article
5+---
6+
7+## Introduction
8+
9+[GitHub Pages](http://pages.github.com) has become a popular static website hosting solution due to its simplicity. Simply push a couple files to a repository and it's off to the races.
10+
11+Working with Hakyll on a GitHub Pages-hosted website is complicated slightly due to Hakyll outputting files to a ```_site``` subdirectory, our desire to have the source code as well as the compiled site stored in a single repository, and our desire to automate it.
12+
13+This guide will walkthrough the creation and setup of a GitHub site that has two independent branches.
14+
15+1. ```master``` - This is where your site lives. It's what you see when you go to ```https://<your repo>.github.io```. This branch *needs* to be called master.
16+2. ```develop``` - This is where your website's source is. That's all your Haskell code, your posts and templates, etc, and it's where you do work from. This name was chosen arbitrarily and can be freely substituted for any name of your choosing.
17+
18+When you're finished, you will be able to, with one command, refresh your website's contents and send any changes to your GitHub Page.
19+
20+In the interest of keeping this guide as simple as possible, I'll be making a few assumptions.
21+
22+1. Haskell is being used with [Stack](http://docs.haskellstack.org/en/stable/README/).
23+2. Creating a user/organization site (vice a project site).
24+3. You haven't changed Hakyll's default output directory of '_site/'.
25+
26+These instructions should be easy to adapt for any situation though.
27+
28+## GitHub Setup
29+
30+1. If required, create a new repository for your blog.
31+2. If required, create a ```master``` branch.
32+2. If applicable/desired, create/add to your repository any files that your site needs that will not be produced by your Hakyll project. For example, ```CNAME``` as outlined [here](https://help.github.com/articles/setting-up-your-pages-site-repository/).
33+3. Create a ```.gitignore``` file with at a minimum, the following entries:
34+
35+```
36+_cache/
37+_site/
38+.stack-work/
39+```
40+
41+## Local setup
42+
43+1. If required, create a new Hakyll project. If you're a stack user, there is a Hakyll template available that makes this step easy.
44+
45+```stack new myblog hakyll-template```
46+
47+2. Create a ```.gitignore``` file in your blog's directory with at a minimum, the same directories listed as in the GitHub repository.
48+3. Use the following git commands to setup your local repository.
49+
50+```
51+git init
52+# create new branch called develop and switch to it.
53+git checkout -b develop
54+# track all the source files for our blog.
55+git add .
56+# make our first commit
57+git commit -m "initial commit."
58+# and add the GitHub repository as a remote.
59+git remote add origin <URL to your GitHub pages repository>
60+```
61+
62+## Deployment
63+
64+So everything's all setup and we're ready to deploy.
65+
66+> **Note:** Performing the following commands from your ```develop``` branch is recommended since you will end up back in that branch at the end.
67+
68+Temporarily save any uncommitted changes that may exist in the current branch.
69+
70+```
71+git stash
72+```
73+
74+Ensure we are in the correct branch.
75+
76+```
77+git checkout develop
78+```
79+
80+Get a clean build of our site.
81+
82+```
83+stack exec myblog clean
84+stack exec myblog build
85+```
86+
87+Update the local list of remote branches to ensure we're able to checkout the branch we want in the next step.
88+
89+```
90+git fetch --all
91+```
92+
93+Switch to the `master` branch.
94+
95+> **Note:** Checking out this branch does not overwrite the files that Hakyll just produced because we have '_site' listed in both .gitignore files.
96+
97+```
98+git checkout -b master --track origin/master
99+```
100+
101+Next, copy the freshly made contents of '_site' over the old ones.
102+
103+> **Note:** Deleting a file from your site's source will not remove it from your `master` repository if it has already been published. An alternative to `cp` is discussed at the end of this guide.
104+
105+```
106+cp -a _site/. .
107+```
108+
109+Commit our changes.
110+
111+```
112+git add -A
113+git commit -m "Publish."
114+```
115+
116+And send them to GitHub.
117+
118+```
119+git push origin master:master
120+```
121+
122+Final clean up and return to the original state.
123+
124+```
125+git checkout develop
126+git branch -D master
127+git stash pop
128+```
129+
130+## Putting it all together
131+
132+Below is a complete listing of all the commands used to automate deployment to Github Pages. A `deployCommand` can be set as part of Hakyll's configuration options. More information and an example is provided [here](https://jaspervdj.be/hakyll/reference/Hakyll-Core-Configuration.html).
133+
134+```
135+# Temporarily store uncommited changes
136+git stash
137+
138+# Verify correct branch
139+git checkout develop
140+
141+# Build new files
142+stack exec myblog clean
143+stack exec myblog build
144+
145+# Get previous files
146+git fetch --all
147+git checkout -b master --track origin/master
148+
149+# Overwrite existing files with new files
150+cp -a _site/. .
151+
152+# Commit
153+git add -A
154+git commit -m "Publish."
155+
156+# Push
157+git push origin master:master
158+
159+# Restoration
160+git checkout develop
161+git branch -D master
162+git stash pop
163+```
164+
165+*And that's it.*
166+
167+## Removing old files with `rsync`
168+
169+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`.
170+
171+```
172+rsync -a --filter='P _site/' \
173+ --filter='P _cache/' \
174+ --filter='P .git/' \
175+ --filter='P .gitignore' \
176+ --filter='P .stack-work' \
177+ --delete-excluded \
178+ _site/ .
179+```
180+
181+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.
web/tutorials/hakyll-3-to-hakyll4-migration-guide.markdownnew file+78
@@ -0,0 +1,78 @@
1+---
2+title: Hakyll 3 to Hakyll 4 migration guide
3+author: Jasper Van der Jeugt
4+type: article
5+---
6+
7+Introduction
8+------------
9+
10+This tutorial gives a quick overview on how you can port your blog/website from
11+Hakyll 3.X to Hakyll 4. A lot of changes have happened, so it might be useful to
12+read through the [tutorial series](/tutorials.html) before porting your website.
13+
14+Arrow becomes Monad
15+-------------------
16+
17+In Hakyll 3.X, `Compiler` was an instance of `Arrow`. Since Hakyll 4, `Compiler`
18+is a `Monad`. This means that previous chains such as:
19+
20+```haskell
21+compile $ someCompiler
22+ >>> someOtherCompiler
23+ >>> anotherCompiler
24+```
25+
26+Now take the general form of:
27+
28+```haskell
29+compile $ someCompiler
30+ >>= someOtherCompiler
31+ >>= anotherCompiler
32+```
33+
34+Page goes away
35+--------------
36+
37+The `Page` type in Hakyll 3.X has been removed and replaced by an `Item` type.
38+`pageCompiler` no longer exists -- where you previously used this, you probably
39+want to use `pandocCompiler` instead.
40+
41+`Page`s were manipulated using `setField`/`getField` functions in Hakyll 3.X.
42+In Hakyll 4, all metadata is completely immutable, so these functions have been
43+removed. In order to format and add fields, use a `Context` -- see the next
44+section.
45+
46+Template changes
47+----------------
48+
49+The template format has become slightly more flexible, whereas in Hakyll 3.X
50+only keys such as this were allowed:
51+
52+```html
53+<h1>$title$</h1>
54+```
55+
56+we now allow arbitrary strings. This will be really useful in the future.
57+
58+```html
59+<h1>$uppercase title$</h1>
60+```
61+
62+Some template functions have been renamed:
63+
64+- `applyTemplateCompiler` becomes: `loadAndApplyTemplate`
65+- `applySelf` becomes: `applyAsTemplate`
66+
67+Instead of setting fields in a `Page` before applying a template, we now use a
68+`Context`. More information on context can be found in
69+[this tutorial](/tutorials/04-compilers.html). For migration, you basically want
70+to map every `setField` to a field in a `Context`.
71+
72+Metacompilers go away
73+---------------------
74+
75+For tags, the [Hakyll.Web.Tags] module still provides a solution. In other
76+cases, the `preprocess` function should be able to compensate for this.
77+
78+[Hakyll.Web.Tags]: /reference/Hakyll-Web-Tags.html
web/tutorials/robertwpearce-01-setup-and-initial-customization.htmlnew file+5
@@ -0,0 +1,5 @@
1+---
2+title: 'Pt. 1: Setup & Initial Customization'
3+url: 'https://robertwpearce.com/hakyll-pt-1-setup-and-initial-customization.html'
4+type: robertwpearce
5+---
web/tutorials/robertwpearce-02-generating-a-sitemap-xml-file.htmlnew file+5
@@ -0,0 +1,5 @@
1+---
2+title: 'Pt. 2: Generating a Sitemap XML File'
3+url: 'https://robertwpearce.com/hakyll-pt-2-generating-a-sitemap-xml-file.html'
4+type: robertwpearce
5+---
web/tutorials/robertwpearce-03-generating-rss-and-atom-xml-feeds.htmlnew file+5
@@ -0,0 +1,5 @@
1+---
2+title: 'Pt. 3: Generating RSS and Atom XML Feeds'
3+url: 'https://robertwpearce.com/hakyll-pt-3-generating-rss-and-atom-xml-feeds.html'
4+type: robertwpearce
5+---
web/tutorials/robertwpearce-04-copying-static-files-for-your-build.htmlnew file+5
@@ -0,0 +1,5 @@
1+---
2+title: 'Pt. 4: Copying Static Files For Your Build'
3+url: 'https://robertwpearce.com/hakyll-pt-4-copying-static-files-for-your-build.html'
4+type: robertwpearce
5+---
web/tutorials/robertwpearce-05-generating-custom-post-filenames-from-a-title-slug.htmlnew file+5
@@ -0,0 +1,5 @@
1+---
2+title: 'Pt. 5 – Generating Custom Post Filenames From a Title Slug'
3+url: 'https://robertwpearce.com/hakyll-pt-5-generating-custom-post-filenames-from-a-title-slug.html'
4+type: robertwpearce
5+---
web/tutorials/using-clay-with-hakyll.markdownnew file+52
@@ -0,0 +1,52 @@
1+---
2+title: Using Clay with Hakyll
3+author: Jasper Van der Jeugt
4+type: article
5+---
6+
7+[Clay](http://sebastiaanvisser.github.com/clay/) is a nice CSS preprocesser
8+written in Haskell. There are multiple options to use this together with Hakyll,
9+but in this short tutorial I focus on what I think is the best way.
10+
11+This method requires every Clay file to have a `main` function which just prints
12+the CSS. This would be an example of such a file:
13+
14+``` haskell
15+{-# LANGUAGE OverloadedStrings #-}
16+import Clay
17+
18+test :: Css
19+test = ...
20+
21+main :: IO ()
22+main = putCss test
23+```
24+
25+If you prefer the compact version, you're going to have an extra import:
26+
27+
28+``` haskell
29+import qualified Data.Text.Lazy.IO as T
30+
31+main :: IO ()
32+main = T.putStr $ renderWith compact test
33+```
34+
35+
36+Let's assume such a file is called `css/foo.hs`. In our compiled site, we want
37+to map this to `css/foo.css`. Hence, the route is a simple `setExtension`. For
38+compilation, we simply pass the Clay file through `runghc` with no options.
39+
40+```haskell
41+match "css/*.hs" $ do
42+ route $ setExtension "css"
43+ compile $ getResourceString >>= withItemBody (unixFilter "runghc" [])
44+
45+-- cabal sandbox users can use (unixFilter "cabal" ["exec", "runghc"])
46+-- given that you've added cabal to your PATH
47+```
48+
49+The major advantage of using this method (as opposed to importing the Clay files
50+in `site.hs`) is that now Hakyll will only recompile the Clay files when
51+necessary, and you don't have to manually recompile your `site.hs` each time you
52+want to tweak your CSS a little.
web/tutorials/using-teasers-in-hakyll.markdownnew file+109
@@ -0,0 +1,109 @@
1+---
2+title: Using teasers in Hakyll
3+author: Ivan Veselov
4+type: article
5+---
6+
7+## Introduction
8+
9+Sometimes it is convenient to have an excerpt of a post displayed on
10+the index page along with a "Read more..." link to encourage your
11+readers to read the rest of the post.
12+
13+You can do this in Hakyll by using "teasers" functionality.
14+
15+## Marking teasers in posts
16+
17+First, you have to put a tag in your post which separates the teaser
18+from the rest of the article. We use `<!--more-->` for this to mimic
19+the [WordPress convention](http://codex.wordpress.org/Customizing_the_Read_More):
20+
21+``` markdown
22+---
23+title: My teasering post
24+---
25+In this post I'll try to explain how to use teasers.
26+
27+<!--more-->
28+
29+And here I am proceeding with the explanation.
30+
31+```
32+
33+This is an HTML comment, so it doesn't have any visual impact on the
34+post, it is there solely for semantic purposes.
35+
36+## Referring to teasers in templates
37+
38+Now, we want to refer to the teaser in the template. We can do this
39+pretty intuitively by using `$teaser$` key:
40+
41+``` html
42+<li class="post_item">
43+ $date$ - $title$
44+ <p>Teaser: $teaser$</p>
45+ <a href="$url$">Read more</a>
46+</li>
47+```
48+
49+## Gluing together
50+
51+The only thing left is to glue those things together. An important
52+question here is on what stage of the compilation we want to extract a
53+teaser. Usually, we want to use pandoc output, i.e. raw HTML without
54+any templates applied (since we do not want some surrounding
55+JavaScript or common text from the templates to be included in the
56+teaser). We can specify this by using snapshots: we save the snapshot
57+during compilation and load it to resolve `$teaser$` key later:
58+
59+``` haskell
60+match "posts/*" $ do
61+ route $ setExtension ".html"
62+ compile $
63+ pandocCompiler
64+ -- save immediately after pandoc, but before the templates are applied
65+ >>= saveSnapshot "content"
66+ >>= loadAndApplyTemplate "templates/post.html" defaultContext
67+ ...
68+```
69+
70+You can read more about snapshots in
71+[Snapshots tutorial](/tutorials/05-snapshots-feeds.html).
72+
73+Then we use this snapshot while generating teasers using the
74+`teaserField` function:
75+
76+``` haskell
77+ loadAndApplyTemplate
78+ "template/postitem.html"
79+ (teaserField "teaser" "content" <> defaultContext)
80+ item
81+```
82+
83+Here, we've just added a new context which knows how to handle
84+`$teaser$` key to the default context (note that we passed the same
85+snapshot name `"content"` which we used while saving).
86+
87+## Optional teasers
88+
89+In case you don't add a `<!--more-->` comment, `$teaser$` will not be defined.
90+This means you can use something like:
91+
92+```html
93+$if(teaser)$
94+ $teaser$
95+$else$
96+ $body$
97+$endif$
98+```
99+
100+## Known issues
101+
102+Since we use an HTML comment `<!--more-->` to separate the teaser,
103+sometimes `pandoc` can "eat" the comment while converting (for example
104+this happens with literate Haskell source). In order to overcome this
105+problem you may try to use something like this as a separator:
106+
107+``` html
108+<div></div><!--more-->
109+```