my-blog.scm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ;;; Haunt --- Static site generator for GNU Guile
  2. ;;; Copyright © 2015 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
  4. ;;;
  5. ;;; This file is part of Haunt.
  6. ;;;
  7. ;;; Haunt is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or
  10. ;;; (at your option) any later version.
  11. ;;;
  12. ;;; Haunt is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ;;; General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with Haunt. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;;
  21. ;; Page builders
  22. ;;
  23. ;;; Code:
  24. (define-module (builders my-blog)
  25. #:use-module (ice-9 match)
  26. #:use-module (srfi srfi-9)
  27. #:use-module (haunt site)
  28. #:use-module (haunt post)
  29. #:use-module (haunt page)
  30. #:use-module (haunt utils)
  31. #:use-module (haunt html)
  32. #:use-module (theme theme)
  33. #:export (
  34. with-layout
  35. render-collection
  36. theme
  37. render-post
  38. my-blog))
  39. (define-record-type <theme>
  40. (make-theme name layout post-template collection-template)
  41. theme?
  42. (name theme-name)
  43. (layout theme-layout)
  44. (post-template theme-post-template)
  45. (collection-template theme-collection-template))
  46. ;; comment
  47. (define (ugly-default-collection-template site title posts prefix)
  48. (define (post-uri post)
  49. (string-append (or prefix "") "/"
  50. (site-post-slug site post) ".html"))
  51. `((section (@ (class "blog"))
  52. (h3 ,title)
  53. ,@(map (lambda (post)
  54. `((h3
  55. (a (@ (href ,(post-uri post)))
  56. ,(post-ref post 'title)))
  57. (time (@ (datetime ,(date->string* (post-date post))))
  58. ,(date->string* (post-date post))))
  59. )
  60. posts))))
  61. (define (ugly-default-post-template post)
  62. `((section (@ (class "basic-section-padding"))
  63. (article
  64. ;;(h2 ,(post-ref post 'title))
  65. (h3 "by " ,(post-ref post 'author)
  66. " — " ,(date->string* (post-date post)))
  67. (div ,(post-sxml post))))))
  68. (define (render-post theme site post)
  69. (let ((title (post-ref post 'title))
  70. (body ((theme-post-template theme) post)))
  71. (with-layout theme site title body)))
  72. (define (with-layout theme site title body)
  73. ((theme-layout theme) site title body))
  74. (define (render-collection theme site title posts prefix)
  75. (let ((body ((theme-collection-template theme) site title posts prefix)))
  76. (with-layout theme site title body)))
  77. (define* (theme #:key
  78. (name "gnucode-theme")
  79. (layout gnucode-layout)
  80. (post-template ugly-default-post-template)
  81. (collection-template ugly-default-collection-template))
  82. (make-theme name layout post-template collection-template))
  83. (define home-theme
  84. (theme #:name "home"
  85. #:layout gnucode-layout
  86. #:post-template ugly-default-post-template
  87. #:collection-template ugly-default-collection-template))
  88. (define* (my-blog #:key (theme home-theme) prefix
  89. (collections
  90. `(("Recent Posts" "index.html" ,posts/reverse-chronological))))
  91. "Return a procedure that transforms a list of posts into pages
  92. decorated by THEME, whose URLs start with PREFIX."
  93. (define (make-file-name base-name)
  94. (if prefix
  95. (string-append prefix "/" base-name)
  96. base-name))
  97. (lambda (site posts)
  98. (define (post->page post)
  99. (let ((base-name (string-append (site-post-slug site post)
  100. ".html")))
  101. (make-page (make-file-name base-name)
  102. (render-post theme site post)
  103. sxml->html)))
  104. (define collection->page
  105. (match-lambda
  106. ((title file-name filter)
  107. (make-page (make-file-name file-name)
  108. (render-collection theme site title (filter posts) prefix)
  109. sxml->html))))
  110. (append (map post->page posts)
  111. (map collection->page collections))))