From 9dd3960e6e5e4f6f1519c6dd59f118ed10bcc874 Mon Sep 17 00:00:00 2001 From: Ralph Amissah Date: Thu, 2 Apr 2020 15:39:43 -0400 Subject: cgi search form, highlight matched text - complication where match foun in url link --- org/out_cgi_search_sqlite.org | 56 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-) (limited to 'org') diff --git a/org/out_cgi_search_sqlite.org b/org/out_cgi_search_sqlite.org index 04d0c47..58375bf 100644 --- a/org/out_cgi_search_sqlite.org +++ b/org/out_cgi_search_sqlite.org @@ -156,18 +156,23 @@ mixin GenericMain!cgi_function_intro; // Handle error } } - { // cgi.d + { // get cgi.d import std.net.curl, std.stdio; + char[] cgi_d; try { - auto cgi_d = get("https://raw.githubusercontent.com/adamdruppe/arsd/master/cgi.d"); + cgi_d = get!HTTP("https://raw.githubusercontent.com/adamdruppe/arsd/master/cgi.d"); + } catch (ErrnoException ex) { + // Handle error + // CurlCode perform(ThrowOnError throwOnError = Yes.throwOnError); + CurlCode perform(ThrowOnError throwOnError = No.throwOnError); + } + if (cgi_d && cgi_d.length > 0) { try { auto f = File(pth_sqlite_cgi.cgi_d_path_out, "w"); f.write(cgi_d); } catch (ErrnoException ex) { // Handle error } - } catch (ErrnoException ex) { - // Handle error } } } @@ -1544,6 +1549,45 @@ auto db = Database(conf.db_path ~ cv.db_selected); return sql_match_offset_count; } void sql_search_query() { + string highlight_text_matched(string _txt, string search_field) { + string _mark_open = "┤"; + string _mark_close = "├"; + string _span_yellow = ""; + string _span_red = ""; + string _span_blue = ""; + string _span_close = ""; + string _sf_str = search_field.strip.split("%%20").join(" ").strip; + string[] _sf_arr = _sf_str.split(regex(r"\s+AND\s+|\s+OR\s+")); + auto rgx_url = regex(r"]+?>"); + foreach (_sf; _sf_arr) { + auto rgx_matched_text = regex(_sf, "i"); + auto rgx_marked_pair = regex(r"┤(?P" ~ _sf ~ ")├", "i"); + if (auto m = _txt.matchFirst(rgx_url)) { + _txt = replaceAll!(m => + _mark_open + ~ m.captures[0] + ~ _mark_close + )(_txt, rgx_matched_text); + _txt = replaceAll!(m => + replaceAll!(u => + u["keep"] + )(m.hit, rgx_marked_pair) + )(_txt, rgx_url); + _txt = replaceAll!(m => + _span_yellow + ~ m["keep"] + ~ _span_close + )(_txt, rgx_marked_pair); + } else { + _txt = replaceAll!(m => + _span_yellow + ~ m.captures[0] + ~ _span_close + )(_txt, rgx_matched_text); + } + } + return _txt; + } string select_field_like(string db_field, string search_field) { string where_ = ""; if (!(search_field.empty)) { @@ -1679,7 +1723,7 @@ LIMIT %%s OFFSET %%s ~ row["ocn"].as!string ~ "" ~ "
" - ~ row["body"].as!string + ~ highlight_text_matched(row["body"].as!string, tf.text) ); } else { cgi.write( @@ -1691,7 +1735,7 @@ LIMIT %%s OFFSET %%s ~ row["ocn"].as!string ~ "" ~ "
" - ~ row["body"].as!string + ~ highlight_text_matched(row["body"].as!string, tf.text) ); } #+END_SRC -- cgit v1.2.3