Class: Smeagol::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/smeagol/controller.rb

Overview

Shared controller.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wiki) ⇒ Controller

Initialize new Controller instance.

Parameters:

  • wiki

    Gollum::Wiki



10
11
12
13
14
15
# 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

Instance Attribute Details

#wikiObject (readonly)

Access to Gollum::Wiki.



18
19
20
# File 'lib/smeagol/controller.rb', line 18

def wiki
  @wiki
end

Instance Method Details

#assetsObject

Collect a list of all files in assets directory. These files are never versioned.



97
98
99
100
# File 'lib/smeagol/controller.rb', line 97

def assets
  files = collect_files(wiki.path, 'assets')
  filter(files)
end

#collect_files(base, offset) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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, &selection) ⇒ Array<String>

Filter files according to settings ‘include` and `exclude` fields. Selection block can be given to further filter the list.

Parameters:

  • files

    Array of wiki files to be filtered.

Returns:

  • (Array<String>)

    Returns Array<String>.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# 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

#posts(version = 'master') ⇒ Object

Collect a list of all posts.



73
74
75
76
77
78
79
80
# 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, version = 'master') ⇒ String

Render wiki file.

Parameters:

  • wiki_file

    Gollum::Page or Gollum::File.

  • version (defaults to: 'master')

    Commit id, branch or tag.

Returns:

  • (String)

    Returns String.



192
193
194
195
# 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) ⇒ String

Render view.

Parameters:

  • view

    Views::Base subclass.

Returns:

  • (String)

    Returns String.



202
203
204
205
206
207
208
209
210
# 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

#settingsObject

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.

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.

Returns:

  • a Settings object.



30
31
32
# File 'lib/smeagol/controller.rb', line 30

def settings
  @settings ||= Settings.load(wiki.path)
end

#view(wiki_file, version = 'master') ⇒ Object

Lookup view by wiki file and version.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

#views(version = 'master') ⇒ Object

Collect a list of all views.



63
64
65
66
67
68
69
70
# 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

#wiki_filesObject

Returns a list of filtered wiki files.



58
59
60
# File 'lib/smeagol/controller.rb', line 58

def wiki_files
  filter(wiki.files + wiki.pages)
end