Skip to content

Instantly share code, notes, and snippets.

View niklasl's full-sized avatar

Niklas Lindström niklasl

View GitHub Profile
import string
import math
CHARS = string.digits + string.lowercase
assert len(CHARS) == 36
CHARS += string.uppercase
assert len(CHARS) == 62
BASE64_CHARS = string.uppercase + string.lowercase + string.digits + "+/"
# -*- coding: UTF-8 -*-
import urllib2
from lxml import etree
from lxml.cssselect import CSSSelector as css
from rdflib import Graph
def check_examples(url, verbose=False):
for title, prologue, text, fmt in find_examples(url):
header = 'Example "%s" as %s: ' % (title, fmt)
import re
regexps = [
r'\d\d',
r'^\d\d$',
r'^\d{2}$',
r'^\d{2,}$',
r'^\d{2,3}$',
r'^\d{2,4}$',
]
@niklasl
niklasl / compute_css_tree.groovy
Created October 17, 2010 14:34
Parses html files (in given dir, matching given glob) and prints out a tree representing the structure (css-selector style).
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='0.9.7')
import org.ccil.cowan.tagsoup.Parser
/**
* Parses html files and prints out a tree representing the structure
* (css-selector style).
*
* Shows the frequency for how many pages a specific subtree appears in. Can
* "anonymize" generated ID:s.
@niklasl
niklasl / gist:905649
Created April 6, 2011 13:41
identifierare-snippet
@property
def identifierare(self):
return "%s %s:%s" % (self.forfattningssamling.kortnamn,
self.arsutgava, self.lopnummer)
fs = require 'fs'
{print} = require 'util'
jsdom = require 'jsdom'
jquery = require 'jquery'
convert = (base=null) ->
$ = @$
@niklasl
niklasl / create_images.coffee
Created July 2, 2011 18:05
Canvas on Node for some simple button images
Canvas = require 'canvas'
fs = require 'fs'
path = require 'path'
render = (width, height, draw, filePath) ->
canvas = new Canvas(width, height)
draw.call canvas.getContext '2d'
out = fs.createWriteStream filePath
@niklasl
niklasl / answers_to_html.coffee
Created July 2, 2011 18:04
Some JSON to HTML
fpath = process.argv[2]
file = require('fs').readFileSync fpath, 'utf-8'
data = JSON.parse file
window = require('jsdom').jsdom(null, null).createWindow()
$ = require('jquery').create window
for key, items of data
dl = $("<dl id='#{key}'></dl>").appendTo $('body')
for num, value of items
dl.append "<dt>#{num}</dt><dd>#{value}</dd>"
@niklasl
niklasl / rdfa_map_sparql_example.mkd
Created July 19, 2011 13:37
A proposal for proxy vocabularies mapped to a mix of vocabularies (using SPARQL 1.1), for use in RDFa.

For the basic case, given data like:

<html lang="en">
  <head><base href="http://example.org/"/></head>
  <body vocab="http://example.org/vocab#" typeof="Site" lang="sv">
    <h1 property="title">The Website</h1>
    By:
    <ul rel="author">
      <li about="/persons/doe" typeof="Person">

Doe

@niklasl
niklasl / context-coerce-terms-as-keys.json
Created October 30, 2011 20:47
JSON-LD Context Designs
{
"iri": "@subject",
"type": "@type",
"@vocab": "http://rinfo.lagrummet.se/ns/2008/11/rinfo/publ#",
"@language": "sv"
"label": "http://www.w3.org/2000/01/rdf-schema#label",
"comment": "http://www.w3.org/2000/01/rdf-schema#comment",
"title": "http://purl.org/dc/terms/title",
"description": "http://purl.org/dc/terms/description",