Class: Smeagol::Views::Base

Inherits:
Mustache
  • Object
show all
Defined in:
lib/smeagol/views/base.rb

Overview

Base class for all views.

Direct Known Subclasses

Form, Page, Versions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(master, file, version = 'master') ⇒ Object

Initializes a new mustache view template data object.

Parameters:

  • master

    Master controller, which creates all the views.

  • page

    The individual wiki page that this view represents.

  • version (defaults to: 'master')

    The tagged version of the page.



20
21
22
23
24
25
26
27
# File 'lib/smeagol/views/base.rb', line 20

def initialize(master, file, version='master')
  @master  = master
  @file    = file
  @wiki    = file.wiki
  @version = version || 'master'

  setup_template_path
end

Instance Attribute Details

#fileObject (readonly)

The Gollum::Page or Gollum::File that this view represents.



47
48
49
# File 'lib/smeagol/views/base.rb', line 47

def file
  @file
end

#versionObject (readonly)

The tagged version that is being viewed.



33
34
35
# File 'lib/smeagol/views/base.rb', line 33

def version
  @version
end

#wikiObject (readonly)

The Gollum::Wiki that this view represents.



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

def wiki
  @wiki
end

Instance Method Details

#base_pathObject

The string base path to prefix internal links.



112
113
114
# File 'lib/smeagol/views/base.rb', line 112

def base_path
  wiki.base_path
end

#custom_layoutObject

Value of layout metadata setting.



180
181
182
# File 'lib/smeagol/views/base.rb', line 180

def custom_layout
  ['layout']
end

#custom_layout?Boolean

Does the metadata specify a custom layout?

Returns:

  • (Boolean)


175
176
177
# File 'lib/smeagol/views/base.rb', line 175

def custom_layout?
  .key?('layout')
end

#default_layoutString

Default template.

Returns:

  • (String)

    Returns String The template included with the Smeagol package.



215
216
217
218
219
# File 'lib/smeagol/views/base.rb', line 215

def default_layout
  @default_layout ||= (
    IO.read(LIBDIR + "/templates/layouts/page.mustache")
  )
end

#filenameObject



50
51
52
# File 'lib/smeagol/views/base.rb', line 50

def filename
  file.filename
end

#layoutObject

Get the layout template for the view.



169
170
171
172
# File 'lib/smeagol/views/base.rb', line 169

def layout
  return nil if custom_layout? && !custom_layout
  custom_layout || standard_layout || default_layout
end

#mathjaxObject

Support mathjax?



227
228
229
# File 'lib/smeagol/views/base.rb', line 227

def mathjax
  settings.mathjax
end

The HTML menu generated from the settings.yml file.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/smeagol/views/base.rb', line 81

def menu_html
  menu = wiki.settings.menu
  if !menu.nil?
    html = "<ul>\n"
    menu.each do |item|
      title, href = item['title'], item['href']
      if version != 'master' && item['href'].index('/') == 0
        prefix = version.start_with?('v') ? "/#{version}" : "/v#{version}"
      else
        prefix = ""
      end
      html << "<li class=\"minibutton\"><a href=\"#{prefix}#{href}\">#{title}</a></li>\n"
    end
    html << "</ul>\n"
  end
end

#metadataHash

TODO:

Can use file.metadata in future version of Gollum.

Embedded metadata.

Returns:

  • (Hash)

    Returns Hash of metadata.



241
242
243
244
245
246
247
248
249
# File 'lib/smeagol/views/base.rb', line 241

def 
  @metadata ||= (
    if md = /\<\!\-\-\-(.*?)\-{2,3}\>\s*\Z/m.match(content)
      YAML.load(md[1])
    else
      {}
    end
  )
end

#nameObject

Page name.



55
56
57
# File 'lib/smeagol/views/base.rb', line 55

def name
  file.name
end

#post?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/smeagol/views/base.rb', line 222

def post?
  false
end

#postsObject

List of posts.



117
118
119
120
121
122
123
124
# File 'lib/smeagol/views/base.rb', line 117

def posts
  @posts ||= @master.posts
  #@posts ||= (
  #  filter(@wiki.pages){ |p| p.post? }.map do |page|
  #    Smeagol::Views::Post.new(page, @version)
  #  end
  #)
end

#ribbon_htmlObject

The HTML for the GitHub ribbon, if enabled. This can be set in the settings file as ‘ribbon`.



100
101
102
103
104
105
106
107
108
109
# File 'lib/smeagol/views/base.rb', line 100

def ribbon_html
  if !source_url.nil? && !wiki.settings.ribbon.nil?
    name, pos = *wiki.settings.ribbon.split(' ')
    pos ||= 'right'
    
    html =  "<a href=\"#{source_url}\">"
    html << "<img style=\"position:absolute; top:0; #{pos}:0; border:0;\" src=\"#{ribbon_url(name, pos)}\" alt=\"Fork me on GitHub\"/>"
    html << "</a>"
  end
end

#ribbon_url(name, pos) ⇒ Object

Generates the correct ribbon url



145
146
147
148
149
150
151
152
# File 'lib/smeagol/views/base.rb', line 145

def ribbon_url(name, pos)
  hexcolors = {'red' => 'aa0000', 'green' => '007200', 'darkblue' => '121621', 'orange' => 'ff7600', 'gray' => '6d6d6d', 'white' => 'ffffff'}
  if hexcolor = hexcolors[name]
    "http://s3.amazonaws.com/github/ribbons/forkme_#{pos}_#{name}_#{hexcolor}.png"
  else
    name
  end
end

#settingsObject



232
233
234
# File 'lib/smeagol/views/base.rb', line 232

def settings
  @master.settings
end

#setup_template_pathObject



36
37
38
39
40
41
42
43
44
# File 'lib/smeagol/views/base.rb', line 36

def setup_template_path
  # See FAQ for Views::Base class
  dir = ::File.join(wiki.path, settings.partials)
  if ::File.directory?(dir)
    self.class.template_path = dir
  else
    self.class.template_path = ::File.join(LIBDIR, 'templates', 'partials')
  end
end

#source_urlObject

The URL of the project source code. This is set in the settings file.



71
72
73
# File 'lib/smeagol/views/base.rb', line 71

def source_url
  settings.source_url
end

#standard_layoutObject

The Mustache template to use for rendering.

Returns:

  • the content of the specified template file in the wiki repository if it exists. Otherwise, it returns ‘nil`.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/smeagol/views/base.rb', line 188

def standard_layout
  name   = ['layout'] || 'page.mustache'
  dir    = ::File.expand_path(::File.join(wiki.path, ::File.dirname(file.path)))
  root   = ::File.expand_path(wiki.path)
  home   = ::File.expand_path('~')
  layout = nil
  loop do
    f = ::File.join(dir, '_layouts', name)
    break (layout = IO.read(f)) if ::File.exist?(f)
    break nil if dir == root
    break nil if dir == home  # just in case
    break nil if dir == '/'
    dir = ::File.dirname(dir)
  end
  return layout
  #names.each do |name|
  #  file = "#{@wiki.path}/#{settings.layout_dir}/#{name}.mustache"
  #  if ::File.exists?(file)
  #    return IO.read(file)
  #  end
  #end
  #nil
end

#taglineObject

The tagline for the wiki. This is set in the settings file.



65
66
67
# File 'lib/smeagol/views/base.rb', line 65

def tagline
  settings.tagline
end

#tracking_idObject

The Google Analytics tracking id from the settings file.



76
77
78
# File 'lib/smeagol/views/base.rb', line 76

def tracking_id
  settings.tracking_id
end

#wiki_titleObject

The title of the wiki. This is set in the settings file.



60
61
62
# File 'lib/smeagol/views/base.rb', line 60

def wiki_title
  settings.title
end