diff --git a/bin/gollum b/bin/gollum index 28f8dc18..4acedd5a 100755 --- a/bin/gollum +++ b/bin/gollum @@ -152,7 +152,7 @@ MSG opts.on('--template-dir [PATH]', 'Specify custom mustache template directory.') do |path| wiki_options[:template_dir] = path end - opts.on('--template-page', 'Use _Template in root as a template for new pages.') do + opts.on('--template-page', 'Use _Template.{ext} as a template for new pages.') do wiki_options[:template_page] = true end opts.on('--lenient-tag-lookup', 'Internal links resolve case-insensitively, will treat spaces as hyphens, and will match the first page found with a certain filename, anywhere in the repository. Provides compatibility with Gollum 4.x.') do diff --git a/lib/gollum.rb b/lib/gollum.rb index 2a8f5a9e..6535d5c8 100644 --- a/lib/gollum.rb +++ b/lib/gollum.rb @@ -33,4 +33,20 @@ module Gollum super(message || "Cannot write #{@dir}/#{@attempted_path}, found #{@dir}/#{@existing_path}.") end end + + class TemplateFilter + @@filters = {} + + def self.add_filter(pattern, &replacement) + @@filters[pattern] = replacement + end + + def self.apply_filters(data) + @@filters.each do |pattern, replacement| + data.gsub!(pattern, replacement.call) + end + data + end + end + end diff --git a/lib/gollum/app.rb b/lib/gollum/app.rb index 8f2131db..2f645b64 100644 --- a/lib/gollum/app.rb +++ b/lib/gollum/app.rb @@ -351,14 +351,11 @@ module Precious get '/create/*' do forbid unless @allow_editing - if settings.wiki_options[:template_page] then - temppage = wiki_page('/_Template') - @template_page = (temppage.page != nil) ? temppage.page.raw_data : 'Template page option is set, but no /_Template page is present or committed.' - end wikip = wiki_page(params[:splat].first) @name = wikip.name @ext = wikip.ext @path = wikip.path + @template_page = load_template(@path) if settings.wiki_options[:template_page] @allow_uploads = wikip.wiki.allow_uploads @upload_dest = find_upload_dest(wikip.fullpath) @@ -638,6 +635,11 @@ module Precious end end + def load_template(path) + template_page = wiki_page(::File.join(path, '_Template')).page || wiki_page('/_Template').page + template_page ? Gollum::TemplateFilter.apply_filters(template_page.raw_data) : nil + end + def update_wiki_page(wiki, page, content, commit, name = nil, format = nil) return if !page || ((!content || page.raw_data == content) && page.format == format)