From bbe609974a1ff7df0962fa2a15b76fab64e400b4 Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Wed, 25 Sep 2019 11:44:42 +0200 Subject: [PATCH] Fix change of format (#1419) * Add empty language def for bibtex --- lib/gollum/app.rb | 4 +-- .../gollum/javascript/editor/langs/bib.js | 8 ++++++ .../javascript/editor/langs/plaintext.js | 8 ++++++ .../public/gollum/javascript/gollum.js.erb | 17 ++++++++++--- lib/gollum/views/helpers.rb | 3 +++ test/test_app.rb | 25 +++++++++++++++++-- 6 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 lib/gollum/public/gollum/javascript/editor/langs/bib.js create mode 100644 lib/gollum/public/gollum/javascript/editor/langs/plaintext.js diff --git a/lib/gollum/app.rb b/lib/gollum/app.rb index d6106774..81192701 100644 --- a/lib/gollum/app.rb +++ b/lib/gollum/app.rb @@ -275,8 +275,8 @@ module Precious end committer.commit - wikip = wiki_page(rename) - page = wikip.page + # Renaming preserves format, so add the page's format to the renamed path to retrieve the renamed page + page = wiki_page("#{rename}.#{Gollum::Page.format_to_ext(page.format)}").page return if page.nil? redirect to("/#{page.escaped_url_path}") end diff --git a/lib/gollum/public/gollum/javascript/editor/langs/bib.js b/lib/gollum/public/gollum/javascript/editor/langs/bib.js new file mode 100644 index 00000000..ee50ed92 --- /dev/null +++ b/lib/gollum/public/gollum/javascript/editor/langs/bib.js @@ -0,0 +1,8 @@ +/** + * BibTeX Language Definition + * + */ + +(function($) { + $.GollumEditor.defineLanguage('bib', {}); +})(jQuery); \ No newline at end of file diff --git a/lib/gollum/public/gollum/javascript/editor/langs/plaintext.js b/lib/gollum/public/gollum/javascript/editor/langs/plaintext.js new file mode 100644 index 00000000..32916adb --- /dev/null +++ b/lib/gollum/public/gollum/javascript/editor/langs/plaintext.js @@ -0,0 +1,8 @@ +/** + * Plain Text Language Definition + * + */ + +(function($) { + $.GollumEditor.defineLanguage('txt', {}); +})(jQuery); \ No newline at end of file diff --git a/lib/gollum/public/gollum/javascript/gollum.js.erb b/lib/gollum/public/gollum/javascript/gollum.js.erb index 918b51fa..26787166 100755 --- a/lib/gollum/public/gollum/javascript/gollum.js.erb +++ b/lib/gollum/public/gollum/javascript/gollum.js.erb @@ -10,9 +10,13 @@ function brokenAvatarImage(image){ // Get path for named route, prefixing baseUrl if necessary // Uses the route definitions in /lib/gollum/views/helpers.rb. // For example, routePath('delete') is equivalent to 'delete_path' in the mustache templates. +var gollumRoutes = $.parseJSON('<%= routes_to_json %>') function routePath(name){ - var routes = $.parseJSON('<%= routes_to_json %>') - path = routes[name] + path = gollumRoutes[name] + return prefixBaseUrl(path); +} + +function prefixBaseUrl(path) { if (baseUrl == undefined ) { console.log('Gollum error: baseUrl undefined') } else if (path == undefined ) { @@ -72,6 +76,12 @@ function abspath(path, name){ return [newPath, newName]; } +var gollumFormats = $.parseJSON('<%= format_extensions %>'); +function formatToExt(format) { + // 'markdown' -> .md, 'asciidoc' -> .asciidoc + return gollumFormats[format]; +} + function setTextDirection () { $('.markdown-body p, .markdown-body span, .markdown-body pre, .markdown-body table').attr('dir','auto'); } @@ -405,6 +415,7 @@ $(document).ready(function() { $(this).attr('disabled', true); var formData = new FormData($('#gollum-editor-form').get(0)); + var newPath = prefixBaseUrl(pagePath() + '/' + pageName() + formatToExt($('#wiki_format')[0].value)); var endpoint = $('#gollum-editor-form').attr("action"); $.ajax({ @@ -414,7 +425,7 @@ $(document).ready(function() { processData: false, contentType: false, success: function(data) { - window.location = window.location.href.replace(/gollum\/edit\//, '') + window.location = newPath; }, error: function(data, textStatus, errorThrown) { if (data.status == 412) { diff --git a/lib/gollum/views/helpers.rb b/lib/gollum/views/helpers.rb index 29c44ba4..676bb4ed 100644 --- a/lib/gollum/views/helpers.rb +++ b/lib/gollum/views/helpers.rb @@ -51,6 +51,9 @@ module Precious define_method :routes_to_json do @@route_methods.to_json end + define_method :format_extensions do + Hash[Gollum::Markup.formats.to_a.map{|fmt| [fmt[0], ".#{Gollum::Page.format_to_ext(fmt[0])}"]}].to_json + end end def page_route(page) diff --git a/test/test_app.rb b/test/test_app.rb index 303972bc..43e6d763 100644 --- a/test/test_app.rb +++ b/test/test_app.rb @@ -173,6 +173,15 @@ context "Frontend" do assert_not_equal page_1.version.sha, page_2.version.sha end + test "rename preserves format" do + page_1 = @wiki.page("B") + post "/gollum/rename/B", :rename => "/C.rst", :message => 'def' + + follow_redirect! + assert_equal '/C.rst.md', last_request.fullpath + assert last_response.ok? + end + test "renames page catches invalid page" do # No such page post "/gollum/rename/no-such-file-here", :rename => "/C", :message => 'def' @@ -194,7 +203,6 @@ context "Frontend" do assert_equal last_response.status, 500 end - test "renames page in subdirectory" do page_1 = @wiki.page("G/H") assert_not_equal page_1, nil @@ -229,7 +237,6 @@ context "Frontend" do assert_not_equal page_1.version.sha, page_2.version.sha end - test "creates page" do post "/gollum/create", :content => 'abc', :page => "D", :format => 'markdown', :message => 'def' @@ -330,6 +337,20 @@ context "Frontend" do assert_equal nil, page_e end + test "edit allows changing format" do + post '/gollum/create', :content => 'create_msg', :page => 'gandalf', + :path => '/', :format => 'markdown', :message => '' + page = @wiki.page('gandalf.md') + assert page + + @wiki.clear_cache + + post '/gollum/edit/', :content => 'new content', :format => 'txt', :page => 'gandalf', :path => '/', :message => '', :etag => page.sha + assert last_response.ok? + assert_nil @wiki.page('gandalf.md') + assert @wiki.page('gandalf.txt') + end + test "page create and edit with dash & page rev" do page = 'c-d-e' path = 'a/b/' # path must end with /