Something went wrong. Try again.
Playing with dynamically serving my static website.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125type t = | Video of string * string option * bool | Raster of string * string option * string option * (int * int) option | Vector of string * string option * string option | Audio of string | Photo of string | Chart of string * string * string * string | GeoJSON of string | Youtube of string | Diagram of string | CompareRaster of string * string * string * string * (int * int) option | Unknown of string list
let find_raw_shortcodes body = let shortcode_regex = Str.regexp {|{{<\(.*\)>}}|} in let rec loop pos acc = try let _ = Str.search_forward shortcode_regex body pos in let loc = Str.match_beginning () in let nextpos = Str.match_end () in let raw = Str.matched_group 1 body in let shortcode = String.trim raw in loop nextpos ((shortcode, (loc, String.length raw + 6)) :: acc) with Not_found -> acc in List.rev (loop 0 [])
let img_expansion args = let filename = Fpath.v (List.hd args) in let _, ext = Fpath.split_ext filename in match ext with | ".svg" -> ( match args with | [ arg1 ] -> Vector (arg1, None, None) | [ arg1; arg2 ] -> Vector (arg1, Some arg2, None) | [ arg1; arg2; arg3 ] -> Vector (arg1, Some arg2, Some arg3) | _ -> Unknown args) | ".mov" | ".mp4" -> ( match args with | [ arg1 ] -> Video (arg1, None, false) | [ arg1; arg2 ] -> Video (arg1, Some arg2, false) | _ -> Unknown args) | _ -> ( match args with | [ arg1 ] -> Raster (arg1, None, None, None) | [ arg1; arg2 ] -> Raster (arg1, Some arg2, None, None) | [ arg1; arg2; arg3 ] -> Raster (arg1, Some arg2, Some arg3, None) | _ -> Unknown args)
let find_labels body = let open Cmarkit in let body = Doc.of_string body in let inline _m acc = function | Inline.Image (l, _) -> let t = match Inline.Link.text l with | Inline.Text (s, _) -> Some s | _ -> None in let r = match Inline.Link.reference l with | `Inline (link, _) -> let s, _ = Option.get (Link_definition.dest link) in s | `Ref (_, _, _) -> "ref" in let args = match t with None -> [ r ] | Some x -> [ r; x ] in Folder.ret (img_expansion args :: acc) | _ -> Folder.default (* let the folder thread the fold *) in let folder = Folder.make ~inline () in Folder.fold_doc folder [] body
let find_codes body = let open Cmarkit in let body = Doc.of_string body in let block _m acc = function | Block.Code_block (node, _) -> ( match Block.Code_block.info_string node with | Some ("d2", _) -> let code_nodes = Block.Code_block.code node in let code_lines = List.map (fun (s, _) -> s) code_nodes in let code = List.fold_left (fun acc l -> Printf.sprintf "%s\n%s" acc l) "" code_lines in Folder.ret (Diagram code :: acc) | _ -> Folder.default) | _ -> Folder.default in let folder = Folder.make ~block () in Folder.fold_doc folder [] body
let find_shortcodes body = let rex = Pcre2.regexp {|"[^"]+"|[\w\.\-]+|} in find_raw_shortcodes body |> List.map (fun (r, loc) -> ( loc, (try Array.to_list (Pcre2.extract_all ~rex r) with Not_found -> []) |> List.map (fun a -> let part = a.(0) in match String.starts_with ~prefix:"\"" part with | false -> part | true -> String.sub part 1 (String.length part - 2)) )) |> List.map (fun (loc, sl) -> ( loc, match sl with | [ "video"; arg1 ] -> Video (arg1, None, false) | [ "video"; arg1; arg2 ] -> Video (arg1, Some arg2, false) | [ "videoloop"; arg1 ] -> Video (arg1, None, true) | [ "videoloop"; arg1; arg2 ] -> Video (arg1, Some arg2, true) | [ "img" ] -> Unknown sl | "img" :: args -> img_expansion args | [ "audio"; arg1 ] -> Audio arg1 | [ "photo"; arg1 ] -> Photo arg1 | [ "youtube"; arg1 ] -> Youtube arg1 | [ "chart"; arg1; arg2; arg3; arg4 ] -> Chart (arg1, arg2, arg3, arg4) | [ "geojson"; arg1 ] -> GeoJSON arg1 | [ "compare"; arg1; arg2 ] -> CompareRaster (arg1, arg2, "before", "after", None) | [ "compare"; arg1; arg2; arg3; arg4 ] -> CompareRaster (arg1, arg2, arg3, arg4, None) | _ -> Unknown sl ))