Skip to content

Commit

Permalink
more template stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
David Ricciardi committed Sep 23, 2010
1 parent 96f0c1c commit 2b02485
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
50 changes: 36 additions & 14 deletions lib/wikicloth/wiki_buffer/var.rb
Expand Up @@ -22,13 +22,17 @@ def tag_size=(val)
end

def skip_html?
true
false
end

def tag_start
@tag_start
end

def tag_start=(val)
@tag_start = val
end

def function_name
@fname
end
Expand All @@ -46,18 +50,24 @@ def to_s
ret.to_s
else
# put template at beginning of buffer
ret = @options[:link_handler].include_resource("#{params[0]}".strip,params[1..-1]).to_s
count = 0
tag_attr = self.params[1..-1].collect { |p|
if p.instance_of?(Hash)
"#{p[:name]}=\"#{p[:value]}\""
else
count += 1
"#{count}=\"#{p}\""
end
}.join(" ")
self.data = ret.blank? ? "" : "<template #{tag_attr}>#{ret}</template>"
""
template_stack = @options[:buffer].buffers.collect { |b| b.get_param("__name") if b.instance_of?(WikiBuffer::HTMLElement) &&
b.element_name == "template" }.compact
if template_stack.last == params[0]
"<span class=\"error\">Template loop detected: &#123;&#123;#{params[0]}&#125;&#125;"
else
ret = @options[:link_handler].include_resource("#{params[0]}".strip,params[1..-1]).to_s
count = 0
tag_attr = self.params[1..-1].collect { |p|
if p.instance_of?(Hash)
"#{p[:name]}=\"#{p[:value]}\""
else
count += 1
"#{count}=\"#{p}\""
end
}.join(" ")
self.data = ret.blank? ? "" : "<template __name=\"#{params[0]}\" #{tag_attr}>#{ret}</template>"
""
end
end
end

Expand Down Expand Up @@ -178,7 +188,19 @@ def new_char()

else
self.data += current_char
@tag_start = false if @tag_start
if @tag_start
# FIXME: template params and templates colliding
if @tag_size > 3
if @tag_size == 5
@options[:buffer].buffers << WikiBuffer::Var.new(self.data,@options)
@options[:buffer].buffers[-1].tag_start = false
self.data = ""
@tag_size = 3
return true
end
end
@tag_start = false
end
end

return true
Expand Down
5 changes: 0 additions & 5 deletions lib/wikicloth/wiki_link_handler.rb
Expand Up @@ -99,11 +99,6 @@ def include_resource(resource, options=[])
if self.params.has_key?(resource)
self.params[resource]
else
# FIXME: hack to keep template loops from raising havoc
@include_count ||= 0
@include_count += 1
raise "Page reached maximum number of includes [1000] (possible template loop?)" if @include_count > 100

ret = template(resource)
unless ret.nil?
@included_templates ||= {}
Expand Down
12 changes: 10 additions & 2 deletions test/wiki_cloth_test.rb
Expand Up @@ -12,7 +12,9 @@ class WikiParser < WikiCloth::Parser
when "testparams"
"{{{def|hello world}}} {{{1}}} {{{test}}} {{{nested|{{{2}}}}}}"
when "moreparamtest"
"{{{{{{p}}}|wtf}}}"
"{{{{{test|bla}}|wtf}}}"
when "loop"
"{{loop}}"
end
end
external_link do |url,text|
Expand Down Expand Up @@ -42,7 +44,7 @@ class WikiClothTest < ActiveSupport::TestCase
data = wiki.to_html
assert data =~ /wtf/

wiki = WikiParser.new(:data => "{{moreparamtest|p=othervar|othervar=whoo}}")
wiki = WikiParser.new(:data => "{{moreparamtest|p=othervar|busted=whoo}}")
data = wiki.to_html
assert data =~ /whoo/
end
Expand All @@ -53,6 +55,12 @@ class WikiClothTest < ActiveSupport::TestCase
assert data =~ /hr/
end

test "template loops" do
wiki = WikiParser.new(:data => "{{#iferror:{{loop}}|loop detected|wtf}}")
data = wiki.to_html
assert data =~ /loop detected/
end

test "input with no newline" do
wiki = WikiParser.new(:data => "{{test}}")
data = wiki.to_html
Expand Down

0 comments on commit 2b02485

Please sign in to comment.