Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to output wrapped eliom elements #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions src/MarkdownHTML.ml
Original file line number Diff line number Diff line change
@@ -1,53 +1,60 @@
(* Copyright (C) 2009 Mauricio Fernandez <[email protected]> *)

module Make_html5 (Html5 : Html5_sigs.T with type 'a Xml.wrap = 'a and type 'a wrap = 'a and type 'a list_wrap = 'a list) = struct
module Make_html5 (Html5 : Html5_sigs.T) = struct

open Markdown
open Html5
module W = Xml.W

let pcdata_w x = W.return @@ pcdata @@ W.return x
let wrap_list f l =
List.fold_right
(fun x l -> W.(cons (return @@ f x) l))
l (W.nil ())

let rec elm_to_html ~render_pre ~render_link ~render_img elm =
let self = elm_to_html ~render_pre ~render_link ~render_img in
let item l = li (List.map self l)
let item l = li (wrap_list self l)

in match elm with
Normal text -> p (par_text_to_html ~render_link ~render_img text)
| Pre (s, kind) -> begin match kind with
Some k -> render_pre ~kind:k s
| None -> pre [pcdata s]
| None -> pre W.(singleton @@ pcdata_w s)
end
| Heading (l, text) ->
let f =
match l with 1 -> h1 | 2 -> h2 | 3 -> h3 | 4 -> h4 | 5 -> h5 | _ -> h6
in f (par_text_to_html render_link render_img text)
| Quote ps -> blockquote (List.map self ps)
| Quote ps -> blockquote @@ wrap_list self ps
| Ulist (fst, others) ->
ul (List.map item (fst :: others))
ul (wrap_list item (fst :: others))
| Olist (fst, others) ->
let item l = li (List.map self l) in
ol (List.map item (fst :: others))
ol (wrap_list item (fst :: others))

and par_text_to_html ~render_link ~render_img =
List.map (text_to_html ~render_link ~render_img)
wrap_list (text_to_html ~render_link ~render_img)

and text_to_html ~render_link ~render_img = function
Text s -> pcdata s
| Emph s -> em [pcdata s]
| Bold s -> b [pcdata s]
| Struck l -> del (List.map (text_to_html ~render_link ~render_img) l)
| Code s -> code [pcdata s]
Text s -> pcdata @@ W.return s
| Emph s -> em @@ W.singleton @@ pcdata_w s
| Bold s -> b @@ W.singleton @@ pcdata_w s
| Struck l -> del (wrap_list (text_to_html ~render_link ~render_img) l)
| Code s -> code @@ W.singleton @@ pcdata_w s
| Anchor id ->
(* would like to do
a ~a:[XHTML.M_01_00.a_name_01_00 id] []
but that'd require switching to M_01_00 everywhere, so cheap hack *)
b ~a:[a_id id] []
b ~a:[a_id @@ W.return id] (W.nil ())
| Link href -> begin match href.href_target with
s when String.length s >= 1 && s.[0] = '#' ->
a ~a:[a_href (uri_of_string s)] [pcdata href.href_desc]
a ~a:[a_href (W.return @@ uri_of_string s)]
@@ W.singleton @@ pcdata_w href.href_desc
| _ -> render_link href
end
| Image href -> render_img href

let to_html ~render_pre ~render_link ~render_img l =
List.map (elm_to_html ~render_pre ~render_link ~render_img) l
W.map (elm_to_html ~render_pre ~render_link ~render_img) l

end
37 changes: 37 additions & 0 deletions src/MarkdownHTML.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
open Markdown

module Make_html5 (Html5 : Html5_sigs.T) : sig

val elm_to_html :
render_pre:(kind:string ->
string ->
([< Html5_types.li_content_fun
> `Blockquote `H1 `H2 `H3 `H4 `H5 `H6 `Ol `P `Pre `Ul ]
as 'a)
Html5.elt) ->
render_link:(href -> Html5_types.phrasing Html5.elt) ->
render_img:(img_ref -> Html5_types.phrasing Html5.elt) ->
paragraph -> 'a Html5.elt

val par_text_to_html :
render_link:(href -> Html5_types.phrasing Html5.elt) ->
render_img:(img_ref -> Html5_types.phrasing Html5.elt) ->
par_text -> Html5_types.phrasing Html5.elt Html5.list_wrap

val text_to_html :
render_link:(href -> Html5_types.phrasing Html5.elt) ->
render_img:(img_ref -> Html5_types.phrasing Html5.elt) ->
text -> Html5_types.phrasing Html5.elt

val to_html :
render_pre:(kind:string ->
string ->
([< Html5_types.li_content_fun
> `Blockquote `H1 `H2 `H3 `H4 `H5 `H6 `Ol `P `Pre `Ul ]
as 'a)
Html5.elt) ->
render_link:(href -> Html5_types.phrasing Html5.elt) ->
render_img:(img_ref -> Html5_types.phrasing Html5.elt) ->
paragraph Html5.list_wrap -> 'a Html5.elt Html5.list_wrap

end