Skip to content

Commit

Permalink
Generate src/html/entities.cr automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Nov 20, 2023
1 parent c7bd41d commit 06e9d00
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 107 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ lib/** linguist-vendored

# produced by scripts/generate_windows_zone_names.cr
src/crystal/system/win32/zone_names.cr linguist-generated
# produced by scripts/generate_html_entities.cr
src/html/entities.cr linguist-generated
# produced by scripts/generate_ssl_server_defaults.cr
src/openssl/ssl/defaults.cr linguist-generated
# produced by scripts/generate_grapheme_properties.cr
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ generate_data: src/crystal/system/win32/zone_names.cr
src/crystal/system/win32/zone_names.cr: scripts/generate_windows_zone_names.cr
$(CRYSTAL) run $<

generate_data: src/html/entities.cr
src/html/entities.cr: scripts/generate_html_entities.cr scripts/html_entities.ecr
$(CRYSTAL) run $<

.PHONY: install
install: $(O)/crystal man/crystal.1.gz ## Install the compiler at DESTDIR
$(INSTALL) -d -m 0755 "$(BINDIR)/"
Expand Down
4 changes: 4 additions & 0 deletions Makefile.win
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ generate_data: src\crystal\system\win32\zone_names.cr
src\crystal\system\win32\zone_names.cr: scripts\generate_windows_zone_names.cr
$(CRYSTAL) run $<

generate_data: src\html\entities.cr
src\html\entities.cr: scripts\generate_html_entities.cr scripts\html_entities.ecr
$(CRYSTAL) run $<

.PHONY: install
install: $(O)\crystal.exe ## Install the compiler at prefix
$(call MKDIR,"$(BINDIR)")
Expand Down
43 changes: 43 additions & 0 deletions scripts/generate_html_entities.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#! /usr/bin/env crystal

require "http"
require "json"
require "ecr"

record Entity, characters : String, codepoints : Array(Int32) do
include JSON::Serializable
include JSON::Serializable::Strict
end

single_char_entities = [] of {String, Entity}
double_char_entities = [] of {String, Entity}

HTTP::Client.get("https://html.spec.whatwg.org/entities.json") do |res|
Hash(String, Entity).from_json(res.body_io).each do |name, entity|
name = name.rchop(';').lchop?('&') || raise "Entity does not begin with &"

entities =
case entity.codepoints.size
when 1; single_char_entities
when 2; double_char_entities
else raise "Unknown entity codepoint size"
end

entities << {name, entity}
end
end

single_char_entities.uniq!(&.first).sort_by!(&.first)
double_char_entities.uniq!(&.first).sort_by!(&.first)

max_entity_name_size = {
single_char_entities.max_of { |name, _| name.size },
double_char_entities.max_of { |name, _| name.size },
}.max

path = "#{__DIR__}/../src/html/entities.cr"
File.open(path, "w") do |file|
ECR.embed "#{__DIR__}/html_entities.ecr", file
end

`crystal tool format #{path}`
24 changes: 24 additions & 0 deletions scripts/html_entities.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This file was automatically generated by running:
#
# scripts/generate_html_entities.cr
#
# DO NOT EDIT

module HTML
# :nodoc:
SINGLE_CHAR_ENTITIES = {
<%- single_char_entities.each do |name, entity| -%>
<%= name.dump %>.to_slice => '\u{<%= "%06X" % entity.codepoints[0] %>}',
<%- end -%>
} of Bytes => Char

# :nodoc:
DOUBLE_CHAR_ENTITIES = {
<%- double_char_entities.each do |name, entity| -%>
<%= name.dump %>.to_slice => "\u{<%= "%04X" % entity.codepoints[0] %>}\u{<%= "%04X" % entity.codepoints[1] %>}",
<%- end -%>
} of Bytes => String

# :nodoc:
MAX_ENTITY_NAME_SIZE = <%= max_entity_name_size %>
end
114 changes: 7 additions & 107 deletions src/html/entities.cr

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 06e9d00

Please sign in to comment.