Wednesday, December 28, 2011

jquery reverse order of children

Reverse the order of children:

(function($){
 $.fn.reverse = function(){  
  return this.each(function(){
   var i,
    $this = $(this),
    children = $this.children(),
    last_index = children.length - 1,
    last_child = $(children[last_index]);
   
   for (i = 0; i < last_index; ++i)
   {
    last_child.after($(children[i]).detach());
   };
  });
 };
})(jQuery);

Thursday, December 22, 2011

download jquery

Once or twice a month I'll find myself working on some random little project with a web UI and so of course I need jquery at which point I wonder if I have the latest version.. hence, a little script to get the latest and greatest:

#!/bin/bash

wget $(\
 wget -qO - http://docs.jquery.com/Downloading_jQuery |\
 sed -rn 's#^.*(http://code.jquery.com/jquery-[[:digit:]]+.[[:digit:]]+.[[:digit:]]+.min.js).*$#\1#p' |\
 head -n 1
 )

todo: check local jquery copy to see if there actually is a new version we need to download

Monday, December 5, 2011

string find and replace in files

The -i option to sed does in place file editing, however it "modifies" each file it touches whether it actually does any replacing or not (so the file mod time is updated). By filtering through grep first, we can be sure we are only messing with files we actually want to. Replace "happy" with "blammo":
find . -type f -print0 | xargs -0 grep -lZ "happy" | xargs -0 sed -i 's/happy/blammo/g'
The -l option to grep prints file names only and stops with the first match (so no duplicates). -Z uses null byte for separator instead of newline.

Tuesday, November 22, 2011

adding syntax highlighting on blogger

This of course started because I wanted to add syntax highlighting to this blog. That led me to an excellent post

http://heisencoder.net/2009/01/adding-syntax-highlighting-to-blogger.html

by Matt Ball about the subject; it worked, and all was well. However in a later bash script post, I noticed it was incorrectly treating a variable as a comment because it had a # in it. This led me to the git project of the syntax highlighter Mr Ball had used (and everyone seems to use), the excellent and excellently named SyntaxHighlighter by alexgorbatchev:

https://github.com/alexgorbatchev/SyntaxHighlighter

After playing around with it a bit, I updated the single line comment code for bash syntax highlighting as well as the auto-loader to allow it to take a path to a directory rather than having to specify the URL of every script that can be used. I created a git project on google code to host the files and and all seems well again. The only thing I had to do on blogger was add these lines to the end of the head section of the template


And then whenever you want to add some syntax highlighting to your post, simply edit the html to include the wrapper for the brush


here is my xml

or


console.log('hello javascript syntax highlighted world');

The idea to use google code to host the static files came from this post

http://blogazor.blogspot.com/2011/06/freely-host-css-js-other-static-files.html

by the good people at blogazor. They use svn so if you would like to use svn that howto is a great read.

Monday, November 14, 2011

import sql files

import all files matching *.mysql in current directory into database "db"
for f in *.mysql; do echo $f && mysql -u username -psecretpw db < $f ; done
echos the file name as it goes

Saturday, November 12, 2011

match a word as long as another word doesn't appear before it

match 'haha' as long as 'example' doesn't come first
/^((?:(?!example).)*)(haha.*)$/
excellent post about *not* matching a word: http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word

recursive file list sorted by mod time

I end up bouncing from one project to another pretty often, and I don't like saving my workspace in my editor so that I can start with a nice, clean, blank slate. But it's nice to know what exactly I've been working on recently, so..
find . -type f | xargs ls -l 2>/dev/null | sort -r -k6,6 -k7,7 | most

Friday, November 4, 2011

hexdump

Quick little one liner for hexdump with some nice, versatile output. 1 byte per line, with
byte #[space]octal value[space]decimal value[space]hex value[space]default character set
hexdump -v -e '"%07_ad" 1/1 " %03o"' -e '1/1 " %03u"' -e '1/1 " %03x"' -e '1/1 " %_c \n"'
And in a bash script taking a file:
#!/bin/bash

hexdump -v -e '"%07_ad" 1/1 " %03o"' -e '1/1 " %03u"' -e '1/1 " %03x"' -e '1/1 " %_c \n"' $1
Example output when called on itself:
0000000 043 035 023 # 
0000001 041 033 021 ! 
0000002 057 047 02f / 
0000003 142 098 062 b 
0000004 151 105 069 i 
0000005 156 110 06e n 
0000006 057 047 02f / 
0000007 142 098 062 b 
0000008 141 097 061 a 
0000009 163 115 073 s 
0000010 150 104 068 h 
0000011 012 010 00a \n 
0000012 012 010 00a \n 
0000013 150 104 068 h 
0000014 145 101 065 e 
0000015 170 120 078 x 
0000016 144 100 064 d 
0000017 165 117 075 u 
0000018 155 109 06d m 
0000019 160 112 070 p 
0000020 040 032 020   
0000021 055 045 02d - 
0000022 166 118 076 v 
0000023 040 032 020   
0000024 055 045 02d - 
0000025 145 101 065 e 
0000026 040 032 020   
0000027 047 039 027 ' 
0000028 042 034 022 " 
0000045 063 051 033 3 
0000046 157 111 06f o 
0000047 042 034 022 " 
0000048 047 039 027 ' 
0000049 040 032 020   
0000050 055 045 02d - 
0000051 145 101 065 e 
0000052 040 032 020   
0000053 047 039 027 ' 
0000054 061 049 031 1 
0000055 057 047 02f / 
0000056 061 049 031 1 
0000057 040 032 020   
0000058 042 034 022 " 
0000059 040 032 020   
0000060 045 037 025 % 
0000061 060 048 030 0 
0000062 063 051 033 3 
0000063 165 117 075 u 
0000064 042 034 022 " 
0000065 047 039 027 ' 
0000066 040 032 020   
0000067 055 045 02d - 
0000068 145 101 065 e 
0000069 040 032 020   
0000070 047 039 027 ' 
0000071 061 049 031 1 
0000072 057 047 02f / 
0000073 061 049 031 1 
0000074 040 032 020   
0000075 042 034 022 " 
0000076 040 032 020   
0000077 045 037 025 % 
0000078 060 048 030 0 
0000079 063 051 033 3 
0000080 170 120 078 x 
0000081 042 034 022 " 
0000082 047 039 027 ' 
0000083 040 032 020   
0000084 055 045 02d - 
0000085 145 101 065 e 
0000086 040 032 020   
0000087 047 039 027 ' 
0000088 061 049 031 1 
0000089 057 047 02f / 
0000090 061 049 031 1 
0000091 040 032 020   
0000092 042 034 022 " 
0000093 040 032 020   
0000094 045 037 025 % 
0000095 137 095 05f _ 
0000096 143 099 063 c 
0000097 040 032 020   
0000098 134 092 05c \ 
0000099 156 110 06e n 
0000100 042 034 022 " 
0000101 047 039 027 ' 
0000102 040 032 020   
0000103 044 036 024 $ 
0000104 061 049 031 1 
0000105 012 010 00a \n 

Sunday, October 23, 2011

ubuntu oneiric ocelot write desktop background image file

here is a script which takes in path to one or more directories containing images and writes a gnome desktop background file to show those images

#!/bin/bash

usage()
{
cat << EOF
usage: $0 -t transition_time -s static_time (-d dirs|-r recursive_dirs)

OPTIONS:
 -h      Show this message
 -t      required. time of transition between images, in seconds
 -s      required. time each image is show
 -d      comma separated list of directories containing images.
 -r      comma separated list of directories that contain images and
         other directories with more images

Either dirs, recursive_dirs, or both is required
EOF
if [[ -z $1 ]]; then exit 1; else exit $1; fi
}

set_img_paths()
{
 dir_path=$1
 find_opts=$2
 
 for file_path in $(find ${dir_path} ${find_opts} -type f)
 do
  mime_type=$(file --mime-type ${file_path})
  if [[ $mime_type =~ ^.*:[[:space:]]+image/(.*)$ ]]
  then
   img_paths[${#img_paths[*]}]=$file_path
  fi
 done
}

while getopts ":hd:r:t:s:" x
do
 case $x in
  h) usage 0 ;;
  t) transition_time=$OPTARG ;;
  s) static_time=$OPTARG ;;
  d) dirs[${#dirs[*]}]=$OPTARG ;;
  r) recursive_dirs[${#recursive_dirs[*]}]=$OPTARG ;;
 esac
done

if [[ -z $transition_time ]]; then usage; fi
if [[ -z $static_time ]]; then usage; fi
if [[ ${#dirs[*]} -eq 0 && ${#recursive_dirs[*]} -eq 0 ]]; then usage; fi

# array for our image paths
declare -a img_paths

# loop over directories to search non-recursively for images
for ((i=0; i<${#dirs[*]}; i++))
do
 set_img_paths "${dirs[$i]}" "-maxdepth 1"
done

# loop over directories to search recursively for images
for ((i=0; i<${#recursive_dirs[*]}; i++))
do
 set_img_paths "${recursive_dirs[$i]}" ""
done

# make sure we have some images
if [[ ${#img_paths[*]} -eq 0 ]]
then
 echo "Error: could not find any images"
 exit 2
fi

# randomize our array
random_order=$(seq ${#img_paths[*]} | shuf)
i=0
for r in $random_order
do
 randomized[$i]=${img_paths[$r-1]}
 i=$(( $i + 1 ))
done

# write our background xml file to stdout
echo ""
for ((i=0; i<${#randomized[*]}; i++))
do
 img_path=${randomized[$i]}
 echo -n "
 
  ${static_time}
  ${img_path}
 
"
 
 # transition to next image, or, if we are showing the last image, transition back to first image
 [[ $(( $i + 1 )) -lt ${#randomized[*]} ]] && next_img=${randomized[$i+1]} || next_img=${randomized[0]}
 echo -n "
 
  ${transition_time}
  ${img_path}
  ${next_img}
 
"
done
echo ""

Tuesday, October 18, 2011

oneiric ocelot 11.10 set background to xml file

gsettings set org.gnome.desktop.background picture-uri 'file:///path/to/file/background.xml' 
 
http://askubuntu.com/questions/67218/how-do-i-create-a-desktop-wallpaper-slideshow-in-oneiric


as best I can tell this property is stored in ~/.gconf/desktop/gnome/background/%gconf.xml

more to come on this in a bit

Thursday, September 29, 2011

remove svn dirs

I'm sure this is out there on the internet already somewhere, but

find . -name ".svn" -type d | xargs rm -rf

Tuesday, September 27, 2011

setting up proxy pass

Make sure you load all the right modules, eg

http://www.webmasterworld.com/forum92/1727.htm

If you are getting a bunch of Error: proxy: ap_get_scoreboard_lb errors, you may need to stop and start apache (instead of reloading/restarting) (http://old.nabble.com/Error%3A-proxy%3A-ap_get_scoreboard_lb%2890%29-failed-in-child-td12128044.html)


Another error I got: client denied by server configuration: proxy:http://example.com

debian/ubuntu "Deny from all" by default in mods-available/proxy.conf, comment that out (http://artur.hefczyc.net/node/10)

Monday, September 26, 2011

add unversioned files to svn

svn st | sed -rn 's/^\?\s+(.*)$/\1/p' | xargs svn add

Monday, September 12, 2011

tasks

When I feel like I have to do some little task, I will post about it here. Usually this means google couldn't tell me the answer to my question on its first page of results, so I tried to do it myself. Wish I would have started this a long time ago. You know what they say.. a cat may look at a king