Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: minor code cleanup #357

Merged
merged 5 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.commonmark.Extension;
import org.commonmark.ext.footnotes.internal.*;
import org.commonmark.parser.Parser;
import org.commonmark.parser.beta.InlineContentParserFactory;
import org.commonmark.renderer.NodeRenderer;
import org.commonmark.renderer.html.HtmlRenderer;
import org.commonmark.renderer.markdown.MarkdownNodeRendererContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.commonmark.ext.footnotes.FootnoteReference;
import org.commonmark.ext.footnotes.InlineFootnote;
import org.commonmark.node.LinkReferenceDefinition;
import org.commonmark.node.Text;
import org.commonmark.parser.InlineParserContext;
import org.commonmark.parser.beta.LinkInfo;
import org.commonmark.parser.beta.LinkProcessor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
import org.commonmark.ext.footnotes.InlineFootnote;
import org.commonmark.node.*;
import org.commonmark.renderer.NodeRenderer;
import org.commonmark.renderer.html.HtmlNodeRendererContext;
import org.commonmark.renderer.html.HtmlWriter;
import org.commonmark.renderer.markdown.MarkdownNodeRendererContext;
import org.commonmark.renderer.markdown.MarkdownWriter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

public class FootnoteMarkdownNodeRenderer implements NodeRenderer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public TableHtmlNodeRenderer(HtmlNodeRendererContext context) {
this.context = context;
}

@Override
protected void renderBlock(TableBlock tableBlock) {
htmlWriter.line();
htmlWriter.tag("table", getAttributes(tableBlock, "table"));
Expand All @@ -25,6 +26,7 @@ protected void renderBlock(TableBlock tableBlock) {
htmlWriter.line();
}

@Override
protected void renderHead(TableHead tableHead) {
htmlWriter.line();
htmlWriter.tag("thead", getAttributes(tableHead, "thead"));
Expand All @@ -33,6 +35,7 @@ protected void renderHead(TableHead tableHead) {
htmlWriter.line();
}

@Override
protected void renderBody(TableBody tableBody) {
htmlWriter.line();
htmlWriter.tag("tbody", getAttributes(tableBody, "tbody"));
Expand All @@ -41,6 +44,7 @@ protected void renderBody(TableBody tableBody) {
htmlWriter.line();
}

@Override
protected void renderRow(TableRow tableRow) {
htmlWriter.line();
htmlWriter.tag("tr", getAttributes(tableRow, "tr"));
Expand All @@ -49,6 +53,7 @@ protected void renderRow(TableRow tableRow) {
htmlWriter.line();
}

@Override
protected void renderCell(TableCell tableCell) {
String tagName = tableCell.isHeader() ? "th" : "td";
htmlWriter.line();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.commonmark.ext.gfm.tables.*;
import org.commonmark.node.Node;
import org.commonmark.renderer.NodeRenderer;
import org.commonmark.renderer.markdown.MarkdownNodeRendererContext;
import org.commonmark.renderer.markdown.MarkdownWriter;
import org.commonmark.text.AsciiMatcher;
Expand All @@ -13,7 +12,7 @@
/**
* The Table node renderer that is needed for rendering GFM tables (GitHub Flavored Markdown) to text content.
*/
public class TableMarkdownNodeRenderer extends TableNodeRenderer implements NodeRenderer {
public class TableMarkdownNodeRenderer extends TableNodeRenderer {
private final MarkdownWriter writer;
private final MarkdownNodeRendererContext context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public TableTextContentNodeRenderer(TextContentNodeRendererContext context) {
this.context = context;
}

@Override
protected void renderBlock(TableBlock tableBlock) {
// Render rows tight
textContentWriter.pushTight(true);
Expand All @@ -30,19 +31,23 @@ protected void renderBlock(TableBlock tableBlock) {
textContentWriter.block();
}

@Override
protected void renderHead(TableHead tableHead) {
renderChildren(tableHead);
}

@Override
protected void renderBody(TableBody tableBody) {
renderChildren(tableBody);
}

@Override
protected void renderRow(TableRow tableRow) {
renderChildren(tableRow);
textContentWriter.block();
}

@Override
protected void renderCell(TableCell tableCell) {
renderChildren(tableCell);
// For the last cell in row, don't render the delimiter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import java.util.*;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class TablesTest extends RenderingTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void testTaskListItems() {

}

@Override
protected String render(String source) {
return RENDERER.render(PARSER.parse(source));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import org.commonmark.testutil.example.Example;
import org.commonmark.testutil.example.ExampleReader;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.util.ArrayList;
import java.util.List;

import static org.commonmark.testutil.Asserts.assertRendering;

@RunWith(Parameterized.class)
public abstract class SpecTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private static boolean isMarker(ParserState state, int index) {
}

public static class Factory extends AbstractBlockParserFactory {
@Override
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
int nextNonSpace = state.getNextNonSpaceIndex();
if (isMarker(state, nextNonSpace)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.commonmark.parser.delimiter.DelimiterProcessor;
import org.commonmark.text.Characters;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.commonmark.internal.util.Escaping;
import org.commonmark.internal.util.LinkScanner;
import org.commonmark.node.DefinitionMap;
import org.commonmark.node.LinkReferenceDefinition;
import org.commonmark.node.SourceSpan;
import org.commonmark.parser.SourceLine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.commonmark.parser.block.ParserState;

import java.util.List;
import java.util.Map;

public class ParagraphParser extends AbstractBlockParser {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.commonmark.internal.util;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.regex.Matcher;
Expand Down
1 change: 1 addition & 0 deletions commonmark/src/main/java/org/commonmark/node/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
public abstract class Block extends Node {

@Override
public Block getParent() {
return (Block) super.getParent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private class RendererContext implements MarkdownNodeRendererContext {
private RendererContext(MarkdownWriter writer) {
// Set fields that are used by interface
this.writer = writer;
Set<Character> escapes = new HashSet<Character>();
Set<Character> escapes = new HashSet<>();
for (MarkdownNodeRendererFactory factory : nodeRendererFactories) {
escapes.addAll(factory.getSpecialCharacters());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;

public class DocumentParserTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.commonmark.node.*;
import org.commonmark.parser.Parser;
import org.commonmark.testutil.Asserts;
import org.junit.Test;

import static org.commonmark.testutil.Asserts.assertRendering;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import org.commonmark.parser.Parser;
import org.commonmark.renderer.NodeRenderer;
import org.commonmark.renderer.html.*;
import org.commonmark.testutil.Asserts;
import org.commonmark.testutil.RenderingTestCase;
import org.commonmark.testutil.TestResources;
import org.junit.Test;

Expand All @@ -19,8 +17,8 @@
import java.util.concurrent.Future;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class HtmlRendererTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

public class LinkReferenceDefinitionNodeTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class ListBlockParserTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import java.util.concurrent.Future;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class ParserTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.commonmark.node.AbstractVisitor;
import org.commonmark.node.Node;
import org.commonmark.node.SourceSpan;

import java.util.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@

import java.util.Set;

import static org.junit.Assert.assertEquals;

public class TextContentRendererTest {

private static final Parser PARSER = Parser.builder().build();
private static final TextContentRenderer COMPACT_RENDERER = TextContentRenderer.builder().build();
private static final TextContentRenderer SEPARATE_RENDERER = TextContentRenderer.builder()
.lineBreakRendering(LineBreakRendering.SEPARATE_BLOCKS).build();
private static final TextContentRenderer STRIPPED_RENDERER = TextContentRenderer.builder().stripNewlines(true).build();
private static final TextContentRenderer STRIPPED_RENDERER = TextContentRenderer.builder()
.lineBreakRendering(LineBreakRendering.STRIP).build();

@Test
public void textContentText() {
Expand All @@ -47,7 +46,6 @@ public void textContentHeading() {
@Test
public void textContentEmphasis() {
String s;
String rendered;

s = "***foo***";
assertCompact(s, "foo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class ThematicBreakParserTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void addAttributes() {
Parser parser = Parser.builder().build();
HtmlRenderer renderer = HtmlRenderer.builder()
.attributeProviderFactory(new AttributeProviderFactory() {
@Override
public AttributeProvider create(AttributeProviderContext context) {
return new ImageAttributeProvider();
}
Expand All @@ -95,6 +96,7 @@ public void customizeRendering() {
Parser parser = Parser.builder().build();
HtmlRenderer renderer = HtmlRenderer.builder()
.nodeRendererFactory(new HtmlNodeRendererFactory() {
@Override
public NodeRenderer create(HtmlNodeRendererContext context) {
return new IndentedCodeBlockNodeRenderer(context);
}
Expand Down
Loading