Debian Codegraph @

lib/normalizer_html_expand_named_char_ref.rb

raw symbols SCIP
1 #!/usr/bin/env ruby
2 #
3 # Copyright(C) 2022 Sutou Kouhei <[email protected]>
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 require "json"
20
21 entities = JSON.parse(File.read(ARGV[0] || "entities.json"))
22
23 puts(<<-HEADER)
24 /*
25 Copyright(C) 2022 Sutou Kouhei <[email protected]>
26
27 This library is free software; you can redistribute it and/or
28 modify it under the terms of the GNU Lesser General Public
29 License as published by the Free Software Foundation; either
30 version 2.1 of the License, or (at your option) any later version.
31
32 This library is distributed in the hope that it will be useful,
33 but WITHOUT ANY WARRANTY; without even the implied warranty of
34 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 Lesser General Public License for more details.
36
37 You should have received a copy of the GNU Lesser General Public
38 License along with this library; if not, write to the Free Software
39 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
40 */
41
42 /*
43 Don't edit this file by hand.
44 This is generated automatically by normalizer_html_expand_named_char_ref.rb.
45 */
46
47 #include "grn_str.h"
48 #include <string.h>
49
50 HEADER
51
52 puts(<<-FUNCTION_START)
53 /* Mapping table:
54 https://html.spec.whatwg.org/multipage/named-characters.html#named-character-references */
55 static bool
56 html_expand_named_char_ref(grn_ctx *ctx,
57 grn_obj *buffer,
58 const char *name,
59 size_t length)
60 {
61 FUNCTION_START
62
63 target_entities = {}
64 entities.each do |name, entity|
65 normalized_name = name.gsub(/\A&|;\z/, "")
66 target_entities[normalized_name] = entity
67 end
68 length_grouped_target_entities = target_entities.group_by do |name, _|
69 name.length
70 end
71 puts(" switch (length) {")
72 length_grouped_target_entities.keys.sort.each do |length|
73 puts(" case #{length} :")
74 length_target_entities = length_grouped_target_entities[length]
75 first_char_grouped_target_entities = length_target_entities.group_by do |name, _|
76 name[0]
77 end
78 puts(" switch (name[0]) {")
79 first_char_grouped_target_entities.keys.sort.each do |first_char|
80 puts(" case '#{first_char}' :")
81 first_char_grouped_target_entities[first_char].each do |name, entity|
82 puts(" if (strncmp(name + 1, \"#{name[1..-1]}\", length - 1) == 0) {")
83 entity["codepoints"].each do |code_point|
84 puts(" grn_text_code_point(ctx, buffer, #{code_point});")
85 end
86 puts(" return true;")
87 puts(" }")
88 end
89 puts(" break;")
90 end
91 puts(" default :")
92 puts(" break;")
93 puts(" }")
94 puts(" break;")
95 end
96 puts(" default :")
97 puts(" break;")
98 puts(" }")
99
100 puts(<<-FUNCTION_END)
101 return false;
102 }
103 FUNCTION_END