Created
December 31, 2011 03:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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