class Smeagol::Controller
Shared controller.
Attributes
Access to Gollum::Wiki
.
Public Class Methods
Initialize new Controller
instance.
wiki - Gollum::Wiki
# File lib/smeagol/controller.rb, line 10 def initialize(wiki) @wiki = wiki #@views = Hash.new{ |h,k| h[k]={} } #@media = Hash.new{ |h,k| h[k]={} } #@preloaded = {} end
Public Instance Methods
Collect a list of all files in assets directory. These files are never versioned.
# File lib/smeagol/controller.rb, line 97 def assets files = collect_files(wiki.path, 'assets') filter(files) end
# File lib/smeagol/controller.rb, line 133 def collect_files(base, offset) list = [] dir = ::File.join(base, offset) return list unless File.directory?(dir) ::Dir.entries(dir).each do |path| next if path == '.' or path == '..' subdir = ::File.join(dir, path) if ::File.directory?(subdir) sublist = collect_files(base, File.join(offset, path)) list.concat(sublist) else list << ::File.join(offset, path) end end list end
Filter files according to settings `include` and `exclude` fields. Selection block can be given to further filter the list.
files - Array of wiki files to be filtered.
Returns [Array<String>].
# File lib/smeagol/controller.rb, line 157 def filter(files, &selection) result = [] files.map do |file| path = (String === file ? file : file.path) unless settings.include.any?{ |x| File.fnmatch?(x, path) } # TODO: If we enforce the use of underscore the we would # not need to filter out settings, partials and static locations. # exclude settings file next if path == Settings::FILE # # exlcude assets # next if path.index('assets') == 0 # exclude template directory (TODO: future version may allow this) next if path.index(settings.partials) == 0 # exclude any files starting with `.` or `_` next if path.split('/').any? do |x| x.start_with?('_') or x.start_with?('.') end # exclude any files specifically exluded by settings next if settings.exclude.any? do |x| ::File.fnmatch?(x, path) || x.end_with?('/') && path.index(x) == 0 end end result << file end result = result.select(&selection) if selection result end
Collect a list of all posts.
# File lib/smeagol/controller.rb, line 73 def posts(version='master') list = [] wiki_files.each do |file| next unless Gollum::Page === file && file.post? list << view(file, version) end list end
Render wiki file.
wiki_file - Gollum::Page
or Gollum::File
. version - Commit id, branch or tag.
Returns [String].
# File lib/smeagol/controller.rb, line 192 def render(wiki_file, version='master') view = view(wiki_file, version) render_view(view) end
Render view.
view - Views::Base
subclass.
Returns [String].
# File lib/smeagol/controller.rb, line 202 def render_view(view) if view.layout content = Mustache.render(view.layout, view) else content = view.content end return content end
Public: The Smeagol
wiki settings. These can be found in the _settings.yml file at the root of the repository. This method caches the settings for all subsequent calls.
TODO: Should settings be coming from current file or from repo version?
This can be tricky. Right now they come from current file, but In future we probably may need to split this into two config files. One that comes from version and one that is current.
Returns a Settings
object.
# File lib/smeagol/controller.rb, line 30 def settings @settings ||= Settings.load(wiki.path) end
Lookup view by wiki file and version.
# File lib/smeagol/controller.rb, line 39 def view(wiki_file, version='master') case wiki_file when Gollum::Page if wiki_file.post? Views::Post.new(self, wiki_file, version) else Views::Page.new(self, wiki_file, version) end when Gollum::File # TODO: Support for other markup formats. if wiki_file.extname == '.mustache' Views::Form.new(self, wiki_file, version) else nil #Views::Raw.new(self, wiki_file, version) end end end
Collect a list of all views.
# File lib/smeagol/controller.rb, line 63 def views(version='master') list = [] wiki_files.each do |file| view = view(file, version) list << view if view end list end
Returns a list of filtered wiki files.
# File lib/smeagol/controller.rb, line 58 def wiki_files filter(wiki.files + wiki.pages) end