#!/usr/bin/env ruby # convert indented itpn packets (validated/canonical form) to markdown indent_width = 2 heading_depth = 0 def read_packets(io) packets = [] current = [] io.each_line do |raw| line = raw.chomp next if line.empty? if line[/\A */].size == 0 && !current.empty? packets << current current = [] end current << line end packets << current unless current.empty? packets end def packet_to_markdown(packet, indent_width, heading_depth) packet.map do |line| spaces = line[/\A */].size level = spaces / indent_width text = line.lstrip if level < heading_depth "#" * (level + 1) + " " + text else (" " * (level - heading_depth)) + "* " + text end end.join("\n") end files = ARGV.empty? ? ["-"] : ARGV files.each do |path| io = path == "-" ? STDIN : File.open(path, "r") read_packets(io).each do |packet| puts packet_to_markdown(packet, indent_width, heading_depth) puts end io.close unless path == "-" end