Skip to content

Instantly share code, notes, and snippets.

@mattwildig
mattwildig / example.haml
Created December 14, 2012 17:01
A Haml :ruby filter that doesn't use locking and doesn't redirect $stdout.
:ruby_no_lock
puts 'This will go to wherever stdout is pointing'
haml_concat 'This will go into the Haml output'
@mattwildig
mattwildig / bin-foo
Last active December 14, 2015 02:29
Gem with dependencies
#!/usr/bin/env ruby
#this should be in bin/ dir - gist doesn't allow directories anymore
require 'haml'
puts Haml::VERSION
@mattwildig
mattwildig / gist:5305812
Last active December 15, 2015 18:39
Rendered new haml autoclose/self-closing/empty tags docs

Empty (void) Tags: /

The forward slash character, when placed at the end of a tag definition, causes Haml to treat it as being an empty (or void) element. Depending on the format, the tag will be rendered either without a closing tag (:html4 or :html5), or as a self-closing tag (:xhtml).

Taking the following as an example:

%br/

<!doctype html>
<title> </title>
<p class="0">

Calling mark at end of scalar of interest produces something like:

{"line1"=>{"value"=>"line2", "line"=>4},
  "line4"=>{"value"=>"line5\nline6\n", "line"=>7}}

Calling mark at end of previous scalar produces something like:

{"line1"=>{"value"=>"line2", "line"=>1},

"line4"=>{"value"=>"line5\nline6\n", "line"=>4}}

# The relative letter frequencies in English. This is taken from Wikipedia
# (https://en.wikipedia.org/wiki/Letter_frequency) and is based on the Concise
# Oxford dictionary.
ENGLISH_FREQ = [8.167, 1.492, 2.782, 4.253, 12.702, 2.228, 2.015, 6.094, 6.966, 0.153, 0.772, 4.025, 2.406, 6.749, 7.507, 1.929, 0.095, 5.987, 6.327, 9.056, 2.758, 0.978, 2.361, 0.150, 1.974, 0.074]
# Reverse a shift of a single letter by another letter.
# 65 is the ASCII value of `A`, so subtracting 65 from the letter's ord
# value gives a number between 0-25, which we can then do arithmetic
# modulo 26 on.
def unshift(letter, shift_letter)
require 'rbnacl'
# Libsodium provides "raw" ChaCha20, but it’s not in RbNaCl. We can work round that with
# this little class.
class ChaCha20Stream
extend RbNaCl::Sodium
sodium_function :get_stream,
:crypto_stream_chacha20,
%i[pointer ulong_long pointer pointer]