This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

IRC Control Codes -> HTML

Old posts that have not been replied to for several years.
Locked
F
FreakyComputer
Voice
Posts: 22
Joined: Fri May 10, 2002 8:00 pm
Location: South Africa

IRC Control Codes -> HTML

Post by FreakyComputer »

Hello everyone :)

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.

Thanks :)
FC
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Please search the forum, as code for this has been discussed within the last month.
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

written by stdragon :

Code: Select all

#
# 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
}
F
FreakyComputer
Voice
Posts: 22
Joined: Fri May 10, 2002 8:00 pm
Location: South Africa

Post by FreakyComputer »

Thank you for your replies :)

I'm using that script as I type this, and I got it show backgrounds too!! :)

See it in action: http://lobby24.knightnet.net and click #Lobby24 Live :lol:

Thanks again,
FC
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

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)

Code: Select all

# mIRC control codes to html (css styles)
# (c) user - #tcl@undernet 2003.03.18

#- Colors like in mirc (0-99)
#- Reverse overrides color but does not reset it
#- No invalid nesting of html tags (which means alot more tags for complicated stuff :/)

proc mirc2html s {
  array set cl {0 #FFF 1 #000 2 #00007F 3 #009300 4 #F00 5 #7F0000 6 #9C009C 7 #FC7F00 8 #FF0 9 #0F0 10 #009393 11 #0FF 12 #0000FC 13 #F0F 14 #7F7F7F 15 #D2D2D2}
  foreach {e o t x(\002) x(\003) x(\026) x(\037) fg bg} [list [string length $s] "" 0 0 0 0 0 "" ""] break
  for {set i 0} {$i<$e} {incr i} {
    switch -- [set c [string index $s $i]] {
      "\002" - "\026" - "\037" {set x($c) [expr {!$x($c)}]}
      "\003" {
        if {[set x($c) [regexp {^[0-9]{1,2}(,[0-9]{1,2})?} [string range $s [incr i] [incr i 5]] fg]]} {
          incr i -[expr {6-[string length $fg]}]
          foreach {fg bg} [split "$fg,$bg" ,] {
            set fg [format %g $fg]
            if {[string length $bg]>0} {set bg [format %g $bg]}
            break
          }
        } else {
          incr i -6; set bg ""
        }
      }
      "\017" {foreach {x(\002) x(\003) x(\026) x(\037) bg} {0 0 0 0 ""} break}
      default {append o $c; continue}
    }
    if {$t} {append o "</span>"} {set t 1}
    set l {}
    if {$x(\002)} {lappend l "font-weight: bold"}
    if {$x(\037)} {lappend l "text-decoration: underline"}
    if {$x(\026)} {
      lappend l "color: $cl(0); background-color: $cl(1)"
    } else {
      if {$x(\003)} {
        if {$fg==99} {lappend l "color: $cl(1)"} else {lappend l "color: $cl([expr {$fg%16}])"}
        if {[string length $bg]>0} {
          if {$bg==99} {
            lappend l "background-color: transparent"
          } else {
            lappend l "background-color: $cl([expr {$bg%16}])"
          }
        }
      }
    }
    if {[llength $l]>0} {append o "<span style=\"[join $l "; "]\">"} {set t 0}
  }
  if {$t} {append o "</span>"}
  regsub -all {<[^>]+></[^>]+>} $o {} o
  set o
}
EDIT: added the regsub at the end
Locked