OSDN Git Service

fix #37610
[luatex-ja/luatexja.git] / tool / chars2defcharrange.rb
1 #! /usr/bin/ruby
2 # -*- coding: utf-8 -*-
3
4 # The following script converts a set of chars except "\s", as Ruby defines,
5 # to the character range definition of LuaTeX-ja.
6
7 # USAGE: ruby __FILE__ ifile rangeNo [> ofile]
8
9 # Example (in Japanese)
10 # 教育漢字リスト (http://www.aozora.gr.jp/kanji_table/kyouiku_list.zip)
11 # に対して適用したいとき.
12 # 1. kyoikukanji.txt に対して,コメント部分の先頭に # をつける編集を加える;
13 # 2. ruby chars2defcharrange.rb kyoikukanji.txt 210 > kyoikukanjiChars.tex
14 #    を実行する.
15
16 def print_usage()
17   print "USAGE: ruby ", __FILE__, "ifile rangeNo [> ofile]\n"
18 end
19
20 if __FILE__ == $0
21   # コマンドライン引数の処理
22   if ARGV.length < 2
23     print_usage()
24     exit
25   end
26   ifile = ARGV[0]
27   rangeNo = ARGV[1]
28
29   # 対象文字列の作成
30   string = ""
31   open(ifile, "r").each_line{|line|
32     if line =~ /#/
33       line = $`
34     end
35     line.gsub!(/\s/){}
36     string += line
37   }
38
39   # 10 進 unicode code point 配列に変換
40   decs = string.unpack("U*")
41
42   # print
43   print "\defcharrange{", rangeNo, "}{"
44   decs.each_with_index{|code, index|
45     if index != 0
46       print ","
47     end
48     print "\"", code.to_s(16)
49   }
50   print "}\n"
51 end