Merge pull request #345 from bootstraponline/file_view

Add file view.
This commit is contained in:
bootstraponline
2012-05-21 08:58:14 -07:00
14 changed files with 689 additions and 0 deletions
+1
View File
@@ -15,6 +15,7 @@ require File.expand_path('../gollum/blob_entry', __FILE__)
require File.expand_path('../gollum/wiki', __FILE__)
require File.expand_path('../gollum/page', __FILE__)
require File.expand_path('../gollum/file', __FILE__)
require File.expand_path('../gollum/file_view', __FILE__)
require File.expand_path('../gollum/markup', __FILE__)
require File.expand_path('../gollum/sanitization', __FILE__)
require File.expand_path('../gollum/tex', __FILE__)
+154
View File
@@ -0,0 +1,154 @@
module Gollum
=begin
FileView requires that:
- All files in root dir are processed first
- Then all the folders are sorted and processed
=end
class FileView
def initialize pages
@pages = pages
end
def enclose_tree string
%Q(<ol class="tree">\n) + string + %Q(\n</ol>)
end
def new_page page
name = page.name
%Q( <li class="file"><a href="#{name}">#{name}</a></li>\n)
end
def new_folder page
new_sub_folder ::File.dirname(page.path), page.name
end
def new_sub_folder path, name
<<-HTML
<li>
<label>#{path}</label> <input type="checkbox" checked />
<ol>
<li class="file"><a href="#{name}">#{name}</a></li>
HTML
end
def end_folder
<<-HTML
</ol>
</li>
HTML
end
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)
path = @pages[ folder_start ]
name = page.name
html += <<-HTML
<li>
<label>#{::File.dirname(path)}</label> <input type="checkbox" checked />
<ol>
<li class="file"><a href="#{name}">#{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
# Process first folder
page = @pages[ sorted_folders[ 0 ][1] ]
html += new_folder page
last_folder = ::File.dirname page.path # define last_folder
# keep track of folder depth, 0 = at root.
depth = 0
# process rest of folders
1.upto(sorted_folders.size - 1) do | index |
page = @pages[ sorted_folders[ index ][1] ]
path = page.path
folder = ::File.dirname path
if last_folder == folder
# same folder
html += new_page page
elsif folder.include?('/')
# check if we're going up or down a depth level
if last_folder.scan('/').size > folder.scan('/').size
# end tag for 1 subfolder & 1 parent folder
# so emit 2 end tags
2.times { html += end_folder; }
depth -= 1
else
depth += 1
end
# subfolder
html += new_sub_folder ::File.dirname(page.path).split('/').last, page.name
else
# depth+1 because we need an additional end_folder
(depth+1).times { html += end_folder; }
depth = 0
# New root folder
html += new_folder page
end
last_folder = folder
end
# Process last folder's ending tags.
(depth+1).times {
depth.times { html += end_folder; }
depth = 0
}
# return the completed html
enclose_tree html
end # end render_files
end # end FileView class
end # end Gollum module
+11
View File
@@ -200,6 +200,17 @@ module Precious
mustache :pages
end
get '/fileview' do
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
@results = Gollum::FileView.new(wiki.pages).render_files
File.open('/tmp/log.txt', 'w') {|f|
f.puts "log!"
f.puts @results
}
@ref = wiki.ref
mustache :file_view
end
get '/*' do
show_page_or_file(params[:splat].first)
end
@@ -0,0 +1,110 @@
/* Just some base styles not needed for example to function */
*, html { font-family: Verdana, Arial, Helvetica, sans-serif; }
#results a:hover {
background-color: #4c4c4c;
}
#home_button {
position: absolute;
top: 10px;
left: 50%;
}
#home_button .minibutton {
/* controls size of home btn */
font-size: 1em;
text-align: center;
}
#results {
position: absolute;
top: 60px;
left: 10px;
}
body, form, ul, li, p, h1, h2, h3, h4, h5
{
margin: 0;
padding: 0;
}
body { background-color: #606061; color: #ffffff; margin: 0; }
img { border: none; }
p
{
font-size: 1em;
margin: 0 0 1em 0;
}
html { font-size: 100%; /* IE hack */ }
body { font-size: 1em; /* Sets base font size to 16px */ }
table { font-size: 100%; /* IE hack */ }
input, select, textarea, th, td { font-size: 1em; }
/* CSS Tree menu styles */
ol.tree
{
padding: 0 0 0 30px;
width: 300px;
}
li
{
position: relative;
margin-left: -15px;
list-style: none;
}
li.file
{
margin-left: -1px !important;
height: 1.5em;
}
li.file a
{
background: url(/images/fileview/document.png) 0 0 no-repeat;
color: #fff;
padding-left: 21px;
text-decoration: none;
display: block;
}
li.file a[href *= '.pdf'] { background: url(/images/fileview/document.png) 0 0 no-repeat; }
li.file a[href *= '.html'] { background: url(/images/fileview/document.png) 0 0 no-repeat; }
li.file a[href $= '.css'] { background: url(/images/fileview/document.png) 0 0 no-repeat; }
li.file a[href $= '.js'] { background: url(/images/fileview/document.png) 0 0 no-repeat; }
li input
{
position: absolute;
left: 0;
margin-left: 0;
opacity: 0;
z-index: 2;
cursor: pointer;
height: 1em;
width: 1em;
top: 0;
}
li input + ol
{
background: url(/images/fileview/toggle-small-expand.png) 40px 0 no-repeat;
margin: -1.188em 0 0 -44px; /* 15px */
height: 1.5em;
}
li input + ol > li { display: none; margin-left: -14px !important; padding-left: 1px; }
li label
{
background: url(/images/fileview/folder-horizontal.png) 15px 1px no-repeat;
cursor: pointer;
display: block;
padding-left: 37px;
}
li input:checked + ol
{
background: url(/images/fileview/toggle-small.png) 40px 5px no-repeat;
margin: -1.5em 0 0 -44px; /* 20px */
padding: 1.563em 0 0 80px;
height: auto;
}
li input:checked + ol > li { display: block; margin: 0 0 0.125em; /* 2px */}
li input:checked + ol > li:last-child { margin: 0 0 0.063em; /* 1px */ }
Binary file not shown.

After

Width:  |  Height:  |  Size: 177 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 271 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 433 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 462 B

@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=utf-8">
<link rel="stylesheet" type="text/css" href="/css/_styles.css" media="all">
<title>File View</title>
</head>
<body>
<div id="home_button">
<ul class="actions">
<li class="minibutton">
<a href="/" class="action-edit-page">Home</a>
</li>
<li class="minibutton" class="jaws">
<a href="#" id="minibutton-new-page">New Page</a>
</li>
</ul>
</div>
{{#has_results}}
<div id="results">
{{{results}}}
</div>
{{/has_results}}
{{#no_results}}
<p id="no-results">
There are no pages in <strong>{{ref}}</strong>.
</p>
{{/no_results}}
</body>
</html>
@@ -7,6 +7,8 @@
</li>
<li class="minibutton"><a href="/pages"
class="action-all-pages">All Pages</a></li>
<li class="minibutton"><a href="/fileview"
class="action-all-pages">File View</a></li>
<li class="minibutton" class="jaws">
<a href="#" id="minibutton-new-page">New Page</a></li>
{{#editable}}
+19
View File
@@ -0,0 +1,19 @@
module Precious
module Views
class FileView < Layout
attr_reader :results, :ref
def title
"All pages in #{@ref}"
end
def has_results
!@results.empty?
end
def no_results
@results.empty?
end
end
end
end