naoa
null+****@clear*****
Sun Feb 21 02:05:50 JST 2016
naoa 2016-02-21 02:05:50 +0900 (Sun, 21 Feb 2016) New Revision: fed4adbf000e753bbd48875c3917dd2022f56dd9 https://github.com/groonga/groonga/commit/fed4adbf000e753bbd48875c3917dd2022f56dd9 Merged 539872a: Merge pull request #487 from naoa/add-highlight Message: proc_highlight: add highlight() Added files: test/command/suite/select/function/highlight/default.expected test/command/suite/select/function/highlight/default.test test/command/suite/select/function/highlight/default_tag.expected test/command/suite/select/function/highlight/default_tag.test test/command/suite/select/function/highlight/html_escape.expected test/command/suite/select/function/highlight/html_escape.test test/command/suite/select/function/highlight/no_normalizer.expected test/command/suite/select/function/highlight/no_normalizer.test test/command/suite/select/function/highlight/normalizer.expected test/command/suite/select/function/highlight/normalizer.test test/command/suite/select/function/highlight/two_keywords.expected test/command/suite/select/function/highlight/two_keywords.test Modified files: lib/grn_proc.h lib/proc.c lib/proc/proc_highlight.c Modified: lib/grn_proc.h (+1 -0) =================================================================== --- lib/grn_proc.h 2016-02-21 02:02:36 +0900 (a691966) +++ lib/grn_proc.h 2016-02-21 02:05:50 +0900 (1df053c) @@ -37,6 +37,7 @@ void grn_proc_init_config_set(grn_ctx *ctx); void grn_proc_init_config_delete(grn_ctx *ctx); void grn_proc_init_edit_distance(grn_ctx *ctx); void grn_proc_init_fuzzy_search(grn_ctx *ctx); +void grn_proc_init_highlight(grn_ctx *ctx); void grn_proc_init_highlight_full(grn_ctx *ctx); void grn_proc_init_highlight_html(grn_ctx *ctx); void grn_proc_init_inspect(grn_ctx *ctx); Modified: lib/proc.c (+1 -0) =================================================================== --- lib/proc.c 2016-02-21 02:02:36 +0900 (c0de421) +++ lib/proc.c 2016-02-21 02:05:50 +0900 (acb5c23) @@ -6364,4 +6364,5 @@ grn_db_init_builtin_query(grn_ctx *ctx) grn_proc_init_object_remove(ctx); grn_proc_init_snippet(ctx); + grn_proc_init_highlight(ctx); } Modified: lib/proc/proc_highlight.c (+117 -0) =================================================================== --- lib/proc/proc_highlight.c 2016-02-21 02:02:36 +0900 (35cd0c4) +++ lib/proc/proc_highlight.c 2016-02-21 02:05:50 +0900 (1ac090c) @@ -236,6 +236,123 @@ highlight_keywords(grn_ctx *ctx, grn_user_data *user_data, } static grn_obj * +func_highlight(grn_ctx *ctx, int nargs, grn_obj **args, + grn_user_data *user_data) +{ + grn_obj *highlighted = NULL; + +#define N_REQUIRED_ARGS 1 + if (nargs > N_REQUIRED_ARGS) { + grn_obj *string = args[0]; + grn_bool use_html_escape = GRN_FALSE; + grn_obj *keywords; + const char *normalizer_name = "NormalizerAuto"; + unsigned int normalizer_name_length = 14; + const char *default_open_tag = NULL; + unsigned int default_open_tag_length = 0; + const char *default_close_tag = NULL; + unsigned int default_close_tag_length = 0; + grn_obj *end_arg = args[nargs - 1]; + int n_args_without_option = nargs; + + if (end_arg->header.type == GRN_PTR) { + grn_obj *hash; + hash = GRN_PTR_VALUE(end_arg); + + if (hash) { + grn_hash_cursor *cursor; + void *key; + grn_obj *value; + int key_size; + if (hash->header.type != GRN_TABLE_HASH_KEY) { + GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, + "highlight(): " + "end argument must be object literal: <%.*s>", + (int)GRN_TEXT_LEN(end_arg), + GRN_TEXT_VALUE(end_arg)); + goto exit; + } + n_args_without_option--; + + cursor = grn_hash_cursor_open(ctx, (grn_hash *)hash, + NULL, 0, NULL, 0, + 0, -1, 0); + if (!cursor) { + GRN_PLUGIN_ERROR(ctx, GRN_NO_MEMORY_AVAILABLE, + "highlight(): couldn't open cursor"); + goto exit; + } + while (grn_hash_cursor_next(ctx, cursor) != GRN_ID_NIL) { + grn_hash_cursor_get_key_value(ctx, cursor, &key, &key_size, (void **)&value); + if (key_size == 10 && !memcmp(key, "normalizer", 10)) { + normalizer_name = GRN_TEXT_VALUE(value); + normalizer_name_length = GRN_TEXT_LEN(value); + } else if (key_size == 11 && !memcmp(key, "html_escape", 11)) { + if (GRN_BOOL_VALUE(value)) { + use_html_escape = GRN_TRUE; + } + } else if (key_size == 16 && !memcmp(key, "default_open_tag", 16)) { + default_open_tag = GRN_TEXT_VALUE(value); + default_open_tag_length = GRN_TEXT_LEN(value); + } else if (key_size == 17 && !memcmp(key, "default_close_tag", 17)) { + default_close_tag = GRN_TEXT_VALUE(value); + default_close_tag_length = GRN_TEXT_LEN(value); + } else { + GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, "invalid option name: %.*s", + key_size, (char *)key); + grn_hash_cursor_close(ctx, cursor); + goto exit; + } + } + grn_hash_cursor_close(ctx, cursor); + } + } + + keywords = + func_highlight_create_keywords_table(ctx, user_data, + normalizer_name, + normalizer_name_length); + + if (keywords) { + grn_obj **keyword_args = args + N_REQUIRED_ARGS; + unsigned int n_keyword_args = n_args_without_option - N_REQUIRED_ARGS; + if (default_open_tag_length == 0 && default_close_tag_length == 0) { + highlighted = highlight_keyword_sets(ctx, user_data, + keyword_args, n_keyword_args, + string, keywords, use_html_escape); + } else { + unsigned int i; + for (i = 0; i < n_keyword_args; i++) { + grn_table_add(ctx, keywords, + GRN_TEXT_VALUE(keyword_args[i]), + GRN_TEXT_LEN(keyword_args[i]), + NULL); + } + highlighted = highlight_keywords(ctx, user_data, + string, keywords, use_html_escape, + default_open_tag, default_open_tag_length, + default_close_tag, default_close_tag_length); + } + } + } +#undef N_REQUIRED_ARGS + +exit : + if (!highlighted) { + highlighted = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_VOID, 0); + } + + return highlighted; +} + +void +grn_proc_init_highlight(grn_ctx *ctx) +{ + grn_proc_create(ctx, "highlight", -1, GRN_PROC_FUNCTION, + func_highlight, NULL, NULL, 0, NULL); +} + +static grn_obj * func_highlight_full(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) { Added: test/command/suite/select/function/highlight/default.expected (+33 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/highlight/default.expected 2016-02-21 02:05:50 +0900 (916ef19) @@ -0,0 +1,33 @@ +table_create Entries TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Entries body COLUMN_SCALAR ShortText +[[0,0.0,0.0],true] +load --table Entries +[ +{"body": "<b>Rroonga</b> is a Ruby binding of Groonga."} +] +[[0,0.0,0.0],1] +select Entries --output_columns 'highlight(body, "groonga", "<span class=\\"keyword1\\">", "</span>" )' --command_version 2 +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "highlight", + "null" + ] + ], + [ + "<b>Rroonga</b> is a Ruby binding of <span class=\"keyword1\">Groonga</span>." + ] + ] + ] +] Added: test/command/suite/select/function/highlight/default.test (+12 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/highlight/default.test 2016-02-21 02:05:50 +0900 (25dbcf6) @@ -0,0 +1,12 @@ +table_create Entries TABLE_NO_KEY +column_create Entries body COLUMN_SCALAR ShortText + +load --table Entries +[ +{"body": "<b>Rroonga</b> is a Ruby binding of Groonga."} +] + +select Entries --output_columns \ + 'highlight(body, \ + "groonga", "<span class=\\"keyword1\\">", "</span>" \ +)' --command_version 2 Added: test/command/suite/select/function/highlight/default_tag.expected (+33 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/highlight/default_tag.expected 2016-02-21 02:05:50 +0900 (e957ef2) @@ -0,0 +1,33 @@ +table_create Entries TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Entries body COLUMN_SCALAR ShortText +[[0,0.0,0.0],true] +load --table Entries +[ +{"body": "Mroonga is a MySQL storage engine based on Groonga."} +] +[[0,0.0,0.0],1] +select Entries --output_columns 'highlight(body, "Groonga", "Mroonga", {"default_open_tag": "<span>", "default_close_tag": "</span>"} )' --command_version 2 +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "highlight", + "null" + ] + ], + [ + "<span>Mroonga</span> is a MySQL storage engine based on <span>Groonga</span>." + ] + ] + ] +] Added: test/command/suite/select/function/highlight/default_tag.test (+13 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/highlight/default_tag.test 2016-02-21 02:05:50 +0900 (073a648) @@ -0,0 +1,13 @@ +table_create Entries TABLE_NO_KEY +column_create Entries body COLUMN_SCALAR ShortText + +load --table Entries +[ +{"body": "Mroonga is a MySQL storage engine based on Groonga."} +] + +select Entries --output_columns \ + 'highlight(body, \ + "Groonga", "Mroonga", \ + {"default_open_tag": "<span>", "default_close_tag": "</span>"} \ +)' --command_version 2 Added: test/command/suite/select/function/highlight/html_escape.expected (+33 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/highlight/html_escape.expected 2016-02-21 02:05:50 +0900 (977bf8e) @@ -0,0 +1,33 @@ +table_create Entries TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Entries body COLUMN_SCALAR ShortText +[[0,0.0,0.0],true] +load --table Entries +[ +{"body": "<b>Rroonga</b> is a Ruby binding of Groonga."} +] +[[0,0.0,0.0],1] +select Entries --output_columns 'highlight(body, "groonga", "<span class=\\"keyword1\\">", "</span>", {"html_escape": true} )' --command_version 2 +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "highlight", + "null" + ] + ], + [ + "<b>Rroonga</b> is a Ruby binding of <span class=\"keyword1\">Groonga</span>." + ] + ] + ] +] Added: test/command/suite/select/function/highlight/html_escape.test (+13 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/highlight/html_escape.test 2016-02-21 02:05:50 +0900 (76adaf2) @@ -0,0 +1,13 @@ +table_create Entries TABLE_NO_KEY +column_create Entries body COLUMN_SCALAR ShortText + +load --table Entries +[ +{"body": "<b>Rroonga</b> is a Ruby binding of Groonga."} +] + +select Entries --output_columns \ + 'highlight(body, \ + "groonga", "<span class=\\"keyword1\\">", "</span>", \ + {"html_escape": true} \ +)' --command_version 2 Added: test/command/suite/select/function/highlight/no_normalizer.expected (+33 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/highlight/no_normalizer.expected 2016-02-21 02:05:50 +0900 (2cb4abe) @@ -0,0 +1,33 @@ +table_create Entries TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Entries body COLUMN_SCALAR ShortText +[[0,0.0,0.0],true] +load --table Entries +[ +{"body": "PGroonga is a PostgreSQL plugin to use groonga as index."} +] +[[0,0.0,0.0],1] +select Entries --output_columns 'highlight(body, "groonga", "<span class=\\"keyword1\\">", "</span>", {"normalizer": ""} )' --command_version 2 +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "highlight", + "null" + ] + ], + [ + "PGroonga is a PostgreSQL plugin to use <span class=\"keyword1\">groonga</span> as index." + ] + ] + ] +] Added: test/command/suite/select/function/highlight/no_normalizer.test (+13 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/highlight/no_normalizer.test 2016-02-21 02:05:50 +0900 (c397854) @@ -0,0 +1,13 @@ +table_create Entries TABLE_NO_KEY +column_create Entries body COLUMN_SCALAR ShortText + +load --table Entries +[ +{"body": "PGroonga is a PostgreSQL plugin to use groonga as index."} +] + +select Entries --output_columns \ + 'highlight(body, \ + "groonga", "<span class=\\"keyword1\\">", "</span>", \ + {"normalizer": ""} \ +)' --command_version 2 Added: test/command/suite/select/function/highlight/normalizer.expected (+33 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/highlight/normalizer.expected 2016-02-21 02:05:50 +0900 (9360de7) @@ -0,0 +1,33 @@ +table_create Entries TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Entries body COLUMN_SCALAR ShortText +[[0,0.0,0.0],true] +load --table Entries +[ +{"body": "PGroonga is a PostgreSQL plugin to use groonga as index."} +] +[[0,0.0,0.0],1] +select Entries --output_columns 'highlight(body, "groonga", "<span class=\\"keyword1\\">", "</span>", {"normalizer": "NormalizerAuto"} )' --command_version 2 +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "highlight", + "null" + ] + ], + [ + "P<span class=\"keyword1\">Groonga</span> is a PostgreSQL plugin to use <span class=\"keyword1\">groonga</span> as index." + ] + ] + ] +] Added: test/command/suite/select/function/highlight/normalizer.test (+13 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/highlight/normalizer.test 2016-02-21 02:05:50 +0900 (0df3f39) @@ -0,0 +1,13 @@ +table_create Entries TABLE_NO_KEY +column_create Entries body COLUMN_SCALAR ShortText + +load --table Entries +[ +{"body": "PGroonga is a PostgreSQL plugin to use groonga as index."} +] + +select Entries --output_columns \ + 'highlight(body, \ + "groonga", "<span class=\\"keyword1\\">", "</span>", \ + {"normalizer": "NormalizerAuto"} \ +)' --command_version 2 Added: test/command/suite/select/function/highlight/two_keywords.expected (+33 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/highlight/two_keywords.expected 2016-02-21 02:05:50 +0900 (94e40a6) @@ -0,0 +1,33 @@ +table_create Entries TABLE_NO_KEY +[[0,0.0,0.0],true] +column_create Entries body COLUMN_SCALAR ShortText +[[0,0.0,0.0],true] +load --table Entries +[ +{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."} +] +[[0,0.0,0.0],1] +select Entries --output_columns 'highlight(body, "Groonga", "<span class=\\"keyword1\\">", "</span>", "mysql", "<span class=\\"keyword2\\">", "</span>" )' --command_version 2 +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "highlight", + "null" + ] + ], + [ + "Mroonga is a <span class=\"keyword2\">MySQL</span> storage engine based on <span class=\"keyword1\">Groonga</span>. <b>Rroonga</b> is a Ruby binding of <span class=\"keyword1\">Groonga</span>." + ] + ] + ] +] Added: test/command/suite/select/function/highlight/two_keywords.test (+13 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/highlight/two_keywords.test 2016-02-21 02:05:50 +0900 (2b400b3) @@ -0,0 +1,13 @@ +table_create Entries TABLE_NO_KEY +column_create Entries body COLUMN_SCALAR ShortText + +load --table Entries +[ +{"body": "Mroonga is a MySQL storage engine based on Groonga. <b>Rroonga</b> is a Ruby binding of Groonga."} +] + +select Entries --output_columns \ + 'highlight(body, \ + "Groonga", "<span class=\\"keyword1\\">", "</span>", \ + "mysql", "<span class=\\"keyword2\\">", "</span>" \ +)' --command_version 2 -------------- next part -------------- HTML����������������������������... Download