Skip to content

Instantly share code, notes, and snippets.

View denisdefreyne's full-sized avatar

Denis Defreyne denisdefreyne

View GitHub Profile
module Nanoc3::Filters
class Tar < Nanoc3::Filter
type :text => :binary
identifier :tar
def run(filename, params={})
# Stores the filenames of all reps to include
filenames = []
preprocess do
# Find items to put in archives
items_to_archive = items.select { |i| i.identifier =~ /^\/files/ }
# Group them by extension
item_groups = items_to_archive.group_by { |i| i[:extension] }
# For each extension (and their items)...
item_groups.each_pair do |extension, items|
require 'mp3info'
require 'yaml'
rule %r{^dst/.*\.mp3$} => [proc { |tn| tn.sub(/^dst\//, 'src/') }] do |t|
puts "*** copying #{t.source}"
# Copy
FileUtils.mkdir_p(File.dirname(t.name))
FileUtils.copy_file(t.source, t.name)
end
# NEW:
def test_compile_with_two_mutually_dependent_reps
with_site(:compilation_rule_content => 'filter :erb') do |site|
File.open('content/foo.html', 'w') do |io|
io.write('<%= @items.find { |i| i.identifier == "/bar/" }.compiled_content %>')
end
File.open('content/bar.html', 'w') do |io|
io.write('<%= @items.find { |i| i.identifier == "/foo/" }.compiled_content %>')
end
## Implementation
class Arrow
class Either
attr_reader :obj
def initialize(obj)
@obj = obj
class FibSlow
def run(n)
if n == 0
0
elsif n == 1
1
else
run(n-1) + run(n-2)
end
#!/usr/bin/env ruby
# encoding: utf-8
require 'nokogiri'
doc = Nokogiri::HTML('<p>abc</p>')
doc.inner_html = '<p>…</p>'
puts doc.inner_html
# output: <p>…</p>
require 'benchmark'
puts "Setting up..."
array = []
10000.times { |i| array << i }
puts "Running..."
Benchmark.bmbm do |bm|
bm.report('.reverse_each') do
10000.times { array.reverse_each { |i| } }
#!/usr/bin/env ruby
# encoding: utf-8
require 'minitest/autorun'
require 'nokogiri'
class MyEncodingTest < MiniTest::Unit::TestCase
def test_replace_document
doc = Nokogiri::HTML('<p>abc</p>')
▸ ls -l content
-rw-r--r-- 1 ddfreyne staff 29 Jun 9 21:21 header1.h
-rw-r--r-- 1 ddfreyne staff 22 Jun 9 21:57 header2.h
-rw-r--r-- 1 ddfreyne staff 87 Jun 9 21:45 main.c
▸ cat content/header1.h
#include "header2.h"