Skip to content

Instantly share code, notes, and snippets.

@nocash
Created May 18, 2017 20:36
Show Gist options
  • Save nocash/b02bf4bb0181cc88c82bb95a6ac12191 to your computer and use it in GitHub Desktop.
Save nocash/b02bf4bb0181cc88c82bb95a6ac12191 to your computer and use it in GitHub Desktop.
A script to convert rem values in files from one base font size to another. Works for me but ymmv.
#!/usr/bin/env ruby
require 'fileutils'
REM_PATTERN = /([\d.-]+)rem\b/
current_size, target_size, *file_paths = ARGV
def trim_num(num)
num.to_f == num.to_i ?
num.to_i :
num.round(2)
end
def convert_rems(size, from_base, to_base)
(size * from_base) / to_base
end
def with_files(paths)
paths.each do |path|
path = File.absolute_path(path)
temp_path = "#{path}.temp"
File.open(path, 'r') do |file|
File.open(temp_path, 'w') do |temp_file|
yield file, temp_file
end
end
FileUtils.move(temp_path, path, force: true)
end
end
with_files(file_paths) do |old_file, new_file|
old_file.each_line do |line|
new_file << line.gsub(REM_PATTERN) { |match|
rem_size = match.to_f
new_size = convert_rems(rem_size, current_size.to_f, target_size.to_f)
new_size = trim_num(new_size)
"#{new_size}rem"
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment