Skip to content

Instantly share code, notes, and snippets.

@SimonSapin
SimonSapin / gist:2933303
Created June 14, 2012 22:17
A Jinja extension that adds a {% showonmatch 'some css selector' %} tag. The content is removed if (parsed as HTML) nothing in it matches the CSS selector
from jinja2 import nodes
from jinja2.ext import Extension
from lxml.cssselect import CSSSelector
from lxml.html import fromstring
class ShowOnMatch(Extension):
tags = set(['showonmatch'])
def parse(self, parser):
@SimonSapin
SimonSapin / gtkprint.py
Last active October 11, 2015 01:28
WeasyPrint 0.20 + GTK print
import cairo
import cairocffi
import pygtk
pygtk.require('2.0')
import gtk
from weasyprint import HTML
def print_html(document):
@SimonSapin
SimonSapin / pil_images.py
Created September 25, 2012 15:58
WeasyPrint + PIL
from weasyprint import images as weasyprint_images
# Only monkey-patch if GDK failed to import
if not hasattr(weasyprint_images, 'PixbufLoader'):
from io import BytesIO
try:
from PIL import Image
except ImportError:
# Yeah, PIL packaging.
import Image
@SimonSapin
SimonSapin / gist:4078445
Created November 15, 2012 12:41
Signed integer converter for Werkzeug routing.
from werkzeug.routing import IntegerConverter
class SignedIntegerConverter(IntegerConverter):
regex = r'-?\d+'
@SimonSapin
SimonSapin / Output
Created August 5, 2013 13:52
Micro-benchmark of three implementation strategies for https://github.com/mozilla/rust/pull/8231 i7-3520M CPU @ 2.90GHz A lookup table is pretty much always faster.
# Output of 'ascii_case_bench --test --bench',
# Built with 'rustc ascii_case_bench.rs -O'
# rustc 0.8-pre (fc57182 2013-08-02 09:13:53 -0700)
running 45 tests
test a_lookup::tests::test_eq_ascii_lower ... ok
test a_lookup::tests::test_to_ascii_lower ... ok
test c_comparisons::tests::test_eq_ascii_lower ... ok
test b_match::tests::test_eq_ascii_lower ... ok
test b_match::tests::test_to_ascii_lower ... ok
@SimonSapin
SimonSapin / peekable.rs
Created August 8, 2013 18:31
Add a .peekable() to iterators. It returns a new iterators that has an additional .peek() function returning a &ref to the next item.
trait Peekable<I, T> {
fn peekable(self) -> PeekingIterator<I, T>;
}
impl<I: Iterator<T>, T> Peekable<I, T> for I {
fn peekable(self) -> PeekingIterator<I, T> {
PeekingIterator{ iter: self, peeked: None }
}
}
@SimonSapin
SimonSapin / urlparser.rs
Created December 4, 2013 20:23
WIP URL parser for Rust, by @jgraham
use std::str;
use std::iterator::{Iterator};
use std::str::{eq, eq_slice, ByteIterator};
use std::vec;
use std::ops;
use std::char;
struct IPv6Address {
data: [u16, ..8]
}
@SimonSapin
SimonSapin / gist:76f900e23e01fd5e44ff
Last active August 29, 2015 14:10
Vertical text in Servo, 2014-12-01

https://medium.com/@slsoftworks/why-should-one-keep-an-eye-out-for-servo-6d53eb6f64ff

Servo is currently basically the only browser engine that supports native rendering of vertical texts

As much as I love hearing from people that Servo is awesome, this is not correct.

First, vertical text support in Servo is currently (2014-12-01) experimental (behind an off-by-default flag), in early stages of development, and very buggy. So saying that we support is is a bit of a stretch :)

Second, other browsers have it too, although I don’t know how complete or how buggy. Microsoft has had some form of it since IE 5 (!), WebKit/Blink have it prefixed, and Firefox has it in Nightly.

@SimonSapin
SimonSapin / gist:b46b1fc78d2d9adae16e
Created January 26, 2015 01:42
Constant extra space merge
// https://twitter.com/mraleph/status/559477604659773440
// merge two sorted arrays in O(1) memory and O(n) time. nice excercise
// sort array of length k + m such that a[0] <= ... <= a[k-1] & a[k] <= ... <= a[k+m-1], in place in O(k+m) time
#![allow(unstable)]
// data.len() == k + m
fn merge(data: &mut [u8], k: usize) {
// M = data[..a] is merged
// A = data[a..b] is initially the first sub-array, of length k
if address :is ["To"] ["servo@noreply.github.com"] {
if header :is ["X-GitHub-Reason"] ["manual", "author", "comment", "mention", "assign"] {
fileinto "Mozilla.Servo.mentions";
stop;
}
if header :matches ["Subject"] "Re: *" {
fileinto "Mozilla.Servo.replies";
stop;
}
fileinto "Mozilla.Servo";