Skip to content

Commit

Permalink
Implemented [limit] method for String.
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspard committed Jan 15, 2011
1 parent f69477e commit 611e5d8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/zena/code_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def step
start_group :value, v
end
start_group :tag, "#{trailing}>"
elsif dotag = scan(/<([^>]+)do\s*=([^>]+)>/)
elsif dotag = scan(/<(\w+)([^>]*?)do\s*=('|")([^\3]*?[^\\])\3([^>]*?)(\/?)>/)
if dotag =~ /\A<(\w+)([^>]*?)do\s*=('|")([^\3]*?[^\\])\3([^>]*?)(\/?)>/
start_group :tag, "<#{$1}#{$2}"
start_group :tag, "do="
Expand Down
18 changes: 15 additions & 3 deletions lib/zena/core_ext/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def url_name
'%' + $1.unpack('H2' * $1.size).join('%').upcase
end.tr(' ', '-')
end

# Retrieve original title from an url_name
def self.from_url_name(str)
CGI.unescape(str.tr('-', ' '))
Expand All @@ -39,7 +39,7 @@ def to_filename
'%' + $1.unpack('H2' * $1.size).join('%').upcase
end
end

# Retrieve original title from filename
def self.from_filename(str)
CGI.unescape(str.gsub('+', '%2B'))
Expand All @@ -49,7 +49,7 @@ def url_name!
replace(url_name)
self
end

def to_filename!
replace(to_filename)
self
Expand Down Expand Up @@ -88,4 +88,16 @@ def abs_path(root)
(root + path).join('/')
end

# Limit the number of characters and append the 'readmore' argument
# or "…" by default if the string is longer then limit.
# If you limit to '20' characters, the final size will max 20 + the size
# of the readmore argument.
def limit(size, readmore = '…')
if self.size > size
self[0..(size-1)] + readmore
else
self
end
end

end
15 changes: 10 additions & 5 deletions lib/zena/parser/zafu_rules.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
=begin
FIXME: remove if tests pass.
module Zena
module Parser
Expand All @@ -22,6 +24,7 @@ def start(mode)
# puts "[#{@space_before}(#{@method})#{@space_after}]"
if @params =~ /\A([^>]*?)do\s*=('|")([^\2]*?[^\\])\2([^>]*)\Z/
#puts $~.to_a.inspect
# we have a sub 'do'
@params = parse_params($1)
@sub_do = $3 # this is used by replace_with
Expand Down Expand Up @@ -150,11 +153,12 @@ def scan_tag(opts={})
opts.merge!(:method=>$1, :params=>$2)
opts.merge!(:text=>'') if $3 != ''
make(:void, opts)
elsif @text =~ /\A<(\w+)([^>]*?)do\s*=('([^>]*?[^\\]|)'|"([^>]*?[^\\]|)")([^>]*?)(\/?)>/
#puts "DO:#{$~.to_a.inspect}" # do tag
#elsif @text =~ /\A<(\w+)([^>]*?)do\s*=('([^>]*?[^\\]|)'|"([^>]*?[^\\]|)")([^>]*?)(\/?)>/
elsif @text =~ /\A<(\w+)([^>]*?)do\s*=('|")([^\3]*?[^\\])\3([^>]*?)(\/?)>/
puts "DO:#{$~.to_a.inspect}" # do tag
eat $&
opts.merge!(:method=>($4||$5), :html_tag=>$1, :html_tag_params=>$2, :params=>$6)
opts.merge!(:text=>'') if $7 != ''
opts.merge!(:method=> $4, :html_tag=>$1, :html_tag_params=>$2, :params=>$5)
opts.merge!(:text=>'') if $6 != ''
make(:void, opts)
elsif @options[:form] && @text =~ /\A<(input|select|textarea|form)([^>]*?)(\/?)>/
eat $&
Expand Down Expand Up @@ -236,4 +240,5 @@ def scan_style
end
end # ZafuRules
end # Parser
end # Zena
end # Zena
=end
2 changes: 2 additions & 0 deletions lib/zena/use/zafu_safe_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def self.join_proc
safe_method_for String, :urlencode => {:class => String, :pre_processor => true, :method => :url_name}
safe_method_for String, :to_i => {:class => Number, :pre_processor => true}
safe_method_for String, :to_s => {:class => String, :pre_processor => true}
safe_method_for String, [:limit, Number] => {:class => String, :pre_processor => true}
safe_method_for String, [:limit, Number, String] => {:class => String, :pre_processor => true}
safe_method_for Number, :to_s => {:class => String, :pre_processor => true}
safe_method_for Object, :blank? => Boolean

Expand Down
5 changes: 4 additions & 1 deletion test/integration/zafu_compiler/display.yml
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,7 @@ admin_links_for_anon:
src: "<r:admin_links>hello</r:admin_links>"
res: ''

# ---- fixed to here
string_limit:
src: "<div do=\"text.limit(10, %Q{<a href='#{url}'>read more</a>})\"/>"
tem: "<div><%= (@node.prop['text'] ? @node.prop['text'].limit(10, \"<a href='#{zen_url(@node)}'>read more</a>\") : nil) %></div>"
res: "<div>status tex<a href='http://test.host/oo/projects-list/Clean-Water-project/page22.html'>read more</a></div>"
21 changes: 21 additions & 0 deletions test/unit/core_ext_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ def test_abs_rel_path
end
end # A string with accents

context 'A long string' do
subject do
"12345678901234567890"
end

should 'limit size on limit' do
assert_equal '123456789012345…', subject.limit(15)
end

should 'append readmore argument on limit' do
assert_equal '123456789012345 <a href="">read more</a>', subject.limit(15, ' <a href="">read more</a>')
end

should 'not append readmore argument if limit is not reached' do
assert_equal '12345678901234567890', subject.limit(25, ' <a href="">read more</a>')
end

should 'not raise on negative limit' do
assert_equal ' <a href="">read more</a>', subject.limit(-35, ' <a href="">read more</a>')
end
end # A string with accents
end

class DirExtTest < Test::Unit::TestCase
Expand Down

0 comments on commit 611e5d8

Please sign in to comment.