Introduced new commands : anchor(anchor), crossReference(anchor, text)

Refactored writeToFile for robustness
Cleanups and fixes
This commit is contained in:
Hugo de Paix de Coeur
2016-02-03 15:20:30 +01:00
parent 6293b6a996
commit 48c48ba56b
6 changed files with 81 additions and 23 deletions

View File

@@ -38,27 +38,27 @@ public abstract class AbstractMarkupDocBuilder implements MarkupDocBuilder {
protected Logger logger = LoggerFactory.getLogger(getClass());
protected void documentTitle(Markup markup, String title){
documentBuilder.append(markup).append(title).append(newLine).append(newLine);
documentBuilder.append(newLine).append(markup).append(title).append(newLine).append(newLine);
}
protected void documentTitleWithAttributes(Markup markup, String title){
documentBuilder.append(markup).append(title).append(newLine);
documentBuilder.append(newLine).append(markup).append(title).append(newLine);
}
protected void sectionTitleLevel1(Markup markup, String title){
documentBuilder.append(markup).append(title).append(newLine);
documentBuilder.append(newLine).append(markup).append(title).append(newLine);
}
protected void sectionTitleLevel2(Markup markup, String title){
documentBuilder.append(markup).append(title).append(newLine);
documentBuilder.append(newLine).append(markup).append(title).append(newLine);
}
protected void sectionTitleLevel3(Markup markup, String title){
documentBuilder.append(markup).append(title).append(newLine);
documentBuilder.append(newLine).append(markup).append(title).append(newLine);
}
protected void sectionTitleLevel4(Markup markup, String title){
documentBuilder.append(markup).append(title).append(newLine);
documentBuilder.append(newLine).append(markup).append(title).append(newLine);
}
@Override
@@ -103,6 +103,12 @@ public abstract class AbstractMarkupDocBuilder implements MarkupDocBuilder {
documentBuilder.append(newLine);
}
@Override
public MarkupDocBuilder crossReference(String anchor) {
crossReference(anchor, null);
return this;
}
@Override
public MarkupDocBuilder newLine(){
documentBuilder.append(newLine);
@@ -114,8 +120,7 @@ public abstract class AbstractMarkupDocBuilder implements MarkupDocBuilder {
return documentBuilder.toString();
}
@Override
public void writeToFile(String directory, String fileNameWithExtension, Charset charset) throws IOException {
protected void writeToFileWithExtension(String directory, String fileNameWithExtension, Charset charset) throws IOException {
Files.createDirectories(Paths.get(directory));
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(directory, fileNameWithExtension), charset)){
writer.write(documentBuilder.toString());

View File

@@ -26,6 +26,7 @@ import java.util.List;
* @author Robert Winkler
*/
public interface MarkupDocBuilder {
MarkupDocBuilder documentTitle(String title);
MarkupDocBuilder documentTitleWithAttributes(String title);
@@ -52,9 +53,18 @@ public interface MarkupDocBuilder {
MarkupDocBuilder unorderedList(List<String> list);
@Deprecated
MarkupDocBuilder tableWithHeaderRow(List<String> rowsInPSV);
MarkupDocBuilder crossReference(String text);
MarkupDocBuilder anchor(String anchor);
/**
* @param anchor Target anchor
* @param text If not null, display this text instead of anchor
*/
MarkupDocBuilder crossReference(String anchor, String text);
MarkupDocBuilder crossReference(String anchor);
MarkupDocBuilder newLine();
@@ -65,11 +75,13 @@ public interface MarkupDocBuilder {
/**
* Writes the content of the builder to a file and clears the builder.
* An extension will be dynamically added to fileName depending on the markup language.
*
* @param directory the directory where the generated file should be stored
* @param fileName the name of the file
* @param fileName the base name of the file without extension
* @param charset the the charset to use for encoding
* @throws java.io.IOException if the file cannot be written
*/
void writeToFile(String directory, String fileName, Charset charset) throws IOException;
}

View File

@@ -39,7 +39,9 @@ public enum AsciiDoc implements Markup {
ITALIC("_"),
LIST_ENTRY("* "),
CROSS_REFERENCE_START("<<"),
CROSS_REFERENCE_END(">>");
CROSS_REFERENCE_END(">>"),
ANCHOR_START("[["),
ANCHOR_END("]]");
private final String markup;

View File

@@ -24,12 +24,16 @@ import io.github.robwin.markup.builder.MarkupDocBuilder;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.regex.Pattern;
/**
* @author Robert Winkler
*/
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){
documentTitle(AsciiDoc.DOCUMENT_TITLE, title);
@@ -62,7 +66,7 @@ public class AsciiDocBuilder extends AbstractMarkupDocBuilder {
@Override
public MarkupDocBuilder sectionTitleLevel4(String title){
sectionTitleLevel3(AsciiDoc.SECTION_TITLE_LEVEL4, title);
sectionTitleLevel4(AsciiDoc.SECTION_TITLE_LEVEL4, title);
return this;
}
@@ -105,6 +109,7 @@ public class AsciiDocBuilder extends AbstractMarkupDocBuilder {
@Override
public MarkupDocBuilder tableWithHeaderRow(List<String> rowsInPSV){
newLine();
documentBuilder.append("[options=\"header\"]").append(newLine);
documentBuilder.append(AsciiDoc.TABLE).append(newLine);
for(String row : rowsInPSV){
@@ -114,16 +119,27 @@ public class AsciiDocBuilder extends AbstractMarkupDocBuilder {
return this;
}
private static String normalizeReferenceAnchor(String anchor) {
return ANCHOR_ESCAPE_PATTERN.matcher(anchor).replaceAll("_");
}
@Override
public MarkupDocBuilder crossReference(String text) {
documentBuilder.append(AsciiDoc.CROSS_REFERENCE_START).append(text).append(AsciiDoc.CROSS_REFERENCE_END);
public MarkupDocBuilder anchor(String anchor) {
documentBuilder.append(AsciiDoc.ANCHOR_START).append(normalizeReferenceAnchor(anchor)).append(AsciiDoc.ANCHOR_END);
return this;
}
@Override
public MarkupDocBuilder crossReference(String anchor, String text) {
if (text == null)
documentBuilder.append(AsciiDoc.CROSS_REFERENCE_START).append(normalizeReferenceAnchor(anchor)).append(AsciiDoc.CROSS_REFERENCE_END);
else
documentBuilder.append(AsciiDoc.CROSS_REFERENCE_START).append(normalizeReferenceAnchor(anchor)).append(",").append(text).append(AsciiDoc.CROSS_REFERENCE_END);
return this;
}
@Override
public void writeToFile(String directory, String fileName, Charset charset) throws IOException {
String fileNameWithExtension = fileName + ".adoc";
super.writeToFile(directory, fileNameWithExtension, charset);
writeToFileWithExtension(directory, fileName + "." + ASCIIDOC_FILE_EXTENSION, charset);
}
}

View File

@@ -31,6 +31,8 @@ import java.util.List;
*/
public class MarkdownBuilder extends AbstractMarkupDocBuilder
{
private static final String MARKDOWN_FILE_EXTENSION = "md";
@Override
public MarkupDocBuilder documentTitle(String title){
documentTitle(Markdown.DOCUMENT_TITLE, title);
@@ -63,7 +65,7 @@ public class MarkdownBuilder extends AbstractMarkupDocBuilder
@Override
public MarkupDocBuilder sectionTitleLevel4(String title){
sectionTitleLevel3(Markdown.SECTION_TITLE_LEVEL4, title);
sectionTitleLevel4(Markdown.SECTION_TITLE_LEVEL4, title);
return this;
}
@@ -111,6 +113,8 @@ public class MarkdownBuilder extends AbstractMarkupDocBuilder
List<String> contentRowsInPSV = rowsInPSV.subList(1, rowsInPSV.size());
String[] headersAsArray = headersInPSV.split(String.format("\\%s", Markdown.TABLE_COLUMN_DELIMITER.toString()));
List<String> headers = Arrays.asList(headersAsArray);
newLine();
// Header
documentBuilder.append(Markdown.TABLE_COLUMN_DELIMITER.toString());
documentBuilder.append(headersInPSV);
@@ -136,15 +140,24 @@ public class MarkdownBuilder extends AbstractMarkupDocBuilder
return this;
}
@Override
// TODO
public MarkupDocBuilder crossReference(String text) {
throw new UnsupportedOperationException("Not yet supported");
@Override
public MarkupDocBuilder anchor(String anchor) {
return this;
}
// TODO
@Override
public MarkupDocBuilder crossReference(String anchor, String text) {
if (text == null)
documentBuilder.append(anchor);
else
documentBuilder.append(text);
return this;
}
@Override
public void writeToFile(String directory, String fileName, Charset charset) throws IOException {
String fileNameWithExtension = fileName + ".md";
super.writeToFile(directory, fileNameWithExtension, charset);
writeToFileWithExtension(directory, fileName + "." + MARKDOWN_FILE_EXTENSION, charset);
}
}