Marco Lazzeri
2005-Sep-22  14:21 UTC
Inserting text with omissis ("...") after maximum length
Hi Railers,
Are there any functions or options for defining maximum printable length 
for a string and then, optionally, inserting omissis?
Something like:
irb> object_instance.that_could_have_a_very_long_description
=> "I''m a VERY LONG LONG LONG LONG description"
[ View ]
<%= h object_instance.that_could_have_a_very_long_description,
      :maxlength => 19, omissis => "..." %>
[ Rendered view ]
"I''m a VERY LONG LONG..."
If not I could write one and submit it.
- Marco
François Beausoleil
2005-Sep-22  14:29 UTC
Re: Inserting text with omissis ("...") after maximum length
Hello Marco, Marco Lazzeri said the following on 2005-09-22 10:21:> "I''m a VERY LONG LONG..." > > > If not I could write one and submit it.What you want is the truncate()[1] function. Enjoy ! François [1] http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000382
Derek Wyatt
2005-Sep-22  14:50 UTC
Re: Inserting text with omissis ("...") after maximum length
I didn''t take a huge check for this but is there a way to strip out HTML fragments from the end? str = "This is a long HTML string with a <a href=http://api.rubyonrails.org>link</a> in it." And if you truncate to 70 chars you get: ---- This is a long HTML string with a <a href=http://api.rubyonrails.org>l... <br> Now some more text that will be included in the href above and will look brutal... --- Regs, D François Beausoleil wrote:> Hello Marco, > > Marco Lazzeri said the following on 2005-09-22 10:21: > >> "I''m a VERY LONG LONG..." >> >> >> If not I could write one and submit it. > > > What you want is the truncate()[1] function. > > Enjoy ! > François > > [1] > http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000382 > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Derek Wyatt - C++ / Ruby / Unix Programmer http://derekwyatt.org
Stephen Caudill
2005-Sep-22  15:07 UTC
Re: Inserting text with omissis ("...") after maximum length
How about: <%= truncate(str.gsub!(/<[^>]+>/),70) %> That would strip HTML (tags, not entities) from the string before truncating it... no need to hunt a fragment down then. - Stephen On 9/22/05, Derek Wyatt <derek-xnmnNL99XLeM5I7KlLknWg@public.gmane.org> wrote:> > I didn''t take a huge check for this but is there a way to strip out > HTML fragments from the end? > > str = "This is a long HTML string with a <a > href=http://api.rubyonrails.org>link</a> in it." > > And if you truncate to 70 chars you get: > ---- > This is a long HTML string with a <a > href=http://api.rubyonrails.org>l... > <br> > Now some more text that will be included in the href above and will > look brutal... > --- > > Regs, > D > > François Beausoleil wrote: > > Hello Marco, > > > > Marco Lazzeri said the following on 2005-09-22 10:21: > > > >> "I''m a VERY LONG LONG..." > >> > >> > >> If not I could write one and submit it. > > > > > > What you want is the truncate()[1] function. > > > > Enjoy ! > > François > > > > [1] > > http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000382 > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > Derek Wyatt - C++ / Ruby / Unix Programmer > http://derekwyatt.org > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Derek Wyatt
2005-Sep-22  19:59 UTC
Re: Inserting text with omissis ("...") after maximum length
Well, i actually am currently using: sub(/<[^>]*\Z/, "") which is a bit better i think since it strips out the last one (i don''t want to obliterate all tags) but it''s not great because it doesn''t handle the original problem i gave. It only strips out things like "<b" or "<a href=http://blah" and the like. Regs, D Stephen Caudill wrote:> How about: > > <%= truncate(str.gsub!(/<[^>]+>/),70) %> > > That would strip HTML (tags, not entities) from the string before > truncating it... no need to hunt a fragment down then. > > - Stephen > > On 9/22/05, Derek Wyatt <derek-xnmnNL99XLeM5I7KlLknWg@public.gmane.org> wrote: > >>I didn''t take a huge check for this but is there a way to strip out >>HTML fragments from the end? >> >>str = "This is a long HTML string with a <a >> href=http://api.rubyonrails.org>link</a> in it." >> >>And if you truncate to 70 chars you get: >>---- >>This is a long HTML string with a <a >> href=http://api.rubyonrails.org>l... >><br> >>Now some more text that will be included in the href above and will >>look brutal... >>--- >> >>Regs, >>D >> >>François Beausoleil wrote: >> >>>Hello Marco, >>> >>>Marco Lazzeri said the following on 2005-09-22 10:21: >>> >>> >>>>"I''m a VERY LONG LONG..." >>>> >>>> >>>>If not I could write one and submit it. >>> >>> >>>What you want is the truncate()[1] function. >>> >>>Enjoy ! >>>François >>> >>>[1] >>>http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000382 >>> >>> >>>_______________________________________________ >>>Rails mailing list >>>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>http://lists.rubyonrails.org/mailman/listinfo/rails >>> >> >> >>-- >>Derek Wyatt - C++ / Ruby / Unix Programmer >>http://derekwyatt.org >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Derek Wyatt - C++ / Ruby / Unix Programmer http://derekwyatt.org