Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
simple commandline tool to output an outline from a markdown document to the terminal
#!/usr/bin/env ruby
# markdown outline
# simple commandline tool to output an outline from a markdown document to the terminal
require 'rubygems'
require 'redcarpet'
class OutlineRenderer < Redcarpet::Render::HTML
attr_accessor :outline
def initialize
@outline = []
super
end
def header(text, header_level)
self.outline << [header_level, text]
""
end
end
def format_outline(outline)
outline.map do |level, text|
spaces = ''
((level - 1) * 4).times{spaces << ' '}
"#{spaces} #{text}"
end.join("\n")
end
renderer = OutlineRenderer.new
r = Redcarpet::Markdown.new(renderer)
content = r.render(File.read(File.expand_path(ARGV[0])))
outline = renderer.outline
puts format_outline(outline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment