Skip to content

Instantly share code, notes, and snippets.

@jronallo
Created December 31, 2011 03:08
Show Gist options
  • Save jronallo/1542659 to your computer and use it in GitHub Desktop.
Save jronallo/1542659 to your computer and use it in GitHub Desktop.
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