Module: ActionView::Helpers::TextHelper
(http://api.rubyonrails.com)
auto_link(text, link = :all, href_options = {}, &block)
テキストに含まれるURLとメールアドレスをAタグで囲えるメソッド。
パラメーター
- text:
- 置き換えたいURLやメールアドレスが含まれるテクスト
- link:
- :all(default), :email_addresses, :urlsの3つから選べる。:allはURLとメールアドレスをすべて変換。
- href_options:
- 属性をハッシュで指定できる。{:target => '_blank'}など。
- &block:
- 置き換えの対象のURLやメールアドレスをブロックで処理できる。
auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
# => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
# say hello to <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :urls)
# => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a>
# or e-mail david@loudthinking.com"
auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :email_addresses)
# => "Visit http://www.loudthinking.com/ or e-mail <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
auto_link(post_body, :all, :target => '_blank') do |text|
truncate(text, 15)
end
# => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."simple_format(text)
シンプルなルールで改行をフォーマットできるメソッド
2つ以上の連続する改行コード(\n\n) => pタグ
ひとつの改行コード(\n) => brタグ
my_text = """Here is some basic text...
...with a line break."""
simple_format(my_text)
# => "<p>Here is some basic text...<br />...with a line break.</p>"
more_text = """We want to put a paragraph...
...right there."""
simple_format(more_text)
# => "<p>We want to put a paragraph...</p><p>...right there.</p>"Pタグがうるさく感じるようなら独自ヘルパーで対応。改行コードをbrタグに置き換え。
application_helper.rb
module ApplicationHelper
def linebreaksbr(text)
return text.gsub(/\r\n|\r|\n/, "<br />")
end
endtextilize(text)
Textile記法をHTMLに変換できるメソッド。
Textileのシンタックスについて詳しくはこちら。
このメソッドを使用するにはRedClothモジュールが必要なので注意。
textilize("*This is Textile!* Rejoice!")
# => "<p><strong>This is Textile!</strong> Rejoice!</p>"
textilize("I _love_ ROR(Ruby on Rails)!")
# => "<p>I <em>love</em> <acronym title="Ruby on Rails">ROR</acronym>!</p>"
textilize("h2. Textile makes markup -easy- simple!")
# => "<h2>Textile makes markup <del>easy</del> simple!</h2>"
textilize("Visit the Rails website "here":http://www.rubyonrails.org/.)
# => "<p>Visit the Rails website <a href="http://www.rubyonrails.org/">here</a>.</p>"textilize_without_paragraph(text)
textilizeとほとんど同じだけど、こちらはPタグを付加しない。
truncate(text, length = 30, truncate_string = "...")
テキストをX文字以内に切り詰めるメソッド。
truncate("Once upon a time in a world far far away", 14)
# => Once upon a...
truncate("Once upon a time in a world far far away")
# => Once upon a time in a world f...
truncate("And they found that many people were sleeping better.", 25, "(clipped)")
# => And they found that many (clipped)
truncate("And they found that many people were sleeping better.", 15, "... (continued)")
# => And they found... (continued)


0 コメント:
コメントを投稿