huge: changed to roda

now works same as before, but much more extensible.  Internally, a
routing tree is used.
This commit is contained in:
Pranshu Sharma 2025-06-01 00:43:19 +10:00
parent e005cc96e9
commit 094189e2cb
3 changed files with 87 additions and 27 deletions

11
Gemfile
View file

@ -1,5 +1,14 @@
gem 'erb' gem 'roda'
gem 'rackup' gem 'rackup'
# For parsing html and making the sidebar. Very fast
gem 'nokogiri' gem 'nokogiri'
# Markdown converter. Uses native C code
gem 'redcarpet' gem 'redcarpet'
# For parsing config. Also uses native C code and bindings, fast.
gem 'tomlib'
# embeded ruby templating lanuage
gem 'erb'

81
app.rb
View file

@ -1,5 +1,5 @@
require 'sinatra' require 'roda'
require 'erb' require 'erb'
require 'nokogiri' require 'nokogiri'
require 'redcarpet' require 'redcarpet'
@ -16,8 +16,7 @@ class Config
@approved_regex = Regexp.union(approved_files) @approved_regex = Regexp.union(approved_files)
toml = Tomlib.load(File.read "config.toml") toml = Tomlib.load(File.read "config.toml")
@layout = toml["layout"] || "layout" @layout = toml["layout"] || "layout.erb"
@layout = @layout.to_sym
# @layout = :layout # @layout = :layout
end end
@ -26,6 +25,7 @@ end
$config = Config.new(); $config = Config.new();
class Sidebar class Sidebar
attr_reader :tree
FileNode = Struct.new(:type, :path, :name) FileNode = Struct.new(:type, :path, :name)
DirNode = Struct.new(:type, :path, :name, :files) DirNode = Struct.new(:type, :path, :name, :files)
@ -45,6 +45,7 @@ class Sidebar
end end
def html def html
return @html if defined?(@html)
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 {
@ -67,7 +68,7 @@ class Sidebar
deal_with_dir doc, @tree, @tree.path deal_with_dir doc, @tree, @tree.path
} }
end end
builder.to_html().split(/\n/)[1..-1].join @html = builder.to_html().split(/\n/)[1..-1].join
end end
private private
@ -105,29 +106,79 @@ end
$sidebar = Sidebar.new $sidebar = Sidebar.new
class Perc < Sinatra::Application class App < Roda
set :views, "."
set :markdown, :layout_engine => :erb
# @sidebar = Sidebar.new
get "/" do route do |r|
erb :main, layout: $config.layout r.root do
pview "main.erb"
end end
get "/*.*" do |path, ext| process_dir r, $sidebar.tree, "."
if ext == "md"
md = markdown File.read(path + ".md"), layout: $config.layout
end
# Now the saga begins,we transform file_tree into a routing tree
def process_dir (r, dir, cdir)
p = Proc.new do
dir.files.each do |f|
dd = File.join(cdir, f.path)
if f.type == :dir
# f is dir
process_dir r, f, dd
else else
erb path.to_sym, layout: $config.layout # f is a file
r.get f.path do
pview dd
end
end
end
"Whatup"
end
if dir.path == "."
p.call
else
r.on dir.path do
p.call
end
end end
end end
helpers do
def tree def tree
$sidebar.html $sidebar.html
end end
def pview(file)
layout = $config.layout
tp = ERB.new(File.read(file))
body = tp.result(binding)
ERB.new(File.read(layout)).result(binding)
end end
end end
# class Perc < Sinatra::Application
# set :views, "."
# set :markdown, :layout_engine => :erb
# # @sidebar = Sidebar.new
# get "/" do
# erb :main, layout: $config.layout
# end
# get "/*.*" do |path, ext|
# if ext == "md"
# md = markdown File.read(path + ".md"), layout: $config.layout
# else
# erb path.to_sym, layout: $config.layout
# end
# end
# helpers do
# def tree
# $sidebar.html
# end
# end
# end

View file

@ -1,2 +1,2 @@
require './app' require './app'
run Perc run App