Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

highlight code and convert it to image using ruby

DZone's Guide to

highlight code and convert it to image using ruby

·
Free Resource

 

#!/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
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}