Debian Codegraph @

lib/extractors.c

raw symbols SCIP
1 /*
2 Copyright (C) 2026 Sutou Kouhei <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "grn_extractor.h"
20 #include "grn_extractors.h"
21
22 typedef struct {
23 bool remove_tag;
24 bool expand_character_reference;
25 } html_extract_options;
26
27 static void
28 html_extract_options_init(grn_ctx *ctx, html_extract_options *options)
29 {
30 options->remove_tag = true;
31 options->expand_character_reference = true;
32 }
33
34 static void
35 html_extract_options_fin(grn_ctx *ctx, html_extract_options *options)
36 {
37 }
38
39 static void
40 html_extract_close_options(grn_ctx *ctx, void *data)
41 {
42 html_extract_options *options = data;
43 html_extract_options_fin(ctx, options);
44 GRN_FREE(options);
45 }
46
47 static void *
48 html_extract_open_options(grn_ctx *ctx,
49 grn_obj *extractor,
50 grn_obj *raw_options,
51 void *user_data)
52 {
53 const char *tag = "[extractor][html]";
54 html_extract_options *options = GRN_CALLOC(sizeof(html_extract_options));
55 if (!options) {
56 ERR(GRN_NO_MEMORY_AVAILABLE,
57 "%s failed to allocate memory for options",
58 tag);
59 return NULL;
60 }
61
62 html_extract_options_init(ctx, options);
63
64 GRN_OPTION_VALUES_EACH_BEGIN(ctx, raw_options, i, name, name_length)
65 {
66 grn_raw_string name_raw;
67 name_raw.value = name;
68 name_raw.length = name_length;
69
70 if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "remove_tag")) {
71 options->remove_tag =
72 grn_vector_get_element_bool(ctx, raw_options, i, options->remove_tag);
73 } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw,
74 "expand_character_reference")) {
75 options->expand_character_reference =
76 grn_vector_get_element_bool(ctx,
77 raw_options,
78 i,
79 options->expand_character_reference);
80 }
81 }
82 GRN_OPTION_VALUES_EACH_END();
83
84 return options;
85 }
86
87 static bool
88 html_expand_numeric_char_ref(grn_ctx *ctx, grn_obj *buffer, uint32_t code_point)
89 {
90 /* Special code points.
91 * See also:
92 * https://html.spec.whatwg.org/multipage/parsing.html#numeric-character-reference-end-state
93 */
94 switch (code_point) {
95 case 0x80:
96 code_point = 0x20AC;
97 break;
98 case 0x82:
99 code_point = 0x201A;
100 break;
101 case 0x83:
102 code_point = 0x0192;
103 break;
104 case 0x84:
105 code_point = 0x201E;
106 break;
107 case 0x85:
108 code_point = 0x2026;
109 break;
110 case 0x86:
111 code_point = 0x2020;
112 break;
113 case 0x87:
114 code_point = 0x2021;
115 break;
116 case 0x88:
117 code_point = 0x02C6;
118 break;
119 case 0x89:
120 code_point = 0x2030;
121 break;
122 case 0x8A:
123 code_point = 0x0160;
124 break;
125 case 0x8B:
126 code_point = 0x2039;
127 break;
128 case 0x8C:
129 code_point = 0x0152;
130 break;
131 case 0x8E:
132 code_point = 0x017D;
133 break;
134 case 0x91:
135 code_point = 0x2018;
136 break;
137 case 0x92:
138 code_point = 0x2019;
139 break;
140 case 0x93:
141 code_point = 0x201C;
142 break;
143 case 0x94:
144 code_point = 0x201D;
145 break;
146 case 0x95:
147 code_point = 0x2022;
148 break;
149 case 0x96:
150 code_point = 0x2013;
151 break;
152 case 0x97:
153 code_point = 0x2014;
154 break;
155 case 0x98:
156 code_point = 0x02DC;
157 break;
158 case 0x99:
159 code_point = 0x2122;
160 break;
161 case 0x9A:
162 code_point = 0x0161;
163 break;
164 case 0x9B:
165 code_point = 0x203A;
166 break;
167 case 0x9C:
168 code_point = 0x0153;
169 break;
170 case 0x9E:
171 code_point = 0x017E;
172 break;
173 case 0x9F:
174 code_point = 0x0178;
175 break;
176 default:
177 break;
178 }
179 grn_text_code_point(ctx, buffer, code_point);
180 return true;
181 }
182
183 #include "normalizer_html_expand_named_char_ref.c"
184
185 typedef enum {
186 HTML_STATE_TEXT,
187 HTML_STATE_IN_TAG,
188 HTML_STATE_IN_CHAR_REF,
189 } html_state;
190
191 static grn_obj *
192 html_extract(grn_ctx *ctx, grn_obj *extractor, grn_extract_data *data)
193 {
194 const char *tag = "[extractor][html]";
195
196 grn_obj *value = grn_extract_data_get_value(ctx, data);
197 if (!grn_type_id_is_text_family(ctx, value->header.domain)) {
198 /* Do nothing */
199 return NULL;
200 }
201
202 grn_obj *table = grn_extract_data_get_table(ctx, data);
203 if (!table) {
204 ERR(GRN_INVALID_ARGUMENT, "%s related table information is missing", tag);
205 return NULL;
206 }
207
208 uint32_t i = grn_extract_data_get_index(ctx, data);
209 html_extract_options *options =
210 grn_table_cache_extractors_options(ctx,
211 table,
212 i,
213 html_extract_open_options,
214 html_extract_close_options,
215 NULL);
216 if (ctx->rc != GRN_SUCCESS) {
217 return NULL;
218 }
219
220 grn_obj *output = grn_obj_open(ctx, GRN_BULK, 0, GRN_DB_TEXT);
221 const char *current = GRN_TEXT_VALUE(value);
222 const char *end = current + GRN_TEXT_LEN(value);
223
224 html_state state = HTML_STATE_TEXT;
225 const char *start_tag = NULL;
226 const char *start_char_ref = NULL;
227
228 int char_length = 0;
229 for (; current < end; current += char_length) {
230 char_length = grn_charlen(ctx, current, end);
231 if (char_length == 0) {
232 /* Set error? */
233 break;
234 }
235
236 if (char_length == 1) {
237 bool processed = false;
238 switch (state) {
239 case HTML_STATE_IN_TAG:
240 if (*current == '>') {
241 if (!options->remove_tag) {
242 GRN_TEXT_PUT(ctx, output, start_tag, (current - start_tag + 1));
243 }
244 state = HTML_STATE_TEXT;
245 start_tag = NULL;
246 processed = true;
247 }
248 break;
249 case HTML_STATE_IN_CHAR_REF:
250 if (*current == ';') {
251 bool valid = false;
252 if (start_char_ref[1] == '#') {
253 long code_point = -1;
254 if (start_char_ref[2] == 'x' || start_char_ref[2] == 'X') {
255 /* "&#xH...;" */
256 const char *code_point_end = NULL;
257 code_point =
258 grn_htoui(start_char_ref + 3, current, &code_point_end);
259 if (code_point_end != current) {
260 code_point = -1;
261 }
262 } else {
263 /* "&#D...;" */
264 const char *code_point_end = NULL;
265 code_point =
266 grn_atoll(start_char_ref + 2, current, &code_point_end);
267 if (code_point_end != current) {
268 code_point = -1;
269 }
270 }
271 if (code_point >= 0) {
272 valid =
273 html_expand_numeric_char_ref(ctx, output, (uint32_t)code_point);
274 }
275 } else {
276 /* "&NAME;" */
277 valid = html_expand_named_char_ref(
278 ctx,
279 output,
280 start_char_ref + 1,
281 (size_t)(current - start_char_ref - 1));
282 }
283 if (valid) {
284 state = HTML_STATE_TEXT;
285 start_char_ref = NULL;
286 processed = true;
287 }
288 } else if (((current - start_char_ref) == 1 && *current == '#') ||
289 ('A' <= *current && *current <= 'Z') ||
290 ('a' <= *current && *current <= 'z') ||
291 ('0' <= *current && *current <= '9')) {
292 processed = true;
293 }
294 break;
295 default:
296 if (options->remove_tag && *current == '<') {
297 state = HTML_STATE_IN_TAG;
298 start_tag = current;
299 processed = true;
300 } else if (options->expand_character_reference && *current == '&') {
301 state = HTML_STATE_IN_CHAR_REF;
302 start_char_ref = current;
303 processed = true;
304 }
305 break;
306 }
307 if (processed) {
308 continue;
309 }
310 }
311
312 switch (state) {
313 case HTML_STATE_IN_TAG:
314 break;
315 case HTML_STATE_IN_CHAR_REF:
316 {
317 /* "&...(any multibyte character)...;" case: Write it as-is. */
318 size_t length =
319 (size_t)(current - start_char_ref) + (size_t)char_length;
320 GRN_TEXT_PUT(ctx, output, start_char_ref, length);
321 state = HTML_STATE_TEXT;
322 start_char_ref = NULL;
323 }
324 break;
325 default:
326 GRN_TEXT_PUT(ctx, output, current, (size_t)char_length);
327 break;
328 }
329 }
330 if (state == HTML_STATE_IN_TAG) {
331 if (!options->remove_tag) {
332 GRN_TEXT_PUT(ctx, output, start_tag, (size_t)(current - start_tag));
333 }
334 }
335
336 return output;
337 }
338
339 typedef struct {
340 grn_json_path *path;
341 } json_extract_options;
342
343 static void
344 json_extract_options_init(grn_ctx *ctx, json_extract_options *options)
345 {
346 options->path = NULL;
347 }
348
349 static void
350 json_extract_options_fin(grn_ctx *ctx, json_extract_options *options)
351 {
352 if (options->path) {
353 grn_json_path_close(ctx, options->path);
354 options->path = NULL;
355 }
356 }
357
358 static void
359 json_extract_close_options(grn_ctx *ctx, void *data)
360 {
361 json_extract_options *options = data;
362 json_extract_options_fin(ctx, options);
363 GRN_FREE(options);
364 }
365
366 static void *
367 json_extract_open_options(grn_ctx *ctx,
368 grn_obj *extractor,
369 grn_obj *raw_options,
370 void *user_data)
371 {
372 const char *tag = "[extractor][json]";
373 json_extract_options *options = GRN_CALLOC(sizeof(json_extract_options));
374 if (!options) {
375 ERR(GRN_NO_MEMORY_AVAILABLE,
376 "%s failed to allocate memory for options",
377 tag);
378 return NULL;
379 }
380
381 json_extract_options_init(ctx, options);
382
383 GRN_OPTION_VALUES_EACH_BEGIN(ctx, raw_options, i, name, name_length)
384 {
385 grn_raw_string name_raw;
386 name_raw.value = name;
387 name_raw.length = name_length;
388
389 if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "path")) {
390 const char *path;
391 unsigned int path_length;
392 grn_id domain;
393
394 path_length =
395 grn_vector_get_element(ctx, raw_options, i, &path, NULL, &domain);
396 if (grn_type_id_is_text_family(ctx, domain) && path_length > 0) {
397 options->path = grn_json_path_open(ctx, path, path_length);
398 }
399 }
400 }
401 GRN_OPTION_VALUES_EACH_END();
402
403 if (!options->path) {
404 if (ctx->rc == GRN_SUCCESS) {
405 ERR(GRN_INVALID_ARGUMENT, "%s path is missing", tag);
406 }
407 json_extract_close_options(ctx, options);
408 return NULL;
409 }
410
411 return options;
412 }
413
414 static grn_obj *
415 json_extract(grn_ctx *ctx, grn_obj *extractor, grn_extract_data *data)
416 {
417 const char *tag = "[extractor][json]";
418
419 grn_obj *value = grn_extract_data_get_value(ctx, data);
420 grn_obj *json;
421 grn_obj json_buffer;
422 GRN_JSON_INIT(&json_buffer, 0);
423 if (value->header.domain == GRN_DB_JSON) {
424 json = value;
425 } else {
426 grn_rc rc = grn_obj_cast(ctx, value, &json_buffer, false);
427 if (rc != GRN_SUCCESS) {
428 GRN_OBJ_FIN(ctx, &json_buffer);
429 return NULL;
430 }
431 json = &json_buffer;
432 }
433
434 grn_obj *table = grn_extract_data_get_table(ctx, data);
435 if (!table) {
436 ERR(GRN_INVALID_ARGUMENT, "%s related table information is missing", tag);
437 GRN_OBJ_FIN(ctx, &json_buffer);
438 return NULL;
439 }
440
441 uint32_t i = grn_extract_data_get_index(ctx, data);
442 json_extract_options *options =
443 grn_table_cache_extractors_options(ctx,
444 table,
445 i,
446 json_extract_open_options,
447 json_extract_close_options,
448 NULL);
449 if (ctx->rc != GRN_SUCCESS) {
450 GRN_OBJ_FIN(ctx, &json_buffer);
451 return NULL;
452 }
453
454 grn_obj *output = grn_obj_open(ctx, GRN_VECTOR, 0, GRN_ID_NIL);
455 grn_rc rc = grn_json_extract(ctx, json, options->path, output);
456 if (rc != GRN_SUCCESS) {
457 grn_obj_close(ctx, output);
458 GRN_OBJ_FIN(ctx, &json_buffer);
459 return NULL;
460 }
461 GRN_OBJ_FIN(ctx, &json_buffer);
462 return output;
463 }
464
465 grn_rc
466 grn_db_init_builtin_extractors(grn_ctx *ctx)
467 {
468 {
469 grn_obj *extractor;
470 extractor = grn_extractor_create(ctx, "ExtractorHTML", -1);
471 grn_extractor_set_extract_func(ctx, extractor, html_extract);
472 }
473
474 {
475 grn_obj *extractor;
476 extractor = grn_extractor_create(ctx, "ExtractorJSON", -1);
477 grn_extractor_set_extract_func(ctx, extractor, json_extract);
478 }
479
480 return GRN_SUCCESS;
481 }