Mechatronics Design (MCTX 3420) Lecure Listing Script (in Ruby)
Makes Main Lectures List with references and link easily from Table (in Excel)
Please click here to download the script & related files
X

#!/usr/bin/env ruby

##############################################
# Mechatronics Design (MCTX 3420) Lecure Page HTML Parser
#
# Written in the spirit of a "mail-merge" script.  This function fills a
#    template (TEMPLATE.html) using a text databse specified
#    in the dbFile variable (lecureDB.txt) and of a fixed format
#    detailed in the dbFile header.  An Excel file (MD_Lectures.xls) is used to
#    generate the lecures and links based on a "simple" table containing the data
#    that is an extension of the Lecture Schedule.
#
# Format:  Really, you should use the Excel file for this.
#    Each line of the lectureDB.txt file becomes a class page.  
#    The three sections (Notes, Links, and Recordings) are separted by "|||",
#     bullet items within each list are delinted by a "###", and
#     variables are separed by ";".  So
#     Class number; PREABLE ||| NOTES AND HANDOUTS  ||| LINKS ||| RECORDINGS ||| TUTES
#     With PREABLE : number; month; day; title.
#     The rest as http://alink/note1.html;note1 for the link
#    
# Written by:
#    Surya Singh (spns@mech) with checking by Marcin Wolski  
#    July 2007 (with minor updates over the semester)
##############################################

##############################################
# ----> USER Defined Constants <----
#  Change to fit the specific case.
##############################################
# Template with codes (#VARIABLE) into which text will be merged INTO
template='TEMPLATE.html'  
# Template for index page which titles will be merged INTO
templateIndex='INDEX.html'
# Folder to write files into (index.html + lecture files
outDir='Lectures'
# Database text file with all the data defined
dbFile='lectureDB.txt'

##############################################
# SUBROUTINE:  getBullets
# Bullet list processing (the number of items in the list can vary)
##############################################
def getBullets col
  col2=col.split('###')
  htmlNotes=''
  col2.each {|note|
    link= note.split(';')[0]
    title= note.split(';')[1]
    if link.length>0
      htmlNotes=htmlNotes+'<li><a href="'+link+'">'+title+'</a></li>'
    else
      htmlNotes=htmlNotes+'<li>'+title+'</li>'
    end
  }
  htmlNotes
end

##############################################
# MAIN CODE
##############################################

# INITIALIZATION
Dir.mkdir outDir if !File.exists? outDir

# FILE OPENING AND READING
tempA= Array.new
tempAindex= Array.new
dbA=Array.new
open(template){ |f| tempA=f.readlines }
open(templateIndex){ |f| tempAindex=f.readlines }
open(dbFile).readlines.each { |f| dbA<<f if f !~ /^\#/  }

# DATABASE PARSING AND MERGING
tempStr=tempA.to_s
tempStrIndex=tempAindex.to_s
i=0;
indexMenu=''
dbA.each {|line|
  i=i+1;
  puts i
  columns=line.split('|||')
  puts 'Field Length Processed OK: '+(columns.length).to_s
  col1=columns[0].split(';')
  lecnr=col1[0]
  month=col1[1]
  day=col1[2]
  lectitle=col1[3]
  #NOTES AND HANDOUTS
  htmlNotes=getBullets columns[1]
 #LINKS
  htmlLinks=getBullets columns[2]
 #RECORDING
  htmlRec=getBullets columns[3]
 #TUTORIAL NAME
   htmlT=columns[4]
   htmlTu=';<b>Tutorial: </b>'+htmlT.split(';')[1]
   htmlTute=getBullets  htmlTu

# FILLING IN FOR TEMPLATE KEYWORDS
 outPage= tempStr.sub(/\#LECNR/,lecnr).sub(/\#MONTH/,month).sub(/\#DAY/,day).sub(/\#TITLE/,lectitle)
 outPage.sub!(/\#NOTES/,htmlNotes)
 outPage.sub!(/\#LINKS/,htmlLinks)
 outPage.sub!(/\#RECORDING/,htmlRec)
 outFile="lect#{i}.html"
 fullPathOutFile=outDir+"/"+outFile
 open(fullPathOutFile, 'w') { |f| f<<outPage  }
 
 #BUILD INDEX
 indexMenu+='<li>'
 puts outFile.to_s+' ... procesed okay!'
 indexMenu+='Lecture '+lecnr.to_s+': '+day.to_s+' '+month.to_s+' <a href="'+outFile+'">'+lectitle+'</a>'
         # INDEX Sub-menu is NOTES & HANDOUTS:
         #indexMenu+='<ul>'+htmlNotes+'</ul>' #ADD NOTES TO EACH LECTURE
         # INDEX Sub-menu is Tutorials
         indexMenu+='<ul>'+htmlTute+'</ul>'+'<br>' #ADD TUTORIAL text TO EACH LECTURE
 indexMenu+='</li>'
 
}

# SAVE INDEX
tempStrIndex.sub!(/\#INDEX/,indexMenu)
open(outDir+'/index.html', 'w') { |f| f<<tempStrIndex  }

# OUTPUT TEXT ON PROGRAM END AND WAIT FOR USER TO ACKNOWLEDGE
puts "thank you"
gets

# -- THE END --