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

Run SettingMacros as a manual filter before markdown

Oliver Günther61e3c9bfe339
diff --git a/lib/open_project/text_formatting/filters/setting_macros_filter.rb b/lib/open_project/text_formatting/filters/setting_macros_filter.rb
index c8b785c748e..81f3479a699 100644
--- a/lib/open_project/text_formatting/filters/setting_macros_filter.rb
+++ b/lib/open_project/text_formatting/filters/setting_macros_filter.rb
@@ -30,23 +30,47 @@
module OpenProject::TextFormatting
module Filters
- class SettingMacrosFilter < PatternMatcherFilter
- def self.matchers
- [
- OpenProject::TextFormatting::Matchers::SettingMacros
- ]
+ class SettingMacrosFilter < HTML::Pipeline::Filter
+ ALLOWED_SETTINGS = %w[
+ host_name
+ base_url
+ ].freeze
+
+ def self.regexp
+ %r{
+ \{\{opSetting:(.+?)\}\}
+ }x
end
def call
- doc.search('.//text()|.//a[@href]').each do |node|
- next if has_ancestor?(node, PREFORMATTED_BLOCKS)
+ return html unless applicable?
+
+ html.gsub(self.class.regexp) do |matched_string|
+ variable = $1.to_s
+ variable.gsub!('\\', '')
- self.class.matchers.each do |matcher|
- matcher.call(node, doc: doc, context: context)
+ if ALLOWED_SETTINGS.include?(variable)
+ send variable
+ else
+ matched_string
end
end
+ end
+
+ private
+
+ def host_name
+ OpenProject::StaticRouting::UrlHelpers.host
+ end
+
+ def base_url
+ OpenProject::Application.root_url
+ end
- doc
+ ##
+ # Faster inclusion check before the regex is being applied
+ def applicable?
+ html.include?('{{opSetting:')
end
end
end
diff --git a/lib/open_project/text_formatting/formats/markdown/formatter.rb b/lib/open_project/text_formatting/formats/markdown/formatter.rb
index 4d90a57b5e3..e9eeabe6c75 100644
--- a/lib/open_project/text_formatting/formats/markdown/formatter.rb
+++ b/lib/open_project/text_formatting/formats/markdown/formatter.rb
@@ -44,8 +44,8 @@ module OpenProject::TextFormatting::Formats::Markdown
def filters
%i[
- markdown
setting_macros
+ markdown
sanitization
task_list
table_of_contents
diff --git a/lib/open_project/text_formatting/matchers/setting_macros.rb b/lib/open_project/text_formatting/matchers/setting_macros.rb
deleted file mode 100644
index 4d1fe466604..00000000000
--- a/lib/open_project/text_formatting/matchers/setting_macros.rb
+++ /dev/null
@@ -1,70 +0,0 @@
-#-- 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
index 2b7a10e00d9..15752e27f72 100644
--- a/spec/lib/open_project/text_formatting/markdown/setting_variable_spec.rb
+++ b/spec/lib/open_project/text_formatting/markdown/setting_variable_spec.rb
@@ -62,10 +62,10 @@ describe OpenProject::TextFormatting,
class="op-uc-link">Link with setting</a>
</p>
<p class="op-uc-p">
- Inline reference to invalid variable: opSetting:smtp_password
+ Inline reference to invalid variable: {{opSetting:smtp_password}}
</p>
<p class="op-uc-p">
- Inline reference to missing variable: opSetting:does_not_exist
+ Inline reference to missing variable: {{opSetting:does_not_exist}}
</p>
EXPECTED
end