text
stringlengths
12
786k
let get_responseURL x = x ## responseURL
let get_responseXML x = Js . Null . toOption x ## responseXML
let get_status x = x ## status
let get_statusText x = x ## statusText
let set_timeout t x = x ## timeout #= t
let get_timeout x = x ## timeout
let set_withCredentials b x = x ## withCredentials #= b
let get_withCredentials x = x ## withCredentials
let set_onabort cb x = x ## onabort #= cb
let get_onabort x = x ## onabort
let set_onerror cb x = x ## onerror #= cb
let get_onerror x = x ## onerror
let set_onload cb x = x ## onload #= cb
let get_onload x = x ## onload
let set_onloadstart cb x = x ## onloadstart #= cb
let get_onloadstart x = x ## onloadstart
let set_onprogress cb x = x ## onprogress #= cb
let get_onprogress x = x ## onprogress
let set_ontimeout cb x = x ## ontimeout #= cb
let get_ontimeout x = x ## ontimeout
let set_onloadend cb x = x ## onloadend #= cb
let get_onloadend x = x ## onloadend
let rec check_break_expr in_loop ( expr , _ ) = match expr with | VarExpr _ -> true | NilExpr -> true | IntExpr _ -> true | StringExpr _ -> true | CallExpr { call_args ; _ } -> List . for_all ( check_break_expr in_loop ) call_args | OpExpr { op_left ; op_right ; _ } -> check_break_expr in_loop op_left && check_break_expr in_loop op_right | RecordExpr { record_fields ; _ } -> List . for_all ( fun ( _ , init_expr , _ ) -> check_break_expr in_loop init_expr ) record_fields | SeqExpr exprs -> List . for_all ( check_break_expr in_loop ) exprs | AssignExpr { assign_rhs ; _ } -> check_break_expr in_loop assign_rhs | IfExpr { if_test ; if_then ; if_else } -> let if_else_ok = match if_else with | None -> true | Some if_else_expr -> check_break_expr in_loop if_else_expr in check_break_expr in_loop if_test && check_break_expr in_loop if_then && if_else_ok | WhileExpr { while_test ; while_body } -> check_break_expr in_loop while_test && check_break_expr true while_body | ForExpr { for_lo ; for_hi ; for_body ; _ } -> check_break_expr in_loop for_lo && check_break_expr in_loop for_hi && check_break_expr true for_body | BreakExpr -> in_loop | ArrayExpr { array_size ; array_init ; _ } -> check_break_expr in_loop array_size && check_break_expr in_loop array_init | LetExpr { let_decls ; let_body } -> List . for_all ( check_break_decl in_loop ) let_decls && check_break_expr in_loop let_body | VarDecl { var_expr ; _ } -> check_break_expr in_loop var_expr | FunDecl fun_decls -> List . for_all ( fun { fun_body ; _ } -> check_break_expr in_loop fun_body ) fun_decls | TypeDecl _ -> true
let assert_break expr = if not ( check_break_expr false expr ) then failwith " break outside of a loop "
let compare_date ( i1 : Rss2 . item ) ( i2 : Rss2 . item ) = let open Rss2 in match i1 . pubDate , i2 . pubDate with | Some d1 , Some d2 -> Syndic . Date . compare d2 d1 | None , Some _ -> 1 | Some _ , None -> - 1 | None , None -> 0
let post = lazy ( let xml = ` String ( 0 , Http . get ( cwn_base_url ^ " cwn . rss " ) ) in let feed = Syndic . Rss2 . parse ( Xmlm . make_input xml ) in let posts = List . sort compare_date feed . Rss2 . items in match posts with p :: _ -> p | [ ] -> failwith " weekly_news : empty feed " ! )
let cwn_date = lazy ( match ( Lazy . force post ) . Rss2 . pubDate with | Some d -> d | None -> failwith " cwn_date " )
let cwn_html_date ( ) = let d = Lazy . force cwn_date in [ Nethtml . Data ( sprintf " % s % d , % d " ( Utils . en_string_of_month ( Date . month d ) ) ( Date . day d ) ( Date . year d ) ) ]
let rec get_elements tag = function | [ ] -> [ ] | Nethtml . Data _ :: tl -> get_elements tag tl | Nethtml . Element ( t , _ , c ) :: tl -> let e = if t = tag then c else get_elements tag c in e @ get_elements tag tl
let rec drop_until n tag = function | [ ] -> [ ] | Nethtml . Data _ :: tl -> drop_until n tag tl | ( Nethtml . Element ( t , _ , _ ) :: tl ) as l -> if t = tag then if n <= 1 then l else drop_until ( n - 1 ) tag tl else drop_until n tag tl
let rec rm_tags tag = function | [ ] -> [ ] | ( Nethtml . Data _ as e ) :: tl -> e :: rm_tags tag tl | Nethtml . Element ( t , args , sub ) :: tl -> if t = tag then rm_tags tag tl else Nethtml . Element ( t , args , rm_tags tag sub ) :: rm_tags tag tl
let cwn_html_page ( ) = let d = Lazy . force cwn_date in let url = cwn_base_url ^ sprintf " % d . % 02d . % 02d . html " ( Date . year d ) ( Utils . int_of_month ( Date . month d ) ) ( Date . day d ) in let content = Http . get url in let html = Nethtml . parse ( new Netchannels . input_string content ) ~ dtd : Utils . relaxed_html40_dtd in let html = get_elements " body " html in let html = drop_until 2 " p " html in rm_tags " script " html
let cwn_html_summary ( ) = match ( Lazy . force post ) . Rss2 . story with | Rss2 . All ( _ , _ , content ) -> let open Nethtml in Nethtml . parse ( new Netchannels . input_string content ) ~ dtd : Utils . relaxed_html40_dtd | Rss2 . Title _ | Rss2 . Description _ -> failwith " cwn_page "
let ( ) = let contents = ref false in let date = ref false in let specs = [ ( " -- contents " , Arg . Set contents , " output the contents of the latest CWN " ) ; ( " -- date " , Arg . Set date , " output the date of the lastest CWN " ) ; ] in let usage = " weekly_news < option " > in Arg . parse ( Arg . align specs ) ( fun _ -> raise ( Arg . Bad " no anomynous argument " ) ) usage ; if ! contents then let out = new Netchannels . output_channel stdout in Nethtml . write out ( cwn_html_page ( ) ) ; out # close_out ( ) else if ! date then let out = new Netchannels . output_channel stdout in Nethtml . write out ( cwn_html_date ( ) ) ; out # close_out ( ) else ( Arg . usage specs usage ; exit 1 )
module Where = Dune_rpc_private . Where . Make ( struct type ' a t = ' a let return a = a module O = struct let ( let + ) x f = f x let ( let * ) x f = f x end end ) ( struct let read_file f = Ok ( Io . String_path . read_file f ) let analyze_path s = match ( Unix . stat s ) . st_kind with | Unix . S_SOCK -> Ok ` Unix_socket | S_REG -> Ok ` Normal_file | _ | ( exception Unix . Unix_error ( Unix . ENOENT , _ , _ ) ) -> Ok ` Other | exception ( Unix . Unix_error _ as e ) -> Error e end )
let build_dir = lazy ( let build_dir = Path . Build . to_string Path . Build . root in match String . drop_prefix build_dir ~ prefix ( : Sys . getcwd ( ) ^ " " ) / with | None -> build_dir | Some s -> Filename . concat " . " s )
let get ( ) = let env = Env . get Env . initial in match Where . get ~ env ~ build_dir ( : Lazy . force build_dir ) with | Ok s -> s | Error exn -> User_error . raise [ Pp . text " Unable to find dune rpc address " ; Exn . pp exn ]
let default ( ) = Where . default ~ build_dir ( : Lazy . force build_dir ) ( )
let to_socket = function | ` Unix p -> Unix . ADDR_UNIX p | ` Ip ( ` Host host , ` Port port ) -> Unix . ADDR_INET ( Unix . inet_addr_of_string host , port )
module Response = struct module Device_info = struct module Session_info = struct module Connection_info = struct type t = { ip : string option ; last_seen : int option ; user_agent : string option ; } [ @@ deriving accessor ] let encoding = let to_tuple t = t . ip , t . last_seen , t . user_agent in let of_tuple v = let ip , last_seen , user_agent = v in { ip ; last_seen ; user_agent } in let with_tuple = obj3 ( opt " ip " string ) ( opt " last_seen " int ) ( opt " user_agent " string ) in conv to_tuple of_tuple with_tuple end type t = { connections : Connection_info . t list option } [ @@ deriving accessor ] let encoding = let to_tuple t = t . connections in let of_tuple v = let connections = v in { connections } in let with_tuple = obj1 ( opt " connections " ( list Connection_info . encoding ) ) in conv to_tuple of_tuple with_tuple end type t = { sessions : Session_info . t list option } [ @@ deriving accessor ] let encoding = let to_tuple t = t . sessions in let of_tuple v = let sessions = v in { sessions } in let with_tuple = obj1 ( opt " sessions " ( list Session_info . encoding ) ) in conv to_tuple of_tuple with_tuple end type t = { user_id : string option ; devices : ( string * Device_info . t ) list option ; } [ @@ deriving accessor ] let encoding = let to_tuple t = t . user_id , t . devices in let of_tuple v = let user_id , devices = v in { user_id ; devices } in let with_tuple = obj2 ( opt " user_id " string ) ( opt " devices " ( assoc Device_info . encoding ) ) in conv to_tuple of_tuple with_tuple end
let shared_constants_class_name = " pack . ocamljavaConstants " |> UTF8 . of_string |> Name . make_for_class_from_external |> ref
module ClassNameMap = Map . Make ( struct type t = Name . for_class let compare x y = Name . compare_for_class x y end ) end
let constants_name_to_main_name : Name . for_class ClassNameMap . t ref = ref ClassNameMap . empty
let constant_fields : ( Field . t list ) list ClassNameMap . t ref = ref ClassNameMap . empty
let is_constants_class cn = ClassNameMap . mem cn ! constants_name_to_main_name
let main_class_of_constants_class cn = ClassNameMap . find cn ! constants_name_to_main_name
let additional_fields cn = try ClassNameMap . find cn ! constant_fields with Not_found -> [ ] inherit ClassTraversal . default_class_definition_iterator method ! class_definition ( _ : AccessFlag . for_class list ) list ( cn : Name . for_class ) for_class ( _ : Name . for_class option ) option ( _ : Name . for_class list ) list ( fields : Field . t list ) list ( _ : Method . t list ) list ( _ : Attribute . for_class list ) list = let s = Name . internal_utf8_for_class cn in let len = UTF8 . length s in let last_dollar = UTF8 . rindex_from s ( pred len ) len ( UChar . of_char ' $ ' ) ' in let s = UTF8 . substring s 0 ( pred last_dollar ) last_dollar in let cn ' = Name . make_for_class_from_internal s in constants_name_to_main_name := ClassNameMap . add cn cn ' ! constants_name_to_main_name ; constant_fields := ClassNameMap . add cn ' fields ! constant_fields end
module IntSet = Set . Make ( struct type t = int let compare ( x : int ) int ( y : int ) int = Pervasives . compare x y end ) end
let extract_ints l = List . fold_left ( fun acc elem -> match elem with | Annotation . Int_value x -> IntSet . add ( Int32 . to_int x ) x acc | _ -> assert false ) false IntSet . empty l
let linked_classes : UTF8 . t list option ref = ref None
type indices = { read_indices : IntSet . t ; written_indices : IntSet . t ; }
let indices : ( UTF8 . t * indices ) indices list ClassNameMap . t ref = ref ClassNameMap . empty
let merge_maps ( dst : indices ClassNameMap . t ) t ( name , uses ) uses = let class_name = Name . make_for_class_from_external name in let value = try let old = ClassNameMap . find class_name dst in { read_indices = IntSet . union old . read_indices uses . read_indices ; written_indices = IntSet . union old . written_indices uses . written_indices ; } with Not_found -> uses in ClassNameMap . add class_name value dst
let get_used_indices ( ) = ClassNameMap . fold ( fun user used acc -> let merge = match ! linked_classes with | Some l -> List . exists ( fun x -> UTF8 . equal x ( Name . external_utf8_for_class user ) user ) user l | None -> false in if merge then begin List . fold_left merge_maps acc used end else acc ) acc ! indices ClassNameMap . empty
let make_remove_indices_function ( ) = let used_indices = get_used_indices ( ) in if ! Args . verbose then begin used_indices |> ClassNameMap . iter ( fun cn { read_indices ; written_indices } -> cn |> Name . external_utf8_for_class |> UTF8 . to_string |> Printf . printf " indices for % S :\ n " ; IntSet . iter ( Printf . printf " - read % d \ n ) " read_indices ; IntSet . iter ( Printf . printf " - written % d \ n ) " written_indices ) written_indices ; Printf . printf " " %! end ; fun name index -> try let indices = ClassNameMap . find name used_indices in ( not ( IntSet . mem index indices . read_indices ) read_indices ) read_indices && ( not ( IntSet . mem ( ~- 1 ) 1 indices . read_indices ) read_indices ) read_indices with Not_found -> false inherit ArchiveTraversal . default_archive_iterator zip val for_constants = new for_constants method ! class_definition cd = let annotations = Attribute . extract_annotations ( cd . ClassDefinition . attributes :> Attribute . t list ) list in let global_uses = ref [ ] in List . iter ( fun ( n , l ) l -> if Name . equal_for_class n Names . global_uses then begin let class_name = ref ( UTF8 . of_string " dummy . Class ) " in let read_indices = ref IntSet . empty in let written_indices = ref IntSet . empty in List . iter ( fun ( n , v ) v -> let n = UTF8 . to_string n in match n , v with | " className " , Annotation . String_value x -> class_name := x | " readIndices " , Annotation . Array_value x -> read_indices := extract_ints x | " writtenIndices " , Annotation . Array_value x -> written_indices := extract_ints x | _ -> ( ) ) l ; global_uses := ( ! class_name , { read_indices = ! read_indices ; written_indices = ! written_indices ; } ) :: ! global_uses ; end ) end annotations ; indices := ClassNameMap . add cd . ClassDefinition . name ! global_uses ! indices ; let is_constants = List . exists ( fun ( n , _ ) _ -> Name . equal_for_class n Names . constants_class ) constants_class annotations in let is_entry_point = List . exists ( fun ( n , ev ) ev -> if Name . equal_for_class n Names . entry_point then begin List . iter ( fun ( k , v ) v -> if UTF8 . equal k Names . linked_classes then match v with | Annotation . Array_value l -> let l = List . map ( function | Annotation . String_value x -> x | _ -> assert false ) false l in if ! Args . verbose then begin Printf . printf " linked classes :\ n " ; List . iter ( fun cn -> Printf . printf " - % S \ n " ( UTF8 . to_string cn ) cn ) cn l ; Printf . printf " " %! end ; linked_classes := Some l | _ -> assert false ) false ev ; true end else false ) false annotations in if is_constants then ClassDefinition . iter for_constants cd ; if is_entry_point then begin let package_name , _ = Name . split_class_name cd . ClassDefinition . name in let class_name = " ocamljavaConstants " |> UTF8 . of_string |> Name . make_for_class_from_external in shared_constants_class_name := Name . gather_class_name package_name class_name end end
let iter_archive filename = let make_iterator x = new iterator x in ArchiveTraversal . iter_file make_iterator filename
let shared_constants : UTF8 . Set . t ref = ref UTF8 . Set . empty
let add_shared_constant name = shared_constants := UTF8 . Set . add ( Name . utf8_for_field name ) name ! shared_constants
let empty_cstr = let code = Attribute ( { . max_stack = u2 1 ; max_locals = u2 1 ; code = [ Instruction . ALOAD_0 ; Instruction . INVOKESPECIAL ( Names . object_ , Misc . make_method_name " < init " , > ( [ ] , ` Void ) Void ) Void ; Instruction . RETURN ] ; exception_table = [ ] ; attributes = [ ] ; } ) in Method ( . Constructor { cstr_flags = [ ` Private ] ; cstr_descriptor = [ ] ; cstr_attributes = [ ` Code code ] code ; } )
let push_int32 = function | ( - 1l ) 1l -> Instruction . ICONST_M1 | 0l -> Instruction . ICONST_0 | 1l -> Instruction . ICONST_1 | 2l -> Instruction . ICONST_2 | 3l -> Instruction . ICONST_3 | 4l -> Instruction . ICONST_4 | 5l -> Instruction . ICONST_5 | n when n >= ( - 128l ) 128l && n <= 127l -> Instruction . BIPUSH ( s1 ( Int32 . to_int n ) n ) n | n when n >= ( - 32768l ) 32768l && n <= 32767l -> Instruction . SIPUSH ( s2 ( Int32 . to_int n ) n ) n | n -> Instruction . LDC_W ( ` Int n ) n
let push_int64 = function | 0L -> [ Instruction . LCONST_0 ] | 1L -> [ Instruction . LCONST_1 ] | 2L -> [ Instruction . ICONST_2 ; Instruction . I2L ] | 3L -> [ Instruction . ICONST_3 ; Instruction . I2L ] | 4L -> [ Instruction . ICONST_4 ; Instruction . I2L ] | 5L -> [ Instruction . ICONST_5 ; Instruction . I2L ] | n -> [ Instruction . LDC2_W ( ` Long n ) n ]
let push_double x = if x = 0 . 0 then Instruction . DCONST_0 else if x = 1 . 0 then Instruction . DCONST_1 else Instruction . LDC2_W ( ` Double x ) x
let compile_shared_constant_class ( ) = let fields_and_inits = List . map ( fun name -> let underscore = UTF8 . index_from name 0 ( UChar . of_char ' _ ' ) ' _ ' in let typ = UTF8 . substring name 0 ( pred underscore ) underscore in let hexa = UTF8 . substring name ( succ underscore ) underscore ( pred ( UTF8 . length name ) name ) name |> UTF8 . to_string in let field_name = Name . make_for_field name in let field = Field ( { . flags = [ ` Public ; ` Static ; ` Final ] ; name = field_name ; descriptor = ` Class Names . value ; attributes = [ ] ; } ) in let init = match UTF8 . to_string_noerr typ with | " INT " -> ( Scanf . sscanf hexa " % LX " identity |> push_int64 ) push_int64 @ [ Instruction . INVOKESTATIC ( Names . value , Misc . make_method_name " createLong " , ( [ ` Long ] Long , ` Class Names . value ) value ) value ] | " INT32 " -> [ Scanf . sscanf hexa " % lX " identity |> push_int32 ; Instruction . INVOKESTATIC ( Names . value , Misc . make_method_name " createInt32 " , ( [ ` Int ] Int , ` Class Names . value ) value ) value ] | " INT64 " -> ( Scanf . sscanf hexa " % LX " identity |> push_int64 ) push_int64 @ [ Instruction . INVOKESTATIC ( Names . value , Misc . make_method_name " createInt64 " , ( [ ` Long ] Long , ` Class Names . value ) value ) value ] | " NATIVEINT " -> ( Scanf . sscanf hexa " % nX " identity |> Int64 . of_nativeint |> push_int64 ) push_int64 @ [ Instruction . INVOKESTATIC ( Names . value , Misc . make_method_name " createNativeInt " , ( [ ` Long ] Long , ` Class Names . value ) value ) value ] | " FLOAT " -> [ Scanf . sscanf hexa " % LX " identity |> Int64 . float_of_bits |> push_double ; Instruction . INVOKESTATIC ( Names . value , Misc . make_method_name " createDouble " , ( [ ` Double ] Double , ` Class Names . value ) value ) value ] | _ -> assert false in let init = init @ [ Instruction . PUTSTATIC ( ! shared_constants_class_name , field_name , ` Class Names . value ) value ] in field , init ) init ( UTF8 . Set . elements ! shared_constants ) shared_constants in let fields , inits = List . split fields_and_inits in let initializer_meth = let code = Attribute ( { . max_stack = u2 12 ; max_locals = u2 0 ; code = ( List . flatten inits ) inits @ [ Instruction . RETURN ] ; exception_table = [ ] ; attributes = [ ] } ) in Method ( . Initializer { init_flags = [ ` Static ] Static ; init_attributes = [ ` Code code ] code } code ) code in let cd = ClassDefinition ( { . access_flags = [ ` Public ; ` Final ; ` Super ] ; name = ! shared_constants_class_name ; extends = Some Names . object_ ; implements = [ ] ; fields = fields ; methods = [ empty_cstr ; initializer_meth ] ; attributes = [ ] } ) in let buff = ByteBuffer . make_of_size 2048 in let os = OutputStream . make_of_buffer buff in ClassFile . write ( ClassDefinition . encode cd ) cd os ; OutputStream . close os ; let contents = ByteBuffer . contents buff in let path = Name . internal_utf8_for_class ! shared_constants_class_name in let path = UTF8 ( . path ++ ( UTF8 . of_string " . class ) ) " in let path = if ! Args . war then UTF8 ( ( . UTF8 . of_string " WEB - INF / classes ) " / ++ path ) path else path in path , contents
type syntax_map = string Hstr . t
let driver : syntax_map = Hstr . create 16
let ( ) = List . iter ( fun ( x , y ) -> Hstr . add driver x y ) [ ( " integer " , " int " ) ; ( " int " , " int " ) ; ( " " , + " infix " ) ; + ( " " , * " infix " ) ; * ( " " , - " infix " ) ; - ( " " , / " infix " ) ; / ( " infix mod " , " infix " ) ; % ( " " , <= " infix " ) ; <= ( " " , >= " infix " ) ; >= ( " " , < " infix " ) ; < ( " " , > " infix " ) ; > ( " " , <> " infix " ) ; <> ( " " , = " infix " ) ; = ( " " , == " infix " ) ; == ( " mixfix { } " , " empty " ) ; ( " mixfix { : _ } " , : " singleton " ) ; ( " mixfix [ _ ] " , " mixfix [ ] " ) ; ( " [ ] " , " Nil " ) ; ( " infix " , :: " Cons " ) ; ( " " , :: " Cons " ) ; ( " infix " , @ " infix " ) ; ++ ( " " , @ " infix " ) ; ++ ( " " , ! " prefix " ) ; ! ( " " , := " infix " ) ; := ( " " , |> " infix " ) ; |> ( " " , <> " infix " ) ; ~= ( " " , ~- " prefix " ) ; ~- ]
let query_syntax str = Hstr . find_opt driver str
type ' a widget = { config : Otoml . t ; func : Defaults . env -> Otoml . t -> ' a Soup . node -> ( unit , string ) result }
type ' a widgets = string list * ( string , ' a widget ) Hashtbl . t
let find_widget plugins name = let plugin_w = Hashtbl . find_opt plugins name in let builtin_w = List . assoc_opt name Builtin_widgets . widgets in match plugin_w , builtin_w with | Some p , Some _ -> let ( ) = Logs . warn @@ fun m -> m " Widget name % s is redefined by a plugin " name in Some p | Some p , None -> Some p | None , Some b -> Some b | _ -> None
let get_widget_config config widget = let widget_tbl = Config . find_table_opt [ Defaults . widgets_table ; widget ] config in match widget_tbl with | Some widget_tbl -> widget_tbl | None -> failwith @@ Printf . sprintf " Trying to lookup a non - existent widget % s " widget
let list_widgets config = let ws = Config . find_table_opt [ Defaults . widgets_table ] config >>= ( fun x -> Some ( Otoml . list_table_keys x ) ) in match ws with | None -> [ ] | Some ws ' -> ws '
let add_widget hash name widget_func widget_config = let widget_rec = { config = widget_config ; func = widget_func } in Hashtbl . add hash name widget_rec
let rec _load_widgets settings config plugins ws hash = match ws with | [ ] -> ( ) | w :: ws ' -> let widget_config = get_widget_config config w in let name = OH . find_string_opt widget_config [ " widget " ] in let fail msg = Printf . ksprintf failwith " Error in [ widgets . % s ] : % s " w msg in begin match name with | None -> fail " missing required option widget " =\< some widget " " >\ | Some name -> let widget_func = find_widget plugins name in begin match widget_func with | None -> begin if not settings . plugin_discovery then let ( ) = Logs . warn @@ fun m -> m " Plugin discovery is disabled , not attempting to find a plugin that implements widget " \% s " " \ name in fail @@ Printf . sprintf " unknown widget " \% s " " \ name else let file_name = Printf . sprintf " % s . lua " name in let file_path = Plugins . lookup_plugin_file settings . plugin_dirs file_name in match file_path with | None -> let dirs_str = String . concat " , " settings . plugin_dirs in let ( ) = Logs . err @@ fun m -> m " Failed to find plugin file % s in directories [ % s ] " file_name dirs_str in fail @@ Printf . sprintf " widget " \% s " \ is not a soupault built - in and is not provided by any available plugin " name | Some plugin_file -> let lua_source = try Soup . read_file plugin_file with Sys_error msg -> fail @@ Printf . sprintf " Could not read plugin file that provides widget " \% s " \: % s " name msg in let ( ) = Hashtbl . add plugins name ( Plugins . make_plugin_function lua_source settings config name ) in let ( ) = Logs . debug @@ fun m -> m " Widget % s is loaded from plugin file % s " name plugin_file in let ( ) = add_widget hash w ( find_widget plugins name |> Option . get ) widget_config in _load_widgets settings config plugins ws ' hash end | Some wf -> let ( ) = add_widget hash w wf widget_config in _load_widgets settings config plugins ws ' hash end end
let get_widget_order hash = let format_bad_deps ds = let format_bad_dep ( n , ns ) = Printf . sprintf " Widget " \% s " \ depends on non - existent widgets : % s " n ( String . concat " , " ns ) in let bad_deps = List . map format_bad_dep ds |> String . concat " \ n " in Printf . sprintf " Found dependencies on non - existent widgets \ n % s " bad_deps in let dep_graph = CCHashtbl . map_list ( fun k v -> ( k , Config . find_strings_or ~ default [ ] : v . config [ " after " ] ) ) hash in let bad_deps = Tsort . find_nonexistent_nodes dep_graph in if bad_deps <> [ ] then Error ( format_bad_deps bad_deps ) else let res = Tsort . sort dep_graph in match res with | Tsort . Sorted ws -> Ok ws | Tsort . ErrorCycle ws -> Error ( Printf . sprintf " Found a circular dependency between widgets : % s " ( String . concat " " ws ) )
let partition_widgets all_widgets index_deps = let rec aux index_deps before_index after_index = match index_deps , after_index with | [ ] , ws -> Ok ( List . rev before_index , ws ) | _ , w :: ws ' -> let index_deps = CCList . remove ~ eq ( ) := ~ key : w index_deps in aux index_deps ( w :: before_index ) ws ' | _ as ds , [ ] -> Error ( Printf . sprintf " Index extraction depends on non - existent widgets : % s " ( String . concat " " ds ) ) in aux index_deps [ ] all_widgets
let load_widgets settings config plugins = let widgets_hash = Hashtbl . create 1024 in match config with | None -> Ok widgets_hash | Some config -> let ws = list_widgets config in try let ( ) = _load_widgets settings config plugins ws widgets_hash in Ok widgets_hash with Failure msg -> Error msg
let get_widgets settings config plugins index_deps = let ( let ) * = Stdlib . Result . bind in let * wh = load_widgets settings config plugins in let * wo = get_widget_order wh in let * before_index , after_index = partition_widgets wo index_deps in let ( ) = Logs . debug @@ fun m -> m " Widget processing order : % s " ( String . concat " " wo ) ; if index_deps <> [ ] then begin Logs . debug @@ fun m -> m " Widgets that will run before metadata extraction : % s " ( String . concat " " before_index ) ; Logs . debug @@ fun m -> m " Widgets that will run after metadata extraction : % s " ( String . concat " " after_index ) end in Ok ( before_index , after_index , wh )
let widget_should_run settings name widget build_profiles site_dir page_file = let disabled = Config . find_bool_or ~ default : false widget . config [ " disabled " ] in if disabled then let ( ) = Logs . debug @@ fun m -> m " Widget " \% s " \ is disabled in the configuration " name in false else let options = Config . get_path_options widget . config in let profile = OH . find_string_opt widget . config [ " profile " ] in if not ( Utils . profile_matches profile build_profiles ) then let ( ) = Logs . debug @@ fun m -> m " Widget " \% s " \ is not enabled in the current build profile " name in false else begin if Path_options . page_included settings options site_dir page_file then true else let ( ) = Logs . debug @@ fun m -> m " Widget " \% s " \ will not run : page % s is excluded by its page / section / regex options " name page_file in false end
type handler = { suffix : string ; label : string ; export : string -> unit }
type configuration = { id : string ; handlers : handler list ; show : bool React . signal }
let export_format_id ( configuration : configuration ) : string = Format . sprintf " export_ % s_select " configuration . id
let export_filename_id ( configuration : configuration ) : string = Format . sprintf " export_ % s_filename " configuration . id
let export_button_id ( configuration : configuration ) : string = Format . sprintf " export_ % s_button " configuration . id
let export_data_label ( configuration : configuration ) : string = Format . sprintf " export_ % s_label " configuration . id
let export_form_id ( configuration : configuration ) : string = Format . sprintf " export_ % s_form " configuration . id
let inline_content ( configuration : configuration ) = let export_filename = Html . input ~ a [ : Html . a_id ( export_filename_id configuration ) ; Html . a_input_type ` Text ; Html . a_class [ " form - control " ] ; Html . a_placeholder " file name " ] ( ) in let export_button = Html . button ~ a [ : Html . a_id ( export_button_id configuration ) ; Html . Unsafe . string_attrib " role " " button " ; Html . a_class [ " btn " ; " btn - default " ] ; Tyxml_js . R . filter_attrib ( Html . a_disabled ( ) ) ( React . S . map not configuration . show ) ] [ Html . cdata " export " ] in let export_formats_select = List . map ( fun handler -> [ % html { |< option value " } =| handler . label { " } ( |>| Html . cdata handler . label ) { |</ option } ] ) >| configuration . handlers in [ % html { |< div class " = form - group " > < select class " = form - control " id " } ( =| export_format_id configuration ) { " |> } | export_formats_select { | </ select > </ div > < div class " = form - group " > < label for } ( =| export_filename_id configuration ) { |></ label > } [ | export_filename ] { | </ div > } ] [ |@ export_button ]
let content configuration = Html . form ~ a [ : Html . a_id ( export_form_id configuration ) ; Html . a_class [ " form - inline " ] ; ] ( inline_content configuration )
let onload ( configuration : configuration ) = let export_button : Dom_html . buttonElement Js . t = Ui_common . id_dom ( export_button_id configuration ) in let export_filename : Dom_html . inputElement Js . t = Ui_common . id_dom ( export_filename_id configuration ) in let export_format : Dom_html . selectElement Js . t = Ui_common . id_dom ( export_format_id configuration ) in let export_button_toggle ( ) : unit = let is_disabled : bool Js . t = Js . bool ( not ( React . S . value configuration . show ) || export_filename . ## value ## trim . ## length = 0 ) in let ( ) = export_button . ## disabled := is_disabled in ( ) in let ( ) = export_button_toggle ( ) in let ( ) = export_filename . ## oninput := Dom_html . handler ( fun _ -> let ( ) = export_button_toggle ( ) in Js . _false ) in let ( ) = export_button . ## onclick := Dom_html . handler ( fun _ -> let handler : handler = List . nth configuration . handlers ( export_format . ## selectedIndex ) in let filename default : string = let root : string = Js . to_string ( export_filename . ## value ) in if String . contains root ' . ' then root else root " . " ^^ default in let ( ) = handler . export ( filename handler . suffix ) in Js . _false ) in ( )
let export_svg ? svg_style_id ( ~ svg_div_id : string ) ( ) : handler = { suffix = " svg " ; label = " svg " ; export = fun filename -> Common . plotSVG ? plotStyleId : svg_style_id svg_div_id filename filename }
let export_png ? svg_style_id ( ~ svg_div_id : string ) ( ) : handler = { suffix = " png " ; label = " png " ; export = fun filename -> Common . plotPNG ? plotStyleId : svg_style_id svg_div_id filename filename }
let export_json ( ~ serialize_json : unit -> string ) : handler = { suffix = " json " ; label = " json " ; export = fun filename -> let data = Js . string ( serialize_json ( ) ) in Common . saveFile ~ data ~ mime " : application / json " ~ filename : filename }
type comment = [ ]
type variable_step = [ ]
type fixed_step = [ ]
type bed_graph_value = string * int * int * float
type item = [ comment | variable_step | fixed_step | ` bed_graph_value of bed_graph_value ]
let module_error e = Error ( ` wig e ) e
module Tags = struct type t = { allow_empty_lines : bool ; sharp_comments : bool ; } [ @@ deriving sexp ] sexp let default = { allow_empty_lines = false ; sharp_comments = true } true let to_string t = sexp_of_t t |> Sexplib . Sexp . to_string let of_string s = try Ok ( Sexplib . Sexp . of_string s |> t_of_sexp ) t_of_sexp with e -> module_error ( ` tags_of_string e ) e end
module Error = struct type parsing = [ | ` cannot_parse_key_values of Pos . t * string | ` empty_line of Pos . t | ` incomplete_input of Pos . t * string list * string option | ` missing_chrom_value of Pos . t * string | ` missing_start_value of Pos . t * string | ` missing_step_value of Pos . t * string | ` wrong_start_value of Pos . t * string | ` wrong_step_value of Pos . t * string | ` unrecognizable_line of Pos . t * string list | ` wrong_bed_graph_value of Pos . t * string | ` wrong_fixed_step_value of Pos . t * string | ` wrong_span_value of Pos . t * string | ` wrong_variable_step_value of Pos . t * string ] [ @@ deriving sexp ] sexp let parsing_error_to_string = let pos ( ) a = Pos . to_string a in function | ` cannot_parse_key_values ( p , s ) s -> sprintf " cannot_parse_key_values ( % a , % S ) S " pos p s | ` empty_line p -> sprintf " empty_line ( % a ) a " pos p | ` incomplete_input ( p , vs , vo ) vo -> sprintf " incomplete_input ( % a , % s , % S ) S " pos p ( String . concat ~ sep " ; : " vs ) vs ( Option . value ~ default " " : vo ) vo | ` missing_chrom_value ( p , v ) v -> sprintf " missing_chrom_value ( % a , % s ) s " pos p v | ` missing_start_value ( p , v ) v -> sprintf " missing_start_value ( % a , % s ) s " pos p v | ` missing_step_value ( p , v ) v -> sprintf " missing_step_value ( % a , % s ) s " pos p v | ` wrong_start_value ( p , v ) v -> sprintf " wrong_start_value ( % a , % s ) s " pos p v | ` wrong_step_value ( p , v ) v -> sprintf " wrong_step_value ( % a , % s ) s " pos p v | ` unrecognizable_line ( p , v ) v -> sprintf " unrecognizable_line ( % a , % s ) s " pos p ( String . concat ~ sep " : " v ) v | ` wrong_bed_graph_value ( p , v ) v -> sprintf " wrong_bed_graph_value ( % a , % s ) s " pos p v | ` wrong_fixed_step_value ( p , v ) v -> sprintf " wrong_fixed_step_value ( % a , % s ) s " pos p v | ` wrong_span_value ( p , v ) v -> sprintf " wrong_span_value ( % a , % s ) s " pos p v | ` wrong_variable_step_value ( p , v ) v -> sprintf " wrong_variable_step_value ( % a , % s ) s " pos p v type to_bed_graph = [ ` not_in_variable_step_state | ` not_in_fixed_step_state ] not_in_fixed_step_state [ @@ deriving sexp ] sexp type t = [ parsing | to_bed_graph ] [ @@ deriving sexp ] sexp end