Skip to content
Closed
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 @@ -61,6 +61,11 @@ private static Node insertNode(Node node, Node insertAfterNode) {
private class AutolinkVisitor extends AbstractVisitor {
int inLink = 0;

@Override
public void visit(AutoLink link) {
visit((Link)link);
}

@Override
public void visit(Link link) {
inLink++;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
package org.commonmark.ext.gfm.strikethrough;

import org.commonmark.node.CustomNode;
import org.commonmark.node.Delimited;

/**
* A strikethrough node containing text and other inline nodes nodes as children.
*/
public class Strikethrough extends CustomNode {
public class Strikethrough extends CustomNode implements Delimited {

private final char delimiterChar;
private final int delimiterCount;

public Strikethrough(char delimiterChar, int delimiterCount) {
this.delimiterChar = delimiterChar;
this.delimiterCount = delimiterCount;
}

@Override
public char getDelimiterChar() {
return delimiterChar;
}

@Override
public int getDelimiterCount() {
return delimiterCount;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void process(Text opener, Text closer, int delimiterCount) {
}

// Normal case, wrap nodes between delimiters in strikethrough.
Node strikethrough = new Strikethrough();
Node strikethrough = new Strikethrough(getDelimiterChar(), delimiterCount);

Node tmp = opener.getNext();
while (tmp != null && tmp != closer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ public void visit(IndentedCodeBlock indentedCodeBlock) {
renderCodeBlock(indentedCodeBlock.getLiteral(), getAttrs(indentedCodeBlock));
}

@Override
public void visit(AutoLink link) {
visit((Link)link);
}

@Override
public void visit(Link link) {
Map<String, String> attrs = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public int parseReference(String s) {

if (!referenceMap.containsKey(normalizedLabel)) {
Link link = new Link(dest, title);
link.setLabel(normalizedLabel);
referenceMap.put(normalizedLabel, link);
}
return this.pos - startPos;
Expand Down Expand Up @@ -565,6 +566,11 @@ private boolean parseCloseBracket() {
String title = null;
boolean isLinkOrImage = false;

// If we are parsing a collapsed or shortcut link/image, this
// definition reference will be populated from the cached
// referenceMap.
Reference definition = null;

// Inline link?
if (this.peek() == '(') {
this.pos++;
Expand Down Expand Up @@ -603,6 +609,7 @@ private boolean parseCloseBracket() {
if (ref != null) {
Link link = referenceMap.get(Escaping.normalizeReference(ref));
if (link != null) {
definition = link;
dest = link.getDestination();
title = link.getTitle();
isLinkOrImage = true;
Expand All @@ -613,7 +620,11 @@ private boolean parseCloseBracket() {
if (isLinkOrImage) {
// If we got here, open is a potential opener
boolean isImage = opener.delimiterChar == '!';
Node linkOrImage = isImage ? new Image(dest, title) : new Link(dest, title);
Reference linkOrImage = isImage ? new Image(dest, title) : new Link(dest, title);
if (definition != null) {
linkOrImage.setLabel(definition.getLabel());
linkOrImage.setDefinition(definition);
}

// Flush text now. We don't need to worry about combining it with adjacent text nodes, as we'll wrap it in a
// link or image node.
Expand Down Expand Up @@ -705,13 +716,13 @@ private boolean parseAutolink() {
String m;
if ((m = this.match(EMAIL_AUTOLINK)) != null) {
String dest = m.substring(1, m.length() - 1);
Link node = new Link("mailto:" + dest, null);
Link node = new AutoLink("mailto:" + dest, null);
node.appendChild(new Text(dest));
appendNode(node);
return true;
} else if ((m = this.match(AUTOLINK)) != null) {
String dest = m.substring(1, m.length() - 1);
Link node = new Link(dest, null);
Link node = new AutoLink(dest, null);
node.appendChild(new Text(dest));
appendNode(node);
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.commonmark.internal.inline;

import org.commonmark.parser.DelimiterProcessor;
import org.commonmark.node.Delimited;
import org.commonmark.node.Emphasis;
import org.commonmark.node.Node;
import org.commonmark.node.StrongEmphasis;
Expand All @@ -26,7 +27,9 @@ public int getDelimiterUse(int openerCount, int closerCount) {

@Override
public void process(Text opener, Text closer, int delimiterUse) {
Node emphasis = delimiterUse == 1 ? new Emphasis() : new StrongEmphasis();
Node emphasis = delimiterUse == 1
? new Emphasis(getDelimiterChar(), delimiterUse)
: new StrongEmphasis(getDelimiterChar(), delimiterUse);

Node tmp = opener.getNext();
while (tmp != null && tmp != closer) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.commonmark.internal.util;

import java.util.Stack;
import org.commonmark.node.Node;

public class Debugging {

/**
* Print a tree-representation of the given node and its children.
*/
public static String toStringTree(Node node) {
StringBuilder b = new StringBuilder();
Stack<Node> stack = new Stack();

visit(b, stack, node);

return b.toString();
}

private static void visit(StringBuilder b, Stack<Node> stack, Node node) {

int sz = stack.size();
for (int i = 0; i < sz; i++) {
b.append('.');
}
b.append(node.toString());
b.append('\n');

Node current = node.getFirstChild();
while (current != null) {
stack.push(current);
visit(b, stack, stack.peek());
stack.pop();
current = current.getNext();
}

}

/**
* Log a simple message. This is not a substitute for a logging
* framework.
*/
public static void log(String msg) {
System.out.println("commonmark-java: " + msg);
}

/**
* Print a stacktrace with the given message.
*/
public static void stacktrace(String msg) {
try {
throw new Exception(msg);
} catch (Exception ex) {
ex.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public void visit(Link link) {
visitChildren(link);
}

@Override
public void visit(AutoLink link) {
visitChildren(link);
}

@Override
public void visit(ListItem listItem) {
visitChildren(listItem);
Expand Down
17 changes: 17 additions & 0 deletions commonmark/src/main/java/org/commonmark/node/AutoLink.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.commonmark.node;

public class AutoLink extends Link {

public AutoLink() {
super();
}

public AutoLink(String destination, String title) {
super(destination, title);
}

@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
9 changes: 9 additions & 0 deletions commonmark/src/main/java/org/commonmark/node/Delimited.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.commonmark.node;

public interface Delimited {

char getDelimiterChar();

int getDelimiterCount();

}
20 changes: 19 additions & 1 deletion commonmark/src/main/java/org/commonmark/node/Emphasis.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
package org.commonmark.node;

public class Emphasis extends Node {
public class Emphasis extends Node implements Delimited {

private final char delimiterChar;
private final int delimiterCount;

public Emphasis(char delimiterChar, int delimiterCount) {
this.delimiterChar = delimiterChar;
this.delimiterCount = delimiterCount;
}

@Override
public char getDelimiterChar() {
return delimiterChar;
}

@Override
public int getDelimiterCount() {
return delimiterCount;
}

@Override
public void accept(Visitor visitor) {
Expand Down
29 changes: 2 additions & 27 deletions commonmark/src/main/java/org/commonmark/node/Image.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,16 @@
package org.commonmark.node;

public class Image extends Node {

private String destination;
private String title;
public class Image extends Reference {

public Image() {
}

public Image(String destination, String title) {
this.destination = destination;
this.title = title;
super(destination, title);
}

@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}

public String getDestination() {
return destination;
}

public void setDestination(String destination) {
this.destination = destination;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

@Override
protected String toStringAttributes() {
return "destination=" + destination + ", title=" + title;
}
}
29 changes: 2 additions & 27 deletions commonmark/src/main/java/org/commonmark/node/Link.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,16 @@
package org.commonmark.node;

public class Link extends Node {

private String destination;
private String title;
public class Link extends Reference {

public Link() {
}

public Link(String destination, String title) {
this.destination = destination;
this.title = title;
super(destination, title);
}

@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}

public String getDestination() {
return destination;
}

public void setDestination(String destination) {
this.destination = destination;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

@Override
protected String toStringAttributes() {
return "destination=" + destination + ", title=" + title;
}
}
Loading