Aditya Mahajan wrote: > I am trying to convert a plain text file into a tex file with > somemarkup. I have the following piece of code that does most of the > work. It works fine but does not "look good". Can someone suggest how > can I improve this code. > > ---------------[snip]---------------------- > file = File.new(filename, 'r') > texfile = File.open(basename + ".tex", 'w') > > CHAPTER = Regexp.new("CHAPTER") > SPACES = Regexp.new("\s\s\s\s") > BLANK = Regexp.new(/^\s*$/) > > chapter = true > verse = false > prev_line = "" > > file.each_line do |line| > if chapter && !BLANK.match(line) > chapter = false > texfile.puts "\\chapter{#{line.chomp}}" > elsif CHAPTER.match(line) > chapter = true > elsif verse && !SPACES.match(line) > texfile.puts '\stoplines\stopnarrower' > texfile.puts line.chomp > verse = false > elsif !verse && BLANK.match(prev_line) && SPACES.match(line) > texfile.puts '\startnarrower\startlines' > texfile.puts line.chomp > verse = true > else > texfile.puts line.chomp > end > prev_line = line > end > ------------------[snip]-------------------- > > Thanks, > Aditya > > -- > Aditya Mahajan, EECS Systems, University of Michigan > http://www.eecs.umich.edu/~adityam || Ph: 7342624008 ruby -p01e'gsub(/(\n\s*\n)((?:^\s{4}.*\n)+)/, "\\1\\startnarrower\\startlines\n\\2\\stoplines\\stopnarrower\n"); gsub(/(CHAPTER\s+)(.*)\n/,"\\chapter{\\2}\n")' in >out