text
stringlengths 0
601k
|
---|
let ns_find_xs ns s = ns_find ( fun ns -> ns . ns_xs ) ns s
|
let ns_find_ns ns s = ns_find ( fun ns -> ns . ns_ns ) ns s
|
let ns_find_tns ns s = ns_find ( fun ns -> ns . ns_tns ) ns s
|
let ns_exists_ns ns s = Mstr . mem s ns . ns_ns
|
let rec ns_rm_ts ns = function | [ ] -> assert false | [ x ] -> { ns with ns_ts = Mstr . remove x ns . ns_ts } | x :: xs -> let x_ns = ns_rm_ts ( Mstr . find x ns . ns_ns ) xs in { ns with ns_ns = Mstr . add x x_ns ns . ns_ns }
|
let rec ns_replace_ts new_ts sl ns = match sl with | [ ] -> assert false | [ x ] -> { ns with ns_ts = Mstr . add x new_ts ns . ns_ts } | x :: xs -> let ns_ns = Mstr . find x ns . ns_ns in let ns_ns = ns_replace_ts new_ts xs ns_ns in { ns with ns_ns = Mstr . add x ns_ns ns . ns_ns }
|
let rec ns_subst_ts old_ns new_ts { ns_ts ; ns_ls ; ns_fd ; ns_xs ; ns_ns ; ns_tns } = { ns_ts = Mstr . map ( ts_subst_ts old_ns new_ts ) ns_ts ; ns_ls = Mstr . map ( ls_subst_ts old_ns new_ts ) ns_ls ; ns_fd = Mstr . map ( ls_subst_ts old_ns new_ts ) ns_fd ; ns_xs = Mstr . map ( xs_subst_ts old_ns new_ts ) ns_xs ; ns_ns = Mstr . map ( ns_subst_ts old_ns new_ts ) ns_ns ; ns_tns = Mstr . map ( ns_subst_ts old_ns new_ts ) ns_tns ; }
|
let rec ns_subst_ty old_ts new_ts ty { ns_ts ; ns_ls ; ns_fd ; ns_xs ; ns_ns ; ns_tns } = { ns_ts = Mstr . map ( ts_subst_ty old_ts new_ts ty ) ns_ts ; ns_ls = Mstr . map ( ls_subst_ty old_ts new_ts ty ) ns_ls ; ns_fd = Mstr . map ( ls_subst_ty old_ts new_ts ty ) ns_fd ; ns_xs = Mstr . map ( xs_subst_ty old_ts new_ts ty ) ns_xs ; ns_ns = Mstr . map ( ns_subst_ty old_ts new_ts ty ) ns_ns ; ns_tns = Mstr . map ( ns_subst_ty old_ts new_ts ty ) ns_tns ; }
|
let mk_td ts kind = { Tast . td_ts = ts ; td_params = [ ] ; td_cstrs = [ ] ; td_kind = kind ; td_private = Tast . Public ; td_manifest = None ; td_attrs = [ ] ; td_spec = None ; td_loc = Location . none ; }
|
let mk_abstract_td ts = mk_td { Ttypes . ts_ident = ts . ts_ident ; ts_args = [ ] ; ts_alias = None } Tast . Pty_abstract
|
let ns_with_primitives = let primitive_tys = [ ( " integer " , ts_integer ) ; ( " int " , ts_int ) ; ( " char " , ts_char ) ; ( " bytes " , ts_bytes ) ; ( " string " , ts_string ) ; ( " float " , ts_float ) ; ( " bool " , ts_bool ) ; ( " unit " , ts_unit ) ; ( " exn " , ts_exn ) ; ( " array " , ts_array ) ; ( " list " , ts_list ) ; ( " option " , ts_option ) ; ( " int32 " , ts_int32 ) ; ( " int64 " , ts_int64 ) ; ( " nativeint " , ts_nativeint ) ; ( " format6 " , ts_format6 ) ; ( " lazy " , ts_lazy ) ; ] in let td_list = mk_td ts_list ( Tast . Pty_variant [ { Tast . cd_cs = fs_list_nil ; cd_ld = [ ] ; cd_loc = Location . none ; cd_attrs = [ ] ; } ; { Tast . cd_cs = fs_list_cons ; cd_ld = [ ] ; cd_loc = Location . none ; cd_attrs = [ ] ; } ; ] ) in let td_option = mk_td ts_option ( Tast . Pty_variant [ { Tast . cd_cs = fs_option_none ; cd_ld = [ ] ; cd_loc = Location . none ; cd_attrs = [ ] ; } ; { Tast . cd_cs = fs_option_some ; cd_ld = [ ] ; cd_loc = Location . none ; cd_attrs = [ ] ; } ; ] ) in let abstract_ts = [ ts_unit ; ts_bool ; ts_int ; ts_integer ; ts_float ; ts_string ; ts_char ; ts_bytes ; ts_exn ; ts_int32 ; ts_int64 ; ts_nativeint ; ts_format6 ; ts_lazy ; ] in List . iter ( fun ts -> Hts . add type_declarations ts ( mk_abstract_td ts ) ) abstract_ts ; Hts . add type_declarations ts_option td_option ; Hts . add type_declarations ts_list td_list ; let primitive_ps = [ ( ps_equ . ls_name . id_str , ps_equ ) ] in let primitive_ls = [ ( none . id_str , fs_option_none ) ; ( some . id_str , fs_option_some ) ; ( nil . id_str , fs_list_nil ) ; ( cons . id_str , fs_list_cons ) ; ] in let ns = List . fold_left ( fun ns ( s , ts ) -> ns_add_ts ~ allow_duplicate : true ns s ts ) empty_ns primitive_tys in List . fold_left ( fun ns ( s , ls ) -> ns_add_ls ~ allow_duplicate : true ns s ls ) ns ( primitive_ls @ primitive_ps )
|
module Mid = Map . Make ( Ident )
|
type known_ids = signature_item Mid . t
|
type file = { fl_nm : Ident . t ; fl_sigs : signature ; fl_export : namespace }
|
type module_uc = { muc_nm : Ident . t ; muc_sigs : signature list ; muc_prefix : string list ; muc_import : namespace list ; muc_export : namespace list ; muc_files : file Mstr . t ; muc_kid : known_ids ; muc_crcm : Coercion . t ; }
|
let muc_add ( ? export = false ) add muc s x = match ( muc . muc_import , muc . muc_export ) with | i0 :: il , e0 :: el -> let i = add ~ allow_duplicate : true i0 s x in let e = if export then add ~ allow_duplicate : false e0 s x else e0 in { muc with muc_import = i :: il ; muc_export = e :: el } | _ -> assert false
|
let add_ts ( ? export = false ) = muc_add ~ export ns_add_ts
|
let add_ls ( ? export = false ) = muc_add ~ export ns_add_ls
|
let add_fd ( ? export = false ) = muc_add ~ export ns_add_fd
|
let add_xs ( ? export = false ) = muc_add ~ export ns_add_xs
|
let add_ns ( ? export = false ) = muc_add ~ export ns_add_ns
|
let add_tns ( ? export = false ) = muc_add ~ export ns_add_tns
|
let add_file muc s file = { muc with muc_files = Mstr . add s file muc . muc_files }
|
let get_file muc s = Mstr . find s muc . muc_files
|
let add_kid muc id s = { muc with muc_kid = Mid . add id s muc . muc_kid }
|
let add_sig muc sig_ = match muc . muc_sigs with | s0 :: sl -> { muc with muc_sigs = ( sig_ :: s0 ) :: sl } | _ -> assert false
|
let add_coer muc ls = { muc with muc_crcm = Coercion . add muc . muc_crcm ls }
|
let add_ns_top ( ? export = false ) muc ns = let add f muc map = Mstr . fold ( fun s v muc -> f muc s v ) map muc in let muc = add ( add_ts ~ export ) muc ns . ns_ts in let muc = add ( add_ls ~ export ) muc ns . ns_ls in let muc = add ( add_fd ~ export ) muc ns . ns_fd in let muc = add ( add_xs ~ export ) muc ns . ns_xs in let muc = add ( add_ns ~ export ) muc ns . ns_ns in let muc = add ( add_tns ~ export ) muc ns . ns_tns in muc
|
let muc_replace_ts muc new_ts sl = match ( muc . muc_import , muc . muc_export ) with | i0 :: il , e0 :: el -> { muc with muc_import = ns_replace_ts new_ts sl i0 :: il ; muc_export = ns_replace_ts new_ts sl e0 :: el ; } | _ -> assert false
|
let muc_subst_ts muc old_ts new_ts = match ( muc . muc_import , muc . muc_export ) with | i0 :: il , e0 :: el -> { muc with muc_import = ns_subst_ts old_ts new_ts i0 :: il ; muc_export = ns_subst_ts old_ts new_ts e0 :: el ; } | _ -> assert false
|
let muc_subst_ty muc old_ts new_ts ty = match ( muc . muc_import , muc . muc_export ) with | i0 :: il , e0 :: el -> { muc with muc_import = ns_subst_ty old_ts new_ts ty i0 :: il ; muc_export = ns_subst_ty old_ts new_ts ty e0 :: el ; } | _ -> assert false
|
let muc_rm_ts muc sl = match ( muc . muc_import , muc . muc_export ) with | i0 :: il , e0 :: el -> { muc with muc_import = ns_rm_ts i0 sl :: il ; muc_export = ns_rm_ts e0 sl :: el ; } | _ -> assert false
|
let open_empty_module muc s = { muc with muc_prefix = s :: muc . muc_prefix ; muc_sigs = [ ] :: muc . muc_sigs ; muc_import = ns_with_primitives :: muc . muc_import ; muc_export = empty_ns :: muc . muc_export ; }
|
let close_module_file muc = match ( muc . muc_import , muc . muc_export , muc . muc_prefix , muc . muc_sigs ) with | _ :: i1 :: il , e0 :: e1 :: el , p0 :: pl , s0 :: sl -> let file = { fl_nm = Ident . create ~ loc : Location . none p0 ; fl_sigs = List . rev s0 ; fl_export = e0 ; } in { muc with muc_prefix = pl ; muc_import = ns_add_ns ~ allow_duplicate : true i1 p0 e0 :: il ; muc_export = e1 :: el ; muc_sigs = sl ; muc_files = Mstr . add p0 file muc . muc_files ; } | _ -> assert false
|
let open_module muc s = match muc . muc_import with | i0 :: _ -> { muc with muc_prefix = s :: muc . muc_prefix ; muc_sigs = [ ] :: muc . muc_sigs ; muc_import = i0 :: muc . muc_import ; muc_export = empty_ns :: muc . muc_export ; } | _ -> assert false
|
let close_module muc = match ( muc . muc_import , muc . muc_export , muc . muc_prefix , muc . muc_sigs ) with | _ :: i1 :: il , e0 :: e1 :: el , p0 :: pl , _ :: sl -> { muc with muc_prefix = pl ; muc_import = ns_add_ns ~ allow_duplicate : true i1 p0 e0 :: il ; muc_export = ns_add_ns ~ allow_duplicate : true e1 p0 e0 :: el ; muc_sigs = sl ; } | _ -> assert false
|
let close_module_functor muc = match ( muc . muc_import , muc . muc_export , muc . muc_prefix , muc . muc_sigs ) with | _ :: i1 :: il , e0 :: e1 :: el , p0 :: pl , _ :: sl -> { muc with muc_prefix = pl ; muc_import = ns_add_ns ~ allow_duplicate : true i1 p0 e0 :: il ; muc_export = e1 :: el ; muc_sigs = sl ; } | _ -> assert false
|
let close_module_type muc = match ( muc . muc_import , muc . muc_export , muc . muc_prefix , muc . muc_sigs ) with | _ :: i1 :: il , e0 :: e1 :: el , p0 :: pl , _ :: sl -> { muc with muc_prefix = pl ; muc_import = ns_add_tns ~ allow_duplicate : true i1 p0 e0 :: il ; muc_export = ns_add_tns ~ allow_duplicate : true e1 p0 e0 :: el ; muc_sigs = sl ; } | _ -> assert false
|
let get_top_sigs muc = match muc . muc_sigs with s0 :: _ -> List . rev s0 | _ -> assert false
|
let get_top_import muc = match muc . muc_import with i0 :: _ -> i0 | _ -> assert false
|
let get_top_export muc = match muc . muc_export with e0 :: _ -> e0 | _ -> assert false
|
let add_sig_contents muc sig_ = let muc = add_sig muc sig_ in let get_cs_pjs = function | Pty_abstract -> [ ] | Pty_variant cdl -> List . map ( fun cd -> cd . cd_cs ) cdl | Pty_record rd -> rd . rd_cs :: List . map ( fun ld -> ld . ld_field ) rd . rd_ldl in match sig_ . sig_desc with | Sig_val ( ( { vd_spec = Some sp ; _ } as v ) , _ ) when sp . sp_pure -> let tyl = List . map ty_of_lb_arg sp . sp_args in let ty = ty_tuple ( List . map ty_of_lb_arg sp . sp_ret ) in let ls = lsymbol ~ field : false v . vd_name tyl ( Some ty ) in let muc = add_ls ~ export : true muc ls . ls_name . id_str ls in add_kid muc ls . ls_name sig_ | Sig_function f -> let muc = add_ls ~ export : true muc f . fun_ls . ls_name . id_str f . fun_ls in let muc = match f . fun_spec with | Some spec when spec . fun_coer -> add_coer muc f . fun_ls | _ -> muc in add_kid muc f . fun_ls . ls_name sig_ | Sig_type ( _ , tdl , _ ) -> let add_td muc td = let s = ( ts_ident td . td_ts ) . id_str in let muc = add_ts ~ export : true muc s td . td_ts in let csl = get_cs_pjs td . td_kind in let muc = List . fold_left ( fun muc cs -> ( if cs . ls_field then add_fd else add_ls ) ~ export : true muc cs . ls_name . id_str cs ) muc csl in let fields = Option . fold ~ none [ ] : ~ some ( : fun spec -> List . map fst spec . ty_fields ) td . td_spec in let muc = List . fold_left ( fun muc ls -> ( if ls . ls_field then add_fd else add_ls ) ~ export : true muc ls . ls_name . id_str ls ) muc fields in add_kid muc td . td_ts . ts_ident sig_ in List . fold_left add_td muc tdl | Sig_exception te -> let s = te . exn_constructor . ext_ident . id_str in let xs = te . exn_constructor . ext_xs in let muc = add_xs ~ export : true muc s xs in add_kid muc te . exn_constructor . ext_ident sig_ | Sig_open ( { opn_id ; _ } , _ ) -> let nm = List . hd ( List . rev opn_id ) in let ns = ns_find_ns ( get_top_import muc ) opn_id in add_ns_top ~ export : false ( add_ns ~ export : false muc nm ns ) ns | _ -> muc
|
let init_muc s = { muc_nm = Ident . create ~ loc : Location . none s ; muc_sigs = [ [ ] ] ; muc_prefix = [ s ] ; muc_import = [ ns_with_primitives ] ; muc_export = [ empty_ns ] ; muc_files = Mstr . empty ; muc_kid = Mid . empty ; muc_crcm = Coercion . empty ; }
|
let wrap_up_muc ( muc : module_uc ) = match ( muc . muc_export , muc . muc_sigs ) with | [ e ] , [ s ] -> { fl_nm = muc . muc_nm ; fl_sigs = List . rev s ; fl_export = e } | _ -> assert false
|
let rec tree_ns f fmt ns = Mstr . iter ( fun s ns -> if f ns = Mstr . empty then pp fmt " [ @% s @\ n ] " @ s else pp fmt " [ @% s :@\ n @ [ @% a ] @@\ n ] " @ s ( tree_ns f ) ( f ns ) ) ns
|
let ns_names nsm = List . map fst ( Mstr . bindings nsm )
|
let print_mstr_vals printer fmt m = let print_elem e = pp fmt " [ @% a ] @@\ n " printer e in Mstr . iter ( fun _ -> print_elem ) m
|
let rec print_nested_ns fmt ns = let print_elem nm e = pp fmt " [ @% a ] @@\ n " ( print_ns nm ) e in Mstr . iter ( fun nm ns -> print_elem nm ns ) ns pp fmt " [ [ @@< hv2 [ >@ Namespace : % s ] @@\ n \ [ @< hv2 > Type symbols @\ n \ % a ] @@\ n \ [ @< hv2 > Logic Symbols @\ n \ % a ] @@\ n \ [ @< hv2 > Field Symbols @\ n \ % a ] @@\ n \ [ @< hv2 > Exception Symbols @\ n \ % a ] @@\ n \ [ @< hv2 > Namespaces @\ n \ % a ] @@\ n \ [ @< hv2 > Type Namespaces @\ n \ % a ] ] ] " @@@ nm ( print_mstr_vals print_ts ) ns_ts ( print_mstr_vals print_ls_decl ) ns_ls ( print_mstr_vals print_ls_decl ) ns_fd ( print_mstr_vals print_xs ) ns_xs print_nested_ns ns_ns print_nested_ns ns_tns
|
let print_file fmt { fl_nm ; fl_sigs ; fl_export } = pp fmt " [ @ module % a @\ n [ @< h2 >@\ n % a @\ n [ @< hv2 > Signatures @\ n % a ] ] ] . " @@@@ Ident . pp fl_nm ( print_ns fl_nm . id_str ) fl_export print_signature fl_sigs
|
let rand_gen = lazy ( Random . State . make_self_init ( ) )
|
let rand_path dir pat = let rand = Random . State . bits ( Lazy . force rand_gen ) land 0xFFFFFF in Fpath . ( dir / Fmt . str pat ( Fmt . str " % 06x " rand ) )
|
let default_dir_init = let from_env var ~ absent = match try Some ( Sys . getenv var ) with Not_found -> None with | None -> absent | Some v -> match Fpath . of_string v with | Error ( ` Msg err ) -> failwith err | Ok v -> v in if Sys . os_type = " Win32 " then from_env " TEMP " ~ absent : Fpath . ( v " . " ) / else from_env " TMPDIR " ~ absent : Fpath . ( v " / tmp " )
|
let default_dir = ref default_dir_init
|
let set_default_dir p = default_dir := p
|
let default_dir ( ) = ! default_dir
|
let create_tmp_path mode dir pat = let err ( ) = R . error_msgf " create temporary file % s in % a : too many failing attemps " ( Fmt . str pat " XXXXXX " ) Fpath . pp dir in let rec go count = if count < 0 then err ( ) else let file = rand_path dir pat in let sfile = Fpath . to_string file in let open_flags = Unix . [ O_WRONLY ; O_CREAT ; O_EXCL ; ] in try Ok ( file , Unix . openfile sfile open_flags mode ) with | Unix . Unix_error ( Unix . EEXIST , _ , _ ) -> go ( count - 1 ) | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> go count | Unix . Unix_error ( e , _ , _ ) -> R . error_msgf " create temporary file % a : % s " Fpath . pp file ( Unix . error_message e ) in go 1000
|
type pattern = ( string -> string , Format . formatter , unit , string ) format4
|
let tmp ( ? mode = 0o644 ) ? dir pat = let dir = match dir with | None -> default_dir ( ) | Some d -> d in create_tmp_path mode dir pat >>= fun ( file , fd ) -> let rec close fd = try Unix . close fd with | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> close fd | Unix . Unix_error _ -> ( ) in close fd ; Ok file
|
type t = heading list
|
let to_string ( H ( size , text ) ) = " H " ^ string_of_int size ^ " " ^ text
|
let get_int = function H ( i , _ ) -> i
|
let toc doc = let rec loop acc = function | [ ] -> List . rev acc | ( b : Omd . block ) :: bs -> ( match b . bl_desc with | Omd . Heading ( s , il ) -> ( match il . il_desc with | Omd . Text heading -> loop ( H ( s , heading ) :: acc ) bs | _ -> loop acc bs ) | _ -> loop acc bs ) in loop [ ] doc
|
let text_to_id s = let replace on sep s = String . concat ~ sep ( String . split ~ on s ) in String . lowercase ( replace ' ' " " - s )
|
let transform doc = let f ( b : Omd . block ) = match b . bl_desc with | Omd . Heading ( _s , il ) -> ( match il . il_desc with | Omd . Text heading -> { b with bl_attributes = ( " id " , text_to_id heading ) :: b . bl_attributes ; } | _ -> b ) | _ -> b in List . map ~ f doc
|
type ' a tree = Br of ' a * ' a tree list
|
let rec pre ppf tree = match tree with | Br ( t , lst ) -> Format . pp_print_string ppf ( to_string t ) ; List . iter ~ f ( : pre ppf ) lst
|
let to_tree lst = let arr = Array . init 7 ~ f ( : fun i -> Br ( H ( i , " " ) , [ ] ) ) in let rec tidy arr until = function | n when n <= until -> ( ) | n -> let t = arr . ( n ) in let ( Br ( v , lst ) ) = arr . ( n - 1 ) in arr . ( n - 1 ) <- Br ( v , lst @ [ t ] ) ; tidy arr until ( n - 1 ) in let rec aux last = function | [ ] -> tidy arr 0 last ; arr . ( 0 ) | [ a ] -> let x = get_int a in arr . ( x ) <- Br ( a , [ ] ) ; aux x [ ] | a :: b :: xs -> let x = get_int a in let y = get_int b in let _t = arr . ( x ) in let ( Br ( p , lst ) ) = arr . ( x - 1 ) in if x = y then ( arr . ( x - 1 ) <- Br ( p , lst @ [ Br ( a , [ ] ) ] ) ; aux x ( b :: xs ) ) else if x > y then ( arr . ( x ) <- Br ( a , [ ] ) ; tidy arr ( y - 1 ) x ; aux x ( b :: xs ) ) else ( arr . ( x ) <- Br ( a , [ ] ) ; aux x ( b :: xs ) ) in aux 0 lst
|
let map_to_item h = let to_elt cl link txt = [ % html " < a class " = cl " href " = ( " " # ^ link ) " " > [ Html . txt txt ] " </ a " ] > in match h with | H ( i , txt ) -> if String . equal txt " " then [ ] else [ to_elt [ " toc - link " ; " toc - item " - ^ string_of_int i ] ( text_to_id txt ) txt ; ]
|
let rec preorder = function | Br ( v , [ ] ) -> [ % html " < ul class ' = toc ' >< li class ' = toc - li ' " > ( map_to_item v ) " </ li ></ ul " ] > | Br ( v , lst ) -> [ % html " < ul class ' = toc ' >< li " > ( map_to_item v @ List . fold_left ~ f ( : fun acc v -> acc @ [ preorder v ] ) ~ init [ ] : lst ) " </ li ></ ul " ] >
|
let pp ppf t = List . iter ~ f ( : fun h -> Format . pp_print_string ppf ( to_string h ^ " \ n " ) ) t
|
let rec accessibility = function | x :: y :: ys -> let a = get_int x in let b = get_int y in if get_int x < get_int y then if b - a = 1 then accessibility ( y :: ys ) else raise ( Failure " Failed because of inproper heading nesting " ) | _ -> assert true
|
let to_html toc = ( try accessibility toc with Failure t -> print_endline " == Failed Heading List " ; == pp Format . std_formatter toc ; raise ( Failure t ) ) ; let tree = to_tree toc in preorder tree
|
let equal = Stdlib . ( = )
|
type toc_settings = { min_level : int ; max_level : int ; max_heading_link_level : int ; toc_class : string option ; toc_class_levels : bool ; numbered_list : bool ; link_here : bool ; link_here_text : string ; link_here_class : string option ; link_here_append : bool ; use_text : bool ; use_slugs : bool ; soft_slug : bool ; slug_regex : string option ; slug_replacement : string option ; slug_force_lowercase : bool ; strip_tags : bool ; valid_html : bool ; min_headings : int ; ignore_heading_selectors : string list ; }
|
let make_counter seed = let counter = ref seed in fun ( ) -> incr counter ; ! counter
|
let get_heading_id settings counter heading = let id = Soup . attribute " id " heading in match id with | Some id -> id | None -> if not ( settings . use_slugs || settings . use_text ) then counter ( ) |> string_of_int else let text = Html_utils . get_element_text heading in begin match text with | None -> counter ( ) |> string_of_int | Some t -> if settings . use_slugs then let regex = if settings . soft_slug then ( Some " \\ s " ) + else settings . slug_regex in let lowercase = if settings . soft_slug then false else settings . slug_force_lowercase in try Utils . slugify ~ lowercase : lowercase ~ regex : regex ~ sub : settings . slug_replacement t with _ -> soupault_error @@ Printf . sprintf " Invalid regex in the slug_regex option : ' % s ' " ( Option . value ~ default " " : regex ) else t end
|
let make_toc_class settings level = match settings . toc_class with | None -> None | Some _class -> let _class = if settings . toc_class_levels then Printf . sprintf " % s -% d " _class level else _class in Some _class
|
let add_item settings heading container = let li = Soup . create_element " li " in let heading_id = Soup . attribute " id " heading |> Option . get in let h_link = Soup . create_element ~ attributes [ " : href " , " " # ^ heading_id ] " a " in let h_content = Html_utils . child_nodes heading in let h_content = if settings . strip_tags then Html_utils . get_element_text h_content |> Option . value ~ default " " : |> Soup . parse else h_content in Soup . append_child h_link h_content ; Soup . append_child li h_link ; Soup . append_child container li ; li
|
let make_heading_linkable settings counter heading = if ( Html_utils . get_heading_level heading ) <= settings . max_heading_link_level then let heading_id = get_heading_id settings counter heading in Soup . set_attribute " id " heading_id heading
|
let add_section_link settings heading = let heading_level = Html_utils . get_heading_level heading in if ( heading_level <= settings . max_heading_link_level ) && ( heading_level >= settings . min_level ) then let heading_id = Soup . attribute " id " heading |> Option . get in let link_text = Soup . parse settings . link_here_text in let link_here = Soup . create_element ~ attributes [ " : href " , " " # ^ heading_id ] " a " in let ( ) = Soup . append_child link_here link_text in Html_utils . add_class settings . link_here_class link_here ; if settings . link_here_append then Soup . append_child heading link_here else Soup . prepend_child heading link_here
|
let make_toc_container settings level = let tag = if settings . numbered_list then " ol " else " ul " in let toc_list = Soup . create_element tag in let toc_class = make_toc_class settings level in Html_utils . add_class toc_class toc_list ; toc_list
|
let level_matches settings h = let level = Html_utils . get_heading_level h in ( level <= settings . max_level ) && ( level >= settings . min_level )
|
let ignored_heading settings soup h = let res = Html_utils . matches_any_of settings . ignore_heading_selectors soup h in match res with | true -> let ( ) = Logs . debug @@ fun m -> m " Heading % s is ignored due to ignore_selector settings " ( Soup . to_string h ) in true | false -> false
|
let rec _make_toc settings depth counter parent tree = let heading = Rose_tree . ( tree . value ) in let children = Rose_tree . ( tree . children ) in let level = Html_utils . get_heading_level heading in if level > settings . max_level then Logs . debug @@ fun m -> m " Heading % s is ignored because its level exceeds max_level ( % d ) " ( Soup . to_string heading ) settings . max_level else if level < settings . min_level then let ( ) = Logs . debug @@ fun m -> m " Heading % s is ignored because its level is below min_level ( % d ) , processings its sub - headings ( if any ) " ( Soup . to_string heading ) settings . min_level in List . iter ( _make_toc settings depth counter parent ) children else let item = add_item settings heading parent in match children with | [ ] -> ( ) | _ -> if not ( List . exists ( level_matches settings ) ( List . map ( fun c -> Rose_tree . ( c . value ) ) children ) ) then ( ) else let container = make_toc_container settings depth in if settings . valid_html then Soup . append_child item container else Soup . append_child parent container ; List . iter ( _make_toc settings ( depth + 1 ) counter container ) children
|
let toc _ config soup = let valid_options = List . append Config . common_widget_options [ " selector " ; " action " ; " strip_tags " ; " min_level " ; " max_level " ; " max_heading_link_level " ; " toc_list_class " ; " toc_class_levels " ; " numbered_list " ; " valid_html " ; " heading_links " ; " heading_link_text " ; " heading_link_class " ; " heading_links_append " ; " use_heading_text " ; " use_heading_slug " ; " soft_slug " ; " slug_regex " ; " slug_replacement_string " ; " slug_force_lowercase " ; " ignore_heading_selectors " ] in let ( ) = Config . check_options valid_options config " widget " \ toc " " \ in let max_level = Config . find_integer_or ~ default : 6 config [ " max_level " ] in let settings = { min_level = Config . find_integer_or ~ default : 1 config [ " min_level " ] ; max_level = max_level ; max_heading_link_level = ( let lvl = Config . find_integer_or ~ default : max_level config [ " max_heading_link_level " ] in if lvl < max_level then begin let ( ) = Logs . warn @@ fun m -> m " max_heading_level cannot be lower than max_level , forcing to max_level " in max_level end else lvl ) ; toc_class = OH . find_string_opt config [ " toc_list_class " ] ; toc_class_levels = Config . find_bool_or ~ default : false config [ " toc_class_levels " ] ; numbered_list = Config . find_bool_or ~ default : false config [ " numbered_list " ] ; link_here = Config . find_bool_or ~ default : false config [ " heading_links " ] ; link_here_text = Config . find_string_or ~ default " " :# config [ " heading_link_text " ] ; link_here_class = OH . find_string_opt config [ " heading_link_class " ] ; link_here_append = Config . find_bool_or ~ default : false config [ " heading_links_append " ] ; use_text = Config . find_bool_or ~ default : false config [ " use_heading_text " ] ; use_slugs = Config . find_bool_or ~ default : false config [ " use_heading_slug " ] ; soft_slug = Config . find_bool_or ~ default : false config [ " soft_slug " ] ; slug_regex = OH . find_string_opt config [ " slug_regex " ] ; slug_replacement = OH . find_string_opt config [ " slug_replacement_string " ] ; slug_force_lowercase = Config . find_bool_or ~ default : true config [ " slug_force_lowercase " ] ; strip_tags = Config . find_bool_or ~ default : false config [ " strip_tags " ] ; valid_html = Config . find_bool_or ~ default : false config [ " valid_html " ] ; min_headings = Config . find_integer_or ~ default : 0 config [ " min_headings " ] ; ignore_heading_selectors = Config . find_strings_or ~ default [ ] : config [ " ignore_heading_selectors " ] ; } in let selector = Config . find_string_result config [ " selector " ] in let action = OH . find_string_opt config [ " action " ] in match selector with | Error _ as e -> e | Ok selector -> begin let container = Soup . select_one selector soup in match container with | None -> let ( ) = Logs . debug @@ fun m -> m " Page has no elements matching selector " \% s " , \ nowhere to insert the ToC " selector in Ok ( ) | Some container -> begin let counter = make_counter 0 in let headings = Html_utils . find_headings soup in let headings = List . filter ( fun e -> not @@ ignored_heading settings soup e ) headings in if ( ( List . length headings ) < settings . min_headings ) then Ok ( ) else let ( ) = List . iter ( fun h -> make_heading_linkable settings counter h ) headings in let headings_tree = headings |> Rose_tree . from_list Html_utils . get_heading_level in match headings_tree with | [ ] -> let ( ) = Logs . debug @@ fun m -> m " Page has no headings , nothing to build a ToC from " in Ok ( ) | _ -> let toc_container = make_toc_container settings 1 in let _ = List . iter ( _make_toc settings 2 counter toc_container ) headings_tree in let ( ) = Html_utils . insert_element action container toc_container in let ( ) = if settings . link_here then List . iter ( fun h -> add_section_link settings h ) headings in Ok ( ) end end
|
module Model = struct type filter = | All | Active | Completed let string_of_filter : filter -> string = function | All -> " All " | Active -> " Active " | Completed -> " Completed " type t = { input : string ; items : ( string * bool ) list ; filter : filter ; editing : ( string * int ) option ; toggle : bool } let items_to_json is = ` List List . ( map ( fun ( msg , state ) -> ` List [ ` String msg ; ` Bool state ] ) is ) let items_of_json = function | ` List lst -> List . map ( function | ` List [ ` String msg ; ` Bool state ] -> ( msg , state ) | _ -> failwith " deserialization error : clear ' todos - ocaml - d3 ' from local storage " ) lst | _ -> failwith " deserialization error : clear ' todos - ocaml - d3 ' from local storage " let init = { input = " " ; items = [ ] ; filter = All ; editing = None ; toggle = false } let change_input t input = { t with input } let change_filter t filter = { t with filter } let update_status t i b = { t with items = List . mapi ( fun j ( t , s ) -> ( t , if i = j then b else s ) ) t . items } let toggle_status t = { t with toggle = not t . toggle ; items = List . map ( fun ( d , _ ) -> ( d , not t . toggle ) ) t . items } let update_description t i d = { t with items = List . mapi ( fun j ( t , s ) -> ( ( if j = i then d else t ) , s ) ) t . items } let start_edit t i = { t with editing = Some ( fst ( List . nth t . items i ) , i ) } let change_edit t d = match t . editing with | None -> assert false | Some ( _ , i ) -> { t with editing = Some ( d , i ) } let commit_edit t = match t . editing with | None -> assert false | Some ( s , i ) -> let t ' = update_description t i s in { t ' with editing = None } let cancel_edit t = { t with editing = None } let add_item t = { t with input = " " ; items = ( t . input , false ) :: t . items } let delete_item t i = let idx = ref 0 in { t with items = List . filter ( fun _ -> idx := ! idx + 1 ; ! idx - 1 <> i ) t . items } let undone t = List . ( length ( filter ( fun ( _ , b ) -> not b ) t . items ) ) let toggle t = { t with toggle = not t . toggle } end
|
module Event = struct type t = | AddInput | ChangeInput of string | Delete of int | Edit of int | ChangeEdit of string | CommitEdit | CancelEdit | Check of int | Uncheck of int | Filter of Model . filter | Toggle let handle t m = match t with | AddInput -> Model . add_item m | ChangeInput i -> Model . change_input m i | Filter f -> Model . change_filter m f | Toggle -> Model . toggle_status m | Delete index -> Model . delete_item m index | Edit index -> Model . start_edit m index | ChangeEdit d -> Model . change_edit m d | CommitEdit -> Model . commit_edit m | CancelEdit -> Model . cancel_edit m | Check index -> Model . update_status m index true | Uncheck index -> Model . update_status m index false end
|
let ifilter_map ~ f xs = let rec loop f i xs = match xs with | [ ] -> [ ] | x :: xs ' -> begin match f x i with | None -> loop f ( i + 1 ) xs ' | Some x ' -> x ' :: ( loop f ( i + 1 ) xs ' ) end in loop f 0 xs ; ;
|
module View = struct open D3 type item = { text : string ; completed : bool ; editing : bool } let items_of_model m = let display = let open Model in match m . filter with | All -> fun _ -> true | Active -> fun x -> not x | Completed -> fun x -> x in ifilter_map m . Model . items ~ f ( : fun ( text , completed ) i -> match display completed , m . Model . editing with | true , Some ( text ' , j ) -> let text = if i = j then text ' else text in Some { text ; completed ; editing = i = j } | true , None -> Some { text ; completed ; editing = false } | false , _ -> None ) let items k = selectAll " li " . | data ( fun m _ -> items_of_model m ) |- nest enter [ append " li " . | E . dblclick ( fun _ _ i -> k ( Event . Edit i ) ) ] |- nest update [ classed " editing " ( fun _ m i -> m . editing ) ; classed " completed " ( fun _ m i -> m . completed ) ; static " div " . | str attr " class " " view " . | seq [ static " input " . | str attr " class " " toggle " . | str attr " type " " checkbox " . | property " checked " ( fun _ m i -> Js . bool m . completed ) . | E . click ( fun _ m i -> if m . completed then k ( Event . Uncheck i ) else k ( Event . Check i ) ) ; static " label " . | text ( fun _ m _ -> m . text ) ; static " button " . | str attr " class " " destroy " . | E . click ( fun _ _ i -> k ( Event . Delete i ) ) ] ; static " input " . | str attr " class " " edit " . | property " value " ( fun _ m i -> Js . string m . text ) . | E . input ( fun e _ _ -> match Js . Opt . to_option e . ## target with | None -> assert false | Some t -> let i = Js . coerce t Dom_html . CoerceTo . input ( fun _ -> assert false ) in k ( Event . ChangeEdit ( Js . to_string i . ## value ) ) ) . | E . keyup ( fun e _ _ -> match e . ## keyCode with | 27 -> k Event . CancelEdit | 13 -> k Event . CommitEdit | _ -> ( ) ) . | E . blur ( fun _ _ _ -> k Event . CancelEdit ) ] |- chain [ exit . <> remove ] let filters k = selectAll " li " . | data ( fun m _ -> let open Model in [ ( All , m . filter = All , " " ) #/ ; ( Active , m . filter = Active , " #/ active " ) ; ( Completed , m . filter = Completed , " #/ completed " ) ] ) |- nest enter [ append " li " . | append " a " . | attr " href " ( fun _ ( _ , _ , href ) _ -> href ) . | text ( fun _ ( f , _ , _ ) _ -> Model . string_of_filter f ) ] |- nest update [ select " a " . | classed " selected " ( fun _ ( _ , active , _ ) _ -> active ) ] . | E . click ( fun _ ( f , _ , _ ) _ -> k ( Event . Filter f ) ) |- nest exit [ remove ] let todoapp k = static " section " . | str attr " id " " todoapp " |- seq [ nest ( static " header " . <> str attr " id " " header " ) [ static " h1 " . | text ( fun _ _ _ -> " todo " ) ; static " input " . <> str attr " id " " new - todo " . | str attr " autofocus " " " . | str attr " placeholder " " What needs to be done " ? . | property " value " ( fun _ m _ -> Js . string m . Model . input ) . | E . input ( fun e _ _ -> match Js . Opt . to_option e . ## target with | None -> assert false | Some t -> let i = Js . coerce t Dom_html . CoerceTo . input ( fun _ -> assert false ) in k ( Event . ChangeInput ( Js . to_string i . ## value ) ) ) . | E . keyup ( fun e m i -> if e . ## keyCode = 13 then k Event . AddInput else ( ) ) ] ; static " section " . <> str attr " id " " main " . | seq [ static " input " . | str attr " id " " toggle - all " . | str attr " type " " checkbox " . | E . click ( fun _ _ _ -> k Event . Toggle ) ; static " ul " . | str attr " id " " todo - list " |- items k ] ; static " footer " . <> str attr " id " " footer " . | seq [ static " span " . <> str attr " id " " todo - count " . | html ( fun _ m _ -> " < strong " > ^ ( string_of_int ( Model . undone m ) ) ^ " </ strong > items left " ) ; static " ul " . <> str attr " id " " filters " |- filters k ] ] let low_footer = let content " = < p > Double - click to edit a todo </ p > < p > Created by < a href ' = http :// computationallyendowed . com ' > Spiros Eliopoulos </ a ></ p > < p > Part of < a href ' = http :// todomvc . com ' > TodoMVC </ a ></ p > " in static " footer " . | str attr " id " " info " . | html ( fun _ _ _ -> content ) let make k = seq [ todoapp k ; low_footer ] end
|
module Storage : sig val get : unit -> ( string * bool ) list val set : ( string * bool ) list -> unit open Js let key = string " todos - ocaml - d3 " let storage = Optdef . case ( Dom_html . window . ## localStorage ) ( fun ( ) -> None ) ( fun s -> Some s ) let get ( ) = match storage with | None -> [ ] | Some s -> begin match Opt . to_option ( s ## getItem ( key ) ) with | Some v -> Model . items_of_json ( Yojson . Basic . from_string ( to_string v ) ) | None -> [ ] end let set v = match storage with | None -> ( ) | Some s -> s ( ## setItem key ( string ( Yojson . Basic . to_string ( Model . items_to_json v ) ) ) ) end
|
let main_lazy ( ) = let model = ref { Model . init with Model . items = Storage . get ( ) } in let rec go ( ) = D3 . run ~ node ( : Dom_html . document . ## body ) ( Lazy . force view ) ! model and view = lazy ( View . make ( fun e -> model := Event . handle e ! model ; Storage . set ( ! model ) . Model . items ; go ( ) ) ) in go ( ) ; ;
|
let main_lwt ( ) = let stream , push , _ = let stream , push = Lwt_stream . create ( ) in stream , ( fun x -> push ( Some x ) ) , ( fun ( ) -> push None ) in let view = View . make push in let init = { Model . init with Model . items = Storage . get ( ) } in let node = ( Dom_html . document . ## body ) in D3 . run ~ node view init ; Lwt_stream . fold ( fun e m -> let m ' = Event . handle e m in Storage . set m ' . Model . items ; D3 . run ~ node view m ' ; m ' ) stream init ; ;
|
let _ = Lwt_js_events . async main_lwt
|
let add_cors_headers ( headers : Cohttp . Header . t ) : Cohttp . Header . t = Cohttp . Header . add_list headers [ ( " access - control - allow - origin " , " " ) ; * ( " access - control - allow - headers " , " Accept , Content - Type " ) ; ( " access - control - allow - methods " , " GET , HEAD , POST , DELETE , OPTIONS , PUT , PATCH " ) ]
|
let allow_cors = let filter handler req = handler req >>| fun response -> response |> Response . headers |> add_cors_headers |> Field . fset Response . Fields . headers response in Rock . Middleware . create ~ name ( : Info . of_string " allow cors " ) ~ filter
|
type todo = { id : int ; url : string ; title : string ; completed : bool ; order : int ; }
|
let stored_todos = ref TodoStorage . empty
|
let get_or_else t path default = let open Ezjsonm in if ( mem t path ) then ( find t path ) else default
|
let todo_of_json json id = let open Ezjsonm in { id = id ; url = " http :// 54 . 72 . 243 . 203 : 3000 / todos " / ^ string_of_int id ; title = get_string ( find json [ " title " ] ) ; completed = get_bool ( get_or_else json [ " completed " ] ( bool false ) ) ; order = get_int ( get_or_else json [ " order " ] ( int 0 ) ) }
|
let updated_todo todo json = let open Ezjsonm in { id = todo . id ; url = " http :// 54 . 72 . 243 . 203 : 3000 / todos " / ^ string_of_int todo . id ; title = get_string ( get_or_else json [ " title " ] ( string todo . title ) ) ; completed = get_bool ( get_or_else json [ " completed " ] ( bool todo . completed ) ) ; order = get_int ( get_or_else json [ " order " ] ( int todo . order ) ) ; }
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.