#!/usr/bin/env ruby require 'linguist' require 'pygments' stdin = ARGF.file filename = stdin.readline.strip # Read first line (filename). contents = stdin.read # Read rest (code). class FakeBlob < Linguist::FileBlob def initialize(path, content, base_bath=nil) super(path, base_bath) @content = content end def data @content end def size @content.bytesize end end blob = FakeBlob.new(filename, contents) detected = if blob.language blob.language.name else "Text only" end # Debugging #puts "File #{filename}" #puts "Code: #{contents}" #print "Language: " #pp detected html = Pygments.highlight(contents, :lexer => detected, :formatter => 'html', :options => { :encoding => 'utf-8', :linenos => 'table', :lineanchors => 'loc', :anchorlinenos => true}) puts html