Fixed anchor normalization
This commit is contained in:
@@ -29,7 +29,6 @@ import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.apache.commons.lang3.StringUtils.*;
|
||||
|
||||
@@ -39,7 +38,6 @@ import static org.apache.commons.lang3.StringUtils.*;
|
||||
public class AsciiDocBuilder extends AbstractMarkupDocBuilder {
|
||||
|
||||
private static final String ASCIIDOC_FILE_EXTENSION = "adoc";
|
||||
private static final Pattern ANCHOR_ESCAPE_PATTERN = Pattern.compile("[^0-9a-zA-Z]+");
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder documentTitle(String title){
|
||||
@@ -127,7 +125,7 @@ public class AsciiDocBuilder extends AbstractMarkupDocBuilder {
|
||||
}
|
||||
|
||||
private static String normalizeReferenceAnchor(String anchor) {
|
||||
return ANCHOR_ESCAPE_PATTERN.matcher(anchor).replaceAll("_");
|
||||
return anchor.trim();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -41,7 +41,8 @@ import static org.apache.commons.lang3.StringUtils.join;
|
||||
public class MarkdownBuilder extends AbstractMarkupDocBuilder
|
||||
{
|
||||
private static final String MARKDOWN_FILE_EXTENSION = "md";
|
||||
private static final Pattern ANCHOR_ESCAPE_PATTERN = Pattern.compile("[^0-9a-zA-Z]+");
|
||||
private static final Pattern ANCHOR_FORBIDDEN_PATTERN = Pattern.compile("[^0-9a-zA-Z-_\\s]+");
|
||||
private static final Pattern ANCHOR_SPACE_PATTERN = Pattern.compile("[\\s]+");
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder documentTitle(String title){
|
||||
@@ -151,7 +152,7 @@ public class MarkdownBuilder extends AbstractMarkupDocBuilder
|
||||
}
|
||||
|
||||
private static String normalizeReferenceAnchor(String anchor) {
|
||||
return ANCHOR_ESCAPE_PATTERN.matcher(anchor).replaceAll("-");
|
||||
return ANCHOR_SPACE_PATTERN.matcher(ANCHOR_FORBIDDEN_PATTERN.matcher(anchor.trim().toLowerCase()).replaceAll("")).replaceAll("-");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -177,7 +178,7 @@ public class MarkdownBuilder extends AbstractMarkupDocBuilder
|
||||
public String crossReferenceAsString(String anchor, String text) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
if (text == null)
|
||||
text = anchor;
|
||||
text = anchor.trim();
|
||||
stringBuilder.append("[").append(text).append("](#").append(normalizeReferenceAnchor(anchor)).append(")");
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
@@ -75,10 +75,10 @@ public class MarkupDocBuilderTest {
|
||||
.italicTextLine("Italic text line b")
|
||||
.unorderedList(Arrays.asList("Entry1", "Entry2", "Entry 2"))
|
||||
.anchor("anchor").newLine()
|
||||
.anchor("\u0240 & this | there").newLine()
|
||||
.anchor(" \u0240 & This | There ").newLine()
|
||||
.crossReference("anchor").newLine()
|
||||
.crossReference("anchor", "text").newLine()
|
||||
.crossReference("\u0240 & this | there").newLine()
|
||||
.crossReference(" \u0240 & This | There ").newLine()
|
||||
.writeToFile("build/tmp", "test", StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@@ -103,10 +103,10 @@ public class MarkupDocBuilderTest {
|
||||
.italicTextLine("Italic text line b")
|
||||
.unorderedList(Arrays.asList("Entry1", "Entry2", "Entry 2"))
|
||||
.anchor("anchor").newLine()
|
||||
.anchor("\u0240 & this | there").newLine()
|
||||
.anchor(" \u0240 & This | There ").newLine()
|
||||
.crossReference("anchor").newLine()
|
||||
.crossReference("anchor", "text").newLine()
|
||||
.crossReference("\u0240 & this | there").newLine()
|
||||
.crossReference(" \u0240 & This | There ").newLine()
|
||||
.writeToFile("build/tmp", "test", StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user