use templates for posts
This commit is contained in:
0
render/blog_index.eml.ml
Normal file
0
render/blog_index.eml.ml
Normal file
@@ -1,10 +1,12 @@
|
||||
(library
|
||||
(name render)
|
||||
(public_name rawleydotxyz.render)
|
||||
(libraries dream lwt database))
|
||||
(libraries dream lwt database)
|
||||
(preprocess
|
||||
(pps lwt_ppx)))
|
||||
|
||||
(rule
|
||||
(targets index.ml layout.ml)
|
||||
(deps index.eml.ml layout.eml.ml)
|
||||
(targets index.ml layout.ml post_layout.ml blog_index.ml)
|
||||
(deps index.eml.ml layout.eml.ml post_layout.eml.ml blog_index.eml.ml)
|
||||
(action
|
||||
(run dream_eml %{deps} --workspace %{workspace_root})))
|
||||
@@ -1,4 +1,4 @@
|
||||
let render body =
|
||||
let render body =
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
|
||||
48
render/post_layout.eml.ml
Normal file
48
render/post_layout.eml.ml
Normal file
@@ -0,0 +1,48 @@
|
||||
let render ~title ~tags ~body =
|
||||
let combine_tags lst =
|
||||
List.fold_left (fun t a -> t ^ " " ^ a) "" lst
|
||||
in
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="description" content="<%s combine_tags tags %>">
|
||||
<title><%s title %></title>
|
||||
<link href="/static/index.css" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>
|
||||
<a href="/">/home/rawley.xyz</a>
|
||||
</h1>
|
||||
<nav>
|
||||
<li>
|
||||
<a href="/blog">blog</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="/resume">resume</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="/philosophy">philosophy</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="/web-ring">web ring</a>
|
||||
</li>
|
||||
</nav>
|
||||
</header>
|
||||
<main>
|
||||
<h2><%s title %></h2>
|
||||
<%s! body %>
|
||||
</main>
|
||||
<footer>
|
||||
<hr>
|
||||
<p>
|
||||
Copyright © Rawley Fowler 2022
|
||||
</p>
|
||||
<p>
|
||||
<b>Disclaimer</b>: All opinions on this site, are that of my own. They do not reflect the opinions of
|
||||
any of my employers; past, present, or future.
|
||||
</p>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
@@ -105,20 +105,6 @@ let handle_error e =
|
||||
let handle_not_found () =
|
||||
Dream.html ?code:(Some 404) not_found_template
|
||||
|
||||
let render_blog_post request =
|
||||
let slug = Dream.param request "post" in
|
||||
let post_t = Database.get_blog_post_by_slug slug in
|
||||
post_t >>= fun post ->
|
||||
match post with
|
||||
| Error e -> handle_error e
|
||||
| Ok p_opt ->
|
||||
match p_opt with
|
||||
| None -> handle_not_found ()
|
||||
| Some p ->
|
||||
Printf.sprintf "<h2>%s</h2>\r\n%s" p.title p.content
|
||||
|> render_page
|
||||
|> Dream.html
|
||||
|
||||
let render_blog_index (_ : Dream.request) =
|
||||
let buff = Buffer.create 512 in
|
||||
let () = Buffer.add_string buff "<h3>Blog</h3> I have an <a href=\"/blog/rss.xml\">rss feed</a> too." in
|
||||
@@ -168,5 +154,17 @@ let render_rss_feed (_ : Dream.request) =
|
||||
let render_simple (module R : SimpleRender) =
|
||||
R.render () |> Layout.render |> Dream.html
|
||||
|
||||
let render_blog_post ~slug =
|
||||
let%lwt post = Database.get_blog_post_by_slug slug in
|
||||
match post with
|
||||
| Ok p_opt ->
|
||||
begin match p_opt with
|
||||
| None -> handle_not_found ()
|
||||
| Some p ->
|
||||
Dream.html @@ Post_layout.render ~title:p.title ~body:p.content ~tags:[p.title]
|
||||
end
|
||||
| Error e -> handle_error e
|
||||
|
||||
|
||||
let render_index () =
|
||||
render_simple (module Index)
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
module BlogPost = Database.BlogPost
|
||||
module type SimpleRender = sig val render : unit -> string end
|
||||
val header_template : string
|
||||
val footer_template : string
|
||||
val replace_sequence : string -> string -> string -> string
|
||||
val html_unescape : string -> string
|
||||
val not_found_template : string
|
||||
val error_template : string
|
||||
val render_page : string -> string
|
||||
val generate_link : BlogPost.t -> string
|
||||
val generate_rss_item : BlogPost.t -> string
|
||||
val handle_error : [< Caqti_error.t ] -> Dream.response Lwt.t
|
||||
val handle_not_found : unit -> Dream.response Lwt.t
|
||||
val render_blog_post : Dream.request -> Dream.response Lwt.t
|
||||
val render_blog_index : Dream.request -> Dream.response Lwt.t
|
||||
val render_rss_feed : Dream.request -> Dream.response Lwt.t
|
||||
val render_simple : (module SimpleRender) -> Dream.response Lwt.t
|
||||
val render_index : unit -> Dream.response Lwt.t
|
||||
Reference in New Issue
Block a user