-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Opens MarkupDocBuilders for additional/external markup builders (incl…
…udes an example for Confluence wiki markup)
- Loading branch information
Showing
4 changed files
with
333 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
src/test/java/io/github/robwin/markup/builder/confluence/ConfluenceMarkupBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
package io.github.robwin.markup.builder.confluence; | ||
|
||
import io.github.robwin.markup.builder.*; | ||
import jdk.nashorn.internal.ir.annotations.Ignore; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.util.List; | ||
|
||
import static org.apache.commons.lang3.StringUtils.isBlank; | ||
import static org.apache.commons.lang3.StringUtils.isNotBlank; | ||
|
||
@Ignore | ||
public final class ConfluenceMarkupBuilder extends AbstractMarkupDocBuilder { | ||
|
||
private static final String FILE_EXTENSION = ".txt"; | ||
|
||
public static final MarkupLanguage CONFLUENCE = new MarkupLanguage(FILE_EXTENSION, ConfluenceMarkupBuilder.class); | ||
|
||
public ConfluenceMarkupBuilder(String lineSeparator) { | ||
super(lineSeparator); | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder documentTitle(String title) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder sectionTitleWithAnchorLevel(int level, String title, String anchor) { | ||
documentBuilder.append(newLine); | ||
documentBuilder.append(".h").append(level + 1).append(" ").append(title); | ||
if (isNotBlank(anchor)) { | ||
documentBuilder.append(" {anchor:").append(anchor).append("}"); | ||
} | ||
documentBuilder.append(newLine).append(newLine); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder block(String text, MarkupBlockStyle style, String title, MarkupAdmonition admonition) { | ||
switch (style) { | ||
case SIDEBAR: | ||
documentBuilder.append(newLine).append("{quote}").append(newLine); | ||
if (isNotBlank(title)) { | ||
documentBuilder.append(title).append(" \\\\ "); | ||
} | ||
documentBuilder.append(text); | ||
documentBuilder.append(newLine).append("{quote}").append(newLine); | ||
break; | ||
case EXAMPLE: | ||
case LITERAL: | ||
documentBuilder.append(newLine); | ||
if (isBlank(title)) { | ||
documentBuilder.append("{panel}"); | ||
} else { | ||
documentBuilder.append("{panel:title=").append(title).append("}"); | ||
} | ||
documentBuilder.append(newLine); | ||
documentBuilder.append(text); | ||
documentBuilder.append(newLine).append("{panel}").append(newLine); | ||
break; | ||
case LISTING: | ||
documentBuilder.append(newLine); | ||
if (isBlank(title)) { | ||
documentBuilder.append("{code}"); | ||
} else { | ||
documentBuilder.append("{code:title=").append(title).append("}"); | ||
} | ||
documentBuilder.append(newLine); | ||
documentBuilder.append(text); | ||
documentBuilder.append(newLine).append("{code}").append(newLine); | ||
break; | ||
case PASSTHROUGH: | ||
documentBuilder.append(newLine).append("{noformat}").append(newLine); | ||
if (isNotBlank(title)) { | ||
documentBuilder.append(title); | ||
documentBuilder.append(newLine); | ||
} | ||
documentBuilder.append(text); | ||
documentBuilder.append(newLine).append("{noformat}").append(newLine); | ||
break; | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder listing(String text, String language) { | ||
documentBuilder.append(newLine); | ||
if (isBlank(language)) { | ||
documentBuilder.append("{code}"); | ||
} else { | ||
documentBuilder.append("{code:language=").append(language).append("}"); | ||
} | ||
documentBuilder.append(newLine); | ||
documentBuilder.append(text); | ||
documentBuilder.append(newLine).append("{code}").append(newLine).append(newLine); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder boldText(String text) { | ||
documentBuilder.append("*").append(text).append("*"); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder italicText(String text) { | ||
documentBuilder.append("_").append(text).append("_"); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder unorderedList(List<String> list) { | ||
documentBuilder.append(newLine).append(newLine); | ||
list.forEach(item -> documentBuilder.append("* ").append(item).append(newLine)); | ||
documentBuilder.append(newLine); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder unorderedListItem(String item) { | ||
documentBuilder.append("* ").append(item); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder tableWithHeaderRow(List<String> rowsInPSV) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder tableWithColumnSpecs(List<MarkupTableColumn> columnSpecs, List<List<String>> cells) { | ||
documentBuilder.append(newLine); | ||
if (columnSpecs != null && !columnSpecs.isEmpty()) { | ||
documentBuilder.append("||"); | ||
columnSpecs.forEach(spec -> documentBuilder.append(escapeCellContent(spec.header)).append("||")); | ||
documentBuilder.append(newLine); | ||
} | ||
if (cells != null && !cells.isEmpty()) { | ||
cells.forEach(row -> { | ||
documentBuilder.append("|"); | ||
row.forEach(cell -> documentBuilder.append(escapeCellContent(cell)).append("|")); | ||
documentBuilder.append(newLine); | ||
}); | ||
} | ||
return this; | ||
} | ||
|
||
private String escapeCellContent(String content) { | ||
if (content == null) { | ||
return " "; | ||
} | ||
return content.replace("|", "\\|").replace(newLine, "\\\\"); | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder anchor(String anchor, String text) { | ||
documentBuilder.append("{anchor:").append(anchor).append("}"); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder crossReferenceRaw(String document, String anchor, String text) { | ||
crossReference(document, anchor, text); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder crossReference(String document, String anchor, String text) { | ||
documentBuilder.append("["); | ||
if (isNotBlank(document)) { | ||
documentBuilder.append(document); | ||
} | ||
documentBuilder.append("#").append(anchor); | ||
if (isNotBlank(text)) { | ||
documentBuilder.append("|").append(document); | ||
} | ||
documentBuilder.append("]"); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder newLine(boolean forceLineBreak) { | ||
documentBuilder.append(" \\\\ ").append(newLine); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder importMarkup(Reader markupText, int levelOffset) throws IOException { | ||
final BufferedReader reader = new BufferedReader(markupText); | ||
reader.lines().forEach(documentBuilder::append); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MarkupDocBuilder copy() { | ||
return this; | ||
} | ||
|
||
@Override | ||
public String addFileExtension(String fileName) { | ||
return fileName + FILE_EXTENSION; | ||
} | ||
} |
Oops, something went wrong.