質問

I am using this plugin:

compile ":html-cleaner:0.2"

I want to keep \n characters in my strings

 def s = "a\nb\nc\n"
 println s             

Prints:

a  
b  
c    

When I use:

println cleanHtml(s, 'none')

It prints:

a b c

You can create a whitelist using:

htmlcleaner {
    whitelists = {
        whitelist("sample") {
            startwith "none"
            allow "b", "p", "i", "span"
        }
    }
}

How can I keep the \n characters from being stripped?

役に立ちましたか?

解決

Disclaimer: I'm not all that familiar with Jsoup or the html-cleaner plugin.

However, I can read the source and documentation for both and it would appear this is not possible with the plugin. Looking at the plugin source code for HtmlCleaner you will see the following relevant line:

return Jsoup.clean(unsafe, whitelists[whitelist])

This is relevant because in order to preserve new line characters in Jsoup you need to set printPretty(false) on the output settings, which is true by default. This SO question & answer outlines this.

It would appear that you will need to use Jsoup directly instead of using the plugin. Alternatively you could fork and patch the plugin to allow this to be configured.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top