def self.array_from_tokens(tokens)
array = []
styles = []
colors = []
link = nil
anchor = nil
fonts = []
sizes = []
character_spacings = []
while token = tokens.shift
case token
when "<b>", "<strong>"
styles << :bold
when "<i>", "<em>"
styles << :italic
when "<u>"
styles << :underline
when "<strikethrough>"
styles << :strikethrough
when "<sub>"
styles << :subscript
when "<sup>"
styles << :superscript
when "</b>", "</strong>"
styles.delete(:bold)
when "</i>", "</em>"
styles.delete(:italic)
when "</u>"
styles.delete(:underline)
when "</strikethrough>"
styles.delete(:strikethrough)
when "</sub>"
styles.delete(:subscript)
when "</sup>"
styles.delete(:superscript)
when "</link>", "</a>"
link = nil
anchor = nil
when "</color>"
colors.pop
when "</font>"
fonts.pop
sizes.pop
character_spacings.pop
else
if token =~ /^<link[^>]*>$/ or token =~ /^<a[^>]*>$/
matches = /href="([^"]*)"/.match(token) || /href='([^']*)'/.match(token)
link = matches[1] unless matches.nil?
matches = /anchor="([^"]*)"/.match(token) || /anchor='([^']*)'/.match(token)
anchor = matches[1] unless matches.nil?
elsif token =~ /^<color[^>]*>$/
matches = /rgb="#?([^"]*)"/.match(token) || /rgb='#?([^']*)'/.match(token)
colors << matches[1] if matches
matches = /c="#?([^"]*)" +m="#?([^"]*)" +y="#?([^"]*)" +k="#?([^"]*)"/.match(token) ||
/c='#?([^']*)' +m='#?([^']*)' +y='#?([^']*)' +k='#?([^']*)'/.match(token)
colors << [matches[1].to_i, matches[2].to_i, matches[3].to_i, matches[4].to_i] if matches
elsif token =~ /^<font[^>]*>$/
matches = /name="([^"]*)"/.match(token) || /name='([^']*)'/.match(token)
fonts << matches[1] unless matches.nil?
matches = /size="([^"]*)"/.match(token) || /size='([^']*)'/.match(token)
sizes << matches[1].to_f unless matches.nil?
matches = /character_spacing="([^"]*)"/.match(token) || /character_spacing='([^']*)'/.match(token)
character_spacings << matches[1].to_f unless matches.nil?
else
string = token.gsub("<", "<").gsub(">", ">").gsub("&", "&")
array << { :text => string,
:styles => styles.dup,
:color => colors.last,
:link => link,
:anchor => anchor,
:font => fonts.last,
:size => sizes.last,
:character_spacing => character_spacings.last }
end
end
end
array
end