debian-mirror-gitlab/lib/banzai/filter/inline_diff_filter.rb

30 lines
905 B
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-03-02 22:35:43 +05:30
# Generated HTML is transformed back to GFM by app/assets/javascripts/behaviors/markdown/marks/inline_diff.js
2016-06-02 11:05:42 +05:30
module Banzai
module Filter
class InlineDiffFilter < HTML::Pipeline::Filter
IGNORED_ANCESTOR_TAGS = %w(pre code tt).to_set
def call
2018-05-09 12:01:36 +05:30
doc.search(".//text()").each do |node|
2016-06-02 11:05:42 +05:30
next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS)
content = node.to_html
html_content = inline_diff_filter(content)
2016-06-02 11:05:42 +05:30
next if content == html_content
2016-06-02 11:05:42 +05:30
node.replace(html_content)
2016-06-02 11:05:42 +05:30
end
doc
end
def inline_diff_filter(text)
html_content = text.gsub(/(?:\[\-(.*?)\-\]|\{\-(.*?)\-\})/, '<span class="idiff left right deletion">\1\2</span>')
html_content.gsub(/(?:\[\+(.*?)\+\]|\{\+(.*?)\+\})/, '<span class="idiff left right addition">\1\2</span>')
end
2016-06-02 11:05:42 +05:30
end
end
end