1

赤布と一緒に構文の強調表示を使用したい。

coderay の説明には次のように書かれています。

Ruby で記述された、選択した言語の高速で簡単な構文の強調表示。RedCloth 統合と LOC カウンターが付属しています。1

しかし、これを行う方法に関するドキュメントが見つかりませんでしたか?

ハイライトを行うコードはどこに配置すればよいですか? 入れようと思ったmodule ApplicationHelperのですが、これでいいのでしょうか?

4

1 に答える 1

3

オプション1:

http://railscasts.com/episodes/207-syntax-highlighting?autoplay=true Rails3 を使用する場合、ヘルパーを次のように変更します。

  def coderay(text) 
    text.gsub!(/\<code(?: lang="(.+?)")?\>(.+?)\<\/code\>/m) do 
      code = CodeRay.scan($2, $1).div(:css => :class) 
      "<notextile>#{code}</notextile>" 
    end 
    return text.html_safe 
  end

HAML を使用する場合は、~ 記号を使用する必要があります。

~ raw textilize(coderay(...))

オプション 2:

CodeRay には RedCloth のサポートが組み込まれています。

  1. 必要な gem ファイルを Gemfile に追加します。
gem "RedCloth", :require => 'redcloth'
gem 'coderay', :require => ['coderay', 'coderay/for_redcloth']
  1. RedCloth で行う場合と同じようにコード文字列をレンダリングします (HAML を使用しているため、= の代わりに ~ を使用します)。
~ raw textilize("Some code here @that should@ be formated.")
~ raw textilize("Some code here @[ruby]that should@ be formated.")
  1. ファイルをレンダリングすることもできます。
# File at /my/file/path/filename.doc
h1. My title 
bc[ruby].. # Nekaj za napisat 
def self.myfun   
puts "asdas" 
end

# Inside your view file
~ raw textilize(File.read("/my/file/path/filename.doc"))

私は2番目のオプションを好みます。

http://en.wikipedia.org/wiki/Textile_(markup_language)で Textile マークアップ言語を使用したスタイリングの詳細を確認できます。

于 2012-01-13T11:49:06.503 に答える