text
stringlengths
0
601k
let child_list = function | { values = ` Element { children ; _ } ; _ } -> Some children | { values = ` Document { roots ; _ } ; _ } -> Some roots | _ -> None
let children node = match child_list node with | Some children -> { eliminate = fun f init -> List . fold_left f init children } | _ -> empty
let rec descendants node = { eliminate = fun f init -> init |> ( children node ) . eliminate ( fun v child -> f v child |> ( descendants ( forget_type child ) ) . eliminate f ) }
let child node = node |> children |> first
let child_element node = node |> children |> elements |> first
let simple_parent node = node . parent
let parent node = match node . parent with | None -> None | Some node when is_document node -> None | Some node -> Some node
let rec general_ancestors get_parent node = { eliminate = fun f init -> match get_parent node with | None -> init | Some parent -> f init parent |> ( general_ancestors get_parent ( forget_type parent ) ) . eliminate f }
let simple_ancestors = general_ancestors simple_parent
let ancestors node = general_ancestors parent node
let siblings node = match simple_parent node with | None -> empty | Some parent -> children parent |> filter ( fun child -> child != ( forget_type node ) )
let split_at_identity function_name v l = let rec loop prefix = function | [ ] -> failwith ( " Soup . " ^ function_name ^ " : internal error : child not in parent ' s child list " ) [ @ coverage off ] | u :: suffix -> if u == v then prefix , suffix else loop ( u :: prefix ) suffix in loop [ ] l
let sibling_lists function_name select node = match simple_parent node with | None -> empty | Some parent -> match child_list parent with | None -> failwith ( " Soup . " ^ function_name ^ " : internal error : parent has no children " ) [ @ coverage off ] | Some children -> let lists = split_at_identity function_name ( ...
let next_siblings node = sibling_lists " next_siblings " snd node
let previous_siblings node = sibling_lists " previous_siblings " fst node
let next_sibling node = next_siblings node |> first
let previous_sibling node = previous_siblings node |> first
let next_element node = next_siblings node |> elements |> first
let previous_element node = previous_siblings node |> elements |> first
let index_of node = match simple_parent node with | None -> 1 | Some parent -> match child_list parent with | None -> failwith " Soup . index_of : internal error : parent has no children " [ @ coverage off ] | Some children -> with_stop ( fun stop -> children |> List . iteri ( fun index child -> if child == ( forget_ty...
let index_of_element element = match simple_parent element with | None -> 1 | Some parent -> with_stop ( fun stop -> parent |> children |> elements |> fold ( fun index element ' -> if element ' == element then stop . throw index else index + 1 ) 1 |> ignore [ @ coverage off ] ; ( failwith ( " Soup . index_of_element : ...
let at_most_n_children count node = match nth ( count + 1 ) ( children node ) with | None -> true | Some _ -> false
let no_children node = at_most_n_children 0 node
let at_most_one_child node = at_most_n_children 1 node
let is_root node = match node . parent with | None -> not ( is_document node ) | Some parent -> is_document parent
let tags name ' node = let name ' = String . lowercase_ascii name ' in node |> descendants |> elements |> filter ( fun element -> name element = name ' )
let tag name node = tags name node |> first
let normalize_children trim children = let rec loop prefix = function | [ ] -> List . rev prefix | node :: rest -> match node . values with | ` Text s -> let s = trim s in if s = " " then loop prefix rest else ( match prefix with | { values = ` Text s ' ; _ } :: prefix ' -> loop ( ( create_text ( s ' ^ s ) ) :: prefix ...
let rec leaf_text node = let trim s = if String . trim s = " " then " " else s in match node . values with | ` Text s -> Some s | ` Element _ | ` Document _ -> let children = child_list node |> require_internal ( " Soup . leaf_text : internal error : node is not a text node , " ^ " but has no child list " ) |> normaliz...
let rec texts node = match node . values with | ` Text s -> [ s ] | ` Element { children ; _ } -> children |> List . map forget_type |> List . map texts |> List . fold_left ( ) @ [ ] | ` Document { roots ; _ } -> roots |> List . map forget_type |> List . map texts |> List . fold_left ( ) @ [ ]
let trimmed_texts node = texts node |> List . map String . trim |> List . filter ( fun s -> String . length s > 0 )
module Selector : sig type t val parse : string -> t val select : ( _ node ) -> t -> element nodes val matches_selector : t -> soup node -> soup node -> bool struct type type_ = Name of string | Universal type attribute = | Present of string | Exactly of string * string | Member of string * string | HasDashSeparatedPre...
let matches_selector root_node selector node = let root_node = forget_type root_node in let node = forget_type node in let selector = Selector . parse selector in Selector . matches_selector selector root_node node
let select selector node = selector |> Selector . parse |> Selector . select node
let select_one selector node = select selector node |> first
let ( ) $ node selector = node |> select_one selector |> require_internal ( Printf . sprintf " Soup . ( ) $: ' % s ' not found . \ n % s " selector " Try Soup . ( ) $? if you ' d prefer returning None instead of an exception . " )
let ( ) $? node selector = node |> select_one selector
let ( ) $$ node selector = node |> select selector
module Infix = struct let ( ) $ = ( ) $ let ( ) $? = ( ) $? let ( ) $$ = ( ) $$ end
let signals root = let root = forget_type root in let rec traverse acc = function | { values = ` Element { name ; attributes ; children } ; _ } -> let start_signal = ` Start_element ( ( " http :// www . w3 . org / 1999 / xhtml " , name ) , List . map ( fun ( n , v ) -> ( " " , n ) , v ) attributes ) in ` End_element ( ...
let pretty_print root = signals root |> Markup . pretty_print |> ( fun s -> Markup . write_html s ) |> Markup . to_string
let to_string root = signals root |> ( fun s -> Markup . write_html s ) |> Markup . to_string
let rec equal_general normalize_children n n ' = let equal_text s s ' = s = s ' in let equal_children children children ' = let children = normalize_children children in let children ' = normalize_children children ' in try List . iter2 ( fun c c ' -> if not ( equal_general normalize_children c c ' ) then raise_notrace...
let equal n n ' = equal_general ( normalize_children ( fun s -> s ) ) ( forget_type n ) ( forget_type n ' )
let equal_modulo_whitespace n n ' = equal_general ( normalize_children String . trim ) ( forget_type n ) ( forget_type n ' )
let mutate_child_list f node = match node . values with | ` Element values -> values . children <- f values . children | ` Document values -> values . roots <- f values . roots | ` Text _ -> failwith " Soup . mutate_child_list : node has no children "
let strip_document node = if is_document node then let children = node |> children |> to_list in ( children |> List . iter ( fun child -> child . parent <- None ) ; mutate_child_list ( fun _ -> [ ] ) node ) ; children else [ node ]
let delete node = match node . parent with | None -> ( ) | Some parent -> mutate_child_list ( List . filter ( fun child -> child != ( forget_type node ) ) ) parent ; node . parent <- None
let insert_at_index k element node = let element = forget_type element in let node = forget_type node in delete node ; let nodes = strip_document node in mutate_child_list ( fun l -> let rec loop prefix index = function | [ ] -> ( List . rev prefix ) @ nodes | x :: l ' -> if k <= index then ( List . rev prefix ) @ node...
let append_child element node = insert_at_index ( ( element |> children |> count ) + 1 ) element node
let prepend_child element node = insert_at_index 1 element node
let insert_before target node = insert_at_index ( index_of target ) ( parent target |> require_internal " Soup . insert_before : target node has no parent " ) node
let insert_after target node = insert_at_index ( ( index_of target ) + 1 ) ( parent target |> require_internal " Soup . insert_after : target node has no parent " ) node
let clear node = mutate_child_list ( fun children -> children |> List . iter ( fun child -> child . parent <- None ) ; [ ] ) node
let replace target node = delete node ; let parent = parent target |> require_internal " Soup . replace : target node has no parent " in let index = index_of target in delete target ; insert_at_index index parent node
let swap target element = let internal = " Soup . swap : internal error : non - element node given " in let target_children = child_list target |> require_internal internal in let element_children = child_list element |> require_internal internal in target_children |> List . iter ( fun child -> child . parent <- Some e...
let wrap target element = delete element ; clear element ; replace target element ; append_child element target
let unwrap node = let parent = parent node |> require_internal " Soup . unwrap : node has no parent " in let index = index_of node in delete node ; let children = match child_list node with | None -> [ ] | Some l -> l in ( try clear node with Failure _ -> ( ) ) ; List . rev children |> List . iter ( insert_at_index ind...
let append_root document node = delete node ; mutate_child_list ( fun f -> f @ [ forget_type node ] ) document ; node . parent <- Some document
let set_name new_name = function | { values = ` Element e ; _ } -> e . name <- new_name |> String . trim |> String . lowercase_ascii | _ -> failwith " Soup . set_name : internal error : not an element " [ @ coverage off ]
let delete_attribute name = function | { values = ` Element e ; _ } -> e . attributes <- e . attributes |> List . filter ( fun ( name ' , _ ) -> name ' <> name ) | _ -> failwith " Soup . delete_attribute : internal error : not an element " [ @ coverage off ]
let set_attribute name value = function | { values = ` Element e ; _ } -> e . attributes <- e . attributes |> List . filter ( fun ( name ' , _ ) -> name ' <> name ) |> fun attributes -> ( name , value ) :: attributes | _ -> failwith " Soup . set_attribute : internal error : not an element " [ @ coverage off ]
let set_classes classes element = classes |> String . concat " " |> fun v -> set_attribute " class " v element
let add_class class_ element = let classes = classes element in if List . mem class_ classes then ( ) else set_classes ( class_ :: classes ) element
let remove_class class_ element = classes element |> List . filter ( fun c -> c <> class_ ) |> function | [ ] -> delete_attribute " class " element | v -> set_classes v element
let create_element ? id ? class_ ? classes ( ? attributes = [ ] ) ? inner_text name = let children = match inner_text with | None -> [ ] | Some s -> [ create_text s ] in let element = create_element name [ ] children in attributes |> List . iter ( fun ( n , v ) -> set_attribute n v element ) ; ( match classes with | No...
module R = struct let select_one s n = select_one s n |> require_internal " Soup . R . select_one : None " let attribute s n = attribute s n |> require_internal " Soup . R . attribute : None " let id n = id n |> require_internal " Soup . R . id : None " let element n = element n |> require_internal " Soup . R . element...
let read_channel channel = Markup . channel channel |> Markup . to_string
let read_file path = Markup . file path |> fst |> Markup . to_string
let write_file path data = Markup . string data |> Markup . to_file path [ @@ coverage off ]
let mkdir dir = try Ok ( FU . mkdir ~ parent : true dir ) with FileUtil . MkdirError e -> Error e
let get_color_style ( ) = if Sys . win32 then ` None else let no_color = Sys . getenv_opt " NO_COLOR " |> Option . is_some in let interactive = Unix . isatty ( Unix . descr_of_out_channel stderr ) in if interactive && ( not no_color ) then ` Ansi_tty else ` None
let color_level ppf l = let app_style = ` Cyan in let err_style = ` Red in let warn_style = ` Yellow in let info_style = ` Green in let debug_style = ` Blue in let f ppf style l = Fmt . pf ppf " % a " Fmt . ( styled style string ) ( Logs . level_to_string ( Some l ) |> String . uppercase_ascii ) in let l = if Option . ...
let pp_header ppf ( l , h ) = match h with | None -> if l = Logs . App then ( ) else Format . fprintf ppf " [ % a ] " color_level ( Some l ) | Some h -> Format . fprintf ppf " [ % s ] " h
let log_reporter = Logs . format_reporter ~ pp_header : pp_header ( )
let setup_logging verbose debug = let level = if debug then Logs . Debug else if verbose then Logs . Info else Logs . Warning in let style = get_color_style ( ) in Logs . set_level ( Some level ) ; Fmt_tty . setup_std_outputs ~ style_renderer : style ( ) ; Logs . set_reporter log_reporter ; if debug then Printexc . rec...
let ( ) +/ left right = FP . concat left right
let list_dirs path = FU . ls path |> FU . filter FU . Is_dir
let make_build_dir build_dir = if ( FU . test FU . Exists build_dir ) then Ok ( ) else let ( ) = Logs . info @@ fun m -> m " Build directory " \% s " \ does not exist , creating " build_dir in mkdir build_dir
let make_page_dir_name settings target_dir page_name = if ( page_name = settings . index_page ) || ( not settings . clean_urls ) then target_dir else target_dir +/ page_name
let load_html settings soupault_config hooks page_file = let load_file preprocessor page_file = try match preprocessor with | None -> Ok ( Soup . read_file page_file ) | Some prep -> let prep_cmd = Printf . sprintf " % s % s " prep ( Filename . quote page_file ) in let ( ) = Logs . info @@ fun m -> m " Calling preproce...
let run_render_hook settings config hooks env soup = let hook = Hashtbl . find_opt hooks " render " in match hook with | Some ( file_name , source_code , hook_config ) -> if not ( Hooks . hook_should_run settings hook_config " render " env . page_file ) then Ok None else let ( ) = Logs . info @@ fun m -> m " Running th...
let render_html_builtin settings soup = let add_doctype settings html_str = let buf = Buffer . create ( 32 * 1024 ) in let doctype = settings . doctype |> String . trim in let ( ) = Buffer . add_string buf doctype ; Buffer . add_char buf ' \ n ' ; Buffer . add_string buf html_str in Buffer . contents buf in let print_h...
let render_html settings config hooks env soup = let res = run_render_hook settings config hooks env soup in match res with | Ok ( Some page_source ) -> Ok page_source | Ok None -> Ok ( render_html_builtin settings soup ) | Error _ as e -> e
let include_content action selector html content = let element = Soup . select_one selector html in match element with | Some element -> Ok ( Html_utils . insert_element ( Some action ) element content ) | None -> Error ( Printf . sprintf " No element in the template matches selector " \% s " , \ nowhere to insert the ...
let make_page settings page_file content = if not settings . generator_mode then Ok content else let page_wrapper_elem = Soup . select_one settings . complete_page_selector content in match page_wrapper_elem with | Some _ -> let ( ) = if settings . generator_mode then Logs . debug @@ fun m -> m " File appears to be a c...
let rec process_widgets env settings ws wh config soup = match ws with | [ ] -> Ok ( ) | w :: ws ' -> begin let open Widgets in let widget = Hashtbl . find wh w in if not ( widget_should_run settings w widget settings . build_profiles settings . site_dir env . page_file ) then ( process_widgets env settings ws ' wh con...
let fix_nav_path settings path page_name = if page_name = settings . index_page then Utils . drop_tail path else path
let make_page_file_name settings page_file target_dir = if settings . clean_urls then ( target_dir +/ settings . index_file ) else let page_file = FP . basename page_file in let extension = Utils . get_extension page_file in let page_file = if Utils . in_list settings . keep_extensions extension then page_file else FP ...
let save_html settings soupault_config hooks env page_source = let save_hook = Hashtbl . find_opt hooks " save " in match save_hook with | Some ( file_name , source_code , hook_config ) -> if Hooks . hook_should_run settings hook_config " save " env . page_file then let ( ) = Logs . info @@ fun m -> m " Running the " \...
let make_page_url settings nav_path orig_path target_dir page_file = let orig_page_file_name = FP . basename page_file in let target_page = if settings . clean_urls then target_dir |> FP . basename else make_page_file_name settings orig_page_file_name " " in let path = if ( ( FP . chop_extension orig_page_file_name ) =...
let extract_metadata settings soupault_config hooks env html = if not ( Autoindex . index_extraction_should_run settings env . page_file ) then ( Ok None ) else let entry = Autoindex . get_entry settings env html in let post_index_hook = Hashtbl . find_opt hooks " post - index " in match post_index_hook with | Some ( f...
let run_pre_process_hook settings config hooks page_file target_dir target_file content = let pre_process_hook = Hashtbl . find_opt hooks " pre - process " in match pre_process_hook with | Some ( file_name , source_code , hook_config ) -> if not ( Hooks . hook_should_run settings hook_config " pre - process " page_file...
let index_insertion_should_run settings index page_name = let aux settings page_name = if not settings . index then Some " indexing is disabled in the configuration " else if settings . index_only then Some " running in the index - only mode " else if ( page_name <> settings . index_page ) then Some ( Printf . sprintf ...
let process_page page_data index index_hash widgets hooks config settings = let ( page_file , page_content , nav_path ) = ( page_data . page_file_path , page_data . page_content , page_data . page_nav_path ) in let ( ) = Logs . info @@ fun m -> m " Processing page % s " page_file in let * content = match page_content w...
let process_page index index_hash widgets hooks config settings page_data = let res = try process_page page_data index index_hash widgets hooks config settings with Soupault_error msg -> Error msg in match res with Ok _ as res -> res | Error msg -> let msg = Printf . sprintf " Could not process page % s : % s " page_da...
type soupault_action = DoActualWork | InitProject | ShowVersion | ShowDefaultConfig | ShowEffectiveConfig
let get_args settings = let ( ) = Arg . current := 0 in let actions = ref [ ] in let sr = ref settings in let args = Arg . align [ ( " -- init " , Arg . Unit ( fun ( ) -> actions := ( InitProject :: ! actions ) ) , " Set up basic directory structure " ) ; ( " -- verbose " , Arg . Unit ( fun ( ) -> sr := { ! sr with ver...
let check_project_dir settings = let ( ) = if ( not ( FU . test FU . Exists settings . default_template ) ) && settings . generator_mode then Logs . warn @@ fun m -> m " Default template is required in generator mode , but template file " \% s " \ does not exist . " settings . default_template in let ( ) = if ( not ( F...
let find_config_file ( ) = let conf_exists = Sys . file_exists Defaults . config_file in let alt_conf_exists = Sys . file_exists Defaults . config_file_alt in match conf_exists , alt_conf_exists with | true , false -> Ok Defaults . config_file | false , true -> Ok Defaults . config_file_alt | true , true -> let ( ) = L...