Allow for overriding only specific Mustache templates/partials. (#1719)
* Allow for overriding only specific templates/partials. Resolves #1450.
This commit is contained in:
@@ -37,11 +37,11 @@ Gollum runs on Unix-like systems using its [adapter](https://github.com/gollum/r
|
||||
## INSTALLATION
|
||||
|
||||
1. Ruby is best installed either via [RVM](https://rvm.io/) or a package manager of choice.
|
||||
2. Gollum is best installed via RubyGems:
|
||||
2. Gollum is best installed via RubyGems:
|
||||
```
|
||||
[sudo] gem install gollum
|
||||
```
|
||||
|
||||
|
||||
Installation examples for individual systems can be seen [here](https://github.com/gollum/gollum/wiki/Installation).
|
||||
|
||||
To run, simply:
|
||||
@@ -126,7 +126,7 @@ Gollum comes with the following command line options:
|
||||
| --no-display-metadata | none | Do not render metadata tables in pages. |
|
||||
| --user-icons | [MODE] | Tell Gollum to use specific user icons for history view. Can be set to `gravatar`, `identicon` or `none`. Default: `none`. |
|
||||
| --mathjax-config | [FILE] | Specify path to a custom MathJax configuration. If not specified, uses the `mathjax.config.js` file from repository root. |
|
||||
| --template-dir | [PATH] | Specify custom mustache template directory. |
|
||||
| --template-dir | [PATH] | Specify custom mustache template directory. Only overrides templates that exist in this directory. |
|
||||
| --template-page | none | Use _Template in root as a template for new pages. Must be committed. |
|
||||
| --emoji | none | Parse and interpret emoji tags (e.g. `:heart:`) except when the leading colon is backslashed (e.g. `\:heart:`). |
|
||||
| --lenient-tag-lookup | none | 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. |
|
||||
|
||||
+1
-2
@@ -149,7 +149,7 @@ MSG
|
||||
'Can be set to \'gravatar\' or \'identicon\'. Default: standard avatar.') do |mode|
|
||||
wiki_options[:user_icons] = mode.to_s
|
||||
end
|
||||
opts.on('--template-dir [PATH]', 'Specify custom mustache template directory.') do |path|
|
||||
opts.on('--template-dir [PATH]', 'Specify custom mustache template directory. Only overrides templates that exist in this directory.') do |path|
|
||||
wiki_options[:template_dir] = path
|
||||
end
|
||||
opts.on('--template-page', 'Use _Template.{ext} as a template for new pages.') do
|
||||
@@ -271,7 +271,6 @@ else
|
||||
Precious::App.set(:environment, ENV.fetch('RACK_ENV', :production).to_sym)
|
||||
Precious::App.set(:gollum_path, gollum_path)
|
||||
Precious::App.set(:wiki_options, wiki_options)
|
||||
Precious::App.settings.mustache[:templates] = wiki_options[:template_dir] if wiki_options[:template_dir]
|
||||
|
||||
if cfg = options[:config]
|
||||
# If the path begins with a '/' it will be considered an absolute path,
|
||||
|
||||
+6
-1
@@ -22,6 +22,7 @@ require 'gollum/views/has_page'
|
||||
require 'gollum/views/has_user_icons'
|
||||
require 'gollum/views/pagination'
|
||||
require 'gollum/views/rss.rb'
|
||||
require 'gollum/views/template_cascade'
|
||||
|
||||
require File.expand_path '../helpers', __FILE__
|
||||
|
||||
@@ -110,7 +111,11 @@ module Precious
|
||||
@wiki_title = settings.wiki_options.fetch(:title, 'Gollum Wiki')
|
||||
|
||||
forbid unless @allow_editing || request.request_method == 'GET'
|
||||
Precious::App.set(:mustache, {:templates => settings.wiki_options[:template_dir]}) if settings.wiki_options[:template_dir]
|
||||
|
||||
if settings.wiki_options[:template_dir]
|
||||
Precious::Views::Layout.extend Precious::Views::TemplateCascade
|
||||
Precious::Views::Layout.template_priority_path = settings.wiki_options[:template_dir]
|
||||
end
|
||||
|
||||
@base_url = url('/', false).chomp('/').force_encoding('utf-8')
|
||||
@page_dir = settings.wiki_options[:page_file_dir].to_s
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
module Precious
|
||||
module Views
|
||||
module TemplateCascade
|
||||
def template_priority_path
|
||||
@@template_priority_path
|
||||
end
|
||||
|
||||
def template_priority_path=(path)
|
||||
@@template_priority_path = File.expand_path(path)
|
||||
@template = nil
|
||||
end
|
||||
|
||||
def first_path_available(name)
|
||||
priority = File.join(template_priority_path, "#{name}.#{template_extension}")
|
||||
default = File.join(template_path, "#{name}.#{template_extension}")
|
||||
File.exists?(priority) ? priority : default
|
||||
end
|
||||
|
||||
# Method should track lib/mustache/settings.rb from Mustache project.
|
||||
def template_file
|
||||
@template_file || first_path_available(template_name)
|
||||
end
|
||||
|
||||
# Method should track lib/mustache.rb from Mustache project.
|
||||
def partial(name)
|
||||
path = first_path_available(name)
|
||||
begin
|
||||
File.read(path)
|
||||
rescue
|
||||
raise if raise_on_context_miss?
|
||||
""
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
<nav>
|
||||
<div style="background-color: red;">NAVBAR_OVERRIDE</div>
|
||||
|
||||
<p>Still include an original partial to ensure the fallback works even when nested from an overriden partial:</p>
|
||||
<div>
|
||||
{{>mobilenav}}
|
||||
</div>
|
||||
</nav>
|
||||
@@ -0,0 +1,12 @@
|
||||
<div id="wiki-wrapper" class="page">
|
||||
|
||||
<p>Include an overridden partial:</p>
|
||||
<div id="head">
|
||||
{{#navbar?}}{{>navbar}}{{/navbar?}}
|
||||
</div>
|
||||
|
||||
<div style="background-color: red;">PAGE_OVERRIDE</div>
|
||||
|
||||
{{>wiki_content}}
|
||||
|
||||
</div>
|
||||
@@ -0,0 +1,43 @@
|
||||
# ~*~ encoding: utf-8 ~*~
|
||||
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
||||
|
||||
class TestTemplateCascade < Minitest::Unit::TestCase
|
||||
include Rack::Test::Methods
|
||||
|
||||
def setup
|
||||
@path = cloned_testpath('examples/lotr.git')
|
||||
Precious::App.set(:gollum_path, @path)
|
||||
Precious::App.set(:wiki_options, {template_dir: testpath('examples/template_cascade')})
|
||||
@wiki = Gollum::Wiki.new(@path)
|
||||
end
|
||||
|
||||
def teardown
|
||||
FileUtils.rm_rf(@path)
|
||||
end
|
||||
|
||||
def app
|
||||
Precious::App
|
||||
end
|
||||
|
||||
def test_overridden_page_template_is_used
|
||||
get '/Home'
|
||||
|
||||
assert last_response.body.include?('PAGE_OVERRIDE')
|
||||
end
|
||||
|
||||
def test_overridden_navbar_partial_is_used
|
||||
get '/Home'
|
||||
|
||||
assert last_response.body.include?('NAVBAR_OVERRIDE')
|
||||
end
|
||||
|
||||
def test_overridden_templates_are_ignore_without_template_dir_set
|
||||
Precious::App.set(:wiki_options, {template_dir: nil})
|
||||
|
||||
get '/Home'
|
||||
assert_equal '/Home', last_request.fullpath
|
||||
assert last_response.ok?
|
||||
assert_no_match /PAGE_OVERRIDE/, last_response.body
|
||||
assert_no_match /NAVBAR_OVERRIDE/, last_response.body
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user