Skip to content

Instantly share code, notes, and snippets.

@RazaChohan
Forked from albertodebortoli/generate_toc.rb
Last active August 29, 2015 14:05
Show Gist options
  • Save RazaChohan/3146e244a755b3af967f to your computer and use it in GitHub Desktop.
Save RazaChohan/3146e244a755b3af967f to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
File.open("your_file.md", '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