diff options
-rw-r--r-- | org/out_cgi_search_sqlite.org | 56 | ||||
-rw-r--r-- | src/doc_reform/io_out/cgi_sqlite_search_form.d | 56 |
2 files changed, 100 insertions, 12 deletions
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 = "<span style=\"background-color: #ffff48\">"; + string _span_red = "<span style=\"background-color: #ff48ff\">"; + string _span_blue = "<span style=\"background-color: #48ffff\">"; + string _span_close = "</span>"; + 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"<a href=[^>]+?>"); + foreach (_sf; _sf_arr) { + auto rgx_matched_text = regex(_sf, "i"); + auto rgx_marked_pair = regex(r"┤(?P<keep>" ~ _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 ~ "</a>" ~ "<br>" - ~ 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 ~ "</a>" ~ "<br>" - ~ row["body"].as!string + ~ highlight_text_matched(row["body"].as!string, tf.text) ); } #+END_SRC 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 = "<span style=\"background-color: #ffff48\">"; + string _span_red = "<span style=\"background-color: #ff48ff\">"; + string _span_blue = "<span style=\"background-color: #48ffff\">"; + string _span_close = "</span>"; + 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"<a href=[^>]+?>"); + foreach (_sf; _sf_arr) { + auto rgx_matched_text = regex(_sf, "i"); + auto rgx_marked_pair = regex(r"┤(?P<keep>" ~ _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 ~ "</a>" ~ "<br>" - ~ 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 ~ "</a>" ~ "<br>" - ~ 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 } } } |