Skip to content

Instantly share code, notes, and snippets.

@plexus
Created April 14, 2014 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plexus/10657334 to your computer and use it in GitHub Desktop.
Save plexus/10657334 to your computer and use it in GitHub Desktop.
require 'kramdown'
class ShoesDown
attr_reader :app, :kramdown_document, :type, :value, :attr, :children, :options
def initialize(markdown)
@kramdown_document = Kramdown::Document.new(markdown, input: 'GFM')
end
def draw(app)
@app = app
convert(kramdown_document.root)
end
# @param el [Kramdown::Element] The element to convert
def convert(el)
@type, @value, @attr, @children, @options =
el.type, el.value, el.attr, el.children, el.options
send(type)
end
def convert_children
children.each {|element| convert(element) }
end
# Each Kramdown element type gets corresponds with a method here,
# See https://github.com/gettalong/kramdown/blob/master/lib/kramdown/element.rb for a list of types
def method_missing(type, *args)
puts "Kramdown #{type} element not supported"
convert_children
end
def text
app.para value
end
def blank
app.para value
end
def header
case options[:level]
when 1
app.banner children.map(&:value).join
when 2
app.title children.map(&:value).join
when 3
app.subtitle children.map(&:value).join
end
end
end
Shoes.app do
ShoesDown.new(<<MD).draw(self)
# Hello from Markdown
## Subtitles and all
Hello good friends, Markdown and Shoes can play along just fine.
MD
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment