Skip to content

Instantly share code, notes, and snippets.

@sauloperez
Created October 21, 2016 10:44
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 sauloperez/a0025306b1de4ebc0a1bf1253ce02d26 to your computer and use it in GitHub Desktop.
Save sauloperez/a0025306b1de4ebc0a1bf1253ce02d26 to your computer and use it in GitHub Desktop.
Markdown TOC generator
#!/usr/bin/env ruby
FILENAME = ARGV[0]
File.open(FILENAME, 'r') do |f|
f.each_line do |line|
forbidden_words = ['Table of contents', 'define', 'pragma']
next if !line.start_with?("#") || forbidden_words.any? { |w| line =~ /#{w}/ }
title = line.gsub("#", "").strip
href = title.gsub(" ", "-").downcase
puts " " * (line.count("#")-1) + "* [#{title}](\##{href})"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment