Skip to content

Commit

Permalink
#409 convert unused "main" method in QuotingExample to QuotingExample…
Browse files Browse the repository at this point in the history
…Test

this test still doesn't verify anything, but at least it's a working sample of html with quotes.
  • Loading branch information
asolntsev committed Oct 12, 2024
1 parent 3281f13 commit 9846e47
Showing 1 changed file with 47 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,55 +1,47 @@
import org.xhtmlrenderer.simple.XHTMLPanel;
import org.xhtmlrenderer.util.XMLUtil;

import javax.swing.*;

import static javax.swing.SwingUtilities.invokeLater;


public class QuotingExample extends JFrame {
//currently we cannot display different quotes based on depth
private static final String DOCUMENT =
"""
<html>
<head>
<style type='text/css'><![CDATA[
* { quotes: '"' '"' "'" "'" }
q:before { content: open-quote }
q:after { content: close-quote }
blockquote p:before { content: open-quote }
blockquote p:after { content: no-close-quote }
blockquote p.last:after { content: close-quote }
]]></style>
</head>
<body>
<blockquote>
<p>This is just a test of the emergency <q>quoting</q> system.</p>
<p>This is only a test.</p>
<p class='last'>Thank you for your cooperation during this <q>test.</q></p>
</blockquote>
</body>
</html>
""";

protected void frameInit() {
super.frameInit();

setTitle("CSS Quoting Example");
setDefaultCloseOperation(EXIT_ON_CLOSE);

XHTMLPanel xr = new XHTMLPanel();
try {
xr.setDocument(XMLUtil.documentFromString(DOCUMENT));
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}

add(xr);

setSize(500, 300);
}

public static void main(String[] args) {
invokeLater(() -> new QuotingExample().setVisible(true));
}
}
package org.xhtmlrenderer.swing;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

import static org.xhtmlrenderer.swing.Java2DRenderer.htmlAsImage;

public class QuotingExampleTest {
private static final Logger log = LoggerFactory.getLogger(QuotingExampleTest.class);

//currently we cannot display different quotes based on depth
private static final String DOCUMENT =
"""
<html>
<head>
<style type='text/css'><![CDATA[
* { quotes: '"' '"' "'" "'" }
q:before { content: open-quote }
q:after { content: close-quote }
blockquote p:before { content: open-quote }
blockquote p:after { content: no-close-quote }
blockquote p.last:after { content: close-quote }
]]></style>
</head>
<body>
<blockquote>
<p>This is just a test of the emergency <q>quoting</q> system.</p>
<p>This is only a test.</p>
<p class='last'>Thank you for your cooperation during this <q>test.</q></p>
</blockquote>
</body>
</html>
""";

@Test
public void exampleWithQuotes() throws Exception {
BufferedImage image = htmlAsImage(DOCUMENT, 600);
File result = new File("target/%s.png".formatted(getClass().getSimpleName()));
ImageIO.write(image, "png", result);
log.info("Generated image from html: {}", result.getAbsolutePath());
}
}

0 comments on commit 9846e47

Please sign in to comment.