#!/usr/bin/env ruby -Ku

Encoding.default_external = "UTF-8" # wake up, smell the coffee

require 'kramdown'
require 'kramdown-parser-gfm'

options = ''
while /\A-([4bck]+)\z/ === ARGV[0]
  ARGV.shift
  options << $1
end

if /k/ === options              # kramdown
  MARKDOWN_BR = "\\\\\n"
end

if /c/ === options              # commonmark
  MARKDOWN_BR = "\\\n"
end

if /b/ === options              # universal HTML
  MARKDOWN_BR = "<br/>\n"
end

MARKDOWN_BR ||= "  \n"          # original Gruber

module Kramdown

  module Converter

    # Converts an element tree to the kramdown format.
    class Kramdown < Base

      # Argh
      def convert_br(_el, _opts)
        MARKDOWN_BR
      end
    end
  end
end

list_indent = 2
list_indent = 4 if /4/ === options

doc = Kramdown::Document.new(ARGF.read, input: 'GFM', gfm_quirks: 'paragraph_end',
                             list_indent: list_indent)
puts doc.to_kramdown
