Skip to content

Commit 59a0534

Browse files
authored
Generate src/html/entities.cr automatically (#13998)
1 parent 6562623 commit 59a0534

File tree

6 files changed

+84
-107
lines changed

6 files changed

+84
-107
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ lib/** linguist-vendored
1010

1111
# produced by scripts/generate_windows_zone_names.cr
1212
src/crystal/system/win32/zone_names.cr linguist-generated
13+
# produced by scripts/generate_html_entities.cr
14+
src/html/entities.cr linguist-generated
1315
# produced by scripts/generate_ssl_server_defaults.cr
1416
src/openssl/ssl/defaults.cr linguist-generated
1517
# produced by scripts/generate_grapheme_properties.cr

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ generate_data: src/crystal/system/win32/zone_names.cr
161161
src/crystal/system/win32/zone_names.cr: scripts/generate_windows_zone_names.cr
162162
$(CRYSTAL) run $<
163163

164+
generate_data: src/html/entities.cr
165+
src/html/entities.cr: scripts/generate_html_entities.cr scripts/html_entities.ecr
166+
$(CRYSTAL) run $<
167+
164168
.PHONY: install
165169
install: $(O)/crystal man/crystal.1.gz ## Install the compiler at DESTDIR
166170
$(INSTALL) -d -m 0755 "$(BINDIR)/"

Makefile.win

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ generate_data: src\crystal\system\win32\zone_names.cr
163163
src\crystal\system\win32\zone_names.cr: scripts\generate_windows_zone_names.cr
164164
$(CRYSTAL) run $<
165165

166+
generate_data: src\html\entities.cr
167+
src\html\entities.cr: scripts\generate_html_entities.cr scripts\html_entities.ecr
168+
$(CRYSTAL) run $<
169+
166170
.PHONY: install
167171
install: $(O)\crystal.exe ## Install the compiler at prefix
168172
$(call MKDIR,"$(BINDIR)")

scripts/generate_html_entities.cr

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#! /usr/bin/env crystal
2+
3+
require "http"
4+
require "json"
5+
require "ecr"
6+
7+
record Entity, characters : String, codepoints : Array(Int32) do
8+
include JSON::Serializable
9+
include JSON::Serializable::Strict
10+
end
11+
12+
single_char_entities = [] of {String, Entity}
13+
double_char_entities = [] of {String, Entity}
14+
15+
HTTP::Client.get("https://html.spec.whatwg.org/entities.json") do |res|
16+
Hash(String, Entity).from_json(res.body_io).each do |name, entity|
17+
name = name.rchop(';').lchop?('&') || raise "Entity does not begin with &"
18+
19+
entities =
20+
case entity.codepoints.size
21+
when 1; single_char_entities
22+
when 2; double_char_entities
23+
else raise "Unknown entity codepoint size"
24+
end
25+
26+
entities << {name, entity}
27+
end
28+
end
29+
30+
single_char_entities.uniq!(&.first).sort_by!(&.first)
31+
double_char_entities.uniq!(&.first).sort_by!(&.first)
32+
33+
max_entity_name_size = {
34+
single_char_entities.max_of { |name, _| name.size },
35+
double_char_entities.max_of { |name, _| name.size },
36+
}.max
37+
38+
path = "#{__DIR__}/../src/html/entities.cr"
39+
File.open(path, "w") do |file|
40+
ECR.embed "#{__DIR__}/html_entities.ecr", file
41+
end
42+
43+
`crystal tool format #{path}`

scripts/html_entities.ecr

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# This file was automatically generated by running:
2+
#
3+
# scripts/generate_html_entities.cr
4+
#
5+
# DO NOT EDIT
6+
7+
module HTML
8+
# :nodoc:
9+
SINGLE_CHAR_ENTITIES = {
10+
<%- single_char_entities.each do |name, entity| -%>
11+
<%= name.inspect %>.to_slice => '\u{<%= "%06X" % entity.codepoints[0] %>}',
12+
<%- end -%>
13+
} of Bytes => Char
14+
15+
# :nodoc:
16+
DOUBLE_CHAR_ENTITIES = {
17+
<%- double_char_entities.each do |name, entity| -%>
18+
<%= name.inspect %>.to_slice => "\u{<%= "%04X" % entity.codepoints[0] %>}\u{<%= "%04X" % entity.codepoints[1] %>}",
19+
<%- end -%>
20+
} of Bytes => String
21+
22+
# :nodoc:
23+
MAX_ENTITY_NAME_SIZE = <%= max_entity_name_size %>
24+
end

src/html/entities.cr

Lines changed: 7 additions & 107 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)