You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Has anyone found a way to use Nokogiri (or Loofah) to replace double-break tags with closing/opening paragraph tags? I have a lot of code in the database with this madness, and I would like to scrub it back out:
<p>Some text here in a logical paragraph.
<br>
<br>
Some more text, apparently a second paragraph.
<br>
<br>
Et cetera...
</p>
and I replied with:
#!/usr/bin/env rubyrequire"nokogiri"html=<<~HTML<p>Some text here in a logical paragraph.<br><br> Some more text, apparently a second paragraph.<br><br> Et cetera...</p><p>foo<brid=1><brid=2> bar<brid=11><brid=12> bar</p><p>baz<brid=3></p><notp>foo<brid=4><brid=5></notp>HTMLdoc=Nokogiri::HTML5::Document.parse(html)putsdoc.to_htmlp_with_brs=doc.xpath(%q{//p[br[following-sibling::br]]})p_with_brs.eachdo |p|
new_p=p.add_previous_sibling("<p>").first# remove blank text nodesp.children.eachdo |c|
c.unlinkifc.text? && c.blank?endp.children.eachdo |c|
nextifc.parent.nil?# already unlinkedifc.name == "br" && c.next_sibling.name == "br"new_p=p.add_previous_sibling("<p>").firstc.next_sibling.unlinkc.unlinkelsec.parent=new_pendendp.unlinkendputsdoc.to_html
which outputs:
<html><head></head><body><p>Some text here in a logical paragraph.
</p><p>
Some more text, apparently a second paragraph.
</p><p>
Et cetera...
</p>
<p>foo
</p><p>
bar
</p><p>
bar
</p>
<p>baz
<br id="3">
</p>
<notp>foo
<br id="4">
<br id="5">
</notp>
</body></html>
I think this could be useful in a scrubber if it's something people commonly do.
From a slack thread at https://rubyonrails-link.slack.com/archives/C05054QPL/p1700056469860939
and I replied with:
which outputs:
I think this could be useful in a scrubber if it's something people commonly do.
cc @walterdavis
The text was updated successfully, but these errors were encountered: