generate the post index
This commit is contained in:
2
src/dune
2
src/dune
@@ -1,3 +1,3 @@
|
||||
(executable
|
||||
(name webcc)
|
||||
(libraries omd ISO8601))
|
||||
(libraries omd ISO8601 str))
|
||||
|
||||
40
src/webcc.ml
40
src/webcc.ml
@@ -13,6 +13,7 @@ let spec =
|
||||
]
|
||||
|
||||
type post_metadata = { title : string ; date : float ; tags : string list ;
|
||||
filename : string ;
|
||||
other : (string * string) list }
|
||||
|
||||
(** Utitilies *)
|
||||
@@ -28,7 +29,7 @@ let with_open_out fn f =
|
||||
| r -> close_out oc; r
|
||||
| exception e -> close_out_noerr oc; raise e
|
||||
|
||||
let read_metadata ic : post_metadata =
|
||||
let read_metadata filename ic : post_metadata =
|
||||
let rec go lst =
|
||||
let line =
|
||||
try input_line ic
|
||||
@@ -48,6 +49,7 @@ let read_metadata ic : post_metadata =
|
||||
{ title = lookup "title";
|
||||
date = date;
|
||||
tags = String.split_on_char ',' (lookup "tags");
|
||||
filename = filename;
|
||||
other = md }
|
||||
|
||||
|
||||
@@ -94,8 +96,8 @@ let format_post ~title ~tags ~date ~contents =
|
||||
format_page ~title ~contents:inner
|
||||
|
||||
(* convert contents from [ic] to a post webpage, output to [oc] *)
|
||||
let process ic oc =
|
||||
let metadata = read_metadata ic in
|
||||
let process filename ic oc =
|
||||
let metadata = read_metadata filename ic in
|
||||
let md = Omd.of_channel ic in
|
||||
let html = format_post
|
||||
~title:(metadata.title)
|
||||
@@ -106,15 +108,36 @@ let process ic oc =
|
||||
output_string oc html
|
||||
|
||||
let compile_posts oc =
|
||||
if !input = [] then process stdin oc
|
||||
if !input = [] then process "<stdin>" stdin oc
|
||||
else begin
|
||||
let f filename = with_open_in filename @@ fun ic -> process ic oc in
|
||||
let f filename = with_open_in filename @@ fun ic -> process filename ic oc in
|
||||
List.iter f !input
|
||||
end
|
||||
|
||||
|
||||
(** Generating index of posts *)
|
||||
(* let generate_index oc = () *)
|
||||
let generate_index oc =
|
||||
let f filename = with_open_in filename (read_metadata filename) in
|
||||
let mds = List.map f !input in
|
||||
let mds = List.sort (fun md1 md2 -> compare md2.date md1.date) mds in
|
||||
(* TODO! Fix the paths here!!!!!! *)
|
||||
let format_item md =
|
||||
(* path to the html file *)
|
||||
let fname = md.filename in
|
||||
let target_fname =
|
||||
let open Str in
|
||||
if string_match (regexp {|content/\(.*\)\.md|}) fname 0
|
||||
then matched_group 1 fname ^ ".html"
|
||||
else fname
|
||||
in
|
||||
String.concat "" [
|
||||
{|<li><a href="/posts/|}; target_fname; {|">|};
|
||||
md.title;
|
||||
{|</a> - |}; ISO8601.Permissive.string_of_date md.date ; {|</li>|}]
|
||||
in
|
||||
let contents = String.concat "\n" @@ List.map format_item mds in
|
||||
let html = format_page ~title:"Recent posts" ~contents in
|
||||
output_string oc html
|
||||
|
||||
(** main *)
|
||||
let main () =
|
||||
@@ -127,7 +150,10 @@ let main () =
|
||||
else
|
||||
with_open_out !output f
|
||||
in
|
||||
with_output compile_posts
|
||||
with_output @@
|
||||
if !gen_index
|
||||
then generate_index
|
||||
else compile_posts
|
||||
|
||||
let () =
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user