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 --- src/doc_reform/io_out/cgi_sqlite_search_form.d | 56 +++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 6 deletions(-) (limited to 'src/doc_reform') diff --git a/src/doc_reform/io_out/cgi_sqlite_search_form.d b/src/doc_reform/io_out/cgi_sqlite_search_form.d index 31cab66..4523c2d 100644 --- a/src/doc_reform/io_out/cgi_sqlite_search_form.d +++ b/src/doc_reform/io_out/cgi_sqlite_search_form.d @@ -1217,6 +1217,45 @@ void cgi_function_intro(Cgi cgi) { 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)) { @@ -1332,7 +1371,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( @@ -1344,7 +1383,7 @@ LIMIT %%s OFFSET %%s ~ row["ocn"].as!string ~ "" ~ "
" - ~ row["body"].as!string + ~ highlight_text_matched(row["body"].as!string, tf.text) ); } } else { @@ -1446,18 +1485,23 @@ configuration "default" { // 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 } } } -- cgit v1.2.3