Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,6 @@ def labeled_check_box_tags(name, collection, options = {})
end.join.html_safe
end

def html_safe_gsub(string, *gsub_args, &)
html_safe = string.html_safe?
result = string.gsub(*gsub_args, &)

# We only mark the string as safe if the previous string was already safe
if html_safe
result.html_safe # rubocop:disable Rails/OutputSafety
else
result
end
end

def authoring(created, author, options = {})
label = options[:label] || :label_added_time_by
I18n.t(label, author: link_to_user(author), age: time_tag(created)).html_safe
Expand Down Expand Up @@ -265,9 +253,9 @@ def accesskey(s)

# Same as Rails' simple_format helper without using paragraphs
def simple_format_without_paragraph(text)
html_safe_gsub(text.to_s, /\r\n?/, "\n")
.then { |res| html_safe_gsub(res, /\n\n+/, "<br /><br />") }
.then { |res| html_safe_gsub(res, /([^\n]\n)(?=[^\n])/, '\1<br />') }
text.to_s.html_safe_gsub(/\r\n?/, "\n")
.then { it.html_safe_gsub(/\n\n+/, "<br /><br />") }
.then { it.html_safe_gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') }
end

def lang_options_for_select(blank = true)
Expand Down
5 changes: 3 additions & 2 deletions lib/core_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
# See COPYRIGHT and LICENSE files for more details.
#++

require "core_extensions/string"
require "core_extensions/time_with_zone"
Dir[File.join(File.dirname(__FILE__), "core_extensions/**/*.rb")].each { |f| require f }

String.prepend CoreExtensions::String
String.prepend CoreExtensions::HtmlSafeGsub
ActiveSupport::SafeBuffer.prepend CoreExtensions::HtmlSafeGsub
ActiveSupport::TimeWithZone.include CoreExtensions::TimeWithZone
48 changes: 48 additions & 0 deletions lib/core_extensions/html_safe_gsub.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 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 COPYRIGHT and LICENSE files for more details.
#++

# Refinement to allow gsub on html_safe strings
module CoreExtensions
module HtmlSafeGsub
##
# Calls #gsub on a string while retaining its original html_safe? property
def html_safe_gsub(*gsub_args, &)
html_safe = html_safe?
result = gsub(*gsub_args, &)

# We only mark the string as safe if the previous string was already safe
if html_safe
result.html_safe # rubocop:disable Rails/OutputSafety
else
result
end
end
end
end
58 changes: 31 additions & 27 deletions lib_static/redmine/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,35 +115,13 @@ def format_date(date_or_time, time_zone: nil, format: Setting.date_format)
# @param links [Hash] Link names mapped to URLs.
# @param external [Boolean] Whether the links should be opened as external links, i.e. in a new tab (default: true)
# @param underline [Boolean] Whether to underline links inserted into the text (default: true)
def link_translate(i18n_key, links: {}, external: true, underline: true) # rubocop:disable Metrics/AbcSize
translation = ::I18n.t(i18n_key.to_s)
result = translation.scan(link_regex).inject(translation) do |t, matches|
link, text, key = matches
link_reference = links[key.to_sym]
href =
case link_reference
when Array
OpenProject::Static::Links.url_for(*link_reference)
else
String(link_reference)
end
target = external ? "_blank" : nil
link_tag = render(
Primer::Beta::Link.new(
href:,
target:,
underline:,
data: { allow_external_link: true }
)
) do |l|
l.with_trailing_visual_icon(icon: :"link-external") if external
text
end
def link_translate(i18n_key, links: {}, external: true, underline: true)
output = ActiveSupport::SafeBuffer.new
output << ApplicationController.helpers.t(i18n_key.to_s)

t.sub(link, link_tag)
output.html_safe_gsub(link_regex) do
create_link_content($3, $2, external:, links:, underline:)
end

result.html_safe
end

##
Expand Down Expand Up @@ -288,5 +266,31 @@ def all_attribute_translations(locale)
end
@cached_attribute_translations[locale]
end

private

def create_link_content(key, text, external:, links:, underline:)
link_reference = links[key.to_sym]
href =
case link_reference
when Array
OpenProject::Static::Links.url_for(*link_reference)
else
String(link_reference)
end
target = external ? "_blank" : nil

render(
Primer::Beta::Link.new(
href:,
target:,
underline:,
data: { allow_external_link: true }
)
) do |l|
l.with_trailing_visual_icon(icon: :"link-external") if external
text
end
end
end
end
10 changes: 5 additions & 5 deletions modules/reporting/lib/widget/table/report_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ def configure_walker
unless row.fields.empty?
subrows[0] = capture do
concat content_tag(:th, show_row(row), class: "top left -breakword", rowspan: subrows.size)
concat html_safe_gsub(subrows[0], "class='normal", "class='top")
concat subrows[0].html_safe_gsub("class='normal", "class='top")
concat content_tag(:th, show_result(row), class: "top right", rowspan: subrows.size)
end
end
subrows[-1] = html_safe_gsub(subrows.last, "class='normal", "class='bottom")
subrows[-1] = html_safe_gsub(subrows.last, "class='top", "class='bottom top")
subrows[-1] = subrows.last.html_safe_gsub("class='normal", "class='bottom")
subrows[-1] = subrows.last.html_safe_gsub("class='top", "class='bottom top")

subrows
end
Expand Down Expand Up @@ -87,7 +87,7 @@ def render_tbody
odd = true
walker.body do |line|
if first
line = html_safe_gsub(line, "class='normal", "class='top")
line = line.html_safe_gsub("class='normal", "class='top")
first = false
end
line = mark_penultimate_column(line)
Expand All @@ -98,7 +98,7 @@ def render_tbody
end

def mark_penultimate_column(line)
html_safe_gsub(line, /(<td class='([^']+)'[^<]+<\/td>)[^<]*<th .+/) do |m|
line.html_safe_gsub(/(<td class='([^']+)'[^<]+<\/td>)[^<]*<th .+/) do |m|
m.sub /class='([^']+)'/, 'class=\'\1 penultimate\''
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/redmine/i18n_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ module OpenProject

before do
allow(::I18n)
.to receive(:t)
.with("translation_with_a_link")
.to receive(:translate)
.with("translation_with_a_link", *any_args)
.and_return("There is a [link](url_1) in this translation! Maybe even [two](url_2)?")
end

Expand Down
Loading