Class: Gollum::FileView
- Inherits:
-
Object
- Object
- Gollum::FileView
- Defined in:
- lib/gollum/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
- #enclose_tree(string) ⇒ Object
- #end_folder ⇒ Object
-
#initialize(pages) ⇒ FileView
constructor
A new instance of FileView.
- #new_folder(folder_path) ⇒ Object
- #new_page(page) ⇒ Object
- #new_sub_folder(path) ⇒ Object
- #render_files ⇒ Object
- #url_for_page(page) ⇒ Object
Constructor Details
#initialize(pages) ⇒ FileView
Returns a new instance of FileView.
8 9 10 |
# File 'lib/gollum/file_view.rb', line 8 def initialize pages @pages = pages end |
Instance Method Details
#enclose_tree(string) ⇒ Object
12 13 14 |
# File 'lib/gollum/file_view.rb', line 12 def enclose_tree string %Q(<ol class="tree">\n) + string + %Q(</ol>) end |
#end_folder ⇒ Object
34 35 36 37 38 39 |
# File 'lib/gollum/file_view.rb', line 34 def end_folder <<-HTML </ol> </li> HTML end |
#new_folder(folder_path) ⇒ Object
22 23 24 |
# File 'lib/gollum/file_view.rb', line 22 def new_folder folder_path new_sub_folder folder_path end |
#new_page(page) ⇒ Object
16 17 18 19 20 |
# File 'lib/gollum/file_view.rb', line 16 def new_page page name = page.name url = url_for_page page %Q( <li class="file"><a href="#{url}">#{name}</a></li>\n) end |
#new_sub_folder(path) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/gollum/file_view.rb', line 26 def new_sub_folder path <<-HTML <li> <label>#{path}</label> <input type="checkbox" checked /> <ol> HTML end |
#render_files ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 |
# File 'lib/gollum/file_view.rb', line 47 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 ] name = page.name url = url_for_page page html += <<-HTML <li> <label>#{::File.dirname(page.path)}</label> <input type="checkbox" checked /> <ol> <li class="file"><a href="#{url}">#{name}</a></li> </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 | index | page = @pages[ sorted_folders[ index ][ 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
41 42 43 44 45 |
# File 'lib/gollum/file_view.rb', line 41 def url_for_page page url = ::File.join(::File.dirname(page.path), page.filename_stripped) url = url[2..-1] if url[0,2] == './' url end |