#!/usr/bin/env ruby ############################################################################ # # convert.rb # # Script to convert rfc2828 text file into html for easier term lookup # # 08 Dec 2005, Brian Ardrey (bardrey@uccs.edu) # ############################################################################ $progname = $0.split(/[\/\\]/)[-1] # add links to references in a line def link_ref(line) line.gsub(/\[([\w\s,]+)\]/) { |ref| replace = '[' refs = $1.split(/,/) refs.each { |r| r.strip! r_link = r.sub(/\s+Part\s+\d+/, '') replace += ', ' if replace.length > 1 replace += '%s' % [r_link, r] } replace += ']' replace } end # add links to see also information in a line def link_see(line) # There are various formats for this information... # See: "confinement property" under Bell-LaPadula Model. # See: cryptographic card, FORTEZZA card, payment card, PC card, smart card, token. # See: access control center. # See: Authentication Header # (See: Basic Encoding Rules.) # See: (secondary definition under) attack. # (See: ("relationship between data integrity service and authentication services" under) data integrity service.) # (E.g., see: TCSEC.) # (e.g., see: RSA) end def convert_3(lines) definitions = Array.new index = Array.new while line = lines.shift terms = Array.new abrvs = Array.new defns = Array.new matched_term = false while line =~ /\s{3}\$\s(.*)$/ entry = $1 term = '' entry.gsub!(/\([a-z][^\)]*\)/, '') while entry != '' and entry =~ /([^\(]+)(?:\(([^\)]+)\))?(.*)$/ term += $1 abrv = $2 entry = $3 unless abrv.nil? abrv.split(/,/).each { |a| abrvs.push(a.gsub(/\"/, '').strip.gsub(/\s+/, ' ')) } end end term.gsub!(/\"/, '') term.strip! term.gsub!(/\s+/, ' ') terms.push(term) matched_term = true line = lines.shift end # put line back on array if it wasn't a term and previous one was # lines.unshift(line) if matched_term; # line = lines.shift while line =~ /^\s{6,7}(?:\d+\.\s*)?\([CDINO]\)/ or line =~ /^\s{6}See:/ defn_line = line while line = lines.shift if line =~ /^\s{3}\$\s/ lines.unshift(line) break elsif line =~ /^\s{6,7}(?:\d+\.\s*)?\([CDINO]\)/ break else defn_line += ' ' + line end end defn_line.strip! defn_line.gsub!(/\s+/, ' ') defn_line = link_ref(defn_line) defns.push(defn_line) end if terms.length > 0 definitions.push([terms, abrvs, defns]) terms.each { |t| index.push(t) } abrvs.each { |a| index.push(a) } end end letters = (index.map { |i| letter = i[0,1].upcase }).sort.uniq fname1 = 'definitions.html' begin fp1 = File.open(fname1, 'w') fp1.puts '' fp1.puts '' fp1.puts 'Internet Security Glossary - Definitions' fp1.puts '' fp1.puts '' fp1.puts '' fp1.puts '

Internet Security Glossary - Definitions

' fp1.puts((letters.map { |l| '%s' % [l, l] }).join('   ')) #fp.1puts((index.map { |i| # letter = i[0,1].upcase # '%s' % [letter, letter] # }).sort.uniq.join('   ')) fp1.puts '
' last_char = nil (definitions.sort { |foo, bar| foo[0][0].upcase <=> bar[0][0].upcase }).each { |defn| if defn[0][0][0,1].upcase != last_char last_char = defn[0][0][0,1].upcase fp1.puts '
' % [last_char] end fp1.puts '
' (defn[0] + defn[1]).each { |d| fp1.puts '

%s

' % [d, d] } defn[2].each { |d| fp1.puts '

%s

' % [d] } fp1.puts '
' } fp1.puts '' fp1.puts '' rescue STDERR.puts "#{$progname}: #{fname1}: #{$!}" ensure fp1.close unless fp1.nil? end # where the hell are these two lines from? #definitions[0][0][0][0,1].upcase #fp.puts '
' % [last_char] fname2 = 'term_index.html' begin fp2 = File.open(fname2, 'w') fp2.puts '' fp2.puts '' fp2.puts 'Internet Security Glossary - Index' fp2.puts '' fp2.puts '' fp2.puts '' fp2.puts '

Internet Security Glossary - Index

' fp2.puts '
' fp2.puts '' fp2.puts '' fp2.puts '' fp2.puts '' rescue STDERR.puts "#{$progname}: #{fname2}: #{$!}" ensure fp2.close unless fp2.nil? end letters end def convert_4(lines) refs = Hash.new while line = lines.shift if line =~ /^\s{3}\[([\w\s]+)\]/ r = $1 ref_line = line.sub(/\[[\w\s]+\]/, '') while line = lines.shift if line =~ /^\s{3}\[[\w\s]+\]/ lines.unshift(line) break else ref_line += ' ' + line end end ref_line.strip! ref_line.gsub!(/\s+/, ' ') refs[r] = ref_line end end if false and refs.length > 0 refs.keys.sort.each { |k| puts "[#{k}] = #{refs[k]}"} end fname = 'references.html' begin fp = File.open(fname, 'w') fp.puts '' fp.puts '' fp.puts 'Internet Security Glossary - References' fp.puts '' fp.puts '' fp.puts '' fp.puts '

Internet Security Glossary - References

' fp.puts '
' refs.keys.sort.each { |k| fp.puts '
' % [k] fp.puts '[%s] %s' % [k, refs[k]] fp.puts '
' } fp.puts '' fp.puts '' rescue STDERR.puts "#{$progname}: #{fname}: #{$!}" ensure fp.close unless fp.nil? end end def convert(fp) lines = Array.new fp.each { |line| lines.push(line) } lines = lines.reject { |line| line =~ /^\s+$/ or line =~ /\s+\[Page \d+\]$/ or line =~ /^RFC\s\d+\s+/ } section = 0 sections = Array.new while ln1 = lines.shift sections[section] = Array.new if sections[section].nil? sections[section].push(ln1) while ln2 = lines.shift if ln2 =~ /^(\d+)\./ section = $1.to_i lines.unshift(ln2) break else sections[section].push(ln2) end end end if false sections.each_with_index { |s, i| puts "-*-*- Section: #{i} -*-*-" if false s.each { |line| puts line } end } end letters = convert_3(sections[3]) convert_4(sections[4]) fname = 'index.html' begin fp = File.open(fname, 'w') fp.puts '' fp.puts '' fp.puts 'Internet Security Glossary (RFC 2828)' fp.puts '' fp.puts '' fp.puts '' fp.puts '

Internet Security Glossary (RFC 2828)

' fp.puts '
' fp.puts '
    ' fp.puts '
  1. ' fp.puts ' Definitions:  ' + (letters.map { |l| '%s' % [l, l] }).join('  ') fp.puts '
  2. ' fp.puts '
  3. References
  4. ' fp.puts '
  5. Index
  6. ' fp.puts '
' fp.puts '' fp.puts '' rescue STDERR.puts "#{$progname}: #{fname}: #{$!}" ensure fp.close unless fp.nil? end end if (ARGV.length < 1) STDERR.puts "usage: #{$progname} " exit 1 end filename = ARGV.shift begin fp = File.open(filename, 'r') convert(fp) rescue STDERR.puts "#{$progname}: #{filename}: #{$!}" ensure fp.close unless fp.nil? end