Class: Gollum::FileView

Inherits:
Object
  • Object
show all
Defined in:
lib/gollum-lib/file_view.rb

Overview

FileView requires that:

- All files in root dir are processed first
- Then all the folders are sorted and processed

Instance Method Summary collapse

Constructor Details

#initialize(pages, options = {}) ⇒ FileView

common use cases: set pages to wiki.pages and show_all to false set pages to wiki.pages + wiki.files and show_all to true



11
12
13
14
15
16
# File 'lib/gollum-lib/file_view.rb', line 11

def initialize(pages, options = {})
  @pages    = pages
  @wiki     = @pages.first ? @pages.first.wiki : nil
  @show_all = options[:show_all] || false
  @checked  = options[:collapse_tree] ? '' : "checked"
end

Instance Method Details

#delete_file(url, valid_page) ⇒ Object



28
29
30
# File 'lib/gollum-lib/file_view.rb', line 28

def delete_file(url, valid_page)
  %Q(<form method="POST" action="/deleteFile/#{url}" onsubmit="return confirm('Do you really want to delete the file #{url}?');"><button type="submit" name="delete" value="true"></button></form>)
end

#enclose_tree(string) ⇒ Object



18
19
20
# File 'lib/gollum-lib/file_view.rb', line 18

def enclose_tree(string)
  sanitize_html(%Q(<ol class="tree">\n) + string + %Q(</ol>))
end

#end_folderObject



44
45
46
# File 'lib/gollum-lib/file_view.rb', line 44

def end_folder
  "</ol></li>\n"
end

#new_folder(folder_path) ⇒ Object



32
33
34
# File 'lib/gollum-lib/file_view.rb', line 32

def new_folder(folder_path)
  new_sub_folder folder_path
end

#new_page(page) ⇒ Object



22
23
24
25
26
# File 'lib/gollum-lib/file_view.rb', line 22

def new_page(page)
  name = page.name
  url, valid_page  = url_for_page page
  %Q(  <li class="file"><a href="#{url}"><span class="icon"></span>#{name}</a>#{valid_page ? "" : delete_file(url, valid_page)}</li>)
end

#new_sub_folder(path) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/gollum-lib/file_view.rb', line 36

def new_sub_folder(path)
  <<-HTML
  <li>
    <label>#{path}</label> <input type="checkbox" #{@checked} />
    <ol>
  HTML
end

#render_filesObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/gollum-lib/file_view.rb', line 65

def render_files
  html         = ''
  count        = @pages.size
  folder_start = -1

  # Process all pages until folders start
  count.times do |index|
    page = @pages[index]
    path = page.path

    unless path.include? '/'
      # Page processed (not contained in a folder)
      html += new_page page
    else
      # Folders start at the next index
      folder_start = index
      break # Pages finished, move on to folders
    end
  end

  # If there are no folders, then we're done.
  return enclose_tree(html) if folder_start <= -1

  # Handle special case of only one folder.
  if (count - folder_start == 1)
    page = @pages[folder_start]
    html += <<-HTML
    <li>
      <label>#{::File.dirname(page.path)}</label> <input type="checkbox" #{@checked} />
      <ol>
      #{new_page page}
     </ol>
    </li>
    HTML

    return enclose_tree(html)
  end

  sorted_folders = []
  (folder_start).upto count - 1 do |index|
    sorted_folders += [[@pages[index].path, index]]
  end

  # http://stackoverflow.com/questions/3482814/sorting-list-of-string-paths-in-vb-net
  sorted_folders.sort! do |first, second|
    a           = first[0]
    b           = second[0]

    # use :: operator because gollum defines its own conflicting File class
    dir_compare = ::File.dirname(a) <=> ::File.dirname(b)

    # Sort based on directory name unless they're equal (0) in
    # which case sort based on file name.
    if dir_compare == 0
      ::File.basename(a) <=> ::File.basename(b)
    else
      dir_compare
    end
  end

  # keep track of folder depth, 0 = at root.
  cwd_array = []
  changed   = false

  # process rest of folders
  (0...sorted_folders.size).each do |i|
    page   = @pages[sorted_folders[i][1]]
    path   = page.path
    folder = ::File.dirname path

    tmp_array = folder.split '/'

    (0...tmp_array.size).each do |index|
      if cwd_array[index].nil? || changed
        html += new_sub_folder tmp_array[index]
        next
      end

      if cwd_array[index] != tmp_array[index]
        changed = true
        (cwd_array.size - index).times do
          html += end_folder
        end
        html += new_sub_folder tmp_array[index]
      end
    end

    html += new_page page
    cwd_array = tmp_array
    changed = false
  end

  enclose_tree(html)
end

#sanitize_html(data) ⇒ Object

end render_files



160
161
162
# File 'lib/gollum-lib/file_view.rb', line 160

def sanitize_html(data)
  @wiki ? @wiki.sanitizer.clean(data) : data
end

#url_for_page(page) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gollum-lib/file_view.rb', line 48

def url_for_page(page)
  url = ''
  valid_page_name = false
  if @show_all
    # Remove ext for valid pages.
    filename = page.filename
    valid_page_name =  Page::valid_page_name?(filename)
    filename = valid_page_name ? filename.chomp(::File.extname(filename)) : filename

    url = ::File.join(::File.dirname(page.path), filename)
  else
    url = ::File.join(::File.dirname(page.path), page.filename_stripped)
  end
  url = url[2..-1] if url[0, 2] == './'
  return url, valid_page_name
end