Skip to content

Instantly share code, notes, and snippets.

@elizabrock
Created May 8, 2010 19:21
Show Gist options
  • Save elizabrock/394722 to your computer and use it in GitHub Desktop.
Save elizabrock/394722 to your computer and use it in GitHub Desktop.
def starts_listing(line)
line.include? "\\begin{lstlisting}"
end
def ends_listing(line)
line.include? "\\end{lstlisting}"
end
def process_file_line(unique_words, line)
unique_words.each do |verb_word|
regex_contents = verb_word.gsub('$','\$').gsub('?','\?').gsub('.','\.').gsub('(','\(').gsub(')','\)').gsub('+','\+').gsub('{','\{').gsub('}','\}').gsub('[','\[').gsub(']','\]').gsub('\\','\\\\')
regex = /\s#{regex_contents}\s/
#puts "Testing regex \/\\s#{regex_contents}\/"
if line.match(regex)
puts "**Line matched [#{verb_word}]"
updated_line = line.gsub(regex, " \\verb|#{verb_word}| ")
puts "<<<Original"
puts line
puts "==========="
puts updated_line
puts ">>>Updated"
STDOUT.flush
case get_input
when 'k', 'K'
line = updated_line
when 's', 'S'
when 'i', 'I'
unique_words.delete(verb_word)
when 'l', 'L'
puts "***skipping line"
return line
when 'v', 'V'
puts "***opening in vim"
File.open('unique_verbs.tmp', 'w') {|f| f.write(line) }
success = system('vim','unique_verbs.tmp')
line = File.open('unique_verbs.tmp', 'r').readlines.join("\n")
puts "***Updated line to: #{line}"
else
puts "***input not matched"
end
end
end
line
end
def process_file_lines(inlines)
matching_lines = `grep "\\verb[\|\!][^\|\!]*[\|\!]" ../chapters/*.tex`
matching_words = matching_lines.scan(/\\verb[|!]([^|!]*)[|!]/)
unique_words = matching_words.flatten.sort.uniq - [""," ","\\n","b","n","'","it","on","s","y","#","-","- ","1","[","]","-","and","for","its","up","the","with","Rails","has","else","get"]
outlines = []
while(inlines.count > 0)
line = inlines.shift
#skip listing blocks
if starts_listing(line)
until ends_listing(line)
outlines << line
line = inlines.shift
end
outlines << line
line = inlines.shift
end
#check for unverbed verbwords
outlines << process_file_line(unique_words, line)
end
outlines
end
def get_input
instructions = "Keep change, Skip this change, skip this Line, Ignore this word in the future, or edit in Vim"
puts instructions
input = gets.chomp
until input.match(/[kKsSiIlLvV]/)
puts "I didn't understand \"#{input}\". #{instructions}"
input = gets.chomp
end
input
end
Dir.glob("../chapters/*.tex") do |filename_with_extension|
puts "*Scanning #{filename_with_extension}"
#http://pleac.sourceforge.net/pleac_ruby/fileaccess.html
File.open("../chapters/#{filename_with_extension}", 'r+') do |f| # open file for update
inlines = f.readlines # read into array of lines
outlines = process_file_lines(inlines)
f.pos = 0 # back to start
f.print outlines # write out modified lines
f.truncate(f.pos) # truncate to new length
end # file is automatically closed
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment