Skip to content

Instantly share code, notes, and snippets.

@cabo
cabo / rfc7405.rb
Created July 2, 2023 08:53
ABNF: Convert RFC 7405 case-sensitive strings to RFC 5234
s = ARGF.read
s.gsub!(/%s"([^"]+)"/) {
"%x#{$1.chars.map{|c|c.ord.to_s(16)}.join(".")}"
}
puts s
@cabo
cabo / jsondiff.rb
Created March 10, 2023 09:46
Diff two JSON files after normalizing them with jq
#!/usr/bin/env ruby
require 'open3'
require 'tempfile'
require 'shellwords'
if ARGV.size != 2
warn "** usage: jsondiff.rb j1 j2"
exit 63
end
files = ARGV.dup.map do |arg|
#!/usr/bin/env ruby
require 'rexml/document'
require 'open3'
default_fn = "svg-fig-00"
d = REXML::Document.new(ARGF)
REXML::XPath.each(d.root, "//svg:svg", {"svg" => "http://www.w3.org/2000/svg"}) do |x|
fn = default_fn.succ!
@cabo
cabo / tag-report.rb
Created September 12, 2021 22:13
What is the state of consumption of the various ranges available for allocating CBOR tags?
#!/usr/bin/env ruby -Ku
# coding: utf-8
require 'rexml/document'
require 'open-uri'
# require 'open-uri/cached'
CBOR_TAGS = "https://www.iana.org/assignments/cbor-tags/cbor-tags.xml"
ACCEPT_XML = {"Accept" => "application/xml"}
@cabo
cabo / minutes-wurst.rb
Created September 5, 2021 15:50
Get recent minutes for an IETF WG as a big Wurst. Useful for mining and searching. Mining PDF not yet implemented.
#!/usr/bin/env ruby -Ku
Encoding.default_external = "UTF-8" # wake up, smell the coffee
require 'kramdown'
# FIX THIS dirname:
# You need to be in the root of the proceedings directory as obtained via rsync,
# e.g. in my case via: rsync -av rsync.ietf.org::proceedings ~/std/proceedings
# Warning: ~ 50 GB!
def remove_indentation(s)
l = s.lines
indent = l.grep(/\S/).map {|l| l[/^\s*/].size}.min
l.map {|l| l.sub(/^ {0,#{indent}}/, "")}.join
end
class Array
def uniq_c
each_with_object(Hash.new(0)) { _2[_1] += 1 }
end
end
#!/usr/bin/env ruby -Ku -i.bak
fail "Usage: section1.rb foo.md" unless ARGV.size == 1
s = ARGF.read
SECTIONS_RE = /Section(?:s? (?:[\w.]+, )*[\w.]+,? and)? [\w.]+/
puts s.gsub(/(#{SECTIONS_RE}\s+of)\s+{{([-\w.]+}})/) { "{{#$1 #$2" }
# Note that we don't fix "Section 3 and 5 of {{foo}}" into "Sections 3 and 5 of {{foo}"
# -- that must be done manually, with all the not so easy to recognize section references
#!/usr/bin/env ruby -Ku -pi.bak
$_.sub!(/^(.*?h')([A-Fa-f0-9]{67,})'/) { # 67!!! 1 byte slop, should be 65
prefix = $1
indent = " " * prefix.size
parts = $2.scan(/.{1,64}/)
"#{prefix}#{parts.join("\n#{indent}")}'"
}
@cabo
cabo / cbor-date-time.rb
Last active April 8, 2018 16:53
Play around with CBOR for Time and DateTime in Ruby
#!/usr/bin/env ruby
require 'cbor'
# Assume that the cbor-diag gem is installed, but don't use it directly
def pretty(item)
IO.popen("cbor2pretty.rb", "wb") {|io| io.write(item)}
end
puts "# Show that the default Time representation is Tag 1"
t = Time.now