summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDan Allen <dallen@redhat.com>2013-06-23 22:44:48 -0600
committerDan Allen <dallen@redhat.com>2013-06-26 00:27:55 -0600
commitdeae0d53464c5b3af5dad2e1b76fa24c661ff69f (patch)
tree2b1dc828507b97c49aa2be877be8f08db7f77410 /lib
parent651b0eba4a827a435d4d093156ddaf389ba079dd (diff)
resolves #438 add basic template cache
Diffstat (limited to 'lib')
-rw-r--r--lib/asciidoctor/document.rb1
-rw-r--r--lib/asciidoctor/renderer.rb22
2 files changed, 22 insertions, 1 deletions
diff --git a/lib/asciidoctor/document.rb b/lib/asciidoctor/document.rb
index 5059db4c..fa7aa586 100644
--- a/lib/asciidoctor/document.rb
+++ b/lib/asciidoctor/document.rb
@@ -610,6 +610,7 @@ class Document < AbstractBlock
render_options[:template_dirs] = @options[:template_dirs]
end
+ render_options[:template_cache] = @options.fetch(:template_cache, true)
render_options[:backend] = @attributes.fetch('backend', 'html5')
render_options[:template_engine] = @options[:template_engine]
render_options[:eruby] = @options.fetch(:eruby, 'erb')
diff --git a/lib/asciidoctor/renderer.rb b/lib/asciidoctor/renderer.rb
index f1794bfe..0f1b56d4 100644
--- a/lib/asciidoctor/renderer.rb
+++ b/lib/asciidoctor/renderer.rb
@@ -3,6 +3,9 @@ module Asciidoctor
# using eRuby templates.
class Renderer
attr_reader :compact
+ attr_reader :cache
+
+ @@global_cache = nil
# Public: Initialize an Asciidoctor::Renderer object.
#
@@ -35,6 +38,13 @@ class Renderer
if (template_dirs = options.delete(:template_dirs))
Helpers.require_library 'tilt'
+ if (template_cache = options[:template_cache]) === true
+ # FIXME probably want to use our own cache object for more control
+ @cache = (@@global_cache ||= Tilt::Cache.new)
+ elsif template_cache
+ @cache = template_cache
+ end
+
view_opts = {
:erb => { :trim => '<>' },
:haml => { :format => :xhtml, :attr_wrapper => '"', :ugly => true, :escape_attrs => false },
@@ -47,8 +57,12 @@ class Renderer
end
slim_loaded = false
+ path_resolver = PathResolver.new
+ # TODO skip scanning a folder if we've already done it for same backend/engine
template_dirs.each do |template_dir|
+ # TODO need to think about safe mode restrictions here
+ template_dir = path_resolver.system_path template_dir, nil
template_glob = '*'
if (engine = options[:template_engine])
template_glob = "*.#{engine}"
@@ -82,7 +96,13 @@ class Renderer
Helpers.require_library 'slim'
end
next unless Tilt.registered? ext_name
- @views[view_name] = Tilt.new(template, nil, view_opts[ext_name.to_sym])
+ if @cache
+ @views[view_name] = @cache.fetch(template, (options = view_opts[ext_name.to_sym])) {
+ Tilt.new(template, nil, options)
+ }
+ else
+ @views[view_name] = Tilt.new template, nil, view_opts[ext_name.to_sym]
+ end
end
require helpers unless helpers.nil?