Never been to DZone Snippets 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

About this user

Tim Morgan http://timmorgan.org

« Newer Snippets
Older Snippets »
Showing 21-30 of 54 total

xorg.conf with TV Out

Took a lot of experimentation to get this to work like I wanted. This is just for my records. The key line is "metamodes" I think.

   1  
   2  Section "Screen"
   3      Identifier     "Screen0"
   4      Device         "Videocard0"
   5      Monitor        "Monitor0"
   6      DefaultDepth    24
   7      Option         "TwinView" "1"
   8      Option         "TVStandard" "HD1080i"
   9      Option         "metamodes" "CRT: 1280x1024@1920x1080 +0+0, TV: 1920x1080 +0+0"
  10      SubSection     "Display"
  11          Depth       24
  12          Virtual    1920 1080
  13          Modes      "1280x1024" "1024x768" "800x600" "640x480"
  14      EndSubSection
  15  EndSection

Ruby strftime without leading zeros

   1  
   2  Date.today.strftime('%b %d, %Y').gsub(/ 0(\d\D)/, ' \1')


Um... strike that. Seems %e does it without the extra work. Thanks Peter!

image_tag with popup for alt attribute

Firefox doesn't display a popup for the alt attribute for images. While this is according to spec, it's slightly annoying to put the same text in both the alt and the title. Put the following in your ApplicationHelper to copy the alt to the title for every image. Please don't tell me how wrong this is. I don't care.

   1  
   2  module ApplicationHelper
   3    def image_tag(location, options)
   4      options[:title] ||= options[:alt]
   5      super(location, options)
   6    end
   7  end

Mobile Phone Email to Text Message Gateways

   1  
   2  AT&T
   3    number@mobile.att.net
   4  CellularOne
   5    number@mobile.celloneusa.com    
   6  Cingular
   7    number@mobile.mycingular.com
   8  Nextel
   9    number@messaging.nextel.com
  10  Sprint
  11    number@messaging.sprintpcs.com
  12  T-Mobile
  13    number@tmomail.net
  14  US Cellular
  15    number@email.uscc.net
  16  Verizon
  17    number@vtext.com
  18  Virgin Mobile
  19    number@vmobl.com

Mongrel Startup Script

I couldn't find one that did exactly what I wanted, so I cooked up this ruby script. Be sure to change app_dir and apps near the top to match your environment. On my ubuntu/debian server, this script resides in /etc/init.d. I ran "sudo update-rc.d -f mongrel defaults" to make it run on startup.

   1  
   2  #!/usr/bin/env ruby
   3  #
   4  # mongrel       Startup script for Mongrel by Tim Morgan
   5  #
   6  # chkconfig: - 85 15
   7  # description: mongrel manages Mongrel
   8  #
   9  
  10  app_dir = '/var/rails'
  11  
  12  apps = {
  13    'hpy' => 8001,
  14    '43verses' => 8002
  15  }
  16  
  17  if ['stop', 'restart'].include? ARGV.first
  18    apps.each do |path, port|
  19      path = File.join app_dir, path
  20      puts "Stopping #{path}..."
  21      `mongrel_rails stop -c #{path} -P log/mongrel.pid`
  22    end
  23  end
  24  
  25  if ['start', 'restart'].include? ARGV.first
  26    apps.each do |path, port|
  27      path = File.join app_dir, path
  28      puts "Starting #{path} on #{port}..."
  29      `mongrel_rails start -d -p #{port} -e production -c #{path} -P log/mongrel.pid`
  30    end
  31  end
  32  
  33  unless ['start', 'stop', 'restart'].include? ARGV.first
  34      puts "Usage: mongrel {start|stop|restart}"
  35      exit
  36  end

.irbrc - tab completion, cross-session history, history file

My own .irbrc file constructed from this and this.

Put this file in your home directory ("C:\Documents and Settings\USERNAME" on Windows)

If on Windows, create an environment variable called HOME and point it to "C:\Documents and Settings\USERNAME".

   1  
   2  require 'irb/completion'
   3  ARGV.concat [ "--readline", "--prompt-mode", "simple" ]
   4  
   5  module Readline
   6    module History
   7      LOG = "#{ENV['HOME']}/.irb-history"
   8  
   9      def self.write_log(line)
  10        File.open(LOG, 'ab') {|f| f << "#{line}\n"}
  11      end
  12  
  13      def self.start_session_log
  14        write_log("\n# session start: #{Time.now}\n\n")
  15        at_exit { write_log("\n# session stop: #{Time.now}\n") }
  16      end
  17    end
  18  
  19    alias :old_readline :readline
  20    def readline(*args)
  21      ln = old_readline(*args)
  22      begin
  23        History.write_log(ln)
  24      rescue
  25      end
  26      ln
  27    end
  28  end
  29  
  30  Readline::History.start_session_log
  31  
  32  require 'irb/ext/save-history'
  33  IRB.conf[:SAVE_HISTORY] = 100
  34  IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
  35  
  36  IRB.conf[:PROMPT_MODE] = :SIMPLE

Lightbox with IFrames

A hacked version of lightbox that uses an IFrame when the url points to anything other than a jpeg. Also, added some syntax that allows a link to specify the height of the lightbox like this:

   1  <a href="http://google.com" rel="lightbox|300">LB with height 300</a>


   1  
   2  // -----------------------------------------------------------------------------------
   3  //
   4  //	Lightbox v2.02
   5  //	by Lokesh Dhakar - http://www.huddletogether.com
   6  //	3/31/06
   7  //
   8  //	hacked to use iframe for non-jpeg urls
   9  //	by Tim Morgan - http://timmorgan.org
  10  //	8/9/06
  11  //
  12  //	For more information on this script, visit:
  13  //	http://huddletogether.com/projects/lightbox2/
  14  //	...and...
  15  //	http://mpov.wordpress.com/2006/08/08/lightbox-with-iframes/
  16  //
  17  //	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
  18  //	
  19  //	Credit also due to those who have helped, inspired, and made their code available to the public.
  20  //	Including: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.org), Thomas Fuchs(mir.aculo.us), and others.
  21  //
  22  //
  23  // -----------------------------------------------------------------------------------
  24  /*
  25  
  26  	Table of Contents
  27  	-----------------
  28  	Configuration
  29  	Global Variables
  30  
  31  	Extending Built-in Objects	
  32  	- Object.extend(Element)
  33  	- Array.prototype.removeDuplicates()
  34  	- Array.prototype.empty()
  35  
  36  	Lightbox Class Declaration
  37  	- initialize()
  38  	- start()
  39  	- changeImage()
  40  	- resizeImageContainer()
  41  	- showImage()
  42  	- updateDetails()
  43  	- updateNav()
  44  	- enableKeyboardNav()
  45  	- disableKeyboardNav()
  46  	- keyboardAction()
  47  	- preloadNeighborImages()
  48  	- end()
  49  	
  50  	Miscellaneous Functions
  51  	- getPageScroll()
  52  	- getPageSize()
  53  	- getKey()
  54  	- listenKey()
  55  	- showSelectBoxes()
  56  	- hideSelectBoxes()
  57  	- pause()
  58  	- initLightbox()
  59  	
  60  	Function Calls
  61  	- addLoadEvent(initLightbox)
  62  	
  63  */
  64  // -----------------------------------------------------------------------------------
  65  
  66  //
  67  //	Configuration
  68  //
  69  var fileLoadingImage = "../images/loading.gif";		
  70  var fileBottomNavCloseImage = "../images/closelabel.gif";
  71  
  72  var iframeWidth = 400;
  73  var iframeHeight = 450;
  74  
  75  var resizeSpeed = 10;	// controls the speed of the image resizing (1=slowest and 10=fastest)
  76  
  77  var borderSize = 10;	//if you adjust the padding in the CSS, you will need to update this variable
  78  
  79  // -----------------------------------------------------------------------------------
  80  
  81  //
  82  //	Global Variables
  83  //
  84  var imageArray = new Array;
  85  var activeImage;
  86  
  87  if(resizeSpeed > 10){ resizeSpeed = 10;}
  88  if(resizeSpeed < 1){ resizeSpeed = 1;}
  89  resizeDuration = (11 - resizeSpeed) * 0.15;
  90  
  91  // -----------------------------------------------------------------------------------
  92  
  93  //
  94  //	Additional methods for Element added by SU, Couloir
  95  //	- further additions by Lokesh Dhakar (huddletogether.com)
  96  //
  97  Object.extend(Element, {
  98  	getWidth: function(element) {
  99  	   	element = $(element);
 100  	   	return element.offsetWidth; 
 101  	},
 102  	setWidth: function(element,w) {
 103  	   	element = $(element);
 104      	element.style.width = w +"px";
 105  	},
 106  	setHeight: function(element,h) {
 107     		element = $(element);
 108      	element.style.height = h +"px";
 109  	},
 110  	setTop: function(element,t) {
 111  	   	element = $(element);
 112      	element.style.top = t +"px";
 113  	},
 114  	setSrc: function(element,src) {
 115      	element = $(element);
 116      	element.src = src; 
 117  	},
 118  	setHref: function(element,href) {
 119      	element = $(element);
 120      	element.href = href; 
 121  	},
 122  	setInnerHTML: function(element,content) {
 123  		element = $(element);
 124  		element.innerHTML = content;
 125  	}
 126  });
 127  
 128  // -----------------------------------------------------------------------------------
 129  
 130  //
 131  //	Extending built-in Array object
 132  //	- array.removeDuplicates()
 133  //	- array.empty()
 134  //
 135  Array.prototype.removeDuplicates = function () {
 136  	for(i = 1; i < this.length; i++){
 137  		if(this[i][0] == this[i-1][0]){
 138  			this.splice(i,1);
 139  		}
 140  	}
 141  }
 142  
 143  // -----------------------------------------------------------------------------------
 144  
 145  Array.prototype.empty = function () {
 146  	for(i = 0; i <= this.length; i++){
 147  		this.shift();
 148  	}
 149  }
 150  
 151  // -----------------------------------------------------------------------------------
 152  
 153  //
 154  //	Lightbox Class Declaration
 155  //	- initialize()
 156  //	- start()
 157  //	- changeImage()
 158  //	- resizeImageContainer()
 159  //	- showImage()
 160  //	- updateDetails()
 161  //	- updateNav()
 162  //	- enableKeyboardNav()
 163  //	- disableKeyboardNav()
 164  //	- keyboardNavAction()
 165  //	- preloadNeighborImages()
 166  //	- end()
 167  //
 168  //	Structuring of code inspired by Scott Upton (http://www.uptonic.com/)
 169  //
 170  var Lightbox = Class.create();
 171  
 172  Lightbox.prototype = {
 173  	
 174  	// initialize()
 175  	// Constructor runs on completion of the DOM loading. Loops through anchor tags looking for 
 176  	// 'lightbox' references and applies onclick events to appropriate links. The 2nd section of
 177  	// the function inserts html at the bottom of the page which is used to display the shadow 
 178  	// overlay and the image container.
 179  	//
 180  	initialize: function() {	
 181  		if (!document.getElementsByTagName){ return; }
 182  		var anchors = document.getElementsByTagName('a');
 183  
 184  		// loop through all anchor tags
 185  		for (var i=0; i<anchors.length; i++){
 186  			var anchor = anchors[i];
 187  			
 188  			var relAttribute = String(anchor.getAttribute('rel'));
 189  			
 190  			// use the string.match() method to catch 'lightbox' references in the rel attribute
 191  			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox'))){
 192  				anchor.onclick = function () {myLightbox.start(this); return false;}
 193  			}
 194  		}
 195  
 196  		// The rest of this code inserts html at the bottom of the page that looks similar to this:
 197  		//
 198  		//	<div id="overlay"></div>
 199  		//	<div id="lightbox">
 200  		//		<div id="outerImageContainer">
 201  		//			<div id="imageContainer">
 202  		//				<img id="lightboxImage">
 203  		//				<div style="" id="hoverNav">
 204  		//					<a href="#" id="prevLink"></a>
 205  		//					<a href="#" id="nextLink"></a>
 206  		//				</div>
 207  		//				<div id="loading">
 208  		//					<a href="#" id="loadingLink">
 209  		//						<img src="images/loading.gif">
 210  		//					</a>
 211  		//				</div>
 212  		//			</div>
 213  		//		</div>
 214  		//		<div id="imageDataContainer">
 215  		//			<div id="imageData">
 216  		//				<div id="imageDetails">
 217  		//					<span id="caption"></span>
 218  		//					<span id="numberDisplay"></span>
 219  		//				</div>
 220  		//				<div id="bottomNav">
 221  		//					<a href="#" id="bottomNavClose">
 222  		//						<img src="images/close.gif">
 223  		//					</a>
 224  		//				</div>
 225  		//			</div>
 226  		//		</div>
 227  		//	</div>
 228  
 229  
 230  		var objBody = document.getElementsByTagName("body").item(0);
 231  		
 232  		var objOverlay = document.createElement("div");
 233  		objOverlay.setAttribute('id','overlay');
 234  		objOverlay.style.display = 'none';
 235  		//objOverlay.onclick = function() { myLightbox.end(); return false; }
 236  		objBody.appendChild(objOverlay);
 237  		
 238  		var objLightbox = document.createElement("div");
 239  		objLightbox.setAttribute('id','lightbox');
 240  		objLightbox.style.display = 'none';
 241  		objBody.appendChild(objLightbox);
 242  	
 243  		var objOuterImageContainer = document.createElement("div");
 244  		objOuterImageContainer.setAttribute('id','outerImageContainer');
 245  		objLightbox.appendChild(objOuterImageContainer);
 246  
 247  		var objImageContainer = document.createElement("div");
 248  		objImageContainer.setAttribute('id','imageContainer');
 249  		objOuterImageContainer.appendChild(objImageContainer);
 250  			
 251  		var objLightboxIframe = document.createElement("iframe");
 252  		objLightboxIframe.setAttribute('id','lightboxIframe');
 253  		objLightboxIframe.style.width = iframeWidth + 'px';
 254  		objLightboxIframe.style.height = iframeHeight + 'px';
 255  		objLightboxIframe.style.border = 'none';
 256  		objImageContainer.appendChild(objLightboxIframe);
 257      
 258  		var objLightboxImage = document.createElement("img");
 259  		objLightboxImage.setAttribute('id','lightboxImage');
 260  		objImageContainer.appendChild(objLightboxImage);
 261  	
 262  		var objHoverNav = document.createElement("div");
 263  		objHoverNav.setAttribute('id','hoverNav');
 264  		objImageContainer.appendChild(objHoverNav);
 265  	
 266  		var objPrevLink = document.createElement("a");
 267  		objPrevLink.setAttribute('id','prevLink');
 268  		objPrevLink.setAttribute('href','#');
 269  		objHoverNav.appendChild(objPrevLink);
 270  		
 271  		var objNextLink = document.createElement("a");
 272  		objNextLink.setAttribute('id','nextLink');
 273  		objNextLink.setAttribute('href','#');
 274  		objHoverNav.appendChild(objNextLink);
 275  	
 276  		var objLoading = document.createElement("div");
 277  		objLoading.setAttribute('id','loading');
 278  		objImageContainer.appendChild(objLoading);
 279  	
 280  		var objLoadingLink = document.createElement("a");
 281  		objLoadingLink.setAttribute('id','loadingLink');
 282  		objLoadingLink.setAttribute('href','#');
 283  		objLoadingLink.onclick = function() { myLightbox.end(); return false; }
 284  		objLoading.appendChild(objLoadingLink);
 285  	
 286  		var objLoadingImage = document.createElement("img");
 287  		objLoadingImage.setAttribute('src', fileLoadingImage);
 288  		objLoadingLink.appendChild(objLoadingImage);
 289  
 290  		var objImageDataContainer = document.createElement("div");
 291  		objImageDataContainer.setAttribute('id','imageDataContainer');
 292  		objImageDataContainer.className = 'clearfix';
 293  		objLightbox.appendChild(objImageDataContainer);
 294  
 295  		var objImageData = document.createElement("div");
 296  		objImageData.setAttribute('id','imageData');
 297  		objImageDataContainer.appendChild(objImageData);
 298  	
 299  		var objImageDetails = document.createElement("div");
 300  		objImageDetails.setAttribute('id','imageDetails');
 301  		objImageData.appendChild(objImageDetails);
 302  	
 303  		var objCaption = document.createElement("span");
 304  		objCaption.setAttribute('id','caption');
 305  		objImageDetails.appendChild(objCaption);
 306  	
 307  		var objNumberDisplay = document.createElement("span");
 308  		objNumberDisplay.setAttribute('id','numberDisplay');
 309  		objImageDetails.appendChild(objNumberDisplay);
 310  		
 311  		var objBottomNav = document.createElement("div");
 312  		objBottomNav.setAttribute('id','bottomNav');
 313  		objImageData.appendChild(objBottomNav);
 314  	
 315  		var objBottomNavCloseLink = document.createElement("a");
 316  		objBottomNavCloseLink.setAttribute('id','bottomNavClose');
 317  		objBottomNavCloseLink.setAttribute('href','#');
 318  		objBottomNavCloseLink.onclick = function() { myLightbox.end(); return false; }
 319  		objBottomNav.appendChild(objBottomNavCloseLink);
 320  	
 321  		var objBottomNavCloseImage = document.createElement("img");
 322  		objBottomNavCloseImage.setAttribute('src', fileBottomNavCloseImage);
 323  		objBottomNavCloseLink.appendChild(objBottomNavCloseImage);
 324  	}