Fix legacy formatting module

pull/6015/head
Oliver Günther 7 years ago
parent 688648581e
commit ea45a2194d
No known key found for this signature in database
GPG Key ID: 88872239EB414F99
  1. 2
      app/helpers/work_packages_helper.rb
  2. 2
      app/views/user_mailer/_issue_details.html.erb
  3. 12
      lib/open_project/text_formatting.rb
  4. 65
      lib/open_project/text_formatting/formatters/textile/legacy_text_formatting.rb
  5. 1
      lib/open_project/text_formatting/matchers/link_handlers/hash_separator.rb
  6. 9
      lib/open_project/text_formatting/renderer.rb
  7. 30
      spec/lib/open_project/text_formatting/textile_spec.rb

@ -266,7 +266,7 @@ module WorkPackagesHelper
else
::OpenProject::TextFormatting::Renderer.format_text(
description_lines.join(''),
object: work_package
attribute: :description
)
end
end

@ -50,4 +50,4 @@ See docs/COPYRIGHT.rdoc for more details.
<% end %>
</ul>
<%= format_text(issue.description, only_path: false, object: issue, project: issue.project) %>
<%= format_text(issue.description, attribute: :description, only_path: false, object: issue, project: issue.project) %>

@ -37,20 +37,16 @@ module OpenProject
# * with an object and one of its attribute: format_text(issue, :description, options)
def format_text(*args)
# Forward to the legacy text formatting for textile syntax
if Setting.text_formatting == 'textile'
return Formatters::Textile::LegacyTextFormatting.format_text(*args)
end
options = args.last.is_a?(Hash) ? args.pop : {}
case args.size
when 1
attribute = nil
object = options[:object]
text = args.shift
when 2
object = args.shift
attr = args.shift
text = object.send(attr).to_s
attribute = args.shift
text = object.send(attribute).to_s
else
raise ArgumentError, 'invalid arguments to format_text'
end
@ -60,6 +56,8 @@ module OpenProject
Renderer.format_text text,
options.merge(
object: object,
request: try(:request),
attribute: attribute,
project: project
)
end

@ -29,57 +29,57 @@
module OpenProject::TextFormatting::Formatters
module Textile
module LegacyTextFormatting
class LegacyTextFormatting
include Redmine::WikiFormatting::Macros::Definitions
include ActionView::Helpers::SanitizeHelper
include ERB::Util # for h()
include Redmine::I18n
# used for the work package quick links
include WorkPackagesHelper
# Used for escaping helper 'h()'
include ERB::Util
# Rails helper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TextHelper
# For route path helpers
include OpenProject::ObjectLinking
# The WorkPackagesHelper is required to get access to the methods
# 'work_package_css_classes' and 'work_package_quick_info'.
include WorkPackagesHelper
include OpenProject::StaticRouting::UrlHelpers
# Truncation
include OpenProject::TextFormatting::Truncation
def initialize(project)
@project = project
end
def controller; end
class << self
# Formats text according to system settings.
# 2 ways to call this method:
# * with a String: format_text(text, options)
# * with an object and one of its attribute: format_text(issue, :description, options)
def format_text(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
case args.size
when 1
obj = options[:object]
text = args.shift
when 2
obj = args.shift
attr = args.shift
text = obj.send(attr).to_s
else
raise ArgumentError, 'invalid arguments to format_text'
end
return '' if text.blank?
def format_text(text, options)
edit = !!options[:edit]
# don't return html in edit mode when textile or text formatting is enabled
return text if edit
project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
object = options[:object]
project = options[:project]
only_path = options.delete(:only_path) != false
# offer 'plain' as readable version for 'no formatting' to callers
format = options[:format] == 'plain' ? '' : options[:format]
format = options.delete(:format) { :textile }
text = OpenProject::TextFormatting::Formatters.formatter_for(format).new(text).to_html
# TODO: transform modifications into WikiFormatting Helper, or at least ask the helper if he wants his stuff to be modified
@parsed_headings = []
text = parse_non_pre_blocks(text) { |text|
[:execute_macros, :parse_inline_attachments, :parse_wiki_links, :parse_redmine_links, :parse_headings, :parse_relative_urls].each do |method_name|
send method_name, text, project, obj, attr, only_path, options
send method_name, text, project, object, options[:attribute], only_path, options
end
}
if @parsed_headings.any?
replace_toc(text, @parsed_headings)
replace_toc(text, @parsed_headings, options)
end
escape_non_macros(text)
@ -452,7 +452,7 @@ module OpenProject::TextFormatting::Formatters
tocitem = strip_tags(content.gsub(/<br \/>/, ' '))
anchor = item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
@parsed_headings << [level, anchor, tocitem]
url = full_url(anchor)
url = full_url(anchor, options[:request])
"<a name=\"#{anchor}\"></a>\n<h#{level} #{attrs}>#{content}<a href=\"#{url}\" class=\"wiki-anchor\">&para;</a></h#{level}>"
end
end
@ -460,7 +460,7 @@ module OpenProject::TextFormatting::Formatters
TOC_RE = /<p>\{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE)
# Renders the TOC with given headings
def replace_toc(text, headings)
def replace_toc(text, headings, options)
text.gsub!(TOC_RE) do
if headings.empty?
''
@ -488,7 +488,7 @@ module OpenProject::TextFormatting::Formatters
elsif started
out << '</li><li>'
end
url = full_url anchor
url = full_url(anchor, options[:request])
out << "<a href=\"#{url}\">#{item}</a>"
current = level
started = true
@ -503,17 +503,12 @@ module OpenProject::TextFormatting::Formatters
#
# displays the current url plus an optional anchor
#
def full_url(anchor_name = '')
def full_url(anchor_name = '', current_request = nil)
return "##{anchor_name}" if current_request.nil?
current = request.original_fullpath
current = current_request.original_fullpath
return current if anchor_name.blank?
"#{current}##{anchor_name}"
end
def current_request
request rescue nil
end
end
end
end
end

@ -60,7 +60,6 @@ module OpenProject::TextFormatting::Matchers
end
def render_version
binding.pry
version = Version.visible.find_by(id: oid)
if version
link_to h(version.name),

@ -35,7 +35,14 @@ module OpenProject::TextFormatting
return '' if text.blank?
# offer 'plain' as readable version for 'no formatting' to callers
format = options.delete(:format) { Setting.text_formatting }
format = options.fetch(:format, Setting.text_formatting)
# Forward to the legacy text formatting for textile syntax
if format == 'textile'
return OpenProject::TextFormatting::Formatters::Textile::LegacyTextFormatting
.new(options[:project])
.format_text(text, options)
end
# Get the associated formatter
pipeline = OpenProject::TextFormatting::Pipeline.new(

@ -30,6 +30,7 @@ require 'spec_helper'
describe OpenProject::TextFormatting do
include OpenProject::TextFormatting
include ERB::Util
include WorkPackagesHelper # soft-dependency
include ActionView::Helpers::UrlHelper # soft-dependency
include ActionView::Context
@ -42,12 +43,16 @@ describe OpenProject::TextFormatting do
describe '.format_text' do
let(:project) { FactoryGirl.create :valid_project }
let(:identifier) { project.identifier }
let(:role) do
FactoryGirl.create :role,
permissions: %i(view_work_packages edit_work_packages
browse_repository view_changesets view_wiki_pages)
end
let(:project_member) do
FactoryGirl.create :user,
member_in_project: project,
member_through_role: FactoryGirl.create(:role,
permissions: [:view_work_packages, :edit_work_packages,
:browse_repository, :view_changesets, :view_wiki_pages])
member_through_role: role
end
let(:issue) do
FactoryGirl.create :work_package,
@ -56,11 +61,13 @@ describe OpenProject::TextFormatting do
type: project.types.first
end
let!(:non_member) do
FactoryGirl.create(:non_member)
end
before do
@project = project
allow(User).to receive(:current).and_return(project_member)
FactoryGirl.create(:non_member)
allow(Setting).to receive(:text_formatting).and_return('textile')
end
@ -585,7 +592,7 @@ describe OpenProject::TextFormatting do
##{issue.id}
</pre>
RAW
RAW
}
let(:expected) {
@ -597,7 +604,7 @@ RAW
##{issue.id}
</pre>
EXPECTED
EXPECTED
}
before do
@ -670,7 +677,7 @@ h3. Acidless Oranges
h2. Attributes
WIKI_TEXT
WIKI_TEXT
}
subject(:html) { format_text(wiki_text) }
@ -714,11 +721,4 @@ WIKI_TEXT
end
end
end
context 'deprecated methods' do
subject { self }
it { is_expected.to respond_to :textilizable }
it { is_expected.to respond_to :textilize }
end
end
Loading…
Cancel
Save