Skip to content

Commit a9b8b69

Browse files
committed
Rough in inline elements
1 parent 5d139ef commit a9b8b69

File tree

1 file changed

+94
-22
lines changed

1 file changed

+94
-22
lines changed

omd_tyxml/omd_tyxml.ml

Lines changed: 94 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,110 @@
11
open Tyxml
22

3-
let of_attributes _ = []
3+
let of_code c = Html.[code ~a:[] [txt c]]
4+
5+
let of_attribute (attr, value) =
6+
Html.Unsafe.string_attrib attr value
7+
8+
let of_attributes attrs =
9+
List.map of_attribute attrs
10+
11+
(* let common_attributes : Omd.attributes -> Html_types.common =
12+
* fun ((attr, value) :: _) -> *)
13+
(* raise (Failure "TODO") *)
14+
15+
(* let image_attributes : Omd.attributes -> Html_types.img_attrib list =
16+
* fun _ -> raise (Failure "TODO") *)
417

518
let rec of_inline ({il_attributes; il_desc} : Omd.inline) =
6-
let a = of_attributes il_attributes in
19+
(* let a = of_attributes il_attributes in *)
20+
(* TODO Attributes *)
21+
let _ = il_attributes in
722
match il_desc with
8-
| Code _ -> raise (Failure "TODO of_inline Code")
23+
| Code c -> of_code c
924
| Concat ls -> List.concat_map of_inline ls
10-
| Emph e -> [Html.em ~a (of_inline e)]
11-
| Strong s -> [Html.strong ~a (of_inline s)]
12-
| Hard_break -> raise (Failure "TODO of_inline Hard_break")
25+
| Emph e -> Html.[em ~a:[] (of_inline e)]
26+
| Strong s -> Html.[strong ~a:[] (of_inline s)]
27+
| Hard_break -> Html.[br ~a:[] ()]
1328
(* TODO Add option for verified html ?*)
14-
| Html raw -> [Tyxml.Html.Unsafe.data raw]
15-
| Image _ -> raise (Failure "TODO of_inline Image")
16-
| Link _ -> raise (Failure "TODO of_inline Link")
17-
| Soft_break -> raise (Failure "TODO of_inline Soft_break")
18-
| Text t -> [Html.txt t]
29+
| Html raw -> Html.Unsafe.[data raw]
30+
| Image img -> of_image img
31+
| Link l -> of_inline_link l
32+
| Soft_break -> Html.[txt "\n"]
33+
| Text t -> Html.[txt t]
34+
35+
and of_link_label ({il_attributes; il_desc} : Omd.inline) =
36+
let _attr = il_attributes in
37+
match il_desc with
38+
| Code c -> of_code c
39+
| Text t -> Html.[txt t]
40+
| Concat l -> List.concat_map of_link_label l
41+
| Emph e -> Html.[em ~a:[] (of_link_label e)]
42+
(* | Image _ -> (??) *)
43+
| _ -> raise (Failure "TODO invalid")
44+
(* | Emph _ -> (??)
45+
* | Strong _ -> (??)
46+
* | Hard_break -> (??)
47+
* | Soft_break -> (??)
48+
* | Link _ -> (??)
49+
* | Image _ -> (??)
50+
* | Html _ -> (??) *)
51+
52+
and of_image (img : Omd.inline Omd.link_def) =
53+
let escaped_url = Omd.Internal.escape_uri img.destination in
54+
let title' = Option.value ~default:"" img.title in
55+
let alt = "TODO" in
56+
(* let attrs = img in *)
57+
Html.[img ~src:escaped_url ~alt ~a:[a_title title'] ()]
58+
59+
and of_inline_link =
60+
fun (l : Omd.inline Omd.link_def) ->
61+
let escaped_url = Omd.Internal.escape_uri l.destination in
62+
Html.[a ~a:[a_href escaped_url] (of_link_label l.label)]
63+
64+
65+
exception Invalid_markdown of string
66+
67+
let of_heading n inline =
68+
let h =
69+
let open Html in
70+
match n with
71+
| 1 -> h1
72+
| 2 -> h2
73+
| 3 -> h3
74+
| 4 -> h4
75+
| 5 -> h5
76+
| 6 -> h6
77+
| m -> raise (Invalid_markdown (Printf.sprintf "heading number %d" m))
78+
in
79+
h ~a:[] (of_inline inline)
80+
1981

2082
let of_block (block : Omd.block) =
21-
match block.bl_desc with
22-
| Paragraph inline -> Html.p (of_inline inline)
23-
| List _ -> raise (Failure "TODO of_block")
24-
| Blockquote _ -> raise (Failure "TODO of_block")
25-
| Thematic_break -> raise (Failure "TODO of_block")
26-
| Heading _ -> raise (Failure "TODO of_block")
27-
| Code_block _ -> raise (Failure "TODO of_block")
28-
| Html_block _ -> raise (Failure "TODO of_block")
29-
| Link_def _ -> raise (Failure "TODO of_block")
30-
| Definition_list _ -> raise (Failure "TODO of_block")
83+
(* FIXME *)
84+
try
85+
match block.bl_desc with
86+
| Paragraph i -> Html.p (of_inline i)
87+
| List _ -> raise (Failure "TODO of_block")
88+
| Blockquote _ -> raise (Failure "TODO of_block")
89+
| Thematic_break -> raise (Failure "TODO of_block")
90+
| Heading (n, i) -> of_heading n i
91+
| Code_block (_, _) -> raise (Failure "TODO of Code_block")
92+
(* Html.pre ~a:[] (of_inline i) *)
93+
| Html_block _ -> raise (Failure "TODO of_block")
94+
| Link_def _ -> raise (Failure "TODO of_block")
95+
| Definition_list _ -> raise (Failure "TODO of_block")
96+
with (Failure err) ->
97+
Html.(h1 [txt ("Error " ^ err)])
3198

3299
let of_omd ?(title="") : Omd.doc -> Tyxml.Html.doc =
33100
fun omd ->
34101
let title' = title in
35-
let body' = List.map of_block omd in
102+
let body' =
103+
try
104+
List.map of_block omd
105+
with (Failure err) ->
106+
Html.[h1 [txt ("Error " ^ err)]]
107+
in
36108
let open Html in
37109
html
38110
(head (title (txt title')) [])

0 commit comments

Comments
 (0)