tipsy / j2html

Java to HTML generator. Enjoy typesafe HTML generation.
https://j2html.com/
Apache License 2.0
765 stars 137 forks source link

question for <code> tag #208

Closed Suna-Wang closed 2 years ago

Suna-Wang commented 2 years ago

I wrote a test case to test the code tag, the case is followed:

public void pre_code_test12() {
    String output = pre(
        code("#include &lt;iostream&gt;" +
            "void main(){" +
            "cout &lt;&lt; &quot;Hello!&quot; &lt;&lt; end1;" +
            "}")
    ).render();

} What I expected is

#include <iostream>void main(){cout << "Hello!" << end1;}

But the output is

#include &lt;iostream&gt;void main(){cout &lt;&lt; &quot;Hello!&quot; &lt;&lt; end1;}

Then the output will be different from the real need. expected:

include void main(){cout << "Hello!" << end1;}

output:

include <iostream>void main(){cout << "Hello!" << end1;}

How can I produce what I want?

sembler commented 2 years ago

@Suna-Wang j2html provides automatic text escaping in most cases. For your example you can use: pre( code("#include <iostream>void main(){cout << "Hello!" << end1;}") ).render()

which will produce this html: <pre><code>#include &lt;iostream&gt;void main(){cout &lt;&lt; &quot;Hello!&quot; &lt;&lt; end1;}</code></pre>

which the browser should render into the output you desired.

Suna-Wang commented 2 years ago

Thank you very much!~