Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 20th, 2012  |  syntax: None  |  size: 1.07 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env ruby
  2.  
  3. require 'cgi'
  4. require 'redcarpet'
  5.  
  6. __DIR__ = File.dirname(__FILE__)
  7.  
  8.  
  9. pygments_loaded = begin
  10.   require 'pygments'
  11. rescue LoadError
  12.   false
  13. else
  14.   class HTMLWithPygments < Redcarpet::Render::HTML
  15.     def block_code(code, language)
  16.       if language
  17.         Pygments.highlight(code, :lexer => language)
  18.       else
  19.         "<pre><code>#{CGI.escapeHTML code}</code></pre>"
  20.       end
  21.     end
  22.   end
  23.  
  24.   true
  25. end
  26.  
  27.  
  28. ghfm_izer = Redcarpet::Markdown.new(
  29.   pygments_loaded ? HTMLWithPygments : Redcarpet::Render::HTML,
  30.   :fenced_code_blocks => true,
  31.   :autolink => true,
  32.   :space_after_headers => true
  33. )
  34.  
  35. css_paths = [
  36.   File.expand_path('./ghfmd_github.css', __DIR__),
  37.   File.expand_path('./ghfmd_github2.css', __DIR__),
  38. ]
  39. css_link_html = css_paths.collect {|p|
  40.   %<<link href="#{p}" media="screen" rel="stylesheet" type="text/css"/>>
  41. }.join("\n")
  42.  
  43. ghfm_html = ghfm_izer.render(STDIN.read)
  44.  
  45. html = %<<html>
  46. <head>
  47. #{css_link_html}
  48. </head>
  49. <body style="background:#EEE;">
  50.         <div id="readme">
  51.                 <article class="markdown-body">
  52. #{ghfm_html}
  53.                 </article>
  54.         </div>
  55. </body>
  56. </html>>
  57. puts html