Compare commits
3 commits
40d8754378
...
4fd0725169
Author | SHA1 | Date | |
---|---|---|---|
4fd0725169 | |||
e1c2687cdf | |||
deec2b8707 |
4 changed files with 112 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Gemfile.lock
|
4
Gemfile
4
Gemfile
|
@ -1 +1,5 @@
|
||||||
gem 'erb'
|
gem 'erb'
|
||||||
|
gem 'rackup'
|
||||||
|
|
||||||
|
gem 'nokogiri'
|
||||||
|
gem 'redcarpet'
|
||||||
|
|
105
app.rb
Normal file
105
app.rb
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
|
||||||
|
require 'sinatra'
|
||||||
|
require 'erb'
|
||||||
|
require 'nokogiri'
|
||||||
|
require 'redcarpet'
|
||||||
|
|
||||||
|
$root = "/home/pranshu/Documents/proj/perc/test"
|
||||||
|
Dir.chdir $root;
|
||||||
|
|
||||||
|
class Sidebar
|
||||||
|
FileNode = Struct.new(:type, :name)
|
||||||
|
DirNode = Struct.new(:type, :name, :files)
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@tree = process_dir "."
|
||||||
|
end
|
||||||
|
|
||||||
|
def show(n=@tree, indent="")
|
||||||
|
if n.type == :dir
|
||||||
|
print indent, n.name, "\n"
|
||||||
|
n.files.each do |r|
|
||||||
|
show r, " " + indent
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print indent, n.name, "\n"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def html
|
||||||
|
builder = Nokogiri::HTML::Builder.new do |doc|
|
||||||
|
def deal_with_dir (doc, dir, path)
|
||||||
|
doc.li {
|
||||||
|
doc.text dir.name
|
||||||
|
doc.ul {
|
||||||
|
dir.files.each do |f|
|
||||||
|
fpath = File.join(path, f.name)
|
||||||
|
if f.type == :dir
|
||||||
|
deal_with_dir doc, f, fpath
|
||||||
|
else
|
||||||
|
doc.a(:href => fpath) {
|
||||||
|
doc.li { doc.text f.name }
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
doc.div {
|
||||||
|
deal_with_dir doc, @tree, @tree.name
|
||||||
|
}
|
||||||
|
end
|
||||||
|
builder.to_html().split(/\n/)[1..-1].join
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def dir_files()
|
||||||
|
# Remove "." and ".." from file list
|
||||||
|
Dir.entries(".").filter {|f| f !~ /^\.\.?$/ }
|
||||||
|
end
|
||||||
|
|
||||||
|
def process_dir(dir)
|
||||||
|
DirNode.new(:dir, dir,
|
||||||
|
Dir.chdir(dir) do
|
||||||
|
dir_files.map do |f|
|
||||||
|
if File.file? f
|
||||||
|
FileNode.new(:file, f)
|
||||||
|
else
|
||||||
|
process_dir f
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
$sidebar = Sidebar.new
|
||||||
|
|
||||||
|
|
||||||
|
class Perc < Sinatra::Application
|
||||||
|
set :views, "."
|
||||||
|
set :markdown, :layout_engine => :erb
|
||||||
|
# @sidebar = Sidebar.new
|
||||||
|
|
||||||
|
get "/" do
|
||||||
|
erb :main
|
||||||
|
end
|
||||||
|
|
||||||
|
get "/*.*" do |path, ext|
|
||||||
|
if ext == "md"
|
||||||
|
md = markdown File.read(path + ".md")
|
||||||
|
|
||||||
|
else
|
||||||
|
erb path.to_sym
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
helpers do
|
||||||
|
def tree
|
||||||
|
$sidebar.html
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
2
config.ru
Normal file
2
config.ru
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
require './app'
|
||||||
|
run Perc
|
Loading…
Add table
Add a link
Reference in a new issue