|
|
@@ -7,10 +7,51 @@ class WikiPagesController < ApplicationController |
|
|
|
end |
|
|
|
|
|
|
|
def show_by_title |
|
|
|
wiki_page = WikiPage.find_by(title: params[:title]) |
|
|
|
title = params[:title] |
|
|
|
version = params[:version].presence |
|
|
|
|
|
|
|
wiki_page = WikiPage.find_by(title:) |
|
|
|
return head :not_found unless wiki_page |
|
|
|
|
|
|
|
render json: wiki_page.as_json.merge(body: wiki_page.body) |
|
|
|
wiki_page.sha = version |
|
|
|
|
|
|
|
body = wiki_page.body |
|
|
|
sha = wiki_page.sha |
|
|
|
pred = wiki_page.pred |
|
|
|
succ = wiki_page.succ |
|
|
|
render json: wiki_page.as_json.merge(body:, sha:, pred:, succ:) |
|
|
|
end |
|
|
|
|
|
|
|
def diff |
|
|
|
id = params[:id] |
|
|
|
from = params[:from] |
|
|
|
to = params[:to].presence |
|
|
|
return head :bad_request if id.blank? || from.blank? |
|
|
|
|
|
|
|
wiki_page_from = WikiPage.find(id) |
|
|
|
wiki_page_to = wiki_page_from.clone |
|
|
|
wiki_page_from.sha = from |
|
|
|
wiki_page_to.sha = to |
|
|
|
|
|
|
|
diffs = Diff::LCS.sdiff(wiki_page_from.body, wiki_page_to.body) |
|
|
|
diff_json = diffs.map { |change| |
|
|
|
case change.action |
|
|
|
when ?= |
|
|
|
{ type: 'context', content: change.old_element } |
|
|
|
when ?| |
|
|
|
[{ type: 'removed', content: change.old_element }, |
|
|
|
{ type: 'added', content: change.new_element }] |
|
|
|
when ?+ |
|
|
|
{ type: 'added', content: change.new_element } |
|
|
|
when ?- |
|
|
|
{ type: 'removed', content: change.old_element } |
|
|
|
end }.flatten.compact |
|
|
|
|
|
|
|
render json: { wiki_page_id: wiki_page_from.id, |
|
|
|
title: wiki_page_from.title, |
|
|
|
older_sha: wiki_page_from.sha, |
|
|
|
newer_sha: wiki_page_to.sha, |
|
|
|
diff: diff_json } |
|
|
|
end |
|
|
|
|
|
|
|
def create |
|
|
@@ -57,8 +98,12 @@ class WikiPagesController < ApplicationController |
|
|
|
|
|
|
|
render json: log.map { |commit| |
|
|
|
wiki_page = WikiPage.find(commit.message.split(' ')[1].to_i) |
|
|
|
wiki_page.sha = commit.id |
|
|
|
user = User.find(commit.author.name.to_i) |
|
|
|
{ sha: commit.id, |
|
|
|
{ sha: wiki_page.sha, |
|
|
|
pred: wiki_page.pred, |
|
|
|
succ: wiki_page.succ, |
|
|
|
|
|
|
|
wiki_page: wiki_page && { id: wiki_page.id, title: wiki_page.title }, |
|
|
|
user: user && { id: user.id, name: user.name }, |
|
|
|
change_type: commit.message.split(' ')[0].downcase[0...(-1)], |
|
|
|