Skip to content

Commit 253ee9d

Browse files
committed
Add input as argument to LinkRenderer
This way, the renderer gets all the necessary data from its arguments instead of requiring users to capture the input text.
1 parent 35f82b2 commit 253ee9d

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ LinkExtractor linkExtractor = LinkExtractor.builder()
7676
.linkTypes(EnumSet.of(LinkType.URL)) // limit to URLs
7777
.build();
7878
Iterable<LinkSpan> links = linkExtractor.extractLinks(input);
79-
String result = Autolink.renderLinks(input, links, (link, sb) -> {
79+
String result = Autolink.renderLinks(input, links, (link, text, sb) -> {
8080
sb.append("<a href=\"");
81-
sb.append(input, link.getBeginIndex(), link.getEndIndex());
81+
sb.append(text, link.getBeginIndex(), link.getEndIndex());
8282
sb.append("\">");
83-
sb.append(input, link.getBeginIndex(), link.getEndIndex());
83+
sb.append(text, link.getBeginIndex(), link.getEndIndex());
8484
sb.append("</a>");
8585
});
8686
result; // "wow <a href=\"http://test.com\">http://test.com</a> such linked"

src/main/java/org/nibor/autolink/Autolink.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ public class Autolink {
1111
*
1212
* @param input the input text
1313
* @param links the links to render, see {@link LinkExtractor} to extract them
14-
* @param linkRenderer the link rendering function
14+
* @param linkRenderer the link rendering implementation
1515
* @return the rendered string
1616
*/
1717
public static String renderLinks(CharSequence input, Iterable<LinkSpan> links, LinkRenderer linkRenderer) {
1818
StringBuilder sb = new StringBuilder(input.length() + 16);
1919
int lastIndex = 0;
2020
for (LinkSpan link : links) {
2121
sb.append(input, lastIndex, link.getBeginIndex());
22-
linkRenderer.render(link, sb);
22+
linkRenderer.render(link, input, sb);
2323
lastIndex = link.getEndIndex();
2424
}
2525
if (lastIndex < input.length()) {
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
package org.nibor.autolink;
22

3+
/**
4+
* Renderer for a link.
5+
*/
36
public interface LinkRenderer {
47

5-
void render(LinkSpan link, StringBuilder sb);
8+
/**
9+
* Render the supplied link of the input text to the supplied output.
10+
*
11+
* @param link the link span of the link to render
12+
* @param input the input text where the link occurs
13+
* @param output the output to write the link to
14+
*/
15+
void render(LinkSpan link, CharSequence input, StringBuilder output);
616

717
}

src/test/java/org/nibor/autolink/AutolinkTestCase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ protected void assertNotLinked(String input) {
1616

1717
protected abstract LinkExtractor getLinkExtractor();
1818

19-
protected String link(final String input, final String marker, final LinkType expectedLinkType) {
19+
protected String link(String input, final String marker, final LinkType expectedLinkType) {
2020
Iterable<LinkSpan> links = getLinkExtractor().extractLinks(input);
2121
return Autolink.renderLinks(input, links, new LinkRenderer() {
2222
@Override
23-
public void render(LinkSpan link, StringBuilder sb) {
23+
public void render(LinkSpan link, CharSequence text, StringBuilder sb) {
2424
if (expectedLinkType != null) {
2525
assertEquals(expectedLinkType, link.getType());
2626
}
2727
sb.append(marker);
28-
sb.append(input, link.getBeginIndex(), link.getEndIndex());
28+
sb.append(text, link.getBeginIndex(), link.getEndIndex());
2929
sb.append(marker);
3030
}
3131
});

0 commit comments

Comments
 (0)