2012-04-16 64 views
1

假设这是很容易的,但有一个很难用换行(因此,当我在Web浏览器中查看源,我可以扫描顶部至底部)斯卡拉NodeSeq减少/ foldLeft用换行

打印出NodeSeq html标签的

现在,NodeSeq被打印为一条长线。

示例代码:

listOfPaths map (jsNode(_)) reduce (_++_) 

def jsNode(path: String): NodeSeq = 
    <script type="text/javascript" src={"/static/js/"+path}></script> 

那么,如何获得\ n在每个节点的结束?

谢谢

回答

4

无论您使用什么来呈现HTML,这都是您的工作。例如,如果你使用scala.xml.PrettyPrinter,你可以这样做:当你调用printer.format(header)

val printer = new xml.PrettyPrinter(80, 2) 
val paths = List("script-1.js", "script-2.js") 
val header = <head>{paths map (jsNode(_)) reduce (_++_)}</head> 

现在,你会得到如下:

<head> 
    <script type="text/javascript" src="/static/js/script-1.js"></script> 
    <script type="text/javascript" src="/static/js/script-2.js"></script> 
</head> 

注意,第一个参数PrettyPrinter构造函数指定页面宽度,第二个指定要缩进的空格数。

如果你只是想要的东西快速和肮脏的,你可以删除文本节点的元素之间(或之后):

paths map (jsNode(_)) reduce (_++ Text("\n") ++ _) 

但是其他的解决方案几乎总是最好。

+0

谢谢,后者,快速和肮脏,现在做的伎俩:只是想快速视觉分析输出,在生产谷歌mod_pagespeed将压缩整个源html到1长不可读的行;-) – virtualeyes 2012-04-17 07:30:42