Merge pull request #5 from Kabhal/master

Correct anchor normalization & cleanups
This commit is contained in:
Robert Winkler
2016-02-03 19:47:55 +01:00
7 changed files with 67 additions and 48 deletions

2
gradlew vendored
View File

@@ -12,7 +12,7 @@ DEFAULT_JVM_OPTS=""
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Use the maximum available, or set MAX_FD != -1 to use that header.
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn ( ) {

View File

@@ -130,13 +130,14 @@ public abstract class AbstractMarkupDocBuilder implements MarkupDocBuilder {
return documentBuilder.toString();
}
protected void writeToFileWithExtension(String directory, String fileNameWithExtension, Charset charset) throws IOException {
@Override
public void writeToFileWithoutExtension(String directory, String fileName, Charset charset) throws IOException {
Files.createDirectories(Paths.get(directory));
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(directory, fileNameWithExtension), charset)){
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(directory, fileName), charset)){
writer.write(documentBuilder.toString());
}
if (logger.isInfoEnabled()) {
logger.info("{} was written to: {}", fileNameWithExtension, directory);
logger.info("{} was written to: {}", fileName, directory);
}
documentBuilder = new StringBuilder();
}

View File

@@ -58,7 +58,7 @@ public interface MarkupDocBuilder {
MarkupDocBuilder table(List<List<String>> cells);
MarkupDocBuilder tableWithColumnSpecs(List<TableColumnSpec> headers, List<List<String>> cells);
MarkupDocBuilder tableWithColumnSpecs(List<MarkupTableColumn> columnSpecs, List<List<String>> cells);
MarkupDocBuilder anchor(String anchor);
@@ -94,21 +94,14 @@ public interface MarkupDocBuilder {
*/
void writeToFile(String directory, String fileName, Charset charset) throws IOException;
class TableColumnSpec {
public String header;
public Integer widthRatio = 0;
public TableColumnSpec() {}
public TableColumnSpec(String header, Integer widthRatio) {
this.header = header;
this.widthRatio = widthRatio;
}
public TableColumnSpec withHeader(String header) {
this.header = header;
return this;
}
public TableColumnSpec withWidthRatio(Integer widthRatio) {
this.widthRatio = widthRatio;
return this;
}
}
/**
* Writes the content of the builder to a file and clears the builder.
*
* @param directory the directory where the generated file should be stored
* @param fileName the name of the file
* @param charset the the charset to use for encoding
* @throws java.io.IOException if the file cannot be written
*/
void writeToFileWithoutExtension(String directory, String fileName, Charset charset) throws IOException;
}

View File

@@ -0,0 +1,24 @@
package io.github.robwin.markup.builder;
public class MarkupTableColumn {
public String header;
public Integer widthRatio = 0;
public MarkupTableColumn() {
}
public MarkupTableColumn(String header, Integer widthRatio) {
this.header = header;
this.widthRatio = widthRatio;
}
public MarkupTableColumn withHeader(String header) {
this.header = header;
return this;
}
public MarkupTableColumn withWidthRatio(Integer widthRatio) {
this.widthRatio = widthRatio;
return this;
}
}

View File

@@ -22,6 +22,7 @@ import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import io.github.robwin.markup.builder.AbstractMarkupDocBuilder;
import io.github.robwin.markup.builder.MarkupDocBuilder;
import io.github.robwin.markup.builder.MarkupTableColumn;
import org.apache.commons.collections.CollectionUtils;
import java.io.IOException;
@@ -29,7 +30,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 +39,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 +126,7 @@ public class AsciiDocBuilder extends AbstractMarkupDocBuilder {
}
private static String normalizeReferenceAnchor(String anchor) {
return ANCHOR_ESCAPE_PATTERN.matcher(anchor).replaceAll("_");
return anchor.trim();
}
@Override
@@ -164,13 +163,13 @@ public class AsciiDocBuilder extends AbstractMarkupDocBuilder {
}
@Override
public MarkupDocBuilder tableWithColumnSpecs(List<TableColumnSpec> columns, List<List<String>> cells) {
public MarkupDocBuilder tableWithColumnSpecs(List<MarkupTableColumn> columnSpecs, List<List<String>> cells) {
Boolean hasHeader = false;
List<String> options = new ArrayList<>();
List<String> cols = new ArrayList<>();
if (CollectionUtils.isNotEmpty(columns)) {
for (TableColumnSpec col : columns) {
if (CollectionUtils.isNotEmpty(columnSpecs)) {
for (MarkupTableColumn col : columnSpecs) {
if (!hasHeader && isNotBlank(col.header)) {
options.add("header");
hasHeader = true;
@@ -183,8 +182,8 @@ public class AsciiDocBuilder extends AbstractMarkupDocBuilder {
documentBuilder.append("[options=\"" + join(options, ",") + "\", cols=\"" + join(cols, ",") + "\"]").append(newLine);
documentBuilder.append(AsciiDoc.TABLE).append(newLine);
if (hasHeader) {
Collection<String> headerList = Collections2.transform(columns, new Function<TableColumnSpec, String>() {
public String apply(final TableColumnSpec header) {
Collection<String> headerList = Collections2.transform(columnSpecs, new Function<MarkupTableColumn, String>() {
public String apply(final MarkupTableColumn header) {
return escapeTableCell(defaultString(header.header));
}
});
@@ -206,6 +205,6 @@ public class AsciiDocBuilder extends AbstractMarkupDocBuilder {
@Override
public void writeToFile(String directory, String fileName, Charset charset) throws IOException {
writeToFileWithExtension(directory, fileName + "." + ASCIIDOC_FILE_EXTENSION, charset);
writeToFileWithoutExtension(directory, fileName + "." + ASCIIDOC_FILE_EXTENSION, charset);
}
}

View File

@@ -22,6 +22,7 @@ import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import io.github.robwin.markup.builder.AbstractMarkupDocBuilder;
import io.github.robwin.markup.builder.MarkupDocBuilder;
import io.github.robwin.markup.builder.MarkupTableColumn;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@@ -41,7 +42,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 +153,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 +179,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();
}
@@ -187,20 +189,20 @@ public class MarkdownBuilder extends AbstractMarkupDocBuilder
}
@Override
public MarkupDocBuilder tableWithColumnSpecs(List<TableColumnSpec> columns, List<List<String>> cells) {
if (CollectionUtils.isEmpty(columns))
public MarkupDocBuilder tableWithColumnSpecs(List<MarkupTableColumn> columnSpecs, List<List<String>> cells) {
if (CollectionUtils.isEmpty(columnSpecs))
throw new RuntimeException("Header is mandatory in Markdown");
newLine();
Collection<String> headerList = Collections2.transform(columns, new Function<TableColumnSpec, String>() {
public String apply(final TableColumnSpec header) {
Collection<String> headerList = Collections2.transform(columnSpecs, new Function<MarkupTableColumn, String>() {
public String apply(final MarkupTableColumn header) {
return escapeTableCell(defaultString(header.header));
}
});
documentBuilder.append(Markdown.TABLE_COLUMN_DELIMITER).append(join(headerList, Markdown.TABLE_COLUMN_DELIMITER.toString())).append(Markdown.TABLE_COLUMN_DELIMITER).append(newLine);
documentBuilder.append(Markdown.TABLE_COLUMN_DELIMITER);
for (TableColumnSpec col : columns) {
for (MarkupTableColumn col : columnSpecs) {
documentBuilder.append(StringUtils.repeat(Markdown.TABLE_ROW.toString(), 3));
documentBuilder.append(Markdown.TABLE_COLUMN_DELIMITER);
}
@@ -221,6 +223,6 @@ public class MarkdownBuilder extends AbstractMarkupDocBuilder
@Override
public void writeToFile(String directory, String fileName, Charset charset) throws IOException {
writeToFileWithExtension(directory, fileName + "." + MARKDOWN_FILE_EXTENSION, charset);
writeToFileWithoutExtension(directory, fileName + "." + MARKDOWN_FILE_EXTENSION, charset);
}
}

View File

@@ -33,7 +33,7 @@ import java.util.List;
public class MarkupDocBuilderTest {
List<String> tableRowsInPSV;
List<MarkupDocBuilder.TableColumnSpec> tableColumns;
List<MarkupTableColumn> tableColumns;
List<List<String>> tableCells;
@@ -45,9 +45,9 @@ public class MarkupDocBuilderTest {
tableRowsInPSV.add("Row 2, Column 1 | Row 2, Column 2 | Row 2, Column 3");
tableColumns = Arrays.asList(
new MarkupDocBuilder.TableColumnSpec().withHeader("Header1"),
new MarkupDocBuilder.TableColumnSpec().withWidthRatio(2),
new MarkupDocBuilder.TableColumnSpec().withHeader("Header3").withWidthRatio(1));
new MarkupTableColumn().withHeader("Header1"),
new MarkupTableColumn().withWidthRatio(2),
new MarkupTableColumn().withHeader("Header3").withWidthRatio(1));
tableCells = new ArrayList<>();
tableCells.add(Arrays.asList("Row 1 | Column 1", "Row 1 | Column 2", "Row 1 | Column 3"));
tableCells.add(Arrays.asList("Row 2 | Column 1", "Row 2 | Column 2", "Row 2 | Column 3"));
@@ -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);
}