Skip to content

Instantly share code, notes, and snippets.

@komiyake
Last active May 9, 2016 01:23
Show Gist options
  • Save komiyake/ef9f1be6ad4a7a991637 to your computer and use it in GitHub Desktop.
Save komiyake/ef9f1be6ad4a7a991637 to your computer and use it in GitHub Desktop.
ディレクトリ内に参照されていないtexファイルがないか調べる
# /usr/bin/ruby
class Finder
attr_reader :source_file_name
def initialize
source_file = ARGV[0]
unless source_file
warn "usage: ruby reference.rb SOURCE_FILE"
exit 1
end
@source_file_path = File.expand_path(source_file)
end
def get_unreferenced_files
files = get_tex_files(File::dirname(@source_file_path))
referenced_files = get_referenced_files(@source_file_path)
files.each do |file| file.downcase! end
referenced_files.each do |file| file.downcase! end
@source_file_path.downcase!
files - referenced_files - [@source_file_path]
end
def get_referenced_files(file_name)
result = Array.new
File.open(file_name).each do |line|
path = make_path(line)
result = result | get_referenced_files(path) | [path] if not path == nil
end
result
end
def make_path(line)
begin
if /\.tex/ =~ line and line[0] != "%"
path = line.scan(/{([\s]*[^\{\}]*\.tex)[\s]*}/)[0][0]
end
path = make_absolute_path(path)
path if FileTest::exist?(path)
rescue
nil
end
end
def make_absolute_path(path)
path = path[2..path.length()] if path =~ /^.\//
File::dirname(@source_file_path) + "/" + path
end
def get_tex_files(path)
result = Array.new
Dir::foreach(path) do |file_name|
next if file_name == "." or file_name == ".."
if file_name =~ /.tex$/
if path =~ /\/$/
result << path + file_name
else
result << path + "/" + file_name
end
end
if FileTest::directory?(path+"/"+file_name)
result = result | get_tex_files(path+"/"+file_name)
end
end
result
end
end
finder = Finder.new
puts finder.get_unreferenced_files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment