由於會需要在 blog 裡分享命令列指令,所以模仿其他的 CodeRay plug-in 寫了一個新的來高亮命令行。

在 Octopress 的目錄裡只需要新增一個 plugins/coderay_term.rb:

require 'coderay'

module CodeRay
module Scanners

# Scanner for terminal commands
class Term < Scanner

  register_for :term

protected

  def scan_tokens encoder, options
    state = :initial

    until eos?
      case state
      when :initial
        if match = scan(/^\s*\$/)
          encoder.text_token match, :directive
          state = :command_found
        elsif match = scan(/.*[\n$]/)
          encoder.text_token match, :comment
        end

      when :command_found
        if match = scan(/.*[\n$]/)
          encoder.text_token match, :function
          state = :initial
        end

      end
    end

    encoder
  end

end

end
end