package slipshow

  1. Overview
  2. Docs

Source file renderers.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
module RenderAttrs = struct
  module C = Cmarkit_renderer.Context
  open Cmarkit

  let add_attr c (key, value) =
    match value with
    | Some { Cmarkit.Attributes.v = value; delimiter = Some d } ->
        let s = Format.sprintf " %s=%c%s%c" key d value d in
        C.string c s
    | Some { Cmarkit.Attributes.v = value; delimiter = None } ->
        C.string c (" " ^ key ^ "=" ^ value)
    | None -> C.string c (" " ^ key)

  let add_attrs c ?(include_id = true) attrs =
    let kv_attrs =
      let kv_attrs = Cmarkit.Attributes.kv_attributes attrs in
      List.map
        (fun ((k, _), v) ->
          let v = match v with None -> None | Some (v, _) -> Some v in
          (k, v))
        kv_attrs
    in
    let class' =
      let class' = Cmarkit.Attributes.class' attrs in
      let class' = List.map (fun (c, _) -> c) class' in
      match class' with
      | [] -> []
      | _ ->
          let v = String.concat " " class' in
          [ ("class", Some { Cmarkit.Attributes.v; delimiter = Some '"' }) ]
    in
    let id =
      let id = Cmarkit.Attributes.id attrs in
      match id with
      | Some (id, _) when include_id ->
          let attr = { Cmarkit.Attributes.v = id; delimiter = Some '"' } in
          [ ("id", Some attr) ]
      | _ -> []
    in
    let attrs = id @ class' @ kv_attrs in
    List.iter (add_attr c) attrs

  let open_block ?(with_newline = true) c tag attrs =
    C.string c "<";
    C.string c tag;
    add_attrs c attrs;
    C.string c ">";
    if with_newline then C.string c "\n"

  let close_block ?(with_newline = true) c tag =
    C.string c "</";
    C.string c tag;
    C.string c ">";
    if with_newline then C.string c "\n"

  let in_block c ?(with_newline = true) tag attrs f =
    open_block ~with_newline c tag attrs;
    f ();
    close_block ~with_newline c tag

  let with_attrs_span c ?(with_newline = true) attrs f =
    if Attributes.is_empty attrs then f ()
    else in_block c ~with_newline "span" attrs f

  let () = ignore with_attrs_span

  let block_lines c = function
    (* newlines only between lines *)
    | [] -> ()
    | (l, _) :: ls ->
        let line c (l, _) =
          C.byte c '\n';
          C.string c l
        in
        C.string c l;
        List.iter (line c) ls
end

let to_string = function
  (* Standard Cmarkit nodes *)
  | Cmarkit.Block.Blank_line _ -> "Blank line"
  | Cmarkit.Block.Block_quote _ -> "Block_quote"
  | Cmarkit.Block.Blocks _ -> "Blocks"
  | Cmarkit.Block.Code_block _ -> "Code_block"
  | Cmarkit.Block.Heading _ -> "Heading"
  | Cmarkit.Block.Html_block _ -> "Html_block"
  | Cmarkit.Block.Link_reference_definition _ -> "Link_reference_definition"
  | Cmarkit.Block.List _ -> "List"
  | Cmarkit.Block.Paragraph _ -> "Paragraph"
  | Cmarkit.Block.Thematic_break _ -> "Thematic_break"
  (* Extension Cmarkit nodes *)
  | Cmarkit.Block.Ext_math_block _ -> "Ext_math_block"
  | Cmarkit.Block.Ext_table _ -> "Ext_table"
  | Cmarkit.Block.Ext_footnote_definition _ -> "Ext_footnote_definition"
  | Cmarkit.Block.Ext_standalone_attributes _ -> "Ext_standalone_attributes"
  | Cmarkit.Block.Ext_attribute_definition _ -> "Ext_attribute_definition"
  (* Slipshow nodes *)
  | Ast.Included _ -> "Included"
  | Ast.Div _ -> "Div"
  | Ast.Slide _ -> "Slide"
  | Ast.Slip _ -> "Slip"
  | Ast.SlipScript _ -> "SlipScript"
  | _ -> "other"

let () = ignore to_string

module C = Cmarkit_renderer.Context

let src uri files =
  match uri with
  | Asset.Uri.Link l -> l
  | Path p -> (
      match Fpath.Map.find_opt p (files : Ast.Files.map) with
      | None -> Fpath.to_string p
      | Some { content; mode = `Base64; _ } ->
          let mime_type = Magic_mime.lookup (Fpath.filename p) in
          let base64 = Base64.encode_string content in
          Format.sprintf "data:%s;base64,%s" mime_type base64)

(* Inspired from Cmarkit's image rendering *)
let pdf c ~uri ~id:_ ~files i attrs =
  let open Cmarkit in
  match uri with
  | Asset.Uri.Link l ->
      Logs.warn (fun m -> m "pdf does not work with urls: ignoring %s" l)
  | Path p ->
      let attrs =
        Attributes.add_class attrs ("slipshow__carousel", Meta.none)
      in
      let src =
        match Fpath.Map.find_opt p (files : Ast.Files.map) with
        | None ->
            Logs.warn (fun m -> m "No pdf found: %a" Fpath.pp p);
            Fpath.to_string p
        | Some { content; mode = `Base64; _ } ->
            let base64 = Base64.encode_string content in
            Format.sprintf "%s" base64
      in
      let plain_text i =
        let lines = Inline.to_plain_text ~break_on_soft:false i in
        String.concat "\n" (List.map (String.concat "") lines)
      in
      C.string c "<span slipshow-pdf";
      C.string c " pdf-src=\"";
      Cmarkit_html.pct_encoded_string c src;
      C.string c "\" alt=\"";
      Cmarkit_html.html_escaped_string c (plain_text (Inline.Link.text i));
      C.byte c '\"';
      RenderAttrs.add_attrs c attrs;
      C.string c ">";
      C.string c "</span>"

(* Inspired from Cmarkit's image rendering *)
let media ?(close = " >") ~media_name c ~uri ~id:_ ~files i attrs =
  let open Cmarkit in
  let src = src uri files in
  let plain_text i =
    let lines = Inline.to_plain_text ~break_on_soft:false i in
    String.concat "\n" (List.map (String.concat "") lines)
  in
  C.byte c '<';
  C.string c media_name;
  C.string c " src=\"";
  Cmarkit_html.pct_encoded_string c src;
  C.string c "\" alt=\"";
  Cmarkit_html.html_escaped_string c (plain_text (Inline.Link.text i));
  C.byte c '\"';
  if false then C.string c " controls";
  RenderAttrs.add_attrs c attrs;
  C.string c close

let custom_html_renderer (files : Ast.Files.map) =
  let open Cmarkit_renderer in
  let open Cmarkit in
  let open Cmarkit_html in
  let default = renderer ~safe:false () in
  let custom_html =
    let inline c = function
      | Inline.Text ((t, (attrs, _)), _) ->
          (* Put text inside spans to be able to apply styles on them *)
          Context.string c "<span";
          add_attrs c attrs;
          Context.byte c '>';
          html_escaped_string c t;
          Context.string c "</span>";
          true
      | Ast.Pdf { uri; id; origin = (l, (attrs, _)), _ } ->
          pdf c ~uri ~id ~files l attrs;
          true
      | Ast.Video { uri; id; origin = (l, (attrs, _)), _ } ->
          media ~media_name:"video" c ~uri ~id ~files l attrs;
          true
      | Ast.Image { uri; id; origin = (l, (attrs, _)), _ } ->
          media ~media_name:"img" c ~uri ~id ~files l attrs;
          true
      | Ast.Audio { uri; id; origin = (l, (attrs, _)), _ } ->
          media ~media_name:"audio" c ~uri ~id ~files l attrs;
          true
      | _ -> false (* let the default HTML renderer handle that *)
    in
    let block c = function
      | Ast.Included ((b, (attrs, _)), _) | Ast.Div ((b, (attrs, _)), _) ->
          let should_include_div =
            let attrs_is_not_empty = not @@ Attributes.is_empty attrs in
            let contains_multiple_blocks =
              let is_multiple l =
                l
                |> List.filter (function
                     | Block.Blank_line _ -> false
                     | _ -> true)
                |> List.length |> ( <= ) 2
              in
              match b with
              | Block.Blocks (l, _) when is_multiple l -> true
              | _ -> false
            in
            attrs_is_not_empty || contains_multiple_blocks
          in
          if should_include_div then
            RenderAttrs.in_block c "div" attrs (fun () -> Context.block c b)
          else Context.block c b;
          true
      | Ast.Carousel ((l, (attrs, _)), _) ->
          let attrs =
            Attributes.add_class attrs ("slipshow__carousel", Meta.none)
          in
          let children_attrs =
            Attributes.make
              ~class':[ ("slipshow__carousel_children", Meta.none) ]
              ()
          in
          RenderAttrs.in_block c "div" attrs (fun () ->
              List.iteri
                (fun i b ->
                  let attrs =
                    if i = 0 then
                      Attributes.add_class children_attrs
                        ("slipshow__carousel_active", Meta.none)
                    else children_attrs
                  in
                  RenderAttrs.in_block c "div" attrs @@ fun () ->
                  Context.block c b)
                l);
          true
      | Ast.Slide (({ content; title }, (attrs, _)), _) ->
          let () =
            RenderAttrs.in_block c "div"
              (Attributes.add_class attrs ("slipshow-rescaler", Meta.none))
            @@ fun () ->
            RenderAttrs.in_block c "div"
              (Attributes.make ~class':[ ("slide", Meta.none) ] ())
            @@ fun () ->
            (match title with
            | None -> ()
            | Some (title, (title_attrs, _)) ->
                RenderAttrs.in_block c "div"
                  (Attributes.add_class title_attrs ("slide-title", Meta.none))
                  (fun () -> Context.inline c title));
            RenderAttrs.in_block c "div"
              (Attributes.make ~class':[ ("slide-body", Meta.none) ] ())
            @@ fun () -> Context.block c content
          in
          true
      | Ast.Slip ((slip, (attrs, _)), _) ->
          let () =
            RenderAttrs.in_block c "div"
              (Attributes.add_class attrs ("slipshow-rescaler", Meta.none))
            @@ fun () ->
            RenderAttrs.in_block c "div"
              (Attributes.make ~class':[ ("slip", Meta.none) ] ())
            @@ fun () ->
            RenderAttrs.in_block c "div"
              (Attributes.make ~class':[ ("slip-body", Meta.none) ] ())
            @@ fun () -> Context.block c slip
          in
          true
      | Ast.SlipScript ((cb, (attrs, _)), _) ->
          let attrs =
            Attributes.add ("type", Meta.none)
              (Some ({ v = "slip-script"; delimiter = None }, Meta.none))
              attrs
          in
          RenderAttrs.in_block c "script" attrs (fun () ->
              RenderAttrs.block_lines c (Block.Code_block.code cb));
          true
      | _ -> false
    in
    make ~inline ~block ()
  in
  compose default custom_html

let to_html_string (doc : Ast.t) =
  Cmarkit_renderer.doc_to_string (custom_html_renderer doc.files) doc.doc