class Wiki::PagesController < ApplicationController before_filter :authenticate_user! before_filter :load_page def index @roots = Page.roots end def create create_params = page_params if params[:markdown_body].present? create_params[:body] = redcarpet(params[:markdown_body]) end @page = Page.new create_params.merge( creator: current_user, parent_id: params[:parent_id] ) if @page.save @page.update_to_gollum(current_user) @page.create_activity key: "page.created", owner: current_user end end def show @space = @page.root @subtree = @page.subtree.group_by(&:parent_id) @last_updated = @page.last_updated end def history end def compare version = @page.versions.select{|x| x.sha == params[:version]}.first filename = version.show.first.b_path #TODO: should cache the diffs (store on save page in delayed job) @old_html = version.repo.git.show({}, "#{version.sha}~1:#{filename}").force_encoding('UTF-8') @new_html = version.repo.git.show({}, "#{version.sha}:#{filename}").force_encoding('UTF-8') giri = Nokogiri::HTML(DaisyDiff.strings(@old_html, @new_html)) @diff_html = giri.css("body") @diff_html.css(".diffpage-html-firstlast").remove @diff_html.css("script").remove @diff_html.xpath('//@onclick').remove @diff_html.xpath('//@onload').remove @diff_html.xpath('//@onerror').remove @diff_html.xpath('//@onabort').remove end def combined @subtree = @page.subtree.arrange render layout: "minimal" end def update update_params = params.require(params[:type]).permit( :title, :body, ) if params[:markdown_body].present? update_params[:body] = redcarpet(params[:markdown_body]) end if (params[:timestamp].present? && @page.last_updated.present?) && ((params[:timestamp].present? && !@page.last_updated.present?) || @page.last_updated.try(:created_at) != Time.parse(params[:timestamp])) @page.errors.add(:base, "This page was edited by someone. Please refresh to get the updated changes. Refreshing the page will remove the your changes.") end if @page.errors.none? && @page.update_attributes(update_params) @page.update_to_gollum(current_user) @last_updated = @page.create_activity key: "page.update_details", owner: current_user end end def new @page = Page.new creator: current_user end def reorder old_parent_id = @page.parent_id old_position = @page.position if @page.reorder!( parent_id: params[:parent_id], position: params[:position] ) @activity = @page.create_activity key: "page.reorder", owner: current_user, parameters: { from_parent_id: old_parent_id, old_position: old_position, parent_id: params[:parent_id], position: params[:position] } end render json: {} end def attach @attachment = Attachment.create_from_uploaded_file(params[:file], current_user, attachable: @page) if @attachment.errors.none? @activity = @page.create_activity key: "page.attachment", owner: current_user, recipient: @attachment end end def subpages_dropdown render partial: 'subpages_dropdown' end def star starred = Starred.where(user_id: current_user.id, starrable_id: @page, starrable_type: @page.class).first if starred && params[:to_star] == "false" starred.destroy end if !starred && params[:to_star] == "true" Starred.create(user: current_user, starrable: @page) end render json: {} end def add_tag @page.tag_list.add(params[:tag]) @page.save @page.__elasticsearch__.index_document render json: {} end def remove_tag @page.tag_list.remove(params[:tag]) @page.save @page.__elasticsearch__.index_document render json: {} end def destroy parent = @page.parent @page.save_and_destroy(current_user) link = root_url link = wiki_page_path(parent) if parent redirect_to link, notice: "Page was deleted.", status: :see_other end protected def load_page @page = Page.find(params[:id]) if params[:id] end private def page_params params.require(params[:type]).permit(:title, :body) end def redcarpet(text) markdown = Redcarpet::Markdown.new( Redcarpet::Render::HTML, no_intra_emphasis: true, tables: true, fenced_code_blocks: true, autolink: true, # disable_indented_code_blocks: true, strikethrough: true, lax_spacing: true, space_after_headers: true, superscript: true, # underline: true, highlight: true, quote: true, footnotes: true, ) markdown.render text end end