Victor
Victor

Reputation: 23972

Decreasing font size in rails

Im trying to achieve the following effect in rails:

If the text is bigger than x characters then make it smaller, the next x characters smaller, the next character smaller, ad infinitum

for example x = 7 would output the following html

Lorem i<small>psum do<small>lor sit<small> amet, <small>consecte
<small>tur adip<small>isicing</small></small></small></small></small></small>

and the css would be small {font-size: 95%}

What is an elegant way to achieve this?

Upvotes: 0

Views: 667

Answers (2)

Mike Tunnicliffe
Mike Tunnicliffe

Reputation: 10772

moritz's answer seems fine, dry-code attempt at iterative version:

def shrink(what,chunk=5)
  result = ''
  0.step(what.length, chunk) do |i|
    if i<what.length
      result << '<small>' if i>0
      result << what[i,chunk]
    end
  end
  result << '</small>'*(what.length/chunk)
  result
end

Upvotes: 1

moritz
moritz

Reputation: 2478

hm. maybe some helper with some recursion?

def shrink(what)
  if ( what.length > 5)
    "#{what[0,4]}<small>#{shrink(what[5,what.length()-1])}</small>"
  else
    what
  end
end

there is a better way to write the recursive call for certain, but i don't know it right know.

Upvotes: 2

Related Questions