Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Somewhat ridiculous XML parser + image generator

Seriously needs caching. SRSLY

<?php
	class ImageGeneration {
		function getInfo($name) {
			$this->loadDefaultInfo();
			$XML = new XMLReader;
			$XML->open("http://chat.hypermutt.net/online/user/".$name.".xml");
			$wn = array();

			while($XML->read()) {
				if ($XML->nodeType == XMLReader::ELEMENT) {
					$wn[] = $XML->name;
				}
				if ($XML->nodeType == XMLReader::TEXT) {
					if ("user/info/nick" == implode('/', $wn)) {
						$this->name = $XML->value . ". ";
					}
					if ("user/info/online" == implode('/', $wn)) {
						$this->away = ($XML->value == "TRUE") ? true : false;
					}


					if ("user/info/connected" == implode('/', $wn)) {
						$this->onlinefor = strtotime($XML->value);
					}
					if ("user/info/timenow" == implode('/', $wn)) {
						$sc = (strtotime($XML->value) - $this->onlinefor);
						$t = floor($sc/86400);
							$c["days"] = ($t > 0) ? $t."D " : ""; $sc = ($sc-$t*86400);
						$t = floor($sc/3600);
							$c["hours"] = ($t > 0) ? $t."H " : ""; $sc = ($sc-$t*3600);
						$t = floor($sc/60);
							$c["minutes"] = ($t > 0) ? $t."M " : "";
						$this->onlinefor = $c["days"].$c["hours"].$c["minutes"];
					}
					

					if ("user/info/away" == implode('/', $wn)) {
						$this->away = ($XML->value == "TRUE") ? true : false;
					}
					if ("user/info/awaymsg" == implode('/', $wn)) {
						$this->awaymessage = $XML->value;
					}
					if ("user/channels/channel/name" == implode('/', $wn)) {
						$this->channels .= str_replace('#','',$XML->value)." ";
					}
				}
				if ($XML->nodeType == XMLReader::END_ELEMENT) {
					array_pop($wn);
				}
			}
			$XML->close();
		}
		
		function loadDefaultInfo() {
			$this->name = ". ";
			$this->online = true;
			$this->onlinefor = "";
			$this->away = true;
			$this->awaymessage = "";
			$this->channels = "";
		}
	
		function drawImage() {
			header("Content-type: image/png");
			$image = imagecreatefrompng("background.png");
				$white = imagecolorallocate($image, 255, 255, 255);
				$red   = imagecolorallocate($image, 255, 0, 0);
				$green = imagecolorallocate($image, 0, 255, 0);
				$feb = "slkscreb.ttf";
				$fnn = "slkscr.ttf";
			
			$sizes["name"]   = imagettfbbox(6, 0, $feb, $this->name);
			$sizes["online"] = imagettfbbox(6, 0, $feb, "online: ");
			$sizes["away"]   = imagettfbbox(6, 0, $feb, "away: ");
			$sizes["yes"]    = imagettfbbox(6, 0, $feb, "yes ");
			$sizes["#"]     = imagettfbbox(6, 0, $feb, "#: ");
			
			imagettftext($image, 6, 0, 22, 8, -$white, $feb, $this->name);
			imagettftext($image, 6, 0, (22 + $sizes["name"][4]), 8, -$white, $feb, "online:");
			imagettftext($image, 6, 0, (22 + $sizes["name"][4] + ($sizes["online"][4] - $sizes["away"][4])), 18, -$white, $feb, "away:");
			imagettftext($image, 6, 0, (22 + $sizes["name"][4] + ($sizes["online"][4] - $sizes["#"][4])),    28, -$white, $feb, "#: ");
			
			imagettftext($image, 6, 0, (22 + $sizes["name"][4] + $sizes["online"][4]),  8, -(($this->online) ? $green : $red), $feb, (($this->online) ? "yes" : "no"));
			imagettftext($image, 6, 0, (22 + $sizes["name"][4] + $sizes["online"][4]), 18, -((!$this->away)  ? $green : $red), $feb, (($this->away)   ? "yes" : "no"));
			
			if ($this->online)
				imagettftext($image, 6, 0, (22 + $sizes["name"][4] + $sizes["online"][4] + $sizes["yes"][4]),  8, $white, $fnn, $this->onlinefor);
			if ($this->away)
				imagettftext($image, 6, 0, (22 + $sizes["name"][4] + $sizes["online"][4] + $sizes["yes"][4]), 18, $white, $fnn, $this->awaymessage);
			imagettftext($image, 6, 0, (22 + $sizes["name"][4] + $sizes["online"][4]), 28, $white, $fnn, $this->channels);
			
			imagepng($image);
			imagedestroy($image);
		}
	}
	
	$x = new ImageGeneration;
	$x->getInfo($_GET['name']);
	$x->drawImage();
?>

Stupid Regex Parser

// Regex Code (lol)

  def parseForVoolish(voolishContent)
    rExps = {}
    rExps[:isVool] = /<<(.+)>>/
    rExps[:voolType] = /<<([a-zA-Z]+) (.+)\s*>>/
    rExps[:typeless] = /<< (.+)\s*>>/
    rExps[:splitQuote] = /\s*---\s*/
    rExps[:splitIRC] = /\s*<<\s*/
    parsedContent = {}

    if voolishContent =~ rExps[:isVool]
      parsedContent[:type] = 'Voolish'
      if voolishContent =~ rExps[:voolType]
        parsedContent[:type] = "#$1"
        tempContent = "#$2"
      else
        parsedContent[:type] = 'link'
        voolishContent =~ rExps[:typeless]
        tempContent = "#$1"
      end
    else
      parsedContent[:type] = 'thought'
      tempContent = voolishContent
    end
    
    if parsedContent[:type] == 'link'
      tempContent = tempContent.split(/\s*<<\s*/)
      if tempContent.length == 1
        parsedContent[:content] = tempContent[0]
      elsif tempContent.length == 2
        parsedContent[:content] = '<a href="' + tempContent[0]  + '">' + tempContent[1] +  '</a>'
      elsif tempContent.length == 3 # Fix (link_to_remote is a function - parameters?
        parsedContent[:content] = '<a href="' + tempContent[0]  + '">' + tempContent[1] +  '</a> ' + tempContent[2]
      end
    end
    
    if parsedContent[:type] == 'thought'
      parsedContent[:content] = tempContent
    end
    
    if parsedContent[:type] == 'tip'
      parsedContent[:content] = tempContent
    end

    if parsedContent[:type] == 'quote'
      tempContent = tempContent.split(rExps[:splitQuote])
      parsedContent[:content] = tempContent[0] , content_tag("small", [" &#8212; ", tempContent[1]])
    end

    if parsedContent[:type] == 'irc'
      tempContent = tempContent.split(rExps[:splitQuote])
      ircContent = tempContent[0].gsub(rExps[:splitIRC], '<br />')
      if tempContent.length == 1
        parsedContent[:content] = ircContent
      elsif tempContent.length == 2
        parsedContent[:content] = ircContent , content_tag("small", [" &#8212; ", tempContent[1]])
      end
    end

    return parsedContent
  end

TDZK Accesskeys

// ==UserScript==
// @name          Dreams
// @description	  Sets new TDZK accessKeys
// @include       *
// ==UserScript==

(function() {
	function $(id) { return document.getElementById(id) }
	function $$(parent, childs) { return parent.getElementsByTagName(childs) }
	
	window.addEventListener("load", function() {
		// Dock
		as = $$(document, 'a');
		for (i=0; i<as.length; i++) {
			temp = as[i].href.split("?")
			if (temp[0] == 'http://nomad.tdzk.com/station/reqDock') {
				as[i].accessKey = "x"
			}
		}
		
		// Trade goods
		if ($('tradeform')) {
			rows = $$($$($('tradeform'), 'tbody')[0], 'tr');
			for (i=0; i<rows.length; i++) {
				row = rows[i]
				accessKeyToAdd = ((i+1) == 10) ? '/' : ((i+1) == 11) ? '*' : ((i+1) == 12) ? '-' : (i+1);
				rowa = $$(row, 'a')
				if (rowa[0]) {
					rowa[0].accessKey = accessKeyToAdd
				}
			}
		}
	}, false);
})();

TDZK Accesskeys

// ==UserScript==
// @name          Dreams
// @description	  Sets new TDZK accessKeys
// @include       *
// ==UserScript==

(function() {
	function $(id) { return document.getElementById(id) }
	function $$(parent, childs) { return parent.getElementsByTagName(childs) }
	
	window.addEventListener("load", function() {
		// Dock
		as = $$(document, 'a');
		for (i=0; i<as.length; i++) {
			temp = as[i].href.split("?")
			if (temp[0] == 'http://nomad.tdzk.com/station/reqDock') {
				as[i].accessKey = "x"
			}
		}
		
		// Trade goods
		if ($('tradeform')) {
			rows = $$($$($('tradeform'), 'tbody')[0], 'tr');
			for (i=0; i<rows.length; i++) {
				row = rows[i]
				accessKeyToAdd = ((i+1) == 10) ? '/' : ((i+1) == 11) ? '*' : ((i+1) == 12) ? '-' : (i+1);
				rowa = $$(row, 'a')
				if (rowa[0]) {
					rowa[0].accessKey = accessKeyToAdd
				}
			}
		}
	}, false);
})();

TDZK Accesskeys

// ==UserScript==
// @name          Dreams
// @description	  Sets new TDZK accessKeys
// @include       *
// ==UserScript==

(function() {
	function $(id) { return document.getElementById(id) }
	function $$(parent, childs) { return parent.getElementsByTagName(childs) }
	
	window.addEventListener("load", function() {
		// Dock
		as = $$(document, 'a');
		for (i=0; i<as.length; i++) {
			temp = as[i].href.split("?")
			if (temp[0] == 'http://nomad.tdzk.com/station/reqDock') {
				as[i].accessKey = "x"
			}
		}
		
		// Trade goods
		if ($('tradeform')) {
			rows = $$($$($('tradeform'), 'tbody')[0], 'tr');
			for (i=0; i<rows.length; i++) {
				row = rows[i]
				accessKeyToAdd = ((i+1) == 10) ? '/' : ((i+1) == 11) ? '*' : ((i+1) == 12) ? '-' : (i+1);
				rowa = $$(row, 'a')
				if (rowa[0]) {
					rowa[0].accessKey = accessKeyToAdd
				}
			}
		}
	}, false);
})();