From 3ade87167a1a7dea226fd4afce2176bd3b81c4de Mon Sep 17 00:00:00 2001 From: JuliusR Date: Sat, 15 Jun 2019 16:21:02 +0200 Subject: [PATCH] Allow Wiki links to pages that have number signs in title --- plugins/wiki/lib/foodsoft_wiki/wiki_parser.rb | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/plugins/wiki/lib/foodsoft_wiki/wiki_parser.rb b/plugins/wiki/lib/foodsoft_wiki/wiki_parser.rb index b9014e01..a15504e5 100644 --- a/plugins/wiki/lib/foodsoft_wiki/wiki_parser.rb +++ b/plugins/wiki/lib/foodsoft_wiki/wiki_parser.rb @@ -9,13 +9,13 @@ module FoodsoftWiki url_for page end - link_attributes_for do |page_anchor| - page, anchor = page_anchor.split(/#/, 2) + link_attributes_for do |page| permalink = Page.permalink(page) - if page.empty? - { href: '#' + anchor } - elsif Page.exists?(:permalink => permalink) - { href: url_for(:wiki_page_path, permalink: permalink, anchor: anchor) } + if Page.exists?(:permalink => permalink) + { href: url_for(:wiki_page_path, permalink: permalink) } + elsif page.include? '#' + # If "Foo#Bar" does not exist then consider "Foo" with anchor. + link_attributes_if_number_sign_contained_in_nonexistent(page, params[:referer]) else { href: url_for(:new_page_path, title: page, parent: params[:referer]), class: 'new_wiki_link' } end @@ -34,6 +34,24 @@ module FoodsoftWiki private + def link_attributes_if_number_sign_contained_in_nonexistent(page, referer) + # Interpret the part after the last number sign as anchor. + arr = page.split('#', -1)# `-1` preserves empty anchor + page = arr[0...-1].join('#') + anchor = arr[-1] + + return { href: '#' + anchor } if page.empty? + + permalink = Page.permalink(page) + if Page.exists?(:permalink => permalink) + { href: url_for(:wiki_page_path, permalink: permalink, anchor: anchor) } + else + # Do not suggest to use number signs in the title. + good_page_title = arr[0] + { href: url_for(:new_page_path, title: good_page_title, parent: referer), class: 'new_wiki_link' } + end + end + def url_for(path_name, options={}) Rails.application.routes.url_helpers.send path_name, options.merge({foodcoop: FoodsoftConfig.scope}) end