generate the post index

This commit is contained in:
Dan Frumin 2020-08-29 12:52:21 +02:00
parent 6234b815f3
commit da996d1b6d
4 changed files with 40 additions and 16 deletions

View File

@ -8,11 +8,12 @@ post_compiler := src/webcc.ml
builddir := _build/default builddir := _build/default
POSTCC := $(builddir)/$(patsubst %.ml,%.exe,$(post_compiler)) POSTCC := $(builddir)/$(patsubst %.ml,%.exe,$(post_compiler))
INDEXCC := $(POSTCC) -i
post-sources := $(shell find $(src)/ -type f) post-sources := $(shell find $(src)/ -type f)
post-htmls := $(patsubst $(src)/%.md,$(www)/%.html,$(post-sources)) post-htmls := $(patsubst $(src)/%.md,$(www)/%.html,$(post-sources))
.PHONY: all serve clean $(post_index) .PHONY: all serve clean $(POSTCC)
all: $(POSTCC) $(post-htmls) $(post_index) $(www_root)/static all: $(POSTCC) $(post-htmls) $(post_index) $(www_root)/static
cp -R ~/www/* $(www_root) cp -R ~/www/* $(www_root)
@ -23,10 +24,8 @@ $(www):
$(www)/%.html: $(src)/%.md $(www) $(www)/%.html: $(src)/%.md $(www)
$(POSTCC) $< -o $@ $(POSTCC) $< -o $@
# make sure that this target is .PHONY! $(post_index): $(post-sources)
$(post_index): $(INDEXCC) $^ -o $@
echo hm > $@
$(www_root)/static: $(www_root)/static:
cp -R static $@ cp -R static $@
@ -35,7 +34,6 @@ $(POSTCC): $(post_compiler)
dune build dune build
clean: clean:
rm $(POSTCC)
rm -fr $(www_root) rm -fr $(www_root)
serve: serve:

View File

@ -1,3 +1,3 @@
(executable (executable
(name webcc) (name webcc)
(libraries omd ISO8601)) (libraries omd ISO8601 str))

View File

@ -13,6 +13,7 @@ let spec =
] ]
type post_metadata = { title : string ; date : float ; tags : string list ; type post_metadata = { title : string ; date : float ; tags : string list ;
filename : string ;
other : (string * string) list } other : (string * string) list }
(** Utitilies *) (** Utitilies *)
@ -28,7 +29,7 @@ let with_open_out fn f =
| r -> close_out oc; r | r -> close_out oc; r
| exception e -> close_out_noerr oc; raise e | 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 rec go lst =
let line = let line =
try input_line ic try input_line ic
@ -48,6 +49,7 @@ let read_metadata ic : post_metadata =
{ title = lookup "title"; { title = lookup "title";
date = date; date = date;
tags = String.split_on_char ',' (lookup "tags"); tags = String.split_on_char ',' (lookup "tags");
filename = filename;
other = md } other = md }
@ -94,8 +96,8 @@ let format_post ~title ~tags ~date ~contents =
format_page ~title ~contents:inner format_page ~title ~contents:inner
(* convert contents from [ic] to a post webpage, output to [oc] *) (* convert contents from [ic] to a post webpage, output to [oc] *)
let process ic oc = let process filename ic oc =
let metadata = read_metadata ic in let metadata = read_metadata filename ic in
let md = Omd.of_channel ic in let md = Omd.of_channel ic in
let html = format_post let html = format_post
~title:(metadata.title) ~title:(metadata.title)
@ -106,15 +108,36 @@ let process ic oc =
output_string oc html output_string oc html
let compile_posts oc = let compile_posts oc =
if !input = [] then process stdin oc if !input = [] then process "<stdin>" stdin oc
else begin 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 List.iter f !input
end end
(** Generating index of posts *) (** 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 *) (** main *)
let main () = let main () =
@ -127,7 +150,10 @@ let main () =
else else
with_open_out !output f with_open_out !output f
in in
with_output compile_posts with_output @@
if !gen_index
then generate_index
else compile_posts
let () = let () =
try try

View File

@ -29,9 +29,9 @@ html {
} }
body { body {
font-family: 'Latin Modern', serif; font-family: 'Latin Modern Roman', Georgia, serif;
font-size: 110%; font-size: 110%;
line-height: 1.8; line-height: 1.4;
max-width: 100ch; max-width: 100ch;
min-height: 100vh; min-height: 100vh;