Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add macro helper to format settings and base url

Oliver Güntheree95ed34edff
diff --git a/.rubocop.yml b/.rubocop.yml
index f746bd22776..e5ec0596dc0 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -150,6 +150,11 @@ RSpec/NestedGroups:
RSpec/DescribeMethod:
Enabled: false
+# Don't force the second argument of describe
+# to match the exact file name
+RSpec/FilePath:
+ SpecSuffixOnly: true
+
# We use let!() to ensure dependencies are created
# instead of let() and referencing them explicitly
RSpec/LetSetup:
diff --git a/config/settings.yml b/config/settings.yml
index 81c72f01d82..023185f2627 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -317,7 +317,7 @@ emails_footer:
default:
en: |-
You have received this notification because you have either subscribed to it, or are involved in it.
- To change your notification preferences, please click here: http://hostname/my/account
+ To change your notification preferences, please click here: opSetting:base_url/my/notifications
start_of_week:
default: ''
first_week_of_year:
diff --git a/lib/open_project/text_formatting/filters/pattern_matcher_filter.rb b/lib/open_project/text_formatting/filters/pattern_matcher_filter.rb
index b8aef6babfe..15d13eb1c31 100644
--- a/lib/open_project/text_formatting/filters/pattern_matcher_filter.rb
+++ b/lib/open_project/text_formatting/filters/pattern_matcher_filter.rb
@@ -38,7 +38,8 @@ module OpenProject::TextFormatting
[
OpenProject::TextFormatting::Matchers::ResourceLinksMatcher,
OpenProject::TextFormatting::Matchers::WikiLinksMatcher,
- OpenProject::TextFormatting::Matchers::AttributeMacros
+ OpenProject::TextFormatting::Matchers::AttributeMacros,
+ OpenProject::TextFormatting::Matchers::SettingMacros
]
end
diff --git a/lib/open_project/text_formatting/matchers/setting_macros.rb b/lib/open_project/text_formatting/matchers/setting_macros.rb
new file mode 100644
index 00000000000..671d0a8c3f4
--- /dev/null
+++ b/lib/open_project/text_formatting/matchers/setting_macros.rb
@@ -0,0 +1,70 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module OpenProject::TextFormatting
+ module Matchers
+ # OpenProject macros for inserting setting values
+ # Examples:
+ # opSetting:host_name # Outputs the Setting.host_name
+ class SettingMacros < RegexMatcher
+ ALLOWED_SETTINGS = %w[
+ host_name
+ base_url
+ ].freeze
+
+ def self.regexp
+ %r{
+ opSetting:(#{ALLOWED_SETTINGS.join("|")})
+ }x
+ end
+
+ ##
+ # Faster inclusion check before the regex is being applied
+ def self.applicable?(content)
+ content.include?('opSetting:')
+ end
+
+ def self.process_match(match, matched_string, _context)
+ variable = match[1]
+ return matched_string unless ALLOWED_SETTINGS.include?(variable)
+
+ send variable
+ end
+
+ def self.host_name
+ OpenProject::StaticRouting::UrlHelpers.host
+ end
+
+ def self.base_url
+ OpenProject::Application.root_url
+ end
+ end
+ end
+end
diff --git a/spec/lib/open_project/text_formatting/markdown/setting_variable_spec.rb b/spec/lib/open_project/text_formatting/markdown/setting_variable_spec.rb
new file mode 100644
index 00000000000..f59615910da
--- /dev/null
+++ b/spec/lib/open_project/text_formatting/markdown/setting_variable_spec.rb
@@ -0,0 +1,68 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+require_relative './expected_markdown'
+
+describe OpenProject::TextFormatting,
+ 'Setting variable' do
+ include_context 'expected markdown modules'
+
+ describe 'attribute label macros' do
+ it_behaves_like 'format_text produces' do
+ let(:raw) do
+ <<~RAW
+ Inline reference to variable setting: opSetting:host_name
+
+ Inline reference to base_url variable: opSetting:base_url
+
+ Inline reference to invalid variable: opSetting:smtp_password
+
+ Inline reference to missing variable: opSetting:does_not_exist
+ RAW
+ end
+
+ let(:expected) do
+ <<~EXPECTED
+ <p class="op-uc-p">
+ Inline reference to variable setting: #{OpenProject::StaticRouting::UrlHelpers.host}
+ </p>
+ <p class="op-uc-p">
+ Inline reference to base_url variable: #{OpenProject::Application.root_url}
+ </p>
+ <p class="op-uc-p">
+ Inline reference to invalid variable: opSetting:smtp_password
+ </p>
+ <p class="op-uc-p">
+ Inline reference to missing variable: opSetting:does_not_exist
+ </p>
+ EXPECTED
+ end
+ end
+ end
+end