Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@celadevra
Last active July 30, 2017 03:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save celadevra/8294149 to your computer and use it in GitHub Desktop.
Save celadevra/8294149 to your computer and use it in GitHub Desktop.
Filter out uniq Chinese characters used in a text file. #ruby
#!/usr/bin/env ruby
everything = ""
chars, charcodes = ""
File.open("main.md", "r") do |file|
while line = file.gets
everything << line
end
chinese_only = everything.gsub(/[\*()%:<>\-\/#\.\+\{\}!\[\]\s\w]+/, '')
chinese_array = chinese_only.scan(/./)
chars = chinese_array.uniq.join #=> string
charcodes = chinese_array.uniq.join
end
File.open("charsfile.txt", "w") do |file|
file.puts chars
end
File.open("subsetter", "w") do |file|
#charcodes.each_codepoint { |c| file.print(c.to_s, ' ') }
file.puts "#!/usr/bin/env fontforge"
file.puts "Open($1);"
file.puts "SelectAll();"
charcodes.each_codepoint { |c| file.print('SelectFewer(0u', c.to_s(16), ");\n") }
file.puts "DetachAndRemoveGlyphs();"
file.puts "Save($2);"
file.puts "Quit(0);"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment