Skip to content

Instantly share code, notes, and snippets.

@Watson1978
Created April 10, 2012 01:11
Show Gist options
  • Save Watson1978/2347737 to your computer and use it in GitHub Desktop.
Save Watson1978/2347737 to your computer and use it in GitHub Desktop.
GitHub Flavored Markdown parser
#!/usr/local/bin/ruby
# To parse GitHub Flavored Markdown
#
# Setup
# $ sudo easy_install pygments
# $ sudo gem install pygments.rb
# $ sudo gem install redcarpet
require 'redcarpet'
require 'pathname'
require 'pygments.rb'
class HTMLwithPygments < Redcarpet::Render::XHTML
def doc_header()
ghf_css_path = File.join File.dirname(File.dirname Pathname.new(__FILE__).realpath),
'ghf_marked.css'
# puts Pygments.styles()
# monokai
# manni
# perldoc
# borland
# colorful
# default
# murphy
# vs
# trac
# tango
# fruity
# autumn
# bw
# emacs
# vim
# pastie
# friendly
# native
# '<style>' + Pygments.css('.highlight',:style => 'vs') + '</style>'
if UNSTYLED
'<div class="md"><article>'
else
'<style>' + File.read(ghf_css_path) + '</style><div class="md"><article>'
end
end
def doc_footer
'</article></div>'
end
def block_code(code, language)
Pygments.highlight(code, :lexer => language, :options => {:encoding => 'utf-8'})
end
end
def fromMarkdown(text)
# options = [:fenced_code => true, :generate_toc => true, :hard_wrap => true, :no_intraemphasis => true, :strikethrough => true ,:gh_blockcode => true, :autolink => true, :xhtml => true, :tables => true]
markdown = Redcarpet::Markdown.new(Redcarpet::Render::SmartyHTML,
:fenced_code_blocks => true,
:no_intra_emphasis => true,
:autolink => true,
:strikethrough => true,
:lax_html_blocks => true,
:superscript => true,
:hard_wrap => true,
:tables => true,
:xhtml => true)
markdown.render(text)
end
path = ENV['MARKED_PATH'] # for Marked.app http://itunes.apple.com/jp/app/marked/id448925439?mt=12
path ||= ARGV[0]
puts fromMarkdown(File.read(path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment