저는 이것이 문제가 해결 될만큼 일반적이라고 생각합니다.하지만 Loofah와 Nokogiri가있는 초심자 인 저는 아직 해결책을 찾지 못했습니다.텍스트에서 HTML을 벗겨 내고 Loofah와 Nokogiri로 자를 포함하고 있습니다.
Nokogiri를 감싸는 HTML 스크러버 라이브러리 인 Loofah를 사용하여 일부 HTML 텍스트를 스크럽하여 표시합니다. 그러나이 텍스트는 전자 메일 주소 등의 경우에 때로는 <
과 >
사이의 문자 (예 : <[email protected]>
)에서 발생합니다. 수세미는 HTML 또는 XML 태그로 생각하고 있으며 텍스트에서 제외합니다.
실제 태그를 제거하는 작업을 수행하면서 이러한 일이 발생하지 않도록하는 방법이 있습니까?
편집 : 여기 실패한 테스트 케이스의 :
require 'test/unit'
require 'test/unit/ui/console/testrunner'
require 'nokogiri'
MAGICAL_REGEXP = /<([^(?:\/|!\-\-)].*)>/
def filter_html(content)
# Current approach in a gist: We capture content enclosed in angle brackets.
# Then, we check if the excerpt right after the opening bracket is a valid HTML
# tag. If it's not, we substitute the matched content (which is the captured
# content enclosed in angle brackets) for the captured content enclosed in
# the HTML entities for the angle brackets. This does not work with nested
# HTML tags, since regular expressions are not meant for this.
content.to_s.gsub(MAGICAL_REGEXP) do |excerpt|
capture = $1
Nokogiri::HTML::ElementDescription[capture.split(/[<> ]/).first] ? excerpt : "<#{capture}>"
end
end
class HTMLTest < Test::Unit::TestCase
def setup
@raw_html = <<-EOS
<html>
<[email protected]>
<p><[email protected]<b class="highlight">bar</b>.baz></p>
<p>
<[email protected]<b class="highlight">bar</b>.baz>
</p>
< don't erase this >
</html>
EOS
@filtered_html = <<-EOS
<html>
<[email protected]>
<p><[email protected]<b class="highlight">bar</b>.baz></p>
<p>
<[email protected]<b class="highlight">bar</b>.baz>
</p>
< don't erase this >
</html>
EOS
end
def test_filter_html
assert_equal(@filtered_html, filter_html(@raw_html))
end
end
# Can you make this test pass?
Test::Unit::UI::Console::TestRunner.run(HTMLTest)
우리는 현재 시도하고 이러한 목표를 달성하기 위해 꽤 나쁜 정규식 해커를 사용하고 있지만, 상태 위의 주석으로,이 태그가 작동하지 않습니다 " 중첩 된 태그가 아닌 태그 내부. 실제로는 <b class="highlight">
요소도 보존하려고합니다.
아래 샘플은 수세미를 사용하지 않지만 응용 프로그램 자체는 다른 곳에서 수행하므로 여기에 추가하기가 어렵지 않습니다. 우리는 어떤 구성 옵션을 사용해야하는지 확실하지 않습니다.
텍스트에 실제로 "<"문자가 포함되어 있거나 "<"엔터티입니까? –
문제를 표시하는 최소한의 스크립트 (입력 및 출력과 함께)를 제공하면 대답하기가 쉽습니다. –
@ mark-thomas : 실패한 테스트 사례를 추가하기 위해 질문을 편집했습니다. 나는 그것이 문제에 대해 밝히기를 희망한다. – Bira