-
-
Save newbold/db84b3951bb366f62d9f to your computer and use it in GitHub Desktop.
Simple Ruby Wiki
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'redcloth' | |
def render_html(page, title, action, content) | |
out = File.open('template.html', "r").read | |
title = '' unless title | |
if action == 'edit' | |
out.gsub!('{view}', '#view { display: none; }') | |
out.gsub!('{edit}', '') | |
else | |
nav = page == 'MainPage' ? '<span class=g>Home</span>' : "<a href=/>Home</a> · <span class=g>#{page}</span>" | |
out.gsub!('{view}', '') | |
out.gsub!('{edit}', '#edit { display: none; }') | |
out.gsub!('{nav}', nav) | |
end | |
out.gsub!('{title}', title) | |
out.gsub!('{content}', content) | |
out.gsub!('{page}', page) | |
out | |
end | |
get '/' do | |
redirect '/MainPage' | |
end | |
post '/:page' do | |
page = params[:page].tr('. ', '') | |
File.open(page, "w") do |f| | |
f.write params[:content] | |
end | |
redirect "/#{page}" | |
end | |
get '/:page/?:action?' do | |
page = params[:page].tr('. ', '') | |
action = params[:action] | |
if File.exists?(page) | |
content = File.open(page, "r").read | |
unless action == 'edit' | |
title, content = content.split("\n", 2) | |
content = '' unless content | |
content = RedCloth.new(content).to_html | |
content.gsub!(/\[\[(.+?)\]\]/u, '<a href="/\1">\1</a>') | |
end | |
else | |
content = "Describe #{page} here." | |
title = page | |
end | |
render_html page, title, action, content | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment