forked from TonTon-UFPA-Comput/site
Renderer independent rendering
This commit is contained in:
parent
a4f6013295
commit
167bddc127
8 changed files with 48 additions and 10 deletions
58
app.rb
58
app.rb
|
|
@ -2,8 +2,48 @@ require 'gollum/app'
|
||||||
require 'sinatra/flash'
|
require 'sinatra/flash'
|
||||||
require "sqlite3"
|
require "sqlite3"
|
||||||
require 'tonton_web'
|
require 'tonton_web'
|
||||||
# TODO: better, simpler route matching
|
require 'pathname'
|
||||||
|
|
||||||
class TonTonWeb::App < Sinatra::Base
|
class TonTonWeb::App < Sinatra::Base
|
||||||
|
# Renders a view by the filename without the extension (without a predefined render)
|
||||||
|
# TODO: pass this to tonton-web code
|
||||||
|
def render_any template_name, base_dir, options = {}
|
||||||
|
views_root = settings.views || "./views"
|
||||||
|
|
||||||
|
# Search for any file with the given name and any extension
|
||||||
|
# Dir.glob returns an array of matching file paths
|
||||||
|
views_dir = Dir.new("#{views_root}/#{base_dir}")
|
||||||
|
|
||||||
|
found = false
|
||||||
|
filename = nil
|
||||||
|
|
||||||
|
while not found and ( filename = views_dir.read ) != nil
|
||||||
|
if File.basename(filename, ".*") == template_name
|
||||||
|
found = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if not found
|
||||||
|
halt 404, "Template named '#{template_name}' not found."
|
||||||
|
end
|
||||||
|
|
||||||
|
file_path = "#{base_dir}/#{template_name}"
|
||||||
|
|
||||||
|
extension = File.extname(filename).delete('.')
|
||||||
|
|
||||||
|
engine = case extension
|
||||||
|
when 'md', 'markdown' then :markdown
|
||||||
|
when 'erb' then :erb
|
||||||
|
else
|
||||||
|
# Fallback: try to use the extension name as the method
|
||||||
|
extension.to_sym
|
||||||
|
end
|
||||||
|
|
||||||
|
# Dynamically call the correct Sinatra method (e.g., erb :hello, markdown :hello)
|
||||||
|
# We use 'send' to call the method by its name symbol
|
||||||
|
send(engine, file_path.to_sym, options)
|
||||||
|
end
|
||||||
|
|
||||||
set :host_authorization, { permitted_hosts: ['localhost', 'mytonton.com.br'] }
|
set :host_authorization, { permitted_hosts: ['localhost', 'mytonton.com.br'] }
|
||||||
|
|
||||||
set :sessions, true
|
set :sessions, true
|
||||||
|
|
@ -16,19 +56,17 @@ class TonTonWeb::App < Sinatra::Base
|
||||||
markdown File.read("readme.md"), layout_engine: :erb, layout: true
|
markdown File.read("readme.md"), layout_engine: :erb, layout: true
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/:name' do
|
# Chapa Sigmóide
|
||||||
markdown request.path_info.to_sym, layout_engine: :erb, layout: true
|
|
||||||
end
|
|
||||||
|
|
||||||
get '/chapa-sigmoide/readme' do
|
get '/chapa-sigmoide' do
|
||||||
redirect "/chapa-sigmoide/readme.markdown"
|
redirect "/chapa-sigmoide/readme.markdown"
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/chapa-sigmoide/:name' do
|
get '/chapa-sigmoide/:name' do
|
||||||
if request.path_info.end_with? '.markdown'
|
render_any params['name'], 'chapa-sigmoide', layout_engine: :erb, layout: :chapa_sigmoide
|
||||||
markdown request.path_info.sub('.markdown', '').to_sym, layout_engine: :erb, layout: :chapa_sigmoide
|
end
|
||||||
elsif request.path_info.end_with? '.erb'
|
|
||||||
erb request.path_info.sub('.erb', '').to_sym, layout_engine: :erb, layout: :chapa_sigmoide
|
get '/chapa-sigmoide/members/:name' do
|
||||||
end
|
render_any params['name'], 'chapa-sigmoide/members', layout_engine: :erb, layout: :chapa_sigmoide
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue