Initial commit
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package io.github.robwin.markup.builder;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Robert Winkler
|
||||
*/
|
||||
public abstract class AbstractMarkupDocBuilder implements MarkupDocBuilder {
|
||||
|
||||
protected StringBuilder documentBuilder = new StringBuilder();
|
||||
protected String newLine = System.getProperty("line.separator");
|
||||
protected Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
protected void documentTitle(Markup markup, String title){
|
||||
documentBuilder.append(markup).append(title).append(newLine).append(newLine);
|
||||
}
|
||||
|
||||
protected void documentTitleWithAttributes(Markup markup, String title){
|
||||
documentBuilder.append(markup).append(title).append(newLine);
|
||||
}
|
||||
|
||||
protected void sectionTitleLevel1(Markup markup, String title){
|
||||
documentBuilder.append(markup).append(title).append(newLine);
|
||||
}
|
||||
|
||||
protected void sectionTitleLevel2(Markup markup, String title){
|
||||
documentBuilder.append(markup).append(title).append(newLine);
|
||||
}
|
||||
|
||||
protected void sectionTitleLevel3(Markup markup, String title){
|
||||
documentBuilder.append(markup).append(title).append(newLine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder textLine(String text){
|
||||
documentBuilder.append(text).append(newLine);
|
||||
return this;
|
||||
}
|
||||
|
||||
protected void paragraph(Markup markup, String text){
|
||||
documentBuilder.append(markup).append(newLine).append(text).append(newLine).append(newLine);
|
||||
}
|
||||
|
||||
protected void listing(Markup markup, String text){
|
||||
delimitedTextLine(markup, text);
|
||||
}
|
||||
|
||||
protected void delimitedTextLine(Markup markup, String text){
|
||||
documentBuilder.append(markup).append(newLine).append(text).append(newLine).append(markup).append(newLine).append(newLine);
|
||||
}
|
||||
|
||||
protected void delimitedTextLineWithoutLineBreaks(Markup markup, String text){
|
||||
documentBuilder.append(markup).append(text).append(markup).append(newLine);
|
||||
}
|
||||
|
||||
protected void preserveLineBreaks(Markup markup){
|
||||
documentBuilder.append(markup).append(newLine);
|
||||
}
|
||||
|
||||
protected void boldTextLine(Markup markup, String text){
|
||||
delimitedTextLineWithoutLineBreaks(markup, text);
|
||||
}
|
||||
|
||||
protected void italicTextLine(Markup markup, String text){
|
||||
delimitedTextLineWithoutLineBreaks(markup, text);
|
||||
}
|
||||
|
||||
protected void unorderedList(Markup markup, List<String> list){
|
||||
documentBuilder.append(newLine);
|
||||
for(String listEntry : list){
|
||||
documentBuilder.append(markup).append(listEntry).append(newLine);
|
||||
}
|
||||
documentBuilder.append(newLine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder newLine(){
|
||||
documentBuilder.append(newLine);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return documentBuilder.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToFile(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());
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("{} was written to: {}", fileNameWithExtension, directory);
|
||||
}
|
||||
documentBuilder = new StringBuilder();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package io.github.robwin.markup.builder;
|
||||
|
||||
/**
|
||||
* @author Robert Winkler
|
||||
*/
|
||||
public interface Markup {
|
||||
public String toString();
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package io.github.robwin.markup.builder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Robert Winkler
|
||||
*/
|
||||
public interface MarkupDocBuilder {
|
||||
MarkupDocBuilder documentTitle(String title);
|
||||
|
||||
MarkupDocBuilder documentTitleWithAttributes(String title);
|
||||
|
||||
MarkupDocBuilder sectionTitleLevel1(String title);
|
||||
|
||||
MarkupDocBuilder sectionTitleLevel2(String title);
|
||||
|
||||
MarkupDocBuilder sectionTitleLevel3(String title);
|
||||
|
||||
MarkupDocBuilder textLine(String text);
|
||||
|
||||
MarkupDocBuilder paragraph(String text);
|
||||
|
||||
MarkupDocBuilder listing(String text);
|
||||
|
||||
MarkupDocBuilder source(String text, String language);
|
||||
|
||||
MarkupDocBuilder boldTextLine(String text);
|
||||
|
||||
MarkupDocBuilder italicTextLine(String text);
|
||||
|
||||
MarkupDocBuilder unorderedList(List<String> list);
|
||||
|
||||
MarkupDocBuilder tableWithHeaderRow(List<String> rowsInCSV);
|
||||
|
||||
MarkupDocBuilder newLine();
|
||||
|
||||
/**
|
||||
* Returns a string representation of the document.
|
||||
*/
|
||||
String toString();
|
||||
|
||||
/**
|
||||
* 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 writeToFile(String directory, String fileName, Charset charset) throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.github.robwin.markup.builder;
|
||||
|
||||
|
||||
import io.github.robwin.markup.builder.asciidoc.AsciiDocBuilder;
|
||||
import io.github.robwin.markup.builder.markdown.MarkdownBuilder;
|
||||
|
||||
/**
|
||||
* @author Robert Winkler
|
||||
*/
|
||||
public final class MarkupDocBuilders {
|
||||
|
||||
private MarkupDocBuilders(){};
|
||||
|
||||
public static MarkupDocBuilder documentBuilder(MarkupLanguage markupLanguage){
|
||||
switch(markupLanguage){
|
||||
case MARKDOWN: return new MarkdownBuilder();
|
||||
case ASCIIDOC: return new AsciiDocBuilder();
|
||||
default: return new AsciiDocBuilder();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package io.github.robwin.markup.builder;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Robert Winkler
|
||||
*/
|
||||
public enum MarkupLanguage {
|
||||
ASCIIDOC(".adoc,.asciidoc"),
|
||||
MARKDOWN(".md,.markdown");
|
||||
|
||||
private final String fileNameExtensions;
|
||||
|
||||
/**
|
||||
* @param fileNameExtensions file name suffix
|
||||
*/
|
||||
private MarkupLanguage(final String fileNameExtensions) {
|
||||
this.fileNameExtensions = fileNameExtensions;
|
||||
}
|
||||
|
||||
public List<String> getFileNameExtensions() {
|
||||
return Arrays.asList(fileNameExtensions.split(","));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package io.github.robwin.markup.builder.asciidoc;
|
||||
|
||||
|
||||
import io.github.robwin.markup.builder.Markup;
|
||||
|
||||
/**
|
||||
* @author Robert Winkler
|
||||
*/
|
||||
public enum AsciiDoc implements Markup {
|
||||
LABELED(":: "),
|
||||
TABLE("|==="),
|
||||
LISTING("----"),
|
||||
HARDBREAKS(":hardbreaks:"),
|
||||
DOCUMENT_TITLE("= "),
|
||||
SECTION_TITLE_LEVEL1("== "),
|
||||
SECTION_TITLE_LEVEL2("=== "),
|
||||
SECTION_TITLE_LEVEL3("==== "),
|
||||
BOLD("*"),
|
||||
ITALIC("_"),
|
||||
LIST_ENTRY("* ");
|
||||
|
||||
private final String markup;
|
||||
|
||||
/**
|
||||
* @param markup AsciiDoc markup
|
||||
*/
|
||||
private AsciiDoc(final String markup) {
|
||||
this.markup = markup;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Enum#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return markup;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package io.github.robwin.markup.builder.asciidoc;
|
||||
|
||||
import io.github.robwin.markup.builder.AbstractMarkupDocBuilder;
|
||||
import io.github.robwin.markup.builder.MarkupDocBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Robert Winkler
|
||||
*/
|
||||
public class AsciiDocBuilder extends AbstractMarkupDocBuilder {
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder documentTitle(String title){
|
||||
documentTitle(AsciiDoc.DOCUMENT_TITLE, title);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder documentTitleWithAttributes(String title) {
|
||||
documentTitle(AsciiDoc.DOCUMENT_TITLE, title);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder sectionTitleLevel1(String title){
|
||||
sectionTitleLevel1(AsciiDoc.SECTION_TITLE_LEVEL1, title);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder sectionTitleLevel2(String title){
|
||||
sectionTitleLevel2(AsciiDoc.SECTION_TITLE_LEVEL2, title);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder sectionTitleLevel3(String title){
|
||||
sectionTitleLevel3(AsciiDoc.SECTION_TITLE_LEVEL3, title);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder paragraph(String text){
|
||||
paragraph(AsciiDoc.HARDBREAKS, text);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder listing(String text){
|
||||
listing(AsciiDoc.LISTING, text);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder boldTextLine(String text){
|
||||
boldTextLine(AsciiDoc.BOLD, text);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder italicTextLine(String text) {
|
||||
italicTextLine(AsciiDoc.ITALIC, text);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder unorderedList(List<String> list){
|
||||
unorderedList(AsciiDoc.LIST_ENTRY, list);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder source(String text, String language){
|
||||
documentBuilder.append(String.format("[source,%s]", language)).append(newLine);
|
||||
listing(AsciiDoc.LISTING, text);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder tableWithHeaderRow(List<String> rowsInCSV){
|
||||
documentBuilder.append("[format=\"csv\", options=\"header\"]").append(newLine);
|
||||
documentBuilder.append(AsciiDoc.TABLE).append(newLine);
|
||||
for(String row : rowsInCSV){
|
||||
documentBuilder.append(row).append(newLine);
|
||||
}
|
||||
documentBuilder.append(AsciiDoc.TABLE).append(newLine).append(newLine);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToFile(String directory, String fileName, Charset charset) throws IOException {
|
||||
String fileNameWithExtension = fileName + ".adoc";
|
||||
super.writeToFile(directory, fileNameWithExtension, charset);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package io.github.robwin.markup.builder.markdown;
|
||||
|
||||
import io.github.robwin.markup.builder.Markup;
|
||||
|
||||
/**
|
||||
* @author Robert Winkler
|
||||
*/
|
||||
public enum Markdown implements Markup {
|
||||
HARDBREAKS(""),
|
||||
TABLE_COLUMN("|"),
|
||||
TABLE_ROW("-"),
|
||||
LISTING("```"),
|
||||
DOCUMENT_TITLE("# "),
|
||||
SECTION_TITLE_LEVEL1("## "),
|
||||
SECTION_TITLE_LEVEL2("### "),
|
||||
SECTION_TITLE_LEVEL3("### "),
|
||||
BOLD("**"),
|
||||
ITALIC("*"),
|
||||
LIST_ENTRY("* ");
|
||||
|
||||
private final String markup;
|
||||
|
||||
/**
|
||||
* @param markup AsciiDoc markup
|
||||
*/
|
||||
private Markdown(final String markup) {
|
||||
this.markup = markup;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Enum#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return markup;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package io.github.robwin.markup.builder.markdown;
|
||||
|
||||
import io.github.robwin.markup.builder.AbstractMarkupDocBuilder;
|
||||
import io.github.robwin.markup.builder.MarkupDocBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Robert Winkler
|
||||
*/
|
||||
public class MarkdownBuilder extends AbstractMarkupDocBuilder
|
||||
{
|
||||
@Override
|
||||
public MarkupDocBuilder documentTitle(String title){
|
||||
documentTitle(Markdown.DOCUMENT_TITLE, title);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder documentTitleWithAttributes(String title) {
|
||||
documentTitle(Markdown.DOCUMENT_TITLE, title);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder sectionTitleLevel1(String title){
|
||||
sectionTitleLevel1(Markdown.SECTION_TITLE_LEVEL1, title);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder sectionTitleLevel2(String title){
|
||||
sectionTitleLevel2(Markdown.SECTION_TITLE_LEVEL2, title);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder sectionTitleLevel3(String title){
|
||||
sectionTitleLevel3(Markdown.SECTION_TITLE_LEVEL3, title);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder paragraph(String text){
|
||||
paragraph(Markdown.HARDBREAKS, text);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder listing(String text){
|
||||
listing(Markdown.LISTING, text);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder source(String text, String language){
|
||||
documentBuilder.append(Markdown.LISTING).append(language).append(newLine).
|
||||
append(text).append(newLine).
|
||||
append(Markdown.LISTING).append(newLine).append(newLine);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder boldTextLine(String text){
|
||||
boldTextLine(Markdown.BOLD, text);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder italicTextLine(String text) {
|
||||
italicTextLine(Markdown.ITALIC, text);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder unorderedList(List<String> list){
|
||||
unorderedList(Markdown.LIST_ENTRY, list);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MarkupDocBuilder tableWithHeaderRow(List<String> rowsInCSV){
|
||||
String headersInCSV = rowsInCSV.get(0);
|
||||
List<String> contentRowsInCSV = rowsInCSV.subList(1, rowsInCSV.size());
|
||||
List<String> headers = Arrays.asList(headersInCSV.split(","));
|
||||
// Header
|
||||
documentBuilder.append(Markdown.TABLE_COLUMN);
|
||||
for(String header : headers){
|
||||
documentBuilder.append(header).append(Markdown.TABLE_COLUMN);
|
||||
}
|
||||
newLine();
|
||||
// Header/Content separator
|
||||
documentBuilder.append(Markdown.TABLE_COLUMN);
|
||||
for(String header : headers){
|
||||
for(int i = 1; i<5; i++) {
|
||||
documentBuilder.append(Markdown.TABLE_ROW);
|
||||
}
|
||||
documentBuilder.append(Markdown.TABLE_COLUMN);
|
||||
}
|
||||
newLine();
|
||||
// Content
|
||||
for(String contentRow : contentRowsInCSV){
|
||||
documentBuilder.append(Markdown.TABLE_COLUMN);
|
||||
List<String> columns = Arrays.asList(contentRow.split(","));
|
||||
for(String columnText : columns){
|
||||
documentBuilder.append(columnText).append(Markdown.TABLE_COLUMN);
|
||||
}
|
||||
newLine();
|
||||
}
|
||||
newLine().newLine();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToFile(String directory, String fileName, Charset charset) throws IOException {
|
||||
String fileNameWithExtension = fileName + ".md";
|
||||
super.writeToFile(directory, fileNameWithExtension, charset);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user