Created
April 1, 2012 21:24
-
-
Save seeRead/2278904 to your computer and use it in GitHub Desktop.
github flavored markdown preview script
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 | |
raise "gimme a file!" unless ARGV[0] | |
# This script allows for quick github-flavored-markdown previewing. | |
# Inspiration/code from the following | |
# http://grosser.it/2009/06/17/quick-markdown-preview-for-github-readme/ | |
# https://gist.github.com/1300939 | |
# https://github.com/alampros/Docter | |
# http://blog.steveklabnik.com/posts/2011-12-21-redcarpet-is-awesome | |
# | |
# You will need the following gems: | |
# * redcarpet | |
# * pygments.rb | |
# | |
# So try running: | |
# gem install redcarpet #may require sudo for you | |
# gem install pygments.rb | |
# | |
# If you are on non-Debain OS, you will need to edit the last line to use your preferred browser. | |
# | |
# For redcarpet/markdown options, see https://github.com/tanoku/redcarpet/blob/master/README.markdown | |
# For pygment options, see http://pygments.org/docs/ | |
# | |
# Make sure to chmod +x this script. | |
# c.f. http://blog.steveklabnik.com/posts/2011-12-21-redcarpet-is-awesome | |
require 'rubygems' | |
require 'redcarpet' | |
require 'pygments.rb' | |
class HTMLwithPygments < Redcarpet::Render::XHTML | |
def doc_header() | |
'<!DOCTYPE html><html><head><style>' + Pygments.css('.highlight',:style => 'perldoc') + '</style></head><body>' | |
end | |
def doc_footer() | |
'</body></html>' | |
end | |
def block_code(code, language) | |
if language && !language.empty? | |
Pygments.highlight(code, :lexer => language, :options => {:encoding => 'utf-8', :linenos=>'table'}) | |
else | |
"<pre><code>#{code}</code></pre>" | |
end | |
end | |
# Add html for encloding the highlighted code block | |
def bc_open(opts) | |
"<div class=\"highlight\"><pre>" | |
end | |
# Close it | |
def bc_close(opts) | |
"</pre></div>\n" | |
end | |
# for better TOC | |
def header(text, header_level) | |
text_slug = text.gsub(/\W/, "_").downcase | |
"<h#{header_level} id='#{text_slug}'>#{text}</h#{header_level}>" | |
end | |
end | |
def markdown(text) | |
renderer = HTMLwithPygments.new(optionize([ | |
:hard_wrap | |
])) | |
markdown = Redcarpet::Markdown.new(renderer, optionize([ | |
:strikethrough, | |
:autolink, | |
:fenced_code_blocks, | |
])) | |
markdown.render(text) | |
end | |
def optionize(options) | |
#options.each_with_object({}) { |option, memo| memo[option] = true } #1.9 ruby only | |
options.inject({}) {|memo, option| memo[option] = true; memo} #http://stackoverflow.com/questions/5481009/why-is-enumerableeach-with-object-deprecated | |
end | |
File.open('/tmp/markdown.html', 'w+') {|f| f.write(markdown(ARGF.read)) } | |
#exec "chromium-browser /tmp/markdown.html" | |
exec "gnome-open /tmp/markdown.html" #gnome (ubuntu specific) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment