Skip to content

Instantly share code, notes, and snippets.

@newbold

newbold/wiki.rb Secret

Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newbold/db84b3951bb366f62d9f to your computer and use it in GitHub Desktop.
Save newbold/db84b3951bb366f62d9f to your computer and use it in GitHub Desktop.
Simple Ruby Wiki
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> &#183; <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