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
# File 'lib/gollum-lib/file_view.rb', line 11

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

Instance Method Details

#delete_file(url, valid_page) ⇒ Object



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

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



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

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

#end_folderObject



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

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

#new_folder(folder_path) ⇒ Object



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

def new_folder(folder_path)
  new_sub_folder folder_path
end

#new_page(page) ⇒ Object



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

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



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

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

#render_filesObject



64
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 64

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

  # return the completed html
  enclose_tree html
end

#url_for_page(page) ⇒ Object



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

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