generate the post index
This commit is contained in:
		
							
								
								
									
										10
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								Makefile
									
									
									
									
									
								
							@@ -8,11 +8,12 @@ post_compiler := src/webcc.ml
 | 
			
		||||
builddir := _build/default
 | 
			
		||||
 | 
			
		||||
POSTCC := $(builddir)/$(patsubst %.ml,%.exe,$(post_compiler))
 | 
			
		||||
INDEXCC := $(POSTCC) -i
 | 
			
		||||
 | 
			
		||||
post-sources := $(shell find $(src)/ -type f)
 | 
			
		||||
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
 | 
			
		||||
	cp -R ~/www/* $(www_root)
 | 
			
		||||
@@ -23,10 +24,8 @@ $(www):
 | 
			
		||||
$(www)/%.html: $(src)/%.md $(www)
 | 
			
		||||
	$(POSTCC) $< -o $@
 | 
			
		||||
 | 
			
		||||
# make sure that this target is .PHONY!
 | 
			
		||||
$(post_index):
 | 
			
		||||
	echo hm > $@
 | 
			
		||||
 | 
			
		||||
$(post_index): $(post-sources)
 | 
			
		||||
	$(INDEXCC) $^ -o $@
 | 
			
		||||
 | 
			
		||||
$(www_root)/static:
 | 
			
		||||
	cp -R static $@
 | 
			
		||||
@@ -35,7 +34,6 @@ $(POSTCC): $(post_compiler)
 | 
			
		||||
	dune build
 | 
			
		||||
 | 
			
		||||
clean:
 | 
			
		||||
	rm $(POSTCC)
 | 
			
		||||
	rm -fr $(www_root)
 | 
			
		||||
 | 
			
		||||
serve:
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										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
 | 
			
		||||
 
 | 
			
		||||
@@ -29,9 +29,9 @@ html {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
body {
 | 
			
		||||
  font-family: 'Latin Modern', serif;
 | 
			
		||||
  font-family: 'Latin Modern Roman', Georgia, serif;
 | 
			
		||||
  font-size: 110%;
 | 
			
		||||
  line-height: 1.8;
 | 
			
		||||
  line-height: 1.4;
 | 
			
		||||
  
 | 
			
		||||
  max-width: 100ch;
 | 
			
		||||
  min-height: 100vh;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user