|
| 1 | +#lang racket/base |
| 2 | + |
| 3 | +(require racket/contract |
| 4 | + racket/date |
| 5 | + racket/format |
| 6 | + racket/list |
| 7 | + racket/match |
| 8 | + threading |
| 9 | + (only-in xml xexpr?) |
| 10 | + |
| 11 | + "../../lang/metadata.rkt" |
| 12 | + "../../paths.rkt" |
| 13 | + "../metadata.rkt") |
| 14 | + |
| 15 | +(provide (contract-out |
| 16 | + [page (-> #:title string? #:body xexpr? xexpr?)] |
| 17 | + [post-page (-> rendered-post? xexpr?)] |
| 18 | + [post-index-page (->i ([total-pages (and/c exact-integer? (>=/c 1))] |
| 19 | + [page-number (total-pages) (and/c exact-integer? (>=/c 1) (<=/c total-pages))] |
| 20 | + [paths+infos (listof (list/c string? rendered-post?))]) |
| 21 | + [result xexpr?])] |
| 22 | + [tag-index-page (->i ([total-pages (and/c exact-integer? (>=/c 1))] |
| 23 | + [page-number (total-pages) (and/c exact-integer? (>=/c 1) (<=/c total-pages))] |
| 24 | + [tag string?] |
| 25 | + [paths+infos (listof (list/c string? rendered-post?))]) |
| 26 | + [result xexpr?])])) |
| 27 | + |
| 28 | +(define (page #:title title #:body body) |
| 29 | + `(html |
| 30 | + (head |
| 31 | + (meta ([charset "utf-8"])) |
| 32 | + (meta ([name "viewport"] [content "width=device-width, initial-scale=1"])) |
| 33 | + (title ,title) |
| 34 | + ,(stylesheet (~a "https://fonts.googleapis.com/css?family" |
| 35 | + "=Merriweather+Sans:400,300,300italic,400italic,700,700italic,800,800italic" |
| 36 | + "|Merriweather:400,300,300italic,400italic,700,700italic,900,900italic" |
| 37 | + "|Fira+Code:300,400,500,600,700")) |
| 38 | + ,(stylesheet "/css/application.min.css") |
| 39 | + ,(stylesheet "/css/pygments.min.css") |
| 40 | + (body |
| 41 | + (header |
| 42 | + (nav ([role "navigation"] [class "navigation-bar"]) |
| 43 | + (ul ([class "navigation-items left"]) |
| 44 | + (li ([class "blog-title-header"]) |
| 45 | + (a ([href "/"]) "Alexis King"))) |
| 46 | + (ul ([class "navigation-items center"])) |
| 47 | + (ul ([class "navigation-items right"]) |
| 48 | + (li (a ([href "/"]) "Home")) |
| 49 | + (li (a ([href "/resume.html"]) "About Me"))))) |
| 50 | + (section ([role "main"]) ,body) |
| 51 | + (footer |
| 52 | + (div ([class "copyright-notice"]) "© " ,(~a (date-year (current-date))) ", Alexis King") |
| 53 | + (div "Built with " |
| 54 | + (a ([href "https://docs.racket-lang.org/scribble/index.html"]) (strong "Scribble")) |
| 55 | + ", the Racket document preparation system.") |
| 56 | + (div "Feeds are available via " (a ([href "/feeds/all.atom.xml"]) "Atom") |
| 57 | + " or " (a ([href "/feeds/all.rss.xml"]) "RSS") ".")))))) |
| 58 | + |
| 59 | +(define (post-page info) |
| 60 | + (match-define (rendered-post title-str title date tags body) info) |
| 61 | + (page #:title title-str |
| 62 | + #:body `(div ([class "content"]) |
| 63 | + (article ([class "main"]) |
| 64 | + ,(build-post-header title date tags) |
| 65 | + ,@body)))) |
| 66 | + |
| 67 | +(define (post-index-page total-pages page-number paths+infos) |
| 68 | + (page #:title "Alexis King’s Blog" |
| 69 | + #:body `(div ([class "content"]) |
| 70 | + ,@(build-post-index index-path total-pages page-number paths+infos)))) |
| 71 | + |
| 72 | +(define (tag-index-page total-pages page-number tag paths+infos) |
| 73 | + (page #:title (~a "Posts tagged ‘" tag "’") |
| 74 | + #:body `(div ([class "content"]) |
| 75 | + (h1 ([class "tag-page-header"]) |
| 76 | + "Posts tagged " (em ,tag)) |
| 77 | + ,@(build-post-index (λ~>> (tag-index-path tag)) |
| 78 | + total-pages page-number paths+infos)))) |
| 79 | + |
| 80 | +(define (build-post-header title date tags) |
| 81 | + (define date-str (post-date->string date)) |
| 82 | + `(header |
| 83 | + (h1 ([class "title"]) ,@title) |
| 84 | + (div ([class "date-and-tags"]) |
| 85 | + (time ([datetime ,date-str]) ,date-str) |
| 86 | + " " (span ([style "margin: 0 5px"]) "⦿") " " |
| 87 | + ,@(~> (for/list ([tag (in-list tags)]) |
| 88 | + `(a ([href ,(tag-index-path tag)]) ,tag)) |
| 89 | + (add-between ", "))))) |
| 90 | + |
| 91 | +(define (build-post-index page-path total-pages page-number paths+infos) |
| 92 | + `[,@(for/list ([path+info (in-list paths+infos)]) |
| 93 | + (match-define (list path info) path+info) |
| 94 | + (build-post-index-entry path info)) |
| 95 | + (ul ([class "pagination"]) |
| 96 | + ,(if (= page-number 1) |
| 97 | + '(li ([class "disabled"]) "←") |
| 98 | + `(li (a ([href ,(page-path (sub1 page-number))]) "←"))) |
| 99 | + ,@(for/list ([i (in-range 1 (add1 total-pages))]) |
| 100 | + `(li ,(if (= i page-number) |
| 101 | + '([class "pagination-number active"]) |
| 102 | + '([class "pagination-number"])) |
| 103 | + (a ([href ,(page-path i)]) ,(~a i)))) |
| 104 | + ,(if (= page-number total-pages) |
| 105 | + '(li ([class "disabled"]) "→") |
| 106 | + `(li (a ([href ,(page-path (add1 page-number))]) "→"))))]) |
| 107 | + |
| 108 | +(define (build-post-index-entry path info) |
| 109 | + (match-define (rendered-post _ title date tags body) info) |
| 110 | + `(article ([class "inline"]) |
| 111 | + ,(build-post-header `[(a ([href ,path]) ,@title)] date tags) |
| 112 | + ; only render up to the start of the first section |
| 113 | + ,@(takef body (match-lambda [(cons 'h2 _) #f] [_ #t])) |
| 114 | + (p (a ([href ,path]) (span ([class "read-more-text"]) "Read more") " →")))) |
| 115 | + |
| 116 | +(define (stylesheet href) |
| 117 | + `(link ([rel "stylesheet"] [type "text/css"] [href ,href]))) |
0 commit comments