-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrasel-convert
executable file
·37 lines (34 loc) · 1.07 KB
/
rasel-convert
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
#!/usr/bin/env ruby
abort_usage = lambda do
abort <<~HEREDOC
usage:
\t$ rasel-convert <file.rasel> <file.rasela>
\t$ rasel-convert <file.rasela> <file.rasel>
\t(pipe support is not implemented)
HEREDOC
end
abort_usage.call unless ARGV[0] and File.file? ARGV[0]
abort_usage.call unless ARGV[0] and ARGV[1] and (!File.exist?(ARGV[1]) || puts("overwriting")) || File.file?(ARGV[1])
require "json/pure"
case ARGV.map &File.method(:extname)
when %w{ .rasel .rasela }
require_relative "../lib/rasel"
RASEL.write_pretty_json ARGV[1], (
File.read(ARGV[0]).tap{ |_| abort "empty source" if _.empty? }.
split("\n").each_with_index.with_object([]) do |(row, i), o|
row.chars.each_with_index do |c, j|
o.push [i, j, c] unless " " == c
end
end
)
when %w{ .rasela .rasel }
File.open ARGV[1], "w" do |file|
file.puts( JSON.load(File.read ARGV[0]).each_with_object([]) do |(y, x, c, a), code|
code[y] ||= []
code[y][x] = c
end.map{ |row| row.map{ |_| _ || " " }.join } )
end
else
abort_usage.call
end
puts :OK