Make truncate() Unicode-aware
This commit is contained in:
parent
2c5e5a0015
commit
d3e79ea84a
4 changed files with 41 additions and 9 deletions
|
@ -1,5 +1,6 @@
|
|||
# The methods added to this helper will be available to all templates in the application.
|
||||
module ApplicationHelper
|
||||
require 'stringsupport'
|
||||
|
||||
# Accepts a container (hash, array, enumerable, your type) and returns a string of option tags. Given a container
|
||||
# where the elements respond to first and last (such as a two-element array), the "lasts" serve as option values and
|
||||
|
@ -111,15 +112,17 @@ module ApplicationHelper
|
|||
PageRenderer.new(page.revisions.last).display_content
|
||||
end
|
||||
|
||||
def truncate(text, length = 30, truncate_string = '...')
|
||||
return text if text.length <= length
|
||||
len = length - truncate_string.length
|
||||
def truncate(text, *args)
|
||||
options = args.extract_options!
|
||||
options.reverse_merge!(:length => 30, :omission => "...")
|
||||
return text if text.num_chars <= options[:length]
|
||||
len = options[:length] - options[:omission].as_utf8.num_chars
|
||||
t = ''
|
||||
text.split.collect do |word|
|
||||
if t.length + word.length <= len
|
||||
if t.num_chars + word.num_chars <= len
|
||||
t << word + ' '
|
||||
else
|
||||
return t.chop + truncate_string
|
||||
return t.chop + options[:omission]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
<ul>
|
||||
<%- @pages_in_category.each do |page| -%>
|
||||
<li>
|
||||
<%= link_to_existing_page page, truncate(page.plain_name, 35) %>
|
||||
<%= link_to_existing_page page, truncate(page.plain_name, :length => 35) %>
|
||||
</li>
|
||||
<%- end -%>
|
||||
</ul>
|
||||
|
@ -37,7 +37,7 @@
|
|||
<ul style="margin-bottom: 10px">
|
||||
<%- @page_names_that_are_wanted.each do |wanted_page_name| -%>
|
||||
<li>
|
||||
<%= link_to_page(wanted_page_name, @web, truncate(WikiWords.separate(wanted_page_name), 35)) %>
|
||||
<%= link_to_page(wanted_page_name, @web, truncate(WikiWords.separate(wanted_page_name), :length => 35)) %>
|
||||
wanted by
|
||||
<%= @web.select.pages_that_reference(wanted_page_name).collect { |referring_page|
|
||||
link_to_existing_page referring_page
|
||||
|
@ -57,7 +57,7 @@
|
|||
<ul style="margin-bottom: 35px">
|
||||
<%- @pages_that_are_orphaned.each do |orphan_page| -%>
|
||||
<li>
|
||||
<%= link_to_existing_page orphan_page, truncate(orphan_page.plain_name, 35) %>
|
||||
<%= link_to_existing_page orphan_page, truncate(orphan_page.plain_name, :length => 35) %>
|
||||
</li>
|
||||
<%- end -%>
|
||||
</ul>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue