-
Notifications
You must be signed in to change notification settings - Fork 7
/
Rakefile
80 lines (67 loc) · 1.87 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task default: [:spec]
if Gem.ruby_version >= Gem::Version.new('2.6')
require 'rubocop/rake_task'
RuboCop::RakeTask.new
task default: [:rubocop]
end
task :generate_nextstep_mappings do
require 'net/http'
url = 'http://ftp.unicode.org/Public/MAPPINGS/VENDORS/NEXT/NEXTSTEP.TXT'
mappings = Net::HTTP.get(URI(url))
.lines
.grep(/^[^#$]/)
.map { |l| l.split("\t", 3) }
.reduce(+'') do |f, (ns, uc, cm)|
f << " #{ns} => #{uc}, #{cm}"
end
map = <<-RUBY
# frozen-string-literal: true
module Nanaimo
module Unicode
# Taken from #{url}
NEXT_STEP_MAPPING = {
#{mappings} }.freeze
end
end
RUBY
File.open('lib/nanaimo/unicode/next_step_mapping.rb', 'w') { |f| f << map }
end
task :generate_quote_maps do
quote_map = {
"\a" => '\\a',
"\b" => '\\b',
"\f" => '\\f',
"\r" => '\\r',
"\t" => '\\t',
"\v" => '\\v',
"\n" => '\\n',
%(') => "\\'",
%(") => '\\"',
'\\' => '\\\\'
}
unquote_map = quote_map.each_with_object("\n" => "\n") do |(value, escaped), map|
map[escaped[1..-1]] = value
map
end
quote_map.delete("'")
0.upto(31) { |i| quote_map[[i].pack('U')] ||= format('\\U%04x', i) }
quote_regexp = Regexp.union(quote_map.keys)
dump_hash = proc do |hash, indent = 4|
hash.reduce(+"{\n") { |dumped, (k, v)| dumped << "#{' ' * (indent + 2)}#{k.dump} => #{v.dump},\n" } << ' ' * indent << '}.freeze'
end
map = <<-RUBY
# frozen-string-literal: true
module Nanaimo
module Unicode
QUOTE_MAP = #{dump_hash[quote_map]}
UNQUOTE_MAP = #{dump_hash[unquote_map]}
QUOTE_REGEXP = #{quote_regexp.inspect}
end
end
RUBY
File.open('lib/nanaimo/unicode/quote_maps.rb', 'w') { |f| f << map }
end