From 01e18dd460ac11d1771090a36fbb4965c3d8b561 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 7 Apr 2012 11:14:55 -0700 Subject: [PATCH] Add a method to all Template classes that states whether the template sllows running arbitrary Ruby script. This is useful for users of Tilt who want to disallow templates that support script or who want to treat them differently. --- lib/tilt/coffee.rb | 4 ++++ lib/tilt/css.rb | 8 ++++++++ lib/tilt/liquid.rb | 4 ++++ lib/tilt/markdown.rb | 24 ++++++++++++++++++++++++ lib/tilt/radius.rb | 4 ++++ lib/tilt/rdoc.rb | 4 ++++ lib/tilt/template.rb | 9 +++++++++ lib/tilt/textile.rb | 4 ++++ lib/tilt/wiki.rb | 8 ++++++++ 9 files changed, 69 insertions(+) diff --git a/lib/tilt/coffee.rb b/lib/tilt/coffee.rb index ac6d6dc2..fafaac8a 100644 --- a/lib/tilt/coffee.rb +++ b/lib/tilt/coffee.rb @@ -45,6 +45,10 @@ def prepare def evaluate(scope, locals, &block) @output ||= CoffeeScript.compile(data, options) end + + def allows_script? + false + end end end diff --git a/lib/tilt/css.rb b/lib/tilt/css.rb index 021bb8b9..8626db17 100644 --- a/lib/tilt/css.rb +++ b/lib/tilt/css.rb @@ -24,6 +24,10 @@ def evaluate(scope, locals, &block) @output ||= @engine.render end + def allows_script? + false + end + private def sass_options options.merge(:filename => eval_file, :line => line, :syntax => :sass) @@ -67,6 +71,10 @@ def prepare def evaluate(scope, locals, &block) @output ||= @engine.to_css end + + def allows_script? + false + end end end diff --git a/lib/tilt/liquid.rb b/lib/tilt/liquid.rb index 74a63afc..33649111 100644 --- a/lib/tilt/liquid.rb +++ b/lib/tilt/liquid.rb @@ -37,5 +37,9 @@ def evaluate(scope, locals, &block) locals['content'] = locals['yield'] @engine.render(locals) end + + def allows_script? + false + end end end diff --git a/lib/tilt/markdown.rb b/lib/tilt/markdown.rb index 396fb126..e549ad53 100644 --- a/lib/tilt/markdown.rb +++ b/lib/tilt/markdown.rb @@ -37,6 +37,10 @@ def prepare def evaluate(scope, locals, &block) @output ||= @engine.to_html end + + def allows_script? + false + end end # Upskirt Markdown implementation. See: @@ -61,6 +65,10 @@ def evaluate(scope, locals, &block) @engine.evaluate(scope, locals, &block) end + def allows_script? + false + end + # Compatibility mode for Redcarpet 1.x class Redcarpet1 < RDiscountTemplate self.default_mime_type = 'text/html' @@ -116,6 +124,10 @@ def prepare def evaluate(scope, locals, &block) @output ||= @engine.render(data) end + + def allows_script? + false + end end end @@ -140,6 +152,10 @@ def prepare def evaluate(scope, locals, &block) @output ||= @engine.to_html end + + def allows_script? + false + end end # Maruku markdown implementation. See: @@ -161,6 +177,10 @@ def prepare def evaluate(scope, locals, &block) @output ||= @engine.to_html end + + def allows_script? + false + end end # Kramdown Markdown implementation. See: @@ -185,6 +205,10 @@ def prepare def evaluate(scope, locals, &block) @output ||= @engine.to_html end + + def allows_script? + false + end end end diff --git a/lib/tilt/radius.rb b/lib/tilt/radius.rb index 88e1a194..64ec9085 100644 --- a/lib/tilt/radius.rb +++ b/lib/tilt/radius.rb @@ -47,5 +47,9 @@ def evaluate(scope, locals, &block) parser = Radius::Parser.new(context, options) parser.parse(data) end + + def allows_script? + false + end end end diff --git a/lib/tilt/rdoc.rb b/lib/tilt/rdoc.rb index 33450e59..60906002 100644 --- a/lib/tilt/rdoc.rb +++ b/lib/tilt/rdoc.rb @@ -29,5 +29,9 @@ def prepare def evaluate(scope, locals, &block) @output ||= @engine.to_s end + + def allows_script? + false + end end end diff --git a/lib/tilt/template.rb b/lib/tilt/template.rb index 227c24f5..3db4a92a 100644 --- a/lib/tilt/template.rb +++ b/lib/tilt/template.rb @@ -92,6 +92,15 @@ def eval_file file || '(__TEMPLATE__)' end + # Whether or not this template engine allows executing Ruby script + # within the template. If this is false, +scope+ and +locals+ will + # generally not be used, nor will the provided block be avaiable + # via +yield+. + # This should be overridden by template subclasses. + def allows_script? + true + end + protected # Called once and only once for each template subclass the first time # the template class is initialized. This should be used to require the diff --git a/lib/tilt/textile.rb b/lib/tilt/textile.rb index fd4f4c13..c5e89c3d 100644 --- a/lib/tilt/textile.rb +++ b/lib/tilt/textile.rb @@ -20,6 +20,10 @@ def prepare def evaluate(scope, locals, &block) @output ||= @engine.to_html end + + def allows_script? + false + end end end diff --git a/lib/tilt/wiki.rb b/lib/tilt/wiki.rb index dd1f2852..1349eb2f 100644 --- a/lib/tilt/wiki.rb +++ b/lib/tilt/wiki.rb @@ -24,6 +24,10 @@ def prepare def evaluate(scope, locals, &block) @output ||= @engine.to_html end + + def allows_script? + false + end end # WikiCloth implementation. See: @@ -46,5 +50,9 @@ def prepare def evaluate(scope, locals, &block) @output ||= @engine.to_html end + + def allows_script? + false + end end end