Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload files and ckeditor #31

Open
wants to merge 15 commits into
base: development
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ source :rubygems
gem "grit", "~> 2.5.0"
gem "gollum", :git => 'git://github.com/github/gollum.git'
gem "RedCloth"
gem "creole"

165 changes: 160 additions & 5 deletions app/controllers/gollum_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,43 @@ class GollumPagesController < ApplicationController
before_filter :find_project, :find_wiki
before_filter :authorize, :except => [ :preview ]

# FIXME: rid off stupid gollum prewiki wikiing
# wtf? wtF? why?
# dirty hack
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ckeditor related? What you mean by prewiki wikiing? What are you trying to achieve?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prewiking: when gollum displays page, it render it by wiki syntax of your choice (creole, textile, markdown, etc).
But before it, gollum expands links in form [[link]]. and one form of gollumwiki is [[Text to display|http://exapmlec.om]], which is exactly oposite as in creole (and as natural). See https://github.com/github/gollum#page-links

class MyMarkup < Gollum::Markup
def render(no_follow = false, encoding = nil)
data = @data.dup
begin
data = GitHub::Markup.render(@name, data)
if data.nil?
raise "There was an error converting #{@name} to HTML."
end
rescue Object => e
data = %{<p class="gollum-error">#{e.message}</p>}
end
data
end
end

class MyGollumFile < Gollum::File
# Find a file in the given Gollum repo.
#
# name - The full String path.
# version - The String version ID to find.
#
# Returns a Gollum::File or nil if the file could not be found.
def find(name, version)
checked = name.downcase
map = @wiki.tree_map_for(version, true)
if entry = map.detect { |entry| entry.path.downcase == checked }
@path = name
@blob = entry.blob(@wiki.repo)
@version = version.is_a?(Grit::Commit) ? version : @wiki.commit_for(version)
self
end
end
end

def index
redirect_to :action => :show, :id => "Home"
end
Expand All @@ -21,11 +58,109 @@ def show
show_page(params[:id])
end

def file
ext = params[:ext]
@file_name = params[:id] + '.' + ext
dir = @project.gollum_wiki.images_directory
mime_type = Mime::Type.lookup_by_extension(ext) || 'text/plain'

if file = @wiki.file(File.join(dir, @file_name))
#if file = file_search(File.join(dir, @file_name))
name = file.name
url = file.url_path
# FIXME: content-type has 'charset=utf8' why?
render :text => file.raw_data, :content_type=> mime_type
else
render :status => 404, :inline => '404 not found:dir:' + dir +',file:' + @file_name
return
end
end

def file_search(path, version = @wiki.ref)
file = @wiki.file_class.new(@wiki)
map = @wiki.tree_map_for(version, true)

if entry = map.detect { |entry| entry.path.downcase == path }
file.path = path
file.blob = entry.blob(@wiki.repo)
file.version = version.is_a?(Grit::Commit) ? version : @wiki.commit_for(version)
self
end

end

# <form><input type=file name=upload[datafile]>
def upload
if request.get?
# render upload.html.erb
return
end



@user = User.current
upload = params[:upload]
name = upload.original_filename
data = upload.read
#dir = @wiki.page_file_dir
dir = @project.gollum_wiki.images_directory

write_file(dir, name, data)

# FIXME:XSS
ckeditor_num = params[:CKEditorFuncNum]
script = <<-EOT
<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction(#{ckeditor_num}, 'img/#{name}', '');</script>
EOT
if ckeditor_num
render :inline => script
#Rails.logger.fatal 'script rednered:' + script
return
else
flash[:notice] = name + ' uploaded'
redirect_to :action => :upload
return
end
end

def newpage
return
end

def write_file(dir, name, data)

message = 'write file ' + name
commit = { :message => message, :name => @user.name, :email => @user.mail }
commiter = Gollum::Committer.new(@wiki, commit)

path = File.join(dir, name)
path = path[1..-1] if path =~ /^\//

commiter.index.add(path, data)
commiter.commit
@wiki.clear_cache

# fixme: do it if not bare
#commiter.update_working_dir( ... )
end

def edit
@page_name = params[:id]
@page = @wiki.page(@page_name)
@content = @page ? @page.text_data : ""
@page_format = @page ? @page.format : @project.gollum_wiki.markup_language.to_sym

if @page
@page_format = @page.format
else
@page_format = @project.gollum_wiki.markup_language.to_sym
end
end

def list
@pages = @wiki.pages
tree = @wiki.tree_map_for(@wiki.ref, true)
dir = @project.gollum_wiki.images_directory
entries = tree.select { |e| e.path.index(dir) == 0 }
@files = entries.map { |e| e.path }
end

def update
Expand All @@ -36,22 +171,40 @@ def update

commit = { :message => params[:page][:message], :name => @user.name, :email => @user.mail }

# zkonvertuj html -> wiki if needed
if @project.gollum_wiki.store_as_wiki
data = params[:page][:formatted_data]
data = ReverseMarkdown.parse data
else
data = params[:page][:raw_data]
end

if @page
@wiki.update_page(@page, @page.name, @page_format, params[:page][:raw_data], commit)
@wiki.update_page(@page, @page.name, @page_format, data, commit)
else
@wiki.write_page(@page_name, @page_format, params[:page][:raw_data], commit)
@wiki.write_page(@page_name, @page_format, data, commit)
end

redirect_to :action => :show, :id => @page_name
end

def raw
name = params[:id]
if page = @wiki.page(name)
render :text => page.raw_data, :content_type => 'text/plain'
else
render :status => 404, :inline => '404 not found:' + name
return
end
end

private

def project_repository_path
return @project.gollum_wiki.git_path
end


def show_page(name)
if page = @wiki.page(name)
@page_name = page.name
Expand Down Expand Up @@ -87,7 +240,9 @@ def find_wiki
gollum_base_path = project_gollum_pages_path
@wiki = Gollum::Wiki.new(git_path,
:base_path => gollum_base_path,
:page_file_dir => wiki_dir)
:page_file_dir => wiki_dir,
:file_class=>::GollumPagesController::MyGollumFile,
:markup_classes => Hash.new(::GollumPagesController::MyMarkup))

end

Expand Down
25 changes: 25 additions & 0 deletions app/helpers/gollum_pages_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'uri'
module GollumPagesHelper
def gollum_include_ckeditor(field_id)
return '' if ! @project.gollum_wiki.use_ckeditor;
ckeditor_include + ckeditor(field_id)
end

def ckeditor_include
javascript_include_tag('ckeditor/ckeditor', :plugin => 'redmine_ckeditor')
end

def ckeditor(field_id)
url = url_for(
:controller => 'gollum_pages',
:action => 'upload',
:authenticity_token => form_authenticity_token)

javascript_tag <<-EOT
CKEDITOR.replace('#{field_id}', {
filebrowserImageUploadUrl : '#{url}',
});
CKEDITOR.config.height='500px';
EOT
end
end
5 changes: 4 additions & 1 deletion app/views/gollum_pages/_sidebar.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<h3><%= l(:Gollum) %></h3>

<%= link_to l(:field_start_page), {:action => 'show'} %><br />
<%= link_to l(:field_start_page), {:controller=>'gollum_pages', :action => 'show'} %><br />
<%= link_to l(:upload), {:controller=>'gollum_pages', :action => 'upload'} %><br />
<%= link_to l(:new_page), {:controller=>'gollum_pages', :action => 'newpage'} %><br />
<%= link_to l(:list_pages), {:controller=>'gollum_pages', :action => 'list'} %><br />
42 changes: 41 additions & 1 deletion app/views/gollum_pages/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
<% if @project.gollum_wiki.store_as_wiki %>
<div class="contextual">
<%= link_to_if_authorized(
l(:button_raw),
{:action => 'raw', :id => @page_name},
:class => 'icon icon-file')
%>
</div>
<h2><%= @page_name %></h2>
<% end %>

<%= form_for :page, :url => project_gollum_page_path(:id => @page_name), :html => { :method => "put" } do |f| -%>
<% if @project.gollum_wiki.use_ckeditor %>
<%= hidden_field_tag 'page[format]', @page_format %>
<% else %>
<p>
<%= label(:gollum_wiki, :markup_language) %>
<%= select(:page, :format, options_for_select(Gollum::Page::FORMAT_NAMES.keys, @page_format), {:disabled => false}) %></p>
</p>
<% end %>

<% if @project.gollum_wiki.store_as_wiki %>
<%= f.text_area :formatted_data, :class => "wiki-edit", :id=> 'raw_data_id', :cols => 100, :rows => 30 %>
<% else %>
<%= f.text_area :raw_data, :class => "wiki-edit", :id=> 'raw_data_id', :cols => 100, :rows => 30 %>
<% end %>
<!--%# < % = wikitoolbar_for 'raw_data_id' % > -->
<% if @project.gollum_wiki.use_ckeditor %>
<% if Redmine::Plugin.installed?('redmine_ckeditor') %>
<% if @project.gollum_wiki.use_ckeditor %>
<%= gollum_include_ckeditor 'raw_data_id' %>
<% end %>
<% else %>
<div class="flash error">Alert: redmine_ckeditor plugin is not installed</div>
<% end %>
<% end %>

<%= f.text_area :raw_data, :class => "wiki-edit", :cols => 100, :row => 25 %>
<p>
<%= f.submit(l(:button_save)) %>
<% #preview
Expand All @@ -15,5 +44,16 @@
<%= link_to(l(:label_preview), previewScript) %>
</p>
<% end %>


<div id="preview"></div>
<%= javascript_include_tag "preview.js", :plugin => 'redmine_gollum' %>


<% content_for :header_tags do %>
<base href='<%= url_for :controller => 'gollum_pages', :action => 'index' %>' />
<% end %>

<% content_for :sidebar do %>
<%= render :partial => 'sidebar' %>
<% end %>
16 changes: 16 additions & 0 deletions app/views/gollum_pages/list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

<h2><%= l(:page_list) %></h2>

<h3>Pages</h3>
<ul>
<% @pages.each do |page| %>
<li> <%= link_to page.name, {:action=>:show, :id=>page.name}%></li>
<% end %>
</ul>

<h3>Files</h3>
<table>
<% @files.each do |filename| %>
<tr><td><a href="<%= filename%>"><img height="30px" src='<%= filename %>'></a></td><td><%= filename %></td></tr>
<% end %>
</table>
13 changes: 13 additions & 0 deletions app/views/gollum_pages/newpage.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

<h2><%= l :create_new_page %> </h2>

<%= form_for :page, :url => url_for(:controller=>'gollum_pages',:action=>'edit') do |f| -%>

<p>
<label label-for='pageid'><%= l :new_page_name%></label>
<input type='text' id='pageid' name='id'>
<input type='submit' value="<%= l(:button_create) %>">
</p>

<% end %>

7 changes: 7 additions & 0 deletions app/views/gollum_pages/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<div class="contextual">
<% if @project.gollum_wiki.store_as_wiki %>
<%= link_to_if_authorized(
l(:button_raw),
{:action => 'raw', :id => @page_name},
:class => 'icon icon-file')
%>
<% end %>
<% if @editable %>
<%= link_to_if_authorized(
l(:button_edit),
Expand Down
9 changes: 9 additions & 0 deletions app/views/gollum_pages/upload.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>Upload file</h1>
<div>Upload:

<%= form_tag({:action => :upload}, :multipart => true) do %>
<%= file_field_tag "upload" %>
<input type='submit' value='upload'>
<% end %>

</div>
17 changes: 16 additions & 1 deletion app/views/gollum_wikis/_edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="box tabular">
<p>
<%= label(:gollum_wiki, :git_path) %>
<%= f.text_field :git_path %>
<%= f.text_field :git_path, :size => 50 %>
</p>
<p>
<%= label(:gollum_wiki, :markup_language) %>
Expand All @@ -13,6 +13,21 @@
<%= label(:gollum_wiki, :page_files_directory) %>
<%= f.text_field :directory %>
</p>
<p>
<%= label(:gollum_images_directory, :images_directory) %>
<%= f.text_field :images_directory %>
</p>
<p>
<% if @gollum_wiki.use_ckeditor && ! Redmine::Plugin.installed?('redmine_ckeditor') %>
<em class="info error">Alert: redmine_ckeditor plugin is not installed</em>
<% end %>
<%= label(:gollum_use_ckeditor, :use_ckeditor) %>
<%= f.check_box :use_ckeditor %>
</p>
<p>
<%= label(:gollum_edit_html_store_as_wiki, :edit_html_store_as_wiki) %>
<%= f.check_box :store_as_wiki %>
</p>
</div>

<p><%= f.submit %></p>
Expand Down
Loading