fixed sectionTitleWithAnchor and auto-generated anchor (and fixed tests)

fixed ConfluenceBuilder : missing replaceNewLinesWithWhiteSpace on title/anchor, removed extra newLine
This commit is contained in:
Hugo de Paix de Coeur
2016-04-08 17:04:57 +02:00
parent 962d934f2c
commit c246352b59
7 changed files with 75 additions and 44 deletions

View File

@@ -88,22 +88,25 @@ public abstract class AbstractMarkupDocBuilder implements MarkupDocBuilder {
}
protected void documentTitle(Markup markup, String title) {
Validate.notBlank(title, "title must not be null");
Validate.notBlank(title, "title must not be blank");
documentBuilder.append(markup).append(replaceNewLinesWithWhiteSpace(title)).append(newLine).append(newLine);
}
protected void sectionTitleWithAnchorLevel(Markup markup, int level, String title, String anchor) {
Validate.notBlank(title, "title must not be null");
protected void sectionTitleLevel(Markup markup, int level, String title) {
Validate.notBlank(title, "title must not be blank");
Validate.inclusiveBetween(1, MAX_TITLE_LEVEL, level);
documentBuilder.append(newLine);
if (anchor != null)
anchor(replaceNewLinesWithWhiteSpace(anchor)).newLine();
documentBuilder.append(StringUtils.repeat(markup.toString(), level + 1)).append(" ").append(replaceNewLinesWithWhiteSpace(title)).append(newLine);
}
@Override
public MarkupDocBuilder sectionTitleLevel(int level, String title) {
return sectionTitleWithAnchorLevel(level, title, null);
protected void sectionTitleWithAnchorLevel(Markup markup, int level, String title, String anchor) {
Validate.notBlank(title, "title must not be blank");
Validate.inclusiveBetween(1, MAX_TITLE_LEVEL, level);
documentBuilder.append(newLine);
if (anchor == null)
anchor = title;
anchor(replaceNewLinesWithWhiteSpace(anchor)).newLine();
documentBuilder.append(StringUtils.repeat(markup.toString(), level + 1)).append(" ").append(replaceNewLinesWithWhiteSpace(title)).append(newLine);
}
@Override
@@ -188,7 +191,7 @@ public abstract class AbstractMarkupDocBuilder implements MarkupDocBuilder {
@Override
public MarkupDocBuilder textLine(String text, boolean forceLineBreak) {
Validate.notBlank(text, "text must not be null");
Validate.notBlank(text, "text must not be blank");
text(replaceNewLines(text));
newLine(forceLineBreak);
return this;
@@ -202,7 +205,7 @@ public abstract class AbstractMarkupDocBuilder implements MarkupDocBuilder {
@Override
public MarkupDocBuilder text(String text) {
Validate.notBlank(text, "text must not be null");
Validate.notBlank(text, "text must not be blank");
documentBuilder.append(replaceNewLines(text));
return this;
}
@@ -214,18 +217,18 @@ public abstract class AbstractMarkupDocBuilder implements MarkupDocBuilder {
@Override
public MarkupDocBuilder block(String text, MarkupBlockStyle style) {
Validate.notBlank(text, "text must not be null");
Validate.notBlank(text, "text must not be blank");
return block(replaceNewLines(text), style, null, null);
}
@Override
public MarkupDocBuilder listingBlock(String text) {
Validate.notBlank(text, "text must not be null");
Validate.notBlank(text, "text must not be blank");
return listingBlock(replaceNewLines(text), null);
}
protected void delimitedBlockText(Markup begin, String text, Markup end) {
Validate.notBlank(text, "text must not be null");
Validate.notBlank(text, "text must not be blank");
if (!StringUtils.isBlank(begin.toString()))
documentBuilder.append(begin).append(newLine);
documentBuilder.append(replaceNewLines(text)).append(newLine);
@@ -235,7 +238,7 @@ public abstract class AbstractMarkupDocBuilder implements MarkupDocBuilder {
}
protected void delimitedTextWithoutLineBreaks(Markup begin, String text, Markup end) {
Validate.notBlank(text, "text must not be null");
Validate.notBlank(text, "text must not be blank");
if (!StringUtils.isBlank(begin.toString()))
documentBuilder.append(begin);
documentBuilder.append(replaceNewLines(text));
@@ -257,7 +260,7 @@ public abstract class AbstractMarkupDocBuilder implements MarkupDocBuilder {
@Override
public MarkupDocBuilder literalTextLine(String text, boolean forceLineBreak) {
Validate.notBlank(text, "text must not be null");
Validate.notBlank(text, "text must not be blank");
literalText(replaceNewLines(text));
newLine(forceLineBreak);
return this;
@@ -274,7 +277,7 @@ public abstract class AbstractMarkupDocBuilder implements MarkupDocBuilder {
@Override
public MarkupDocBuilder boldTextLine(String text, boolean forceLineBreak) {
Validate.notBlank(text, "text must not be null");
Validate.notBlank(text, "text must not be blank");
boldText(replaceNewLines(text));
newLine(forceLineBreak);
return this;
@@ -302,7 +305,7 @@ public abstract class AbstractMarkupDocBuilder implements MarkupDocBuilder {
}
protected void unorderedList(Markup markup, List<String> list) {
Validate.notEmpty(list, "list must not be null");
Validate.notEmpty(list, "list must not be empty");
documentBuilder.append(newLine);
for (String listEntry : list) {
unorderedListItem(markup, listEntry);
@@ -311,13 +314,13 @@ public abstract class AbstractMarkupDocBuilder implements MarkupDocBuilder {
}
protected void unorderedListItem(Markup markup, String item) {
Validate.notBlank(item, "item must not be null");
Validate.notBlank(item, "item must not be blank");
documentBuilder.append(markup).append(item).append(newLine);
}
@Override
public MarkupDocBuilder anchor(String anchor) {
Validate.notBlank(anchor, "anchor must not be null");
Validate.notBlank(anchor, "anchor must not be blank");
return anchor(anchor, null);
}

View File

@@ -74,6 +74,12 @@ public class AsciiDocBuilder extends AbstractMarkupDocBuilder {
return this;
}
@Override
public MarkupDocBuilder sectionTitleLevel(int level, String title) {
sectionTitleLevel(AsciiDoc.TITLE, level, title);
return this;
}
@Override
public MarkupDocBuilder sectionTitleWithAnchorLevel(int level, String title, String anchor) {
sectionTitleWithAnchorLevel(AsciiDoc.TITLE, level, title, anchor);
@@ -82,7 +88,7 @@ public class AsciiDocBuilder extends AbstractMarkupDocBuilder {
@Override
public MarkupDocBuilder paragraph(String text, boolean hardbreaks) {
Validate.notBlank(text, "text must not be null");
Validate.notBlank(text, "text must not be blank");
if (hardbreaks)
documentBuilder.append("[%hardbreaks]").append(newLine);
text = text.trim();

View File

@@ -32,8 +32,7 @@ import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.apache.commons.lang3.StringUtils.defaultString;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.*;
public final class ConfluenceMarkupBuilder extends AbstractMarkupDocBuilder {
@@ -87,24 +86,36 @@ public final class ConfluenceMarkupBuilder extends AbstractMarkupDocBuilder {
@Override
public MarkupDocBuilder documentTitle(String title) {
Validate.notBlank(title, "title must not be null");
Validate.notBlank(title, "title must not be blank");
documentBuilder.append(String.format(TITLE_FORMAT, 1, title));
documentBuilder.append(newLine).append(newLine);
return this;
}
@Override
public MarkupDocBuilder sectionTitleWithAnchorLevel(int level, String title, String anchor) {
Validate.notBlank(title, "title must not be null");
public MarkupDocBuilder sectionTitleLevel(int level, String title) {
Validate.notBlank(title, "title must not be blank");
Validate.inclusiveBetween(1, MAX_TITLE_LEVEL, level);
documentBuilder.append(newLine);
documentBuilder.append(String.format(TITLE_FORMAT, level + 1, title));
if (isNotBlank(anchor)) {
documentBuilder.append(" ");
anchor(anchor);
documentBuilder.append(newLine);
}
documentBuilder.append(String.format(TITLE_FORMAT, level + 1, replaceNewLinesWithWhiteSpace(title)));
documentBuilder.append(newLine);
return this;
}
@Override
public MarkupDocBuilder sectionTitleWithAnchorLevel(int level, String title, String anchor) {
Validate.notBlank(title, "title must not be blank");
Validate.inclusiveBetween(1, MAX_TITLE_LEVEL, level);
documentBuilder.append(newLine);
documentBuilder.append(String.format(TITLE_FORMAT, level + 1, replaceNewLinesWithWhiteSpace(title)));
if (isBlank(anchor))
anchor = title;
documentBuilder.append(" ");
anchor(replaceNewLinesWithWhiteSpace(anchor));
documentBuilder.append(newLine);
return this;
@@ -112,7 +123,7 @@ public final class ConfluenceMarkupBuilder extends AbstractMarkupDocBuilder {
@Override
public MarkupDocBuilder paragraph(String text, boolean hardbreaks) {
Validate.notBlank(text, "text must not be null");
Validate.notBlank(text, "text must not be blank");
text = text.trim();
if (hardbreaks)
@@ -257,7 +268,7 @@ public final class ConfluenceMarkupBuilder extends AbstractMarkupDocBuilder {
private String formatCellContent(String cell) {
cell = replaceNewLines(cell.trim(), ConfluenceMarkup.LINE_BREAK.toString());
if (StringUtils.isBlank(cell)) {
if (isBlank(cell)) {
return " ";
}
return escapeCellPipes(cell);

View File

@@ -78,6 +78,12 @@ public class MarkdownBuilder extends AbstractMarkupDocBuilder {
return this;
}
@Override
public MarkupDocBuilder sectionTitleLevel(int level, String title) {
sectionTitleLevel(Markdown.TITLE, level, title);
return this;
}
@Override
public MarkupDocBuilder sectionTitleWithAnchorLevel(int level, String title, String anchor) {
sectionTitleWithAnchorLevel(Markdown.TITLE, level, title, anchor);
@@ -86,7 +92,7 @@ public class MarkdownBuilder extends AbstractMarkupDocBuilder {
@Override
public MarkupDocBuilder paragraph(String text, boolean hardbreaks) {
Validate.notBlank(text, "text must not be null");
Validate.notBlank(text, "text must not be blank");
text = text.trim();
if (hardbreaks)

View File

@@ -6,6 +6,7 @@
[[_level-1a]]
== Section with anchor Level 1a
[[_section_with_anchor_level_1a]]
== Section with anchor Level 1a
=== Section Level 2a
@@ -13,6 +14,7 @@
[[_level-2a]]
=== Section with anchor Level 2a
[[_section_with_anchor_level_2a]]
=== Section with anchor Level 2a
==== Section Level 3a
@@ -20,6 +22,7 @@
[[_level-3a]]
==== Section with anchor Level 3a
[[_section_with_anchor_level_3a]]
==== Section with anchor Level 3a
===== Section Level 4a
@@ -27,6 +30,7 @@
[[_level-4a]]
===== Section with anchor Level 4a
[[_section_with_anchor_level_4a]]
===== Section with anchor Level 4a
====== Section Level 5a
@@ -34,6 +38,7 @@
[[_level-5a]]
====== Section with anchor Level 5a
[[_section_with_anchor_level_5a]]
====== Section with anchor Level 5a
Paragraph with long text bla bla bla bla bla

View File

@@ -5,36 +5,31 @@ h2. Section Level 1a
h2. Section with anchor Level 1a {anchor:level-1a}
h2. Section with anchor Level 1a
h2. Section with anchor Level 1a {anchor:section_with_anchor_level_1a}
h3. Section Level 2a
h3. Section with anchor Level 2a {anchor:level-2a}
h3. Section with anchor Level 2a
h3. Section with anchor Level 2a {anchor:section_with_anchor_level_2a}
h4. Section Level 3a
h4. Section with anchor Level 3a {anchor:level-3a}
h4. Section with anchor Level 3a
h4. Section with anchor Level 3a {anchor:section_with_anchor_level_3a}
h5. Section Level 4a
h5. Section with anchor Level 4a {anchor:level-4a}
h5. Section with anchor Level 4a
h5. Section with anchor Level 4a {anchor:section_with_anchor_level_4a}
h6. Section Level 5a
h6. Section with anchor Level 5a {anchor:level-5a}
h6. Section with anchor Level 5a
h6. Section with anchor Level 5a {anchor:section_with_anchor_level_5a}
Paragraph with long text bla bla bla bla bla
Line1

View File

@@ -6,6 +6,7 @@
<a name="level-1a"></a>
## Section with anchor Level 1a
<a name="section-with-anchor-level-1a"></a>
## Section with anchor Level 1a
### Section Level 2a
@@ -13,6 +14,7 @@
<a name="level-2a"></a>
### Section with anchor Level 2a
<a name="section-with-anchor-level-2a"></a>
### Section with anchor Level 2a
#### Section Level 3a
@@ -20,6 +22,7 @@
<a name="level-3a"></a>
#### Section with anchor Level 3a
<a name="section-with-anchor-level-3a"></a>
#### Section with anchor Level 3a
##### Section Level 4a
@@ -27,6 +30,7 @@
<a name="level-4a"></a>
##### Section with anchor Level 4a
<a name="section-with-anchor-level-4a"></a>
##### Section with anchor Level 4a
###### Section Level 5a
@@ -34,6 +38,7 @@
<a name="level-5a"></a>
###### Section with anchor Level 5a
<a name="section-with-anchor-level-5a"></a>
###### Section with anchor Level 5a
Paragraph with long text bla bla bla bla bla