Never been to TextSnippets 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

Liam Clancy http://www.metafeather.net/

« Newer Snippets
Older Snippets »
14 total  XML / RSS feed 

JS Regex's for sourcecode

Extracted from SyntaxHilighter

        MultiLineCComments : new RegExp('/\\*[\\s\\S]*?\\*/', 'gm'),
        SingleLineCComments : new RegExp('//.*$', 'gm'),
        SingleLinePerlComments : new RegExp('#.*$', 'gm'),
        DoubleQuotedString : new RegExp('"(?:\\.|(\\\\\\")|[^\\""\\n])*"','g'),
        SingleQuotedString : new RegExp("'(?:\\.|(\\\\\\')|[^\\''\\n])*'", 'g')

Download with cURL in a Loop Until Completed

From Entropy.ch:

Here’s a little terminal command I use to repeatedly attempt to download a file. This is useful if the transfer aborts frequently or if the server is busy:

The while/do/done loop keeps calling curl, with pauses of 10 seconds. curl will keep returning a failure exit code, which is what keeps the while loop going if inverted with the ! (logical not) operator. The -C - flag tells curl to continue at the last byte position.

while ! curl -C - -O 'http://download.parallels.com/GA/Parallels%20Desktop%203186%20Mac%20en.dmg'; 
do sleep 10; 
done

Zero data on a Linux disk

For use with VMWare when you don't run an X server:
cat /dev/zero > zero.dat ; sync ; sleep 1 ; sync ; rm -f zero.dat

Use SVN, Ant and Tomcat in XCode

I found Tim Fanelli's Blog ( http://www.timfanelli.com/cgi-bin/pyblosxom.cgi/j2ee/ ) entry quite helpful

In xcode, create a new empty project with the same dir any sources were checked out from.

Name the project with the same name used as the SVN module. NOTE: the .xcodeproj file should end up in the same directory as your build.xml file.

From the finder, dragged the src/ directory into the top level project name item in the project window.

Removed any unwanted items from the folder hierarchy now in xcode. For example, remove all generated build .jar files.

Project -> New Target. Chose 'Shell Script Target'.

In the new Target, double clicked on "Run Script". Under the "General" tab, entered, "/usr/bin/ant;"

Project -> New Build Phase -> New Copy Files Build Phase

Control + Click "Copy Files" (or right click if you have Mighty Mouse)

Add "Empty File in Project". Choose the dist/ directory. Create a file matching the output from your Ant build e.g. Project.war

Double click on the "Copy Files" phase. Type the absolute path to your tomcat deployment directory.

Project -> New Custom Executable. Enter "Tomcat" and make the path "/usr/bin/java".

Select the Arguments Tab enter:

-classpath /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/commons-logging-api.jar
-Dcatalina.home=/opt/tomcat
-Xmx128m
-Xms64m
-Xincgc
-Djava.awt.headless=true
org.apache.catalina.startup.Bootstrap -debug start


Under the Debugging tab, choose "Java Debugger".

URL parsing Regex

A single regex to parse and breakup a full URL including query parameters and anchors e.g.

https://www.google.com/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash

^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$


RexEx positions:

url: RegExp['$&'],
protocol:RegExp.$2,
host:RegExp.$3,
path:RegExp.$4,
file:RegExp.$6,
query:RegExp.$7,
hash:RegExp.$8

XSLT and XHTML DTD's

A quick code snippet to include the right XHTML DTD in your XSL generated output. This took me a bit of reseach to find out:

<xsl:output 
method="xml" 
encoding="utf-8" 
omit-xml-declaration="yes" 
indent="no" 
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" 
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" />


Another tidbit I found today was:

exclude-result-prefixes="media" 


to exclude unwanted namespace declarations from the output.

Disable Shift+Click selection in browsers

I wanted the table to allow the user to select a range of rows with a Shift+Click… The problem is that, by default, the browser will select the page’s text content. This isn’t something you’ll want to do very often but it is possible to get around this:

Firefox can do this from your CSS:

table {

    -moz-user-select: none;
}
On the table element IE needs:

the_table_node.onselectstart = function () {

    return false;
};

Javascript optimisation - while

Using as a benchmark the task of iterating over a live collection produced by getElementsByTagName. IE/Win, Gecko and Safari all agree as to the fastest means:

var i = 0, el, els = document.getElementsByTagName(nodeName);
while (el = els[i++]) {
    // [...]
}


Using a while loop and storing the live collection in a variable is the quickest technique in all of these browsers.

Enable svn+ssh remote logins

Installing Subversion for local use is general an easy install, but allowing remote access to your svn repository over SSH can be problomatic dependent upon your OS and the means taken to install.

For Darwinports and Fink on OS X the install location has to be added to users $PATHs, but there are extra steps outlined here for use of the svn+ssh means of access:

http://subversion.tigris.org/faq.html#ssh-svnserve-location

A much easier alternate is to sym link the svn binaries to a place on the default PATH (used by the SSH login):

#For Darwinports
ln -s /opt/local/bin/sv* /usr/bin/

#For Fink
ln -s /sw/bin/sv* /usr/bin/


This links all the binaries at once.

See Clearsilver runtime template data (in Trac)

?hdfdump=1


Append the above to a Clearsilver generated page URL for a structured view of the template data.

Stop bell/beep in bash

setterm -blength 0


Sets the bell length to zero for all applications

Get packet information on OS X

ipconfig getpacket en0


According to the manual page for ipconfig, this command appears to be unique to Mac OS X.

The command will display a bunch of useful info, including:

server_identifier (ip): That's your DHCP server's IP address.
yiaddr: Your machine's IP address.
chaddr: Your machine's MAC address.
domain_name_server: Your domain name server(s).

Ant OS specific switch

<property file="${antutil.includes}/${os.name}-${os.arch}.properties" />


Use to include properties per OS

Find and change a users uid

Most useful for providing a consistent uid on machines accessing an NFS mount:

$ sudo find . -xdev -user <old-uid> -print -exec chown <new-uid> {} \;
« Newer Snippets
Older Snippets »
14 total  XML / RSS feed