Advertisement
Guest User

Site

a guest
May 19th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 1.94 KB | None | 0 0
  1. #lang web-server/insta
  2.  
  3. ; A blog is a (listof post)
  4. ; and a post is a (post title body)
  5. (struct post (title body))
  6.  
  7. ; BLOG: blog
  8. ; The static blog.
  9. (define BLOG
  10.   (list (post "Second Post" "This is another post")
  11.         (post "First Post" "This is my first post")))
  12.  
  13. ; start: request -> response
  14. ; Consumes a request and produces a page that displays all of the
  15. ; web content.
  16. (define (start request)
  17.   (define a-blog
  18.     (cond [(can-parse-post? (request-bindings request))
  19.            (cons (parse-post (request-bindings request))
  20.                  BLOG)]
  21.           [else
  22.            BLOG]))
  23.   (render-blog-page a-blog request))
  24.  
  25.  
  26. ; can-parse-post?: bindings -> boolean
  27. ; Produces true if bindings contains values for 'title and 'body.
  28. (define (can-parse-post? bindings)
  29.   (and (exists-binding? 'title bindings)
  30.        (exists-binding? 'body bindings)))
  31.  
  32.  
  33. ; parse-post: bindings -> post
  34. ; Consumes a bindings, and produces a post out of the bindings.
  35. (define (parse-post bindings)
  36.   (post (extract-binding/single 'title bindings)
  37.         (extract-binding/single 'body bindings)))
  38.  
  39. ; render-blog-page: blog request -> response
  40. ; Consumes a blog and a request, and produces an HTML page
  41. ; of the content of the blog.
  42. (define (render-blog-page a-blog request)
  43.   (response/xexpr
  44.    `(html (head (title "My Blog"))
  45.           (body
  46.            (h1 "My Blog")
  47.            ,(render-posts a-blog)
  48.            (form
  49.             (input ((name "title")))
  50.             (input ((name "body")))
  51.             (input ((type "submit"))))))))
  52.  
  53. ; render-post: post -> xexpr
  54. ; Consumes a post, produces an xexpr fragment of the post.
  55. (define (render-post a-post)
  56.   `(div ((class "post"))
  57.         ,(post-title a-post)
  58.         (p ,(post-body a-post))))
  59.  
  60.  
  61. ; render-posts: blog -> xexpr
  62. ; Consumes a blog, produces an xexpr fragment
  63. ; of all its posts.
  64. (define (render-posts a-blog)
  65.   `(div ((class "posts"))
  66.         ,@(map render-post a-blog)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement