forked from jboss-developer/jboss-eap-quickstarts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-flavored-markdown.rb
executable file
·64 lines (58 loc) · 1.98 KB
/
github-flavored-markdown.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env ruby
# Processor for Github flavored markdown, inspired by:
# https://github.com/alampros/Docter/blob/master/bin/github-flavored-markdown.rb
#
# Current version of this script can be found here:
# https://gist.github.com/1300939
#
# Adapted for Redcarpet version 2 by Ralph von der Heyden
# http://github.com/ralph
# http://twitter.com/ralph
# http://www.rvdh.de
#
# You will need the following gems:
# * redcarpet version 2
# * albino
#
# You also need the pygments library for syntax highlighting:
# sudo easy_install pygments
# Make sure the pygmentize bin is in your $PATH, or link it like that:
# sudo ln -s /usr/local/bin/pygmentize /usr/bin
#
# Make sure to chmod +x this script.
#
# Usage:
# github-flavored-markdown.rb document.md
require 'rubygems'
require 'redcarpet'
require 'pygments.rb'
# create a custom renderer that allows highlighting of code blocks
class HTMLWithPygmentsAndPants < Redcarpet::Render::HTML
include Redcarpet::Render::SmartyPants
def block_code(code, language)
Pygments.highlight(code, :lexer => language, :options => {:encoding => 'utf-8'})
end
end
def markdown(text)
renderer = HTMLWithPygmentsAndPants.new(optionize [
:with_toc_data,
:xhtml
])
markdown = Redcarpet::Markdown.new(renderer, optionize([
:fenced_code_blocks,
:no_intra_emphasis,
:tables,
:autolink,
:strikethrough,
:space_after_headers,
:with_toc_data
]))
toc = Redcarpet::Markdown.new(Redcarpet::Render::HTML_TOC).render(text)
text.gsub!("\[TOC\]", toc)
rendered = markdown.render(text)
'<html><head><title>README</title><link href="https://raw.github.com/pmuir/github-flavored-markdown/gh-pages/shared/css/documentation.css" rel="stylesheet"></link><link href="https://raw.github.com/github/github-flavored-markdown/gh-pages/shared/css/pygments.css" rel="stylesheet"></link></head><body>' + rendered + '</body></html>'
end
def optionize(options)
options.each_with_object({}) { |option, memo| memo[option] = true }
end
puts markdown(ARGF.read)