A
Aditya Mahajan
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
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