
Untitled
By: a guest on
Jul 20th, 2012 | syntax:
None | size: 1.07 KB | hits: 13 | expires: Never
#!/usr/bin/env ruby
require 'cgi'
require 'redcarpet'
__DIR__ = File.dirname(__FILE__)
pygments_loaded = begin
require 'pygments'
rescue LoadError
false
else
class HTMLWithPygments < Redcarpet::Render::HTML
def block_code(code, language)
if language
Pygments.highlight(code, :lexer => language)
else
"<pre><code>#{CGI.escapeHTML code}</code></pre>"
end
end
end
true
end
ghfm_izer = Redcarpet::Markdown.new(
pygments_loaded ? HTMLWithPygments : Redcarpet::Render::HTML,
:fenced_code_blocks => true,
:autolink => true,
:space_after_headers => true
)
css_paths = [
File.expand_path('./ghfmd_github.css', __DIR__),
File.expand_path('./ghfmd_github2.css', __DIR__),
]
css_link_html = css_paths.collect {|p|
%<<link href="#{p}" media="screen" rel="stylesheet" type="text/css"/>>
}.join("\n")
ghfm_html = ghfm_izer.render(STDIN.read)
html = %<<html>
<head>
#{css_link_html}
</head>
<body style="background:#EEE;">
<div id="readme">
<article class="markdown-body">
#{ghfm_html}
</article>
</div>
</body>
</html>>
puts html