简体   繁体   English

如何使用Nokogiri用<p>标签包装HTML无标记文本?

[英]How do I wrap HTML untagged text with <p> tag using Nokogiri?

I have to parse an HTML document into different new files. 我必须将HTML文档解析为不同的新文件。 The problem is that there are text nodes which have not been wrapped with "<p>" tags, instead they having "<br>" tags at the end of each paragraph. 问题是有些文本节点没有用"<p>"标签包装,而是在每个段落的末尾都有"<br>"标签。

I want to wrap this text with <p> tags using Nokogiri: 我想用Nokogiri用<p>标签包装这个文本:

<div id="f15"><b>Footnote 15</b>: Catullus iii, 12.</div>
<div class="pgmonospaced pgheader"><br/>
<br/>
End of the Project abc<br/>
<br/>
*** END OF THIS PROJECT XYZ ***<br/>
<br/>
***** This file should be named new file.html... *****<br/>
<br/></div>

After searching around some forums and doing some debugging locally, i have found the following solution to my problem. 在搜索一些论坛并在本地进行一些调试之后,我找到了以下解决方案来解决我的问题。

html_doc = Nokogiri::HTML.parse('path/to/html_file')
html_doc
html_doc.search("//br/preceding-sibling::text()|//br/following-sibling::text()").each do |node|
    node.replace(Nokogiri.make("<p>#{node.to_html}</p>"))
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从嵌套中获取文本 <p> 使用html敏捷包在外部html中标记? - how do i get the text from a nested <p> tag in an external html using html agility pack? 如何使用Nokogiri在标签下获取所有文本? - How do I get all the text under a tag with Nokogiri? 如何对<a>标签</a>的文字进行自动换行<a>?</a> - How do I word wrap the text of an <a> tag? 如何使用Nokogiri在页面上创建HTML标签结构的轮廓? - How do I create an outline of the HTML tag structure on the page using Nokogiri? 如何在 pre 标签中包装文本? - How do I wrap text in a pre tag? 我如何包装所有文本节点 <p> 在div中可能还包含其他 <p> 标签和<strong>使用jQuery之类的</strong>标签<strong>?</strong> - How do I wrap all text nodes with <p> in a div that also may contain other <p> tags and tags such as <strong> using jQuery? 如何使用JavaScript / Jquery将文字包装在链接标记中? - How do I wrap text within a link tag using JavaScript/Jquery? 如何在Internet Explorer的预标签中包装文字? - How do I wrap text in a pre tag for Internet explorer? 如何在一个范围内包装嵌套在锚标记中的文本? - How do I wrap text nested in an anchor tag, within a span? 使用CSS我应该使用p标签来包装文本还是div足够? - With CSS should I use p tag to wrap text or will div suffice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM