Skip to content

Instantly share code, notes, and snippets.

@danlucraft
danlucraft / extract.rb
Last active August 26, 2022 17:20
Extract annotations from PDFs with pdf-reader gem
require 'pdf-reader'
require './markup_receiver'
doc = PDF::Reader.new(ARGV[0])
$objects = doc.objects
def is_note?(object)
object[:Type] == :Annot && [:Text, :FreeText].include?(object[:Subtype])
end
@danlucraft
danlucraft / disqus.js
Last active March 19, 2020 20:33
Disqus embed code for a single page javascript app, with optional SSO.
// Enable or reset Disqus for this page as required, with optional SSO.
// There must be a div with id "disqus_thread" when called.
//
// config is required and should have the format:
//
// {
// shortname: "..",
// title: "..",
// identifier: "..",
// url: ".."
@danlucraft
danlucraft / schema.js
Last active January 17, 2020 09:04
Make sure there is always some text after a link inline, so typing after a link is not impossible
inlines: {
link: {
// No link should be present but empty (default normalize should remove
// it if this returns false).
text: function(string) {
return string.length !== 0
},
// If the text node after a link inline is empty...
@danlucraft
danlucraft / day2.js
Created November 14, 2016 17:04
3D from Scratch: Day 2 Source
var PIXEL_WIDTH = 160
var PIXEL_HEIGHT = 120
var WIN_WIDTH = window.innerWidth
var WIN_HEIGHT = window.innerHeight
var ratio_width = WIN_WIDTH / PIXEL_WIDTH
var ratio_height = WIN_HEIGHT / PIXEL_HEIGHT
var pixel_size = Math.floor(Math.min(ratio_width, ratio_height))
require 'jruby/profiler'
profile_data = JRuby::Profiler.profile do
# code to be profiled....
end
my_output_stream = java.io.ByteArrayOutputStream.new
print_stream = java.io.PrintStream.new(my_output_stream)
profile_printer = JRuby::Profiler::GraphProfilePrinter.new(profile_data)
App.DesignsRoute = Ember.Route.extend({
setupController: function(controller) {
controller.set("model", App.Design.findAll());
}
});
App.DesignsController = Ember.ArrayController.extend({
sortProperties: ["name"],
sortAscending: true
});
@danlucraft
danlucraft / vcr_json_aware_matching.rb
Created July 10, 2013 11:31
How to make VCR not fail to match the request body because the JSON hashes have been serialised in a different order
VCR.configure do |v|
v.register_request_matcher :json_aware_body do |r1, r2|
r1.body == r2.body || (
r1.headers["Content-Type"].inspect =~ /json/ and JSON.parse(r1.body) == JSON.parse(r2.body))
end
end
VCR.use_cassette('example', :match_requests_on => [:json_aware_body]) do
#...
@danlucraft
danlucraft / gist:5722203
Last active December 18, 2015 03:59
Jenkins checks out the code into /workspaces/geoip-service/src
cd workspaces/geoip-service
rm -rf tmp
rm -rf src/geoip-service
rm -f geoip-service
mkdir tmp
mv src/* tmp/
mkdir src/geoip-service
mv tmp/* src/geoip-service/
echo $PWD
export GOPATH=$PWD
@danlucraft
danlucraft / gist:5712925
Created June 5, 2013 10:14
Calling return in a block can have slightly subtle consequences. Here are some tests with comments that show how it works.
require 'minitest/autorun'
# Show what return in a block does to return values and code after the block is run
class TestReturn < Minitest::Unit::TestCase
def setup
@post_code_run = false
end
# Method under test
@danlucraft
danlucraft / scrape-starred-posts.rb
Last active December 14, 2015 22:39
A script to scrape all your starred posts from Google Reader (including post content). I downloaded 3100 blog posts starred over the last 5 years.
# Script to download starred posts from Google Reader (including post content).
# Requires that your starred posts are in a public feed.
#
# You can find the public feed by going to "Reader settings", then "Folders and Tags",
# setting "starred items" to public and then going to "view public page". That url is the
# one to pass to the script.
#
# I guess it would work for other folders too.
#
# Usage: