forked from crystal-lang/crystal
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate
src/html/entities.cr
automatically
- Loading branch information
1 parent
c7bd41d
commit 06e9d00
Showing
6 changed files
with
84 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.