自动从Haml输出中删除所有换行符 [英] Auto-removing all newlines from Haml output
问题描述
我在Rails 3应用程序中使用Haml,它的换行符使我发疯!例如,
I'm using Haml in a Rails 3 app, and its newlines drive me nuts! For example,
%span
foo
提供为
<span>
foo
</span>
但我宁愿想要
<span>foo</foo>
原因(除了更清晰的XML外)是,对于我的Selenium测试,多余的换行符是一个问题,因为它们使执行诸如"//span[.='foo']"
之类的XPath查询的能力混乱了.因此,我不得不写'\nfoo\n'
而不是(ew!),或使用//span[contains(text(), 'foo')]
,它们匹配的范围太广.
The reason (apart from being cleaner XML) is that extra newlines are a problem for my Selenium tests because they mess up the ability to do XPath queries like "//span[.='foo']"
. So I'd have to write '\nfoo\n'
instead (ew!), or use //span[contains(text(), 'foo')]
, which matches too broadly.
我知道我可以使用鳄鱼运算符(<"和>")来去除空格,但是由于我从来没有想过 出现换行符,输出,最后我只是将它们机械地添加到每一行的末尾.看起来好像很干燥.
I know I could use the alligator operators ("<" and ">") to strip whitespace, but since I don't ever have a case where I want the newlines to appear in the output, I'd just end up adding them mechanically at the end of each line. And that just seems very unDRY.
所以现在我看到两个解决方案:
So now I see two solutions:
- 强制Haml永远不要发出换行符,除非它们来自Ruby表达式.我看到一些
nuke_inner_whitespace
/nuke_outer_whitespace
变量可能会在Haml代码周围散布,但我不确定如何在不借助免费的猴子补丁的情况下更改它们. - 插入Rails,将
gsub!("\n", "")
应用于所有渲染的HTML. (对于textarea和pres,我仍然可以使用~ "foo\nbar"
来使Haml发出foo
bar
.)但是,挂接到Rails的正确位置在哪里?我对代码有些迷茫.
- Force Haml to never emit newlines unless they come from a Ruby expression. I see some
nuke_inner_whitespace
/nuke_outer_whitespace
variables spread around the Haml code that might do the job, but I'm not sure how to change them without resorting to gratuitous monkey-patching. - Hook into Rails to apply a
gsub!("\n", "")
to all rendered HTML. (For textarea's and pre's, I could still use~ "foo\nbar"
to make Haml emitfoo
bar
.) But where's the right place to hook into Rails? I'm a little lost in the code.
任何指针或其他建议表示赞赏!
Any pointers or other suggestions appreciated!
更新:我已经在下面使用Jason的猴子补丁了一段时间,我开始认为这是不值得的.例如.如果要获取<span>[del]</span> <span>foo</span>
,很难不在nuked之间留空格.甚至以下内容也会在页面上显示为[del]foo
Update: I've used Jason's monkey patch below for a while now and I'm beginning to think that it's not worth it. E.g. if I want to get <span>[del]</span> <span>foo</span>
, it's difficult to not have the blank space in between nuked. Even the following will render as [del]foo
on the page:
%span
= '[del] '
%span
foo
因此,我想我要回过头来手动添加鳄鱼操作员(请参见下面的自我解答).生活和学习.
So I think I'm going back to adding alligator operators manually (see my self-answer down below). Live and learn.
再次感谢Jason! :)
Thanks again to Jason! :)
推荐答案
如果在元素名称的末尾放置一个小于号,则内容周围的空白将被抑制:
If you place a less-than sign at the end of the element name the whitespace around the content will be suppressed:
%span<
foo
有关详细信息,请参见Haml参考中的去除空白
See whitespace removal in the Haml reference for more details.
似乎没有一种清除所有标签上这些标志的干净方法,但是以下猴子补丁在Haml 3.0.24上可以正常使用:
There doesn't appear to be any clean way to force these flags on for all tags but the following monkey patch works fine with Haml 3.0.24:
module Haml::Precompiler
def parse_tag_with_nuked_whitespace(line)
result = parse_tag_without_nuked_whitespace line
unless result.size == 9 && [false,true].include?(result[4]) && [false,true].include?(result[5])
raise "Unexpected parse_tag output: #{result.inspect}"
end
result[4] = true # nuke_outer_whitespace
result[5] = true # nuke_inner_whitespace
result
end
alias_method_chain :parse_tag, :nuked_whitespace
end
叉起Haml并在Engine选项中添加一个选项以始终删除空格可能并不难. parse_tag方法可以选中此选项(如果已启用)并将内部和外部标志设置为true.我将其留给读者练习. :)
It probably wouldn't be hard to fork Haml and add an option to the Engine options to always nuke whitespace. The parse_tag method could check this option if enabled and set the inner and outer flags to true. I'll leave this as an exercise for the reader. :)
这篇关于自动从Haml输出中删除所有换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!