]> git.armaanb.net Git - stagit.git/blob - highlight
Add synatx highlighting with Pygment.
[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 detected = Linguist::FileBlob.new(filename).language
12
13 # Debugging
14 #puts "File #{filename}"
15 #puts "Code:
16 #{contents}"
17 #print "Language: "
18 #pp detected
19
20 html = Pygments.highlight(contents,
21   :lexer => detected.name,
22   :formatter => 'html',
23   :options => {
24     :encoding => 'utf-8',
25     :linenos => 'table',
26     :lineanchors => 'loc',
27     :anchorlinenos => true})
28
29 puts html