This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
forked from commonmark/commonmark-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request commonmark#274 from commonmark/dingus-app
Add DingusApp for quickly testing out renderings
- Loading branch information
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
commonmark-integration-test/src/test/java/org/commonmark/ui/DingusApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package org.commonmark.ui; | ||
|
||
import org.commonmark.parser.Parser; | ||
import org.commonmark.renderer.html.HtmlRenderer; | ||
import org.commonmark.renderer.text.TextContentRenderer; | ||
|
||
import java.awt.*; | ||
import javax.swing.*; | ||
import javax.swing.event.ChangeEvent; | ||
import javax.swing.event.ChangeListener; | ||
import javax.swing.event.DocumentEvent; | ||
import javax.swing.event.DocumentListener; | ||
|
||
/** | ||
* Simple UI to quickly test out different rendering of CommonMark inputs. | ||
* Similar to <a href="https://spec.commonmark.org/dingus/">commonmark.js dingus</a>. | ||
**/ | ||
public class DingusApp { | ||
|
||
private final Parser parser = Parser.builder().build(); | ||
private final TextContentRenderer textRenderer = TextContentRenderer.builder().build(); | ||
private final HtmlRenderer htmlRenderer = HtmlRenderer.builder().build(); | ||
|
||
private final JTabbedPane tabbedPane; | ||
private final JEditorPane htmlVisualRendererOutput; | ||
private final JTextArea htmlSourceRendererOutput; | ||
private final JTextArea textRendererOutput; | ||
|
||
public static void main(String[] args) { | ||
new DingusApp().run(); | ||
} | ||
|
||
private DingusApp() { | ||
tabbedPane = new JTabbedPane(); | ||
|
||
htmlVisualRendererOutput = new JEditorPane(); | ||
htmlVisualRendererOutput.setEnabled(false); | ||
htmlVisualRendererOutput.setContentType("text/html"); | ||
|
||
htmlSourceRendererOutput = new JTextArea(); | ||
htmlSourceRendererOutput.setEnabled(false); | ||
htmlSourceRendererOutput.setLineWrap(true); | ||
htmlSourceRendererOutput.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); | ||
|
||
textRendererOutput = new JTextArea(); | ||
textRendererOutput.setEnabled(false); | ||
textRendererOutput.setLineWrap(true); | ||
textRendererOutput.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); | ||
} | ||
|
||
private void run() { | ||
JFrame frame = new JFrame("commonmark-java dingus"); | ||
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | ||
frame.setMinimumSize(new Dimension(400, 300)); | ||
frame.setSize(new Dimension(1200, 675)); | ||
|
||
final JTextArea input = new JTextArea(); | ||
input.setBorder(BorderFactory.createTitledBorder("Input")); | ||
input.setLineWrap(true); | ||
input.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); | ||
|
||
input.getDocument().addDocumentListener(new DocumentListener() { | ||
@Override | ||
public void insertUpdate(DocumentEvent e) { | ||
updateOutput(input.getText()); | ||
} | ||
|
||
@Override | ||
public void removeUpdate(DocumentEvent e) { | ||
updateOutput(input.getText()); | ||
} | ||
|
||
@Override | ||
public void changedUpdate(DocumentEvent e) { | ||
} | ||
}); | ||
|
||
tabbedPane.addTab("HTML rendered", htmlVisualRendererOutput); | ||
tabbedPane.addTab("HTML source", htmlSourceRendererOutput); | ||
tabbedPane.addTab("Plain text", textRendererOutput); | ||
|
||
tabbedPane.addChangeListener(new ChangeListener() { | ||
@Override | ||
public void stateChanged(ChangeEvent e) { | ||
updateOutput(input.getText()); | ||
} | ||
}); | ||
|
||
input.setText("# Example\n" + | ||
"Enter text *here* and see how it renders on the right.\n\n" + | ||
"* Try\n* this\n\n" + | ||
"```\nor this\n```"); | ||
updateOutput(input.getText()); | ||
|
||
frame.setLayout(new GridLayout()); | ||
frame.add(input); | ||
frame.add(tabbedPane); | ||
|
||
frame.setVisible(true); | ||
} | ||
|
||
private void updateOutput(String inputText) { | ||
if (tabbedPane.getSelectedComponent() == htmlVisualRendererOutput) { | ||
String rendered = htmlRenderer.render(parser.parse(inputText)); | ||
htmlVisualRendererOutput.setText(rendered); | ||
} else if (tabbedPane.getSelectedComponent() == htmlSourceRendererOutput) { | ||
String rendered = htmlRenderer.render(parser.parse(inputText)); | ||
htmlSourceRendererOutput.setText(rendered); | ||
} else if (tabbedPane.getSelectedComponent() == textRendererOutput) { | ||
String rendered = textRenderer.render(parser.parse(inputText)); | ||
textRendererOutput.setText(rendered); | ||
} | ||
} | ||
} |