Created
February 1, 2016 15:28
-
-
Save tarot/213c3d454606a68b9a36 to your computer and use it in GitHub Desktop.
ridgepoleのSchemafileのカラムをソートするやつ(テーブルはソートされてる)
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
module SchemafileNormalizer | |
Table = Struct.new(:facing, :columns) | |
def self.normalize!(filename) | |
src = File.open(filename) { |io| io.read } | |
File.open(filename, 'w') { |io| io.write(sort_column(src)) } | |
end | |
def self.sort_column(src) | |
src.each_line.with_object([Table.new([], [])]) { |line, a| | |
a << Table.new([], []) if line.match(/\Aend/) | |
if line.match(/\A\s+t\./) | |
a.last.columns << line | |
else | |
a.last.facing << line | |
end | |
}.map { |e| | |
e.facing + e.columns.sort_by{ |e| e.sub(/\A[^"]+"([^"]+)".*\Z/m, '\1')} | |
}.flatten.join('') | |
end | |
end | |
SchemafileNormalizer.normalize!('Schemafile') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment