Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
ojamin committed Mar 23, 2010
0 parents commit fb21737
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 0 deletions.
20 changes: 20 additions & 0 deletions MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2010 [name of plugin creator]

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 changes: 13 additions & 0 deletions README
@@ -0,0 +1,13 @@
MagicWiki
=========

Introduction goes here.


Example
=======

Example goes here.


Copyright (c) 2010 [name of plugin creator], released under the MIT license
23 changes: 23 additions & 0 deletions Rakefile
@@ -0,0 +1,23 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the magic_wiki plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the magic_wiki plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'MagicWiki'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
13 changes: 13 additions & 0 deletions app/controllers/application_controller.rb
@@ -0,0 +1,13 @@
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details

# Scrub sensitive parameters from your log
# filter_parameter_logging :password



end
44 changes: 44 additions & 0 deletions app/controllers/wiki_controller.rb
@@ -0,0 +1,44 @@
class WikiController < ApplicationController

private
def wiki_sub(str)
switches = WikiItem::get_switches
switches.keys.each {|k|
if switches[k].class == String
str.gsub! k, switches[k]
elsif switches[k].class == Hash && (scn = str.scan(k).flatten).size > 0
if switches[k][:inline]
str.gsub! k, render_to_string(:inline => switches[k][:content].gsub('\1', scn[1]), :type => switches[k][:inline])
end
end
}
return str
end

public
def view
render :text => "" and return unless params[:name] && params[:name].size > 0
@page = WikiItem.find_by_name(params[:name]) || WikiItem.new(:name => params[:name], :content => "")
if params[:commit] && params[:content]
@page.content = params[:content]
@page.save
end
@text = []
@text << render_to_string(:inline => "= link_to_remote :Edit, :url => {:controller => :wiki, :action => :edit, :id => '#{params[:name]}'}, :update =>:main_wiki_box", :type => :haml)
@text << "<br /><br />"
@text << "Empty page for #{params[:name]}" unless @page.content && @page.content.size > 0
@page.content.split("\n").each {|l|
@text << wiki_sub(l.gsub("\r", ""))
}
@text = @text.join("\n")
@text.html_safe!
render :partial => "view", :layout => true, :locals => {:page => @page, :text => @text}
end

def edit
render :text => "" and return unless params[:name] && params[:name].size > 0
@page = WikiItem.find_by_name(params[:name]) || WikiItem.new(:name => params[:name], :content => "")
render :partial => "edit", :locals => {:page => @page}
end

end
31 changes: 31 additions & 0 deletions app/models/wiki_item.rb
@@ -0,0 +1,31 @@
class WikiItem < ActiveRecord::Base

@@wiki_switches = {
/'''([^']*)'''/ => '<b>\1</b>',
/''([^']*)''/ => '<i>\1</i>',
/^\{$/ => '<ul>',
/^\[$/ => '<ol>',
/^\*(.*)$/ => '<li>\1</li>',
/^\]$/ => '</ol>',
/^\}$/ => '</ul>',
/^h1\. (.*)$/ => '<a name="\1"></a>' + "\n" + '<h1>\1</h1>',
/^h2\. (.*)$/ => '<a name="\1"></a>' + "\n" + '<h2>\1</h2>',
/^h3\. (.*)$/ => '<h3>\1</h3>',
/^h4\. (.*)$/ => '<h4>\1</h4>',
/^h5\. (.*)$/ => '<h5>\1</h5>',
/^h6\. (.*)$/ => '<h6>\1</h6>',
/^centre\. (.*)$/ => '<center>\1</center>',
/^center\. (.*)$/ => '<center>\1</center>',
/^youtube\. (.*)$/ => '<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/$1&hl=en_GB&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/\1&hl=en_GB&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>',
/(\[\[([A-Za-z0-9_?: \/\-]+)\]\])/ => {:content => '= link_to "\1", :controller => :wiki, :action => "\1"', :inline => 'haml'},
/\[([A-Za-z0-9_?: \/\-.&]+[^|])\]/ => '<a href="\1">\1</a>',
/\[([^|]+)\|([A-Za-z0-9_?: \/\-.&]+)\]/ => '<a href="\2">\1</a>',
/-{4}/ => '<hr />',
/^$/ => '<br />'
}

def self.get_switches
return @@wiki_switches
end

end
5 changes: 5 additions & 0 deletions app/views/layouts/application.haml
@@ -0,0 +1,5 @@
%html
%head
!= javascript_include_tag :defaults
%body
= yield
6 changes: 6 additions & 0 deletions app/views/wiki/_edit.haml
@@ -0,0 +1,6 @@
- form_tag "/wiki/#{page.name}" do
= text_area_tag :content, page.content, :size => "80x30"
%br
= submit_tag :Update
= link_to_remote :Cancel, :url => {:controller => :wiki, :action => page.name}, :update => :main_wiki_box

2 changes: 2 additions & 0 deletions app/views/wiki/_view.haml
@@ -0,0 +1,2 @@
#main_wiki_box
= text
5 changes: 5 additions & 0 deletions config/routes.rb
@@ -0,0 +1,5 @@
ActionController::Routing::Routes.draw do |map|
map.connect 'wiki/edit/:name', :controller => :wiki, :action => :edit
map.connect 'wiki/update/:name', :controller => :wiki, :action => :update
map.connect 'wiki/:name', :controller => :wiki, :action => :view
end
1 change: 1 addition & 0 deletions init.rb
@@ -0,0 +1 @@
# Include hook code here
1 change: 1 addition & 0 deletions install.rb
@@ -0,0 +1 @@
# Install hook code here
1 change: 1 addition & 0 deletions lib/magic_wiki.rb
@@ -0,0 +1 @@
# MagicWiki
14 changes: 14 additions & 0 deletions migrate/20100322173840_create_wiki_items.rb
@@ -0,0 +1,14 @@
class CreateWikiItems < ActiveRecord::Migration
def self.up
create_table :wiki_items do |t|
t.string :name
t.text :content

t.timestamps
end
end

def self.down
drop_table :wiki_items
end
end
4 changes: 4 additions & 0 deletions tasks/magic_wiki_tasks.rake
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :magic_wiki do
# # Task goes here
# end
8 changes: 8 additions & 0 deletions test/magic_wiki_test.rb
@@ -0,0 +1,8 @@
require 'test_helper'

class MagicWikiTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
3 changes: 3 additions & 0 deletions test/test_helper.rb
@@ -0,0 +1,3 @@
require 'rubygems'
require 'active_support'
require 'active_support/test_case'
1 change: 1 addition & 0 deletions uninstall.rb
@@ -0,0 +1 @@
# Uninstall hook code here

0 comments on commit fb21737

Please sign in to comment.