LoginSignup
9
8

More than 5 years have passed since last update.

Pygments.rb を使う

Posted at

シンタックスをハイライトしたいとき便利。PythonのpygmentsにFFIで繋ぎにいっているそうです。狂気じみてていいですね。

Python をインストール

独自の便利ツールを入れます。こちらの記事に詳しいです。 homebrewで入れる時は、easy_installをはじめとした色々な実行ファイルが謎のディレクトリ(/usr/local/share/python)に配置されるのでシンボリックリンクなどを貼るか$PATHをいじる。

Pygments のインストール

setup
pip install pygments
ln -s /usr/local/share/python/pygmentize /usr/local/bin/pygmentize

やってみよう

kaiseki.rb
require 'pygments'

Pygments.start '/usr/local/bin/pygmentize'

code = File.read '/Users/udzura/.rvm/gems/ruby-1.9.3-p194@samples/gems/sinatra-1.3.3/lib/sinatra/base.rb'
puts Pygments.highlight code, lexer: 'ruby', formatter: 'terminal'

http://gyazo.udzura.jp/ad1d5ced39ece3d296eef4ebd6b51e5a.png

おまけ

pry-coolline というルッビージェムでは、自作で構文解析器とかを実装してるっぽくてヤバそう

言及

Rubyでコードブロックに色を付けるときのこと で「ときどきSegmentation faultになる」と言っているが、そんなに落ちるような印象を受けなかった。バージョンが上がり安定しているのかもしれない。「pythonの環境をちゃんと整えれば」がどれくらい整える感じなのか分からない……。

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8