require 'gollum/app' require 'sinatra/flash' require "sqlite3" require 'tonton_web' require 'pathname' 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 = {} for extension in ['.markdown', '.erb'] template_name.delete_suffix!(extension) end 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}" engine = File.extname(filename).delete('.').to_sym # 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 :sessions, true get '/' do redirect "/home" end get '/home' do markdown File.read("readme.md"), layout_engine: :erb, layout: true end # Chapa Sigmóide get '/chapa-sigmoide' do redirect "/chapa-sigmoide/readme.markdown" end get '/chapa-sigmoide/:name' do render_any params['name'], 'chapa-sigmoide', layout_engine: :erb, layout: :chapa_sigmoide end get '/chapa-sigmoide/members/:name' do render_any params['name'], 'chapa-sigmoide/members', layout_engine: :erb, layout: :chapa_sigmoide end end