質問
私はレールのサイトのコンテンツがされている言語だ。思表示のスニペットの、それぞれの"もっと読む..る"をクリックしてください。
入れは行っていません。簡単な切り捨に原文のまま働かないため、例えば..
>> "This is an [example](http://example.com)"[0..25]
=> "This is an [example](http:"
理想的にしたいのは、著者(オプション)を挿入マーカーの指定に使用する"スニペット"だ250、追加"..."で、例えば..
This article is an example of something or other.
This segment will be used as the snippet on the index page.
^^^^^^^^^^^^^^^
This text will be visible once clicking the "Read more.." link
のマーカーが考えるようにEOFマーカーのかっこは無視でき表示した場合の文書)
を使用してい maruku の値下げに加工(RedClothは非常に偏繊維、BlueClothはバギーですし、私のネイティブ-ルビーパーサを否定peg-値下げおよびRDiscount)
また年の値下げ結果、以下のように表示されたHTML辺)にあり、切り捨HTMLの正しい選択肢となるがこない markdown()
のドキュメント全体での最初の数です。
なので、オプションをして私が考えられることはいは望)..
- 追加は、"短く"オプションのmarukuパーサは、限解析の最初のx英語で"より抜粋"のマーカー.
- Write/ペパーサ-agnosticの値下げに切り詰め呼
- 書/見知的HTML切り捨機能
解決
- 書/見知的HTML切り捨機能
以下のから http://mikeburnscoder.wordpress.com/2006/11/11/truncating-html-in-ruby/, 一部修正しく切り詰めHTML、簡単にでき要素の文字列の前に閉まることが期待される。
>> puts "<p><b><a href=\"hi\">Something</a></p>".truncate_html(5, at_end = "...")
=> <p><b><a href="hi">Someth...</a></b></p>
修正したコード:
require 'rexml/parsers/pullparser'
class String
def truncate_html(len = 30, at_end = nil)
p = REXML::Parsers::PullParser.new(self)
tags = []
new_len = len
results = ''
while p.has_next? && new_len > 0
p_e = p.pull
case p_e.event_type
when :start_element
tags.push p_e[0]
results << "<#{tags.last}#{attrs_to_s(p_e[1])}>"
when :end_element
results << "</#{tags.pop}>"
when :text
results << p_e[0][0..new_len]
new_len -= p_e[0].length
else
results << "<!-- #{p_e.inspect} -->"
end
end
if at_end
results << "..."
end
tags.reverse.each do |tag|
results << "</#{tag}>"
end
results
end
private
def attrs_to_s(attrs)
if attrs.empty?
''
else
' ' + attrs.to_a.map { |attr| %{#{attr[0]}="#{attr[1]}"} }.join(' ')
end
end
end
他のヒント
こうソリューション作品だった。
- HTMLに変換す
- 切り詰めます。
削除のHTMLタグが半分に減と
html_string.gsub(/<[^>]*$/, "")
そして、使用Hpricotにクリーンで密閉じてタグ
html_string = Hpricot( html_string ).to_s
私はこのヘルパー、キャッシュがない性能の問題です。
あなたは何もなく、「^」の文字からなる行を見つけるために正規表現を使用することができます:
markdown_string = <<-eos
This article is an example of something or other.
This segment will be used as the snippet on the index page.
^^^^^^^^^^^^^^^
This text will be visible once clicking the "Read more.." link
eos
preview = markdown_string[0...(markdown_string =~ /^\^+$/)]
puts preview
むしろテキストを切り捨てるしようとするよりも、なぜ2つの入力ボックス、「オープニング宣伝文」とメイン「根性」のための1のための1つを持っていません。そうすれば、あなたの著者はfunkly EOFマーカーのいくつかの並べ替えに依存することなく、ショーの時にされている内容を正確に把握されます。
私は「二つの入力」のアプローチに同意する必要がありますが、あなたは完全なコンテンツを表示するときに1に2つの入力をミックスするバックグラウンド・ロジックを変更することができるので、コンテンツライターは、心配する必要はありませんでしょう。
full_content = input1 + input2 // perhaps with some complementary html, for a better formatting
それはこの場合に適用されますが、完全を期すために、以下の溶液を添加した場合わかりません。あなたはマークダウンレンダリング内容を切り捨てている場合は、メソッドをstrip_tags使用することができます:
truncate(strip_tags(markdown(article.contents)), length: 50)
を発信元: http://devblog.boonecommunitynetwork.com/rails-and-markdown/する
だけの作品シンプルなオプションます:
truncate(markdown(item.description), length: 100, escape: false)