Skip to content

Instantly share code, notes, and snippets.

View michalbcz's full-sized avatar

Michal Bernhard michalbcz

View GitHub Profile
@michalbcz
michalbcz / gist:3360573
Created August 15, 2012 14:25
groovy - super basic stats and usage of class extensions
// ------------------- methods and extensions definitions -----------------------------
List.metaClass.grepWithIndex = { yield ->
def greppedCollection = []
delegate.eachWithIndex { value, index ->
if (yield(value, index)) {
greppedCollection << value
}
@michalbcz
michalbcz / gist:2837835
Created May 30, 2012 17:38
groovy - html modification with state-of-art library JSoup -
@Grapes(
@Grab(group='org.jsoup', module='jsoup', version='1.6.2')
)
import org.jsoup.*
import org.jsoup.nodes.*
/**
* during build time we combine all the js files to single file (to speed up page loading)
* this script is part of the this script and its purpose is to collect all the external js files refered in
* MasterPage.html (template for the page) to feed up function which do the real combining and minifying to
@michalbcz
michalbcz / gist:2784124
Created May 24, 2012 20:44
groovy's AntBuilder and external ant tasks (example with Google Closure The JavaScript Compiler)
import groovy.grape.Grape;
// to explain why I am not using @Grape annotation
// see http://stackoverflow.com/questions/1641116/groovy-with-grape-and-antbuilder-classloader-problem
Grape.grab(
group: 'com.google.javascript',
module: 'closure-compiler',
version: 'r1918',
classLoader:this.class.classLoader.rootLoader /* needed because of ant.taskdef classloader */)
@michalbcz
michalbcz / gist:2757676
Created May 20, 2012 11:12
web scraping with groovy (real example with bandzone.cz the czech site promoting amateur czech bands) - all this is cooked with jQuery-like html querying (great Jsoup library), groovy in-house json parsing, emulating ajax requests, working with (malformed
@Grab(group='commons-httpclient', module='commons-httpclient', version='3.1')
import org.apache.commons.httpclient.*
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.commons.httpclient.methods.PostMethod
import org.apache.commons.httpclient.cookie.CookiePolicy
@Grab(group='org.jsoup', module='jsoup', version='1.6.2')
import org.jsoup.Jsoup
import groovy.json.*
@michalbcz
michalbcz / gist:2757630
Created May 20, 2012 10:47
groovy - when you're missing collectWithIndex method...
List.metaClass.collectWithIndex = { yield ->
def collected = []
delegate.eachWithIndex { listItem, index ->
collected << yield(listItem, index)
}
return collected
}
assert [1, 1, 1, 1, 1].collectWithIndex { it, index -> it + index } == [1, 2, 3, 4, 5]
@michalbcz
michalbcz / copy_and_flatten_my_pics_and_zip_it.groovy
Created October 22, 2011 11:57
copy and flatten directories with pictures and zip it to single file as I am lame in Krusader file manager :)
def ant = new AntBuilder() /* rules */
/* copy my roadtrip pictures and videos from CD and flatten it */
ant.copy(todir: "/home/michal/Pictures/Norsko2004", flatten: true /* if false (default) it copies directory structure too */) {
fileset(dir:"/media/Norsko 2004/Norsko 2004", casesensitive: false) {
include(name: "**/*.jpg") /* pictures... */
include(name: "**/*.mov") /* ...and videos */
}
}