Class: Gollum::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/gollum-site/site.rb,
lib/gollum-site/version.rb

Constant Summary collapse

VERSION =
"0.1.12"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Site

Returns a new instance of Site.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gollum-site/site.rb', line 12

def initialize(path, options = {})
  @wiki = Gollum::Wiki.new(path, {
                             :markup_classes => Hash.new(Gollum::SiteMarkup),
                             :page_class => Gollum::SitePage,
                             :base_path => options[:base_path],
                             :sanitization => sanitization(options),
                             :history_sanitization => sanitization(options)
                           })
  @wiki.site = self
  @output_path = options[:output_path] || "_site"
  @version = options[:version] || "master"
end

Instance Attribute Details

#layoutsObject (readonly)

Returns the value of attribute layouts.



9
10
11
# File 'lib/gollum-site/site.rb', line 9

def layouts
  @layouts
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



8
9
10
# File 'lib/gollum-site/site.rb', line 8

def output_path
  @output_path
end

#pagesObject (readonly)

Returns the value of attribute pages.



10
11
12
# File 'lib/gollum-site/site.rb', line 10

def pages
  @pages
end

Class Method Details

.default_layout_dirObject



4
5
6
# File 'lib/gollum-site/site.rb', line 4

def self.default_layout_dir()
  ::File.join(::File.dirname(::File.expand_path(__FILE__)), "layout")
end

Instance Method Details

#decode_git_path(path) ⇒ Object

Decode octal sequences (NNN) in tree path names.

path - String path name.

Returns a decoded String.



109
110
111
112
113
114
115
116
# File 'lib/gollum-site/site.rb', line 109

def decode_git_path(path)
  if path[0] == ?" && path[-1] == ?"
    path = path[1...-1]
    path.gsub!(/\\\d{3}/)   { |m| m[1..-1].to_i(8).chr }
  end
  path.gsub!(/\\[rn"\\]/) { |m| eval(%("#{m.to_s}")) }
  path
end

#generateObject

Public: generate the static site



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gollum-site/site.rb', line 81

def generate()
  prepare
  ::Dir.mkdir(@output_path) unless ::File.exists? @output_path

  @pages.each do |name, page|
    SiteLog.debug("Starting page generation - #{name}")
    page.generate(@output_path, @version)
    SiteLog.debug("Finished page generation - #{name}")
  end

  @files.each do |path, data|
    path = ::File.join(@output_path, path)
    ::FileUtils.mkdir_p(::File.dirname(path))
    ::File.open(path, "w") do |f|
      f.write(data)
    end
  end
end

#ignored?(path) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
# File 'lib/gollum-site/site.rb', line 118

def ignored?(path)
  @ignored_list ||= ignored_list
  @ignored_list.include? path
end

#ignored_list(version = 'master') ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/gollum-site/site.rb', line 123

def ignored_list(version = 'master')
  in_work_tree do
    ignore = ls_files(:ignored => true, :exclude_from => '.gollumignore')
    if version == :working
      ignore += ls_files(:ignored => true, :exclude_from => '.gollumignore', :other => true)
    else
      ignore += ls_files(:other => true)
    end
    # grit does not correctly handle multiple options with the same name
    ignore += ls_files(:ignored => true, :exclude => '.gollumignore')
    ignore += ls_files(:ignored => true, :exclude => '.gitignore')
    ignore += ls_files(:ignored => true, :exclude => '_Sidebar.*')
    ignore += ls_files(:ignored => true, :exclude => '_Footer.*')
  end
end

#in_work_treeObject



144
145
146
147
148
149
150
# File 'lib/gollum-site/site.rb', line 144

def in_work_tree
  cwd = Dir.pwd
  Dir.chdir(@wiki.repo.git.work_tree)
  result = yield
  Dir.chdir(cwd)
  result
end

#ls(version = 'master') ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gollum-site/site.rb', line 54

def ls(version = 'master')
  if version == :working
    return in_work_tree do
      working = ls_files(:exclude => @output_path, :others => true, :cached => true)
      deleted = ls_files(:exclude => @output_path, :deleted => true)
      ignored = ignored_list(version)
      valid_files = working - deleted - ignored
      valid_files.map do |path|
        OpenStruct.new(:path => path, :data => IO.read(path))
      end
    end
  else
    return @wiki.tree_map_for(version).reject {|entry| ignored? entry.path }.map do |entry|
      OpenStruct.new(:path => entry.path, :data => entry.blob(@wiki.repo).data)
    end
  end
end

#ls_files(opts) ⇒ Object



139
140
141
142
# File 'lib/gollum-site/site.rb', line 139

def ls_files(opts)
  opts.merge!(:z => true, :exclude_standard => true)
  @wiki.repo.git.native(:ls_files, opts).split("\0").map {|path| decode_git_path(path) }
end

#prepareObject

Prepare site for specified version



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gollum-site/site.rb', line 26

def prepare()
  @pages = {}
  @files = {}
  @layouts = {}

  @commit = @version == :working ? @wiki.repo.head.commit : @wiki.repo.commit(@version)
  items = self.ls(@version)

  items.each do |item|
    filename = ::File.basename(item.path)
    dirname = ::File.dirname(item.path)
    if filename =~ /^_Layout.html/
      # layout
      @layouts[item.path] = ::Liquid::Template.parse(item.data)
    elsif @wiki.page_class.valid_page_name?(filename)
      # page
      page = @wiki.page_class.new(@wiki)
      blob = OpenStruct.new(:name => filename, :data => item.data)
      page.populate(blob, dirname)
      page.version = @commit
      @pages[page.name.downcase] = page
    else
      # file
      @files[item.path] = item.data
    end
  end
end

#sanitization(options) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/gollum-site/site.rb', line 72

def sanitization(options)
  site_sanitization = Gollum::SiteSanitization.new
  site_sanitization.elements.concat options[:allow_elements] || []
  site_sanitization.attributes[:all].concat options[:allow_attributes] || []
  site_sanitization.protocols['a']['href'].concat options[:allow_protocols] || []
  site_sanitization
end

#to_liquidObject



100
101
102
# File 'lib/gollum-site/site.rb', line 100

def to_liquid
  { "pages" => @pages }
end