I'd like to know, is there a script that can convert IRC Control Codes (ie. colour, bold, underline, reverse/italic) to normal HTML code? If not, can someone please help me in the correct direction to do this, as you can't just replace all BOLD chars with <b> since HTML needs a closing tag too.
#
# irc_to_html.tcl -- by stdragon
#
set html_color(00) "white"
set html_color(01) "black"
set html_color(02) "blue"
set html_color(03) "green"
set html_color(04) "lightred"
set html_color(05) "brown"
set html_color(06) "purple"
set html_color(07) "orange"
set html_color(08) "yellow"
set html_color(09) "lightgreen"
set html_color(10) "cyan"
set html_color(11) "lightcyan"
set html_color(12) "lightblue"
set html_color(13) "pink"
set html_color(14) "grey"
set html_color(15) "lightgrey"
proc irc_to_html {text} {
global html_color
set chars [split $text ""]
set len [llength $chars]
set output ""
set in_bold 0
set in_color 0
set in_uline 0
for {set i 0} {$i < $len} {incr i} {
switch -exact [lindex $chars $i] {
"\002" {
if {$in_bold} {
append output "</b>"
set in_bold 0
} else {
append output "<b>"
set in_bold 1
}
}
"\003" {
incr i
set c [lindex $chars $i]
if {$i < $len && [string is integer $c]} {
incr i
set d [lindex $chars $i]
if {$i < $len && [string is integer $d]} {
incr i
set num "$c$d"
} else {
set num "0$c"
}
if {$in_color} { append output "</font>" }
append output "<font color="
if {[info exists html_color($num)]} {
append output $html_color($num)
} else {
append output "black"
}
append output ">"
set in_color 1
# Skip past background color if it's there.
set c [lindex $chars $i]
if {$i < $len && $c == ","} {
incr i
set c [lindex $chars $i]
if {$i < $len && [string is integer $c]} {
incr i
set c [lindex $chars $i]
if {![string is integer $c]} { incr i -1 }
} else { incr i -2 }
} else { incr i -1 }
} else {
if {$in_color} { append output "</font>" }
set in_color 0
incr i -1
}
}
"\015" {
if {$in_bold} { append output "</b>" }
if {$in_color} { append output "</font>" }
if {$in_uline} { append output "</u>" }
set in_bold 0
set in_color 0
set in_uline 0
}
"\031" {
if {$in_uline} {
append output "</u>"
set in_uline 0
} else {
append output "<u>"
set in_uline 1
}
}
default {
append output [lindex $chars $i]
}
}
}
if {$in_bold} { append output "</b>" }
if {$in_color} { append output "</font>" }
if {$in_uline} { append output "</u>" }
return $output
}
If you feel like trying something different...
This code's not much tested but should deal with every possible combination of control codes in the same way as mirc does.
Feel free to flame me on irc if it doesn't work like it's supposed to
(sorry for the long lines)