是否可以使用Nokogiri解析样式表? [英] Is it possible to parse a stylesheet with Nokogiri?
问题描述
我花了我的必要的两个小时Googling这个,我找不到任何好的答案,所以让我们看看人类能否击败Google电脑。
I've spent my requisite two hours Googling this, and I can not find any good answers, so let's see if humans can beat Google computers.
以解析Ruby中的样式表,以便我可以将这些样式应用于文档中的元素(以使样式内联)。所以,我想采取像
I want to parse a stylesheet in Ruby so that I can apply those styles to elements in my document (to make the styles inlined). So, I want to take something like
<style>
.mystyle {
color:white;
}
</style>
并且能够将其解压缩为某种类型的Nokogiri对象。
And be able to extract it into a Nokogiri object of some sort.
Nokogiri类CSS :: Parser( http://nokogiri.rubyforge.org/nokogiri/Nokogiri/CSS/Parser.html )肯定有一个有前途的名称,但我找不到任何文档,它是什么或它是如何工作的,所以我不知道它是否可以做我在这里之后。
The Nokogiri class "CSS::Parser" (http://nokogiri.rubyforge.org/nokogiri/Nokogiri/CSS/Parser.html) certainly has a promising name, but I can't find any documentation on what it is or how it works, so I have no idea if it can do what I'm after here.
我的最终目标是能够编写代码: p>
My end goal is to be able to write code something like:
a_web_page = Nokogiri::HTML(html_page_as_string)
parsed_styles = Nokogiri::CSS.parse(html_page_as_string)
parsed_styles.each do |style|
existing_inlined_style = a_web_page.css(style.declaration) || ''
a_web_page.css(style.declaration)['css'] = existing_inlined_style + style.definition
end
这将从样式表中提取样式,并将它们全部作为内联样式添加到我的文档中。
Which would extract styles from a stylesheet and add them all as inlined styles to my document.
推荐答案
p> @molf肯定有一个很好的开始,但它仍然需要调试一些问题,使其在生产中工作。这是目前的测试版本:
@molf definitely had a great start there, but it still required debugging a handful of problems to get it working in production. Here is the current, tested version of this:
html = Nokogiri::HTML(html_string)
css = CssParser::Parser.new
css.add_block!(html_string) # Warning: This line modifies the string passed into it. In potentially bad ways. Make sure the string has been duped and stored elsewhere before passing this.
css.each_selector do |selector, declarations, specificity|
next unless selector =~ /^[\d\w\s\#\.\-]*$/ # Some of the selectors given by css_parser aren't actually selectors.
begin
elements = html.css(selector)
elements.each do |match|
match["style"] = [match["style"], declarations].compact.join(" ")
end
rescue
logger.info("Couldn't parse selector '#{selector}'")
end
end
html_with_inline_styles = html.to_s
这篇关于是否可以使用Nokogiri解析样式表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!