Skip to content

Commit 30f2024

Browse files
committed
Initial implementation for tags
1 parent 652c61d commit 30f2024

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

canopy_main.ml

+9-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ module Main (C: CONSOLE) (RES: Resolver_lwt.S) (CON: Conduit_mirage.S) (S:Cohtt
6262
Store.fill_cache articles_hashtable >>= fun _ ->
6363
S.respond_string ~status:`OK ~body:"" ()
6464

65+
| "tags"::tagname::_ ->
66+
let is_in_tags = List.exists ((=) tagname) in
67+
let articles = KeyHashtbl.fold (fun _ v l -> if is_in_tags v.tags then v::l else l)
68+
articles_hashtable [] in
69+
let content = Canopy_templates.template_listing articles in
70+
respond_html ~status:`OK ~title:"Listing" ~content
71+
6572
| key ->
6673
begin
6774
match KeyHashtbl.find_opt articles_hashtable key with
@@ -76,8 +83,9 @@ module Main (C: CONSOLE) (RES: Resolver_lwt.S) (CON: Conduit_mirage.S) (S:Cohtt
7683
| Some article ->
7784
let content = Canopy_templates.template_article article in
7885
respond_html ~status:`OK ~title:article.title ~content
79-
end in
86+
end
8087

88+
in
8189
let callback _ request _ =
8290
let uri = Cohttp.Request.uri request in
8391
dispatcher (Uri.path uri)

canopy_templates.ml

+20-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ end
1414

1515
module StringHtml = Html5.Make_printer(StringPrinter)
1616

17+
let (++) = List.append
18+
19+
let template_taglist tags =
20+
let format_tag tag =
21+
let taglink = Printf.sprintf "/tags/%s" in
22+
a ~a:[taglink tag |> a_href; a_class ["tag"]] [pcdata tag] in
23+
List.map format_tag tags |> div ~a:[a_class ["tags"]]
24+
1725
let template_links keys =
1826
let paths = List.map (function
1927
| x::_ -> x
@@ -33,13 +41,13 @@ let template_main ~config ~content ~title ~keys =
3341
html
3442
(head
3543
(Html5.M.title (pcdata title))
36-
(List.append [
44+
([
3745
meta ~a:[a_charset "UTF-8"] ();
3846
link ~rel:[`Stylesheet] ~href:"/static/bower/bootstrap/dist/css/bootstrap.min.css" ();
3947
link ~rel:[`Stylesheet] ~href:"/static/css/style.css" ();
4048
script ~a:[a_src "/static/bower/jquery/dist/jquery.min.js"] (pcdata "");
4149
script ~a:[a_src "/static/bower/bootstrap/dist/js/bootstrap.min.js"] (pcdata "")
42-
] mathjax)
50+
] ++ mathjax)
4351
)
4452
(body
4553
[
@@ -54,7 +62,7 @@ let template_main ~config ~content ~title ~keys =
5462
span ~a:[a_class ["icon-bar"]][];
5563
span ~a:[a_class ["icon-bar"]][]
5664
];
57-
a ~a:[a_class ["navbar-brand"]; a_href config.index_page][pcdata config.blog_name]
65+
a ~a:[a_class ["navbar-brand"]; a_href ("/" ^ config.index_page)][pcdata config.blog_name]
5866
];
5967
div ~a:[a_class ["collapse navbar-collapse collapse"]] [
6068
ul ~a:[a_class ["nav navbar-nav navbar-right"]] links
@@ -73,12 +81,15 @@ let template_main ~config ~content ~title ~keys =
7381
let template_article article =
7482
let author = "Written by " ^ article.author in
7583
let updated = "Last updated: " ^ article.date in
84+
let tags = template_taglist article.tags in
7685
[div ~a:[a_class ["post"]] [
7786
h2 [pcdata article.title];
7887
span ~a:[a_class ["author"]] [pcdata author];
7988
br ();
8089
span ~a:[a_class ["date"]] [pcdata updated];
8190
br ();
91+
tags;
92+
br ();
8293
Html5.M.article [Unsafe.data article.content]
8394
]]
8495

@@ -87,12 +98,12 @@ let template_listing_entry article =
8798
let abstract = match article.abstract with
8899
| None -> []
89100
| Some abstract -> [p ~a:[a_class ["list-group-item-text abstract"]] [pcdata abstract]] in
90-
let content = List.append [
91-
h4 ~a:[a_class ["list-group-item-heading"]] [pcdata article.title];
92-
span ~a:[a_class ["author"]] [pcdata author];
93-
br ();
94-
] abstract in
95-
a ~a:[a_href article.uri; a_class ["list-group-item"]] content
101+
let content = [
102+
h4 ~a:[a_class ["list-group-item-heading"]] [pcdata article.title];
103+
span ~a:[a_class ["author"]] [pcdata author];
104+
br ();
105+
] in
106+
a ~a:[a_href article.uri; a_class ["list-group-item"]] (content ++ abstract)
96107

97108
let template_listing articles =
98109
let entries = List.map template_listing_entry articles in

canopy_types.ml

+14-6
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@ type article = {
55
abstract : string option;
66
uri : string;
77
date: string;
8+
tags: string list;
89
}
910

11+
let map_opt fn default = function
12+
| None -> default
13+
| Some v -> fn v
14+
15+
let assoc_opt k l =
16+
match List.assoc k l with
17+
| v -> Some v
18+
| exception Not_found -> None
19+
1020
let meta_assoc str =
1121
Re_str.split (Re_str.regexp "\n") str |>
1222
List.map (fun meta ->
@@ -18,6 +28,7 @@ let meta_assoc str =
1828

1929
let article_of_string uri str date =
2030
try
31+
let split_tags = Re_str.split (Re_str.regexp ",") in
2132
let r_meta = Re_str.regexp "---" in
2233
let s_str = Re_str.bounded_split r_meta str 2 in
2334
match s_str with
@@ -31,12 +42,9 @@ let article_of_string uri str date =
3142
let assoc = meta_assoc meta in
3243
let author = List.assoc "author" assoc in
3344
let title = List.assoc "title" assoc in
34-
let abstract =
35-
try
36-
Some (List.assoc "abstract" assoc)
37-
with
38-
| Not_found -> None in
39-
Some {title; content; author; uri; abstract; date}
45+
let tags = assoc_opt "tags" assoc |> map_opt split_tags [] in
46+
let abstract = assoc_opt "abstract" assoc in
47+
Some {title; content; author; uri; abstract; date; tags}
4048
| _ -> None
4149
with
4250
| _ -> None

less/style.less

+6
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ article {
6767
margin-top: 30px;
6868
}
6969

70+
a.tag {
71+
margin-right: 3px;
72+
font-size: 0.7em;
73+
color: #777;
74+
}
75+
7076
footer {
7177
margin-top: 20px;
7278
font-size: 0.7em;

0 commit comments

Comments
 (0)