]> git.armaanb.net Git - stagit.git/blob - highlight
Redirect stdout to correct file.
[stagit.git] / highlight
1 #!/usr/bin/env ruby
2
3 require 'linguist'
4 require 'pygments'
5
6 stdin = ARGF.file
7
8 filename = stdin.readline.strip  # Read first line (filename).
9 contents = stdin.read            # Read rest (code).
10
11 class FakeBlob < Linguist::FileBlob
12   def initialize(path, content, base_bath=nil)
13     super(path, base_bath)
14     @content = content
15   end
16
17   def data
18     @content
19   end
20
21   def size
22     @content.bytesize
23   end
24 end
25
26 blob = FakeBlob.new(filename, contents)
27 detected = if blob.language
28              blob.language.name
29            else
30              "Text only"
31            end
32
33 # Debugging
34 #puts "File #{filename}"
35 #puts "Code:
36 #{contents}"
37 #print "Language: "
38 #pp detected
39
40 html = Pygments.highlight(contents,
41   :lexer => detected,
42   :formatter => 'html',
43   :options => {
44     :encoding => 'utf-8',
45     :linenos => 'table',
46     :lineanchors => 'loc',
47     :anchorlinenos => true})
48
49 puts html