fabianmoronzirfas
3/9/2016 - 9:22 AM

Jekyll renderer for Marked.app

Jekyll renderer for Marked.app

#!/usr/bin/ruby
#  Github-flavored markdown to HTML, in a command-line util.
#
#  $ cat README.md | ./ghmarkdown.rb
#
#  Notes:
#
#  You will need to install Pygments for syntax coloring
#
#    $ pip install pygments
#
#  Install the gems rdiscount, albino, and nokogiri
#
#  To work with http://markedapp.com/ I also had to
#    $ sudo ln -s /usr/local/bin/pygmentize /usr/bin
#

require 'rubygems'
require 'rdiscount'
require 'albino'
require 'nokogiri'
require 'enumerator'

def markdown(text)
  options = [:smart,]
  text.gsub!(/---(.*)---/m, '')
  # Replace {% highlight lang [linenos] %}<data>{% endhighlight %} with colourised code.
  text.gsub!(/\{\{( *)?"(.*?)"\}\}/, '\1\2')
  text.gsub!(/^\{% highlight (.+?) (linenos)? ?%\}(.*?)^\{% endhighlight %\}/m) do |match|
    data = Albino.colorize($3, $1).to_s
    if $2
      pre, data, post = data.split(/<.?pre>/)
      # data.gsub!(/^(.+)/, '<span class="lineno">#</span>\1')
      data = data.split(/\n/)
      data = data.each_with_index.map do |line, index|
        "<span class='lineno'>#{index + 1}</span>#{line}\n"
      end
      data = "#{pre}<pre>#{data}</pre>#{post}"
    end
    data
  end
  RDiscount.new(text, *options).to_html
end

puts markdown(ARGF.read)