Created
April 10, 2012 01:11
-
-
Save Watson1978/2347737 to your computer and use it in GitHub Desktop.
GitHub Flavored Markdown parser
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/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