Platinum Partner

highlight code and convert it to image using ruby

 

#!/usr/bin/env ruby
require 'pygmentize' # gem install pygmentize
require 'selenium-webdriver' # gem install selenium-webdriver

exit unless code_path = ARGV.shift

file_path = File.absolute_path 'code.html'
image_path = File.absolute_path 'code_image.png'
code = Pygmentize.process File.read(code_path), :ruby
html = <<-EOT



#{code}

EOT

File.open( file_path, 'w' ) do |file|
	file.write html
end

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "file://#{file_path}"
driver.save_screenshot(image_path)
driver.close

File.delete file_path

# usage imagize.rb /path/to/code/file.rb
#result is similar to :
# http://imgur.com/HYknU7c
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}