I submitted a previous
post on re-writing and copying HTML files with Ruby. I
could not let that rather large and unruly script go as is and had to refactor, a.k.a. extract, much of the script code out into a ruby class. Here is the ruby class:
class ParsingFile
def createTop(value)
topString = ""
topString.concat("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n")
topString.concat("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n")
topString.concat("<head>\n")
topString.concat("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n")
topString.concat("<title>Bible Reading Guide</title>\n\n")
topString.concat("<link href=\"dailyReading.css\" rel=\"stylesheet\" type=\"text/css\">\n")
topString.concat("</head>\n\n")
topString.concat("<body>\n\n")
topString.concat("<table>\n")
topString.concat("<tr>\n")
topString.concat("<td class=\"titleRowCell\"> #{value} </td>\n")
topString.concat("<td class=\"titleRowVerse\"> Passage </td>\n")
topString.concat("</tr>\n")
return topString
end
def createContent(line, lineNum, parseString)
htmlLine = ""
if lineNum.modulo(2) == 0
htmlLine.concat("<tr class=\"alternateRow\">")
else
htmlLine.concat("<tr>")
end
htmlLine.concat("<td class=\"dayCell\">Day #{lineNum}</td>")
htmlLine.concat("<td class=\"verseCell\">")
if line.scan(";").length > 0
#split the line, get the 2 seperate verses, then form the html
verseArray = line.split(";")
verseArray.each {|seperateVerse|
p = seperateVerse.index(parseString)
p = p + parseString.length
i = seperateVerse.index("')")
e = i - p
verse = seperateVerse.slice(p, e)
htmlLine.concat("<a href=\"http://bible.gospelcom.net/bible?version=ESV&passage=#{verse}\" target=\"_blank\">")
htmlLine.concat("#{verse.gsub("+", " ")}</a>")
htmlLine.concat(" ; ") if seperateVerse != verseArray.last
}
htmlLine.concat("</td>")
else
p = line.index(parseString)
p = p + parseString.length
i = line.index("')")
e = i - p
verse = line.slice(p, e)
htmlLine.concat("<a href=\"http://bible.gospelcom.net/bible?version=ESV&passage=#{verse}\" target=\"_blank\">")
htmlLine.concat("#{verse.gsub("+", " ")}</a></td>")
end
htmlLine.concat("</tr>\n")
return htmlLine
end
def createBottom
bottomString = ""
bottomString.concat("<tr>\n")
bottomString.concat("<td class=\"titleRowCell\"> </td>\n")
bottomString.concat("<td class=\"titleRowVerse\"> </td>\n")
bottomString.concat("</tr>\n")
bottomString.concat("</table>\n")
bottomString.concat("</body>\n")
bottomString.concat("</html>\n")
return bottomString
end
end
Here is the refactored script that is much cleaner and, in my view, easier to follow.
origindir = "C:/pathToFile/"
fileNames = {
"1" => "January",
"2" => "February",
"3" => "March",
"4" => "April",
"5" => "May",
"6" => "June",
"7" => "July",
"8" => "August",
"9" => "September",
"10" => "October",
"11" => "November",
"12" => "December"
}
fileNames.each {|key, value|
f = File.new("#{origindir}#{key}.txt", "w+")
aFile = File.new("#{key}.html")
#make sure you have the ParsingFile class
require 'ParsingFile.rb'
#instantiate the ParsingFile object
pf = ParsingFile.new()
#start of the html file markup
#call createTop here
f.write(pf.createTop(value))
lineNum = 1
s = "passage="
aFile.each_line {|line|
if line.scan(/#{s}/).length > 0
f.write(pf.createContent(line, lineNum, s))
lineNum += 1
end
}
#end of the html file markup
f.write(pf.createBottom())
}