Skip to content

Instantly share code, notes, and snippets.

View bchapuis's full-sized avatar

Bertil Chapuis bchapuis

View GitHub Profile
@bchapuis
bchapuis / Haml.java
Created May 9, 2011 21:13
Dependency
// read the template file
InputStream stream = getClass().getClassLoader().getResourceAsStream("myFile.haml");
String template = IOUtils.toString(stream);
// initialize the options
Map<Object, Object> options = new HashMap<Object, Object>();
options.put("format", "html5");
// initialize the engine
HamlEngine engine = new HamlEngine(template, options);
@bchapuis
bchapuis / image.php
Created January 4, 2012 21:37
Cakephp crop and resize image helper
<?php
class ImageHelper extends Helper {
var $helpers = array('Html');
var $cacheDir = 'cache'; // relative to IMAGES_URL path
function resize($path, $dst_w, $dst_h, $htmlAttributes = array(), $return = false) {
$types = array(1 => "gif", "jpeg", "png", "swf", "psd", "wbmp"); // used to determine image type
@bchapuis
bchapuis / fullscreen.java
Created January 4, 2012 21:57
Mac osx fullscreen java application
try {
Class util = Class.forName("com.apple.eawt.FullScreenUtilities");
Class params[] = new Class[2];
params[0] = Window.class;
params[1] = Boolean.TYPE;
Method method = util.getMethod("setWindowCanFullScreen", params);
method.invoke(util, this, true);
} catch (ClassNotFoundException e) {
// log exception
} catch (NoSuchMethodException e) {
@bchapuis
bchapuis / dockicon.java
Created January 4, 2012 21:59
Mac osx java application dock icon
try {
Class util = Class.forName("com.apple.eawt.Application");
Method getApplication = util.getMethod("getApplication", new Class[0]);
Object application = getApplication.invoke(util);
Class params[] = new Class[1];
params[0] = Image.class;
Method setDockIconImage = util.getMethod("setDockIconImage", params);
URL url = App.class.getClassLoader().getResource("icon.png");
Image image = Toolkit.getDefaultToolkit().getImage(url);
setDockIconImage.invoke(application, image);
@bchapuis
bchapuis / UrlIsSame.java
Created May 14, 2012 07:24
Compare two urls and follow redirects
public static boolean isSame(URL a, URL b, int depth) throws IOException {
HttpURLConnection conn = (HttpURLConnection) b.openConnection();
conn.setInstanceFollowRedirects(false);
conn.connect();
int d = 0;
while (conn.getResponseCode() >= 300 && conn.getResponseCode() <= 399 && d < depth) {
b = new URL(conn.getHeaderField("Location"));
conn = (HttpURLConnection) b.openConnection();
conn.setInstanceFollowRedirects(false);
conn.connect();
@bchapuis
bchapuis / placeholdercolor.less
Created May 14, 2012 12:40
Less: mixin for placeholder color
.placeholder(@color) {
&::-webkit-input-placeholder {
color: @color;
}
&:-moz-placeholder {
color: @color;
}
}
@bchapuis
bchapuis / prettyjson.sh
Created December 18, 2012 13:10
pretty json command line
echo '{"foo": "lorem", "bar": "ipsum"}' | python -mjson.tool
@bchapuis
bchapuis / getUrlParameter.js
Last active July 10, 2021 13:37
Retrieve the value of a GET parameter using javascript
function decodeUrlParameter(str) {
return decodeURIComponent((str+'').replace(/\+/g, '%20'));
}
function getUrlParameter(name) {
return decodeUrlParameter(decodeURI(
(RegExp(name + '=' + '(.*?)(&|$)').exec(location.search)||[,null])[1]
));
}
@bchapuis
bchapuis / decodeUrlParameter.js
Last active April 22, 2024 21:17
Replace the plus sign which encode spaces in GET parameters using javascript.
function decodeUrlParameter(str) {
return decodeURIComponent((str+'').replace(/\+/g, '%20'));
}
@bchapuis
bchapuis / portForwarding.sh
Last active December 17, 2015 07:59
Iptables port forwarding
iptables -t nat -F
iptables -t nat -A OUTPUT -d localhost -p tcp --dport 80 -j
REDIRECT --to-ports 9000
iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT
--to-port 9000