Skip to content

Instantly share code, notes, and snippets.

@tarot
Created February 1, 2016 15:28
Show Gist options
  • Save tarot/213c3d454606a68b9a36 to your computer and use it in GitHub Desktop.
Save tarot/213c3d454606a68b9a36 to your computer and use it in GitHub Desktop.
ridgepoleのSchemafileのカラムをソートするやつ(テーブルはソートされてる)
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