2015-01-19 01:48:03 +01:00
|
|
|
module FoodsoftWiki
|
|
|
|
class WikiParser < WikiCloth::Parser
|
2015-01-19 01:48:42 +01:00
|
|
|
template do |template|
|
|
|
|
Foodsoft::ExpansionVariables.get(template)
|
|
|
|
end
|
|
|
|
|
2015-01-19 01:48:03 +01:00
|
|
|
url_for do |page|
|
|
|
|
url_for page
|
|
|
|
end
|
|
|
|
|
2019-06-15 16:21:02 +02:00
|
|
|
link_attributes_for do |page|
|
2015-01-19 01:48:03 +01:00
|
|
|
permalink = Page.permalink(page)
|
2023-05-12 13:01:12 +02:00
|
|
|
if Page.exists?(permalink: permalink)
|
2019-06-15 16:21:02 +02:00
|
|
|
{ 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])
|
2015-01-19 01:48:03 +01:00
|
|
|
else
|
|
|
|
{ href: url_for(:new_page_path, title: page, parent: params[:referer]), class: 'new_wiki_link' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
section_link do |_section|
|
|
|
|
''
|
2015-01-19 01:48:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_html(render_options = {})
|
|
|
|
# workaround for wikicloth 0.8.0 https://github.com/nricciar/wikicloth/pull/59
|
|
|
|
render_options[:locale] ||= I18n.locale
|
|
|
|
super(render_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-06-15 16:21:02 +02:00
|
|
|
def link_attributes_if_number_sign_contained_in_nonexistent(page, referer)
|
|
|
|
# Interpret the part after the last number sign as anchor.
|
2021-03-01 15:27:26 +01:00
|
|
|
arr = page.split('#', -1) # `-1` preserves empty anchor
|
2019-06-15 16:21:02 +02:00
|
|
|
page = arr[0...-1].join('#')
|
|
|
|
anchor = arr[-1]
|
|
|
|
|
|
|
|
return { href: '#' + anchor } if page.empty?
|
|
|
|
|
|
|
|
permalink = Page.permalink(page)
|
2023-05-12 13:01:12 +02:00
|
|
|
if Page.exists?(permalink: permalink)
|
2019-06-15 16:21:02 +02:00
|
|
|
{ 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
|
|
|
|
|
2021-03-01 15:27:26 +01:00
|
|
|
def url_for(path_name, options = {})
|
|
|
|
Rails.application.routes.url_helpers.send path_name, options.merge({ foodcoop: FoodsoftConfig.scope })
|
2015-01-19 01:48:03 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|