Add src/hk_search_provider_mock.erl
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465%%%-------------------------------------------------------------------%%% @doc HK-SEARCH :: bundled reference implementation of%%% `hk_search_provider'.%%%%%% Entirely self-contained and deterministic -- it fabricates%%% plausible-shaped results locally rather than calling any real%%% search API. Its purpose is to make the pipeline runnable and%%% testable (stage sequencing, event emission, ranking,%%% deduplication, iteration limits) without external network%%% access or provider credentials. It is registered as the default%%% via `{hyperkitty, search_provider, hk_search_provider_mock}' in%%% `config/sys.config' and is meant to be swapped for a real%%% adapter module implementing the same `hk_search_provider'%%% behaviour in any non-development deployment.%%% @end%%%--------------------------------------------------------------------module(hk_search_provider_mock).-behaviour(hk_search_provider).
-export([plan/2, search/2, fetch/1, extract/2]).
plan(Query, _Opts) -> {ok, [Query, <<Query/binary, " overview">>, <<Query/binary, " latest">>]}.
search(SubQuery, _Opts) -> Slug = slugify(SubQuery), Results = [ #{url => <<"https://example.org/", Slug/binary, "/a">>, title => <<"About: ", SubQuery/binary>>, snippet => <<"A reference result discussing ", SubQuery/binary, ".">>}, #{url => <<"https://example.net/articles/", Slug/binary>>, title => <<SubQuery/binary, " -- overview">>, snippet => <<"An overview of ", SubQuery/binary, " with background and context.">>} ], {ok, Results}.
fetch(Url) -> Body = <<"<html><body><h1>", Url/binary, "</h1>" "<p>Mock content for ", Url/binary, ". This stands in for a real " "page fetch so the retrieval/extraction stages have something to " "operate on without a live network dependency.</p></body></html>">>, {ok, #{content_type => <<"text/html">>, body => Body}}.
extract(Body, <<"text/html">>) -> %% Deliberately naive: strip tags rather than depend on an HTML %% parser this mock doesn't need. A real provider adapter would %% use a proper HTML/readability extractor here. Text = re_strip_tags(Body), {ok, #{text => Text, citation_map => #{<<"p1">> => #{start => 0, length => byte_size(Text)}}}}.
%%% internal
slugify(Bin) -> Lower = string:lowercase(binary_to_list(Bin)), Slug = [case C of C when C >= $a, C =< $z -> C; C when C >= $0, C =< $9 -> C; _ -> $- end || C <- Lower], list_to_binary(Slug).
re_strip_tags(Html) -> case re:run(Html, "<[^>]*>", [global, {capture, none}]) of _ -> re:replace(Html, "<[^>]*>", " ", [global, {return, binary}]) end.src/hk_search_provider_mock.erlAdded+65