Commit c7d32a7d authored by Dario Pinto's avatar Dario Pinto
Browse files

add article preview rendering

parent 81849ecd
......@@ -8,51 +8,78 @@ type article =
; url : string
}
let compare_articles a1 a2 = compare (a2.date, a2.title) (a1.date, a1.title)
let error msg =
Format.eprintf "error: %s@." msg;
Format.pp_print_flush Format.err_formatter ();
exit 1
(* The code below is used to extract a small preview from the content field of a article type with the help of Omd *)
(* The code below is used to extract a small preview from the content field of an article type with the help of Omd *)
open Omd
let handle_html html =
let c = Markup.string html |> Markup.parse_html |> Markup.signals in
let buff = Buffer.create 512 in
let fmt = Format.formatter_of_buffer buff in
Markup.iter
(fun element ->
match element with
| `End_element -> ()
| `Start_element (_, _) -> ()
| `Text ls -> List.iter (fun s -> Format.fprintf fmt "%s " s) ls
| _ -> () )
c;
Format.pp_print_flush fmt ();
Buffer.contents buff
let rec handle_inline = function
| Concat (attr, attr_inline_list) ->
Concat (attr, List.map handle_inline attr_inline_list)
| Text (attr, s) -> Text (attr, s)
| Emph (attr, attr_inline) -> Emph (attr, handle_inline attr_inline)
| Strong (attr, attr_inline) -> Strong (attr, handle_inline attr_inline)
| Code (attr, s) -> Code (attr, s)
| Hard_break attr -> Hard_break attr
| Soft_break attr -> Soft_break attr
| Link (attr, attr_link) -> Link (attr, attr_link)
| Image (attr, attr_link) -> Image (attr, attr_link)
| Html (attr, s) -> Html (attr, s)
| Concat (_attr, attr_inline_list) ->
List.fold_left (fun acc i -> acc ^ handle_inline i) "" attr_inline_list
| Text (_attr, s) -> s
| Emph (_attr, attr_inline) -> handle_inline attr_inline
| Strong (_attr, attr_inline) -> handle_inline attr_inline
| Code (_attr, s) -> s
| Hard_break _attr -> " "
| Soft_break _attr -> " "
| Link (_attr, attr_link) ->
Option.value attr_link.title ~default:"[link name]"
| Image (_attr, attr_link) ->
Option.value attr_link.title ~default:"[image name]"
| Html (_attr, str) -> handle_html str
let rec handle_block = function
| Paragraph (attr, inline) -> Paragraph (attr, handle_inline inline)
| List (attr, list_type, list_spacing, attr_block_list_list) ->
List
( attr
, list_type
, list_spacing
, List.map (List.map handle_block) attr_block_list_list )
| Blockquote (attr, attr_block_list) ->
Blockquote (attr, List.map handle_block attr_block_list)
| Thematic_break attr -> Thematic_break attr
| Heading (attr, i, attr_inline) ->
Heading (attr, i, handle_inline attr_inline)
| Code_block (attr, s1, s2) -> Code_block (attr, s1, s2)
| Html_block (attr, str) -> Html_block (attr, str)
| Definition_list (attr, attr_def_elt_list) ->
Definition_list (attr, attr_def_elt_list)
| Paragraph (_attr, inline) -> handle_inline inline
| List (_attr, _list_type, _list_spacing, attr_block_list_list) ->
Format.asprintf "%a"
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "@.")
(fun fmt block -> Format.fprintf fmt "%s" (handle_block block)) )
(List.flatten attr_block_list_list)
| Blockquote (_attr, attr_block_list) ->
Format.asprintf "%a"
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "@.")
(fun fmt block -> Format.fprintf fmt "%s" (handle_block block)) )
attr_block_list
| Thematic_break _attr -> ""
| Heading (_attr, _i, attr_inline) -> handle_inline attr_inline
| Code_block (_attr, _s1, _s2) -> ""
| Html_block (_attr, str) -> handle_html str
| Definition_list (_attr, _attr_def_elt_list) -> ""
let find_preview doc = Omd.to_html (List.map handle_block doc)
let find_preview doc =
String.sub
( String.concat " "
@@ List.filter
(fun x -> x <> "")
(List.map (fun block -> handle_block block) doc) )
0 300
let preview article_content =
let _content = Omd.of_string article_content |> find_preview in
Format.sprintf "%s..." "This will hold some preview of article soon"
(* The code above is used to extract a small preview from the content field of a article type with the help of Omd *)
let content = Omd.of_string article_content |> find_preview in
Format.sprintf "%s..." content
(* The code above is used to extract a small preview from the content field of an article type with the help of Omd *)
(** [normalize_url target] takes a target URL and rids it of unwanted
characters, such as utf8, and spaces *)
......@@ -139,13 +166,13 @@ let get_article_data raw_articles =
List.map
(fun article ->
match Content.read article with
| None -> failwith "invalid article data"
| None -> failwith "Couldn't read article data"
| Some data -> (
match
article_of_string data
(Filename.basename (Filename.chop_suffix article ".md"))
with
| None -> failwith "invalid article data"
| None -> failwith "Invalid article data"
| Some data -> data ) )
raw_articles
......@@ -188,9 +215,7 @@ let categories_count =
let links_to_home_pages =
Format.sprintf
{|
<div class="row">
{|<div class="row">
<h5>
<a href="/blog">Home!</a>
</h5>
......@@ -230,7 +255,7 @@ let pp_article_excerpt fmt article =
</div>
</div>
<br />
%s<a href="/blog/%s">(Read more...)</a>
%s <a href="/blog/%s">(Read more)</a>
<hr class="featurette-divider"/>
<br />|}
article.url article.title
......@@ -290,14 +315,7 @@ let specific_article_header title authors (year, month, day) category tags =
(** [given_category category] Displays the list of articles corresponding to the
request category *)
let given_category category =
let articles_by_date =
List.sort
(fun a1 a2 ->
match compare a2.date a1.date with
| 0 -> compare a2.title a1.title
| n -> n )
articles_data
in
let articles_by_date = List.sort compare_articles articles_data in
let articles_of_category =
List.filter
(fun article -> String.equal (normalize_url article.category) category)
......@@ -310,14 +328,7 @@ let given_category category =
(** [given_author ocp_author] Displays the list of articles written by a given
[ocp_author] *)
let given_author ocp_author =
let articles_by_date =
List.sort
(fun a1 a2 ->
match compare a2.date a1.date with
| 0 -> compare a2.title a1.title
| n -> n )
articles_data
in
let articles_by_date = List.sort compare_articles articles_data in
let articles_of_author =
List.filter
(fun article ->
......@@ -367,13 +378,6 @@ let authors_home =
(** [home_page] this is the home page for the blog, articles appear as excerpts
from most recent to oldest *)
let home_page =
let articles_by_date =
List.sort
(fun a1 a2 ->
match compare a2.date a1.date with
| 0 -> compare a2.title a1.title
| n -> n )
articles_data
in
let articles_by_date = List.sort compare_articles articles_data in
Format.asprintf {|<h1 id="page-title">Blog</h1>%s%a@.|} links_to_home_pages
pp_blog_posts articles_by_date
......@@ -6,11 +6,12 @@ tags=opam,release
<div class="row">
<div class="col-lg-12" align="center">
<img src="/blog/assets/img/logo_opam_blue.png" class="img-fluid rounded"/>
<img src="/blog/assets/img/logo_opam_blue.png" class="img-fluid rounded" alt="Logo OCqqqqqqqqqq"/>
</div>
</div>
<br />
<em>
Feedback on this post is welcomed on <a href="https://discuss.ocaml.org/t/ann-opam-2-1-0/8255">Discuss</a>!
</em>
......
(executable
(public_name server)
(modules content server template blog blog_content)
(libraries dream omd ubase))
(libraries dream omd ubase markup))
(rule
(targets template.ml)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment