Added toml config plus fixed some stuff
This commit is contained in:
parent
4fd0725169
commit
e005cc96e9
1 changed files with 42 additions and 14 deletions
56
app.rb
56
app.rb
|
@ -3,13 +3,31 @@ require 'sinatra'
|
||||||
require 'erb'
|
require 'erb'
|
||||||
require 'nokogiri'
|
require 'nokogiri'
|
||||||
require 'redcarpet'
|
require 'redcarpet'
|
||||||
|
require 'tomlib'
|
||||||
|
|
||||||
$root = "/home/pranshu/Documents/proj/perc/test"
|
$root = "/home/pranshu/Documents/proj/perc/test"
|
||||||
Dir.chdir $root;
|
Dir.chdir $root;
|
||||||
|
|
||||||
|
|
||||||
|
class Config
|
||||||
|
attr_reader :approved_regex, :layout
|
||||||
|
def initialize
|
||||||
|
approved_files = [/\.md$/, /\.erb$/]
|
||||||
|
@approved_regex = Regexp.union(approved_files)
|
||||||
|
toml = Tomlib.load(File.read "config.toml")
|
||||||
|
|
||||||
|
@layout = toml["layout"] || "layout"
|
||||||
|
@layout = @layout.to_sym
|
||||||
|
# @layout = :layout
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
$config = Config.new();
|
||||||
|
|
||||||
class Sidebar
|
class Sidebar
|
||||||
FileNode = Struct.new(:type, :name)
|
FileNode = Struct.new(:type, :path, :name)
|
||||||
DirNode = Struct.new(:type, :name, :files)
|
DirNode = Struct.new(:type, :path, :name, :files)
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@tree = process_dir "."
|
@tree = process_dir "."
|
||||||
|
@ -17,12 +35,12 @@ class Sidebar
|
||||||
|
|
||||||
def show(n=@tree, indent="")
|
def show(n=@tree, indent="")
|
||||||
if n.type == :dir
|
if n.type == :dir
|
||||||
print indent, n.name, "\n"
|
print indent, n.path, "\n"
|
||||||
n.files.each do |r|
|
n.files.each do |r|
|
||||||
show r, " " + indent
|
show r, " " + indent
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
print indent, n.name, "\n"
|
print indent, n.path, "\n"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -30,14 +48,14 @@ class Sidebar
|
||||||
builder = Nokogiri::HTML::Builder.new do |doc|
|
builder = Nokogiri::HTML::Builder.new do |doc|
|
||||||
def deal_with_dir (doc, dir, path)
|
def deal_with_dir (doc, dir, path)
|
||||||
doc.li {
|
doc.li {
|
||||||
doc.text dir.name
|
doc.text dir.path
|
||||||
doc.ul {
|
doc.ul {
|
||||||
dir.files.each do |f|
|
dir.files.each do |f|
|
||||||
fpath = File.join(path, f.name)
|
fpath = File.join(path, f.path)
|
||||||
if f.type == :dir
|
if f.type == :dir
|
||||||
deal_with_dir doc, f, fpath
|
deal_with_dir doc, f, fpath
|
||||||
else
|
else
|
||||||
doc.a(:href => fpath) {
|
doc.a(:href => "/#{fpath}") {
|
||||||
doc.li { doc.text f.name }
|
doc.li { doc.text f.name }
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -46,7 +64,7 @@ class Sidebar
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
doc.div {
|
doc.div {
|
||||||
deal_with_dir doc, @tree, @tree.name
|
deal_with_dir doc, @tree, @tree.path
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
builder.to_html().split(/\n/)[1..-1].join
|
builder.to_html().split(/\n/)[1..-1].join
|
||||||
|
@ -54,17 +72,27 @@ class Sidebar
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def getname(file)
|
||||||
|
File.basename(file, File.extname(file))
|
||||||
|
end
|
||||||
|
|
||||||
def dir_files()
|
def dir_files()
|
||||||
# Remove "." and ".." from file list
|
# Remove "." and ".." from file list
|
||||||
Dir.entries(".").filter {|f| f !~ /^\.\.?$/ }
|
Dir.entries(".").filter {|f| f !~ /^\./ }.filter do |f|
|
||||||
|
if File.file? f
|
||||||
|
f =~ $config.approved_regex
|
||||||
|
else
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def process_dir(dir)
|
def process_dir(dir)
|
||||||
DirNode.new(:dir, dir,
|
DirNode.new(:dir, dir, getname(dir),
|
||||||
Dir.chdir(dir) do
|
Dir.chdir(dir) do
|
||||||
dir_files.map do |f|
|
dir_files.map do |f|
|
||||||
if File.file? f
|
if File.file? f
|
||||||
FileNode.new(:file, f)
|
FileNode.new(:file, f, getname(f))
|
||||||
else
|
else
|
||||||
process_dir f
|
process_dir f
|
||||||
end
|
end
|
||||||
|
@ -83,15 +111,15 @@ class Perc < Sinatra::Application
|
||||||
# @sidebar = Sidebar.new
|
# @sidebar = Sidebar.new
|
||||||
|
|
||||||
get "/" do
|
get "/" do
|
||||||
erb :main
|
erb :main, layout: $config.layout
|
||||||
end
|
end
|
||||||
|
|
||||||
get "/*.*" do |path, ext|
|
get "/*.*" do |path, ext|
|
||||||
if ext == "md"
|
if ext == "md"
|
||||||
md = markdown File.read(path + ".md")
|
md = markdown File.read(path + ".md"), layout: $config.layout
|
||||||
|
|
||||||
else
|
else
|
||||||
erb path.to_sym
|
erb path.to_sym, layout: $config.layout
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue