Capturing the first paragraph of a text block using RoR?

The name pretty much explains this. I am writing a block blog for myself with Ruby 1.9.3 and Rails 3.2.2, and I need to be able to grab the first paragraph from the message (which is stored as a text unit in the database) for use as a summary message for the first page. What is the easiest way to do this?

Google has found very little on this issue; this is not like a normal need.

+3
source share
3 answers

The answer becomes obvious as soon as you start thinking about what exactly defines a “paragraph” in your world.

If this is the first list of characters following two newlines? Sort of

str.split("\n\n", 2)[0]

can work.

HTML, <p>, nokogiri

Nokogiri::HTML.parse(input_string).css('p').first.text

, . , , , .. . , .

+12

, -, RoR, Holger Just Nokogiri RedCloth, html:

Nokogiri::HTML.parse(raw RedCloth.new(input_string).to_html).css('p').first.text
0

str.lines [0] ... truncate() ActionView Helper,

-1

All Articles