From c246352b59e4135fd9ec8da1850d2c8564877b96 Mon Sep 17 00:00:00 2001 From: Hugo de Paix de Coeur Date: Fri, 8 Apr 2016 17:04:57 +0200 Subject: [PATCH] fixed sectionTitleWithAnchor and auto-generated anchor (and fixed tests) fixed ConfluenceBuilder : missing replaceNewLinesWithWhiteSpace on title/anchor, removed extra newLine --- .../internal/AbstractMarkupDocBuilder.java | 41 ++++++++++--------- .../internal/asciidoc/AsciiDocBuilder.java | 8 +++- .../ConfluenceMarkupBuilder.java | 37 +++++++++++------ .../internal/markdown/MarkdownBuilder.java | 8 +++- .../resources/expected/asciidoc/test.adoc | 5 +++ .../expected/confluenceMarkup/test.txt | 15 +++---- src/test/resources/expected/markdown/test.md | 5 +++ 7 files changed, 75 insertions(+), 44 deletions(-) diff --git a/src/main/java/io/github/swagger2markup/markup/builder/internal/AbstractMarkupDocBuilder.java b/src/main/java/io/github/swagger2markup/markup/builder/internal/AbstractMarkupDocBuilder.java index 06085f82..bf581085 100644 --- a/src/main/java/io/github/swagger2markup/markup/builder/internal/AbstractMarkupDocBuilder.java +++ b/src/main/java/io/github/swagger2markup/markup/builder/internal/AbstractMarkupDocBuilder.java @@ -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 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); } diff --git a/src/main/java/io/github/swagger2markup/markup/builder/internal/asciidoc/AsciiDocBuilder.java b/src/main/java/io/github/swagger2markup/markup/builder/internal/asciidoc/AsciiDocBuilder.java index b04e311a..5f020fcc 100644 --- a/src/main/java/io/github/swagger2markup/markup/builder/internal/asciidoc/AsciiDocBuilder.java +++ b/src/main/java/io/github/swagger2markup/markup/builder/internal/asciidoc/AsciiDocBuilder.java @@ -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(); diff --git a/src/main/java/io/github/swagger2markup/markup/builder/internal/confluenceMarkup/ConfluenceMarkupBuilder.java b/src/main/java/io/github/swagger2markup/markup/builder/internal/confluenceMarkup/ConfluenceMarkupBuilder.java index a1c3f0c7..ebb08616 100644 --- a/src/main/java/io/github/swagger2markup/markup/builder/internal/confluenceMarkup/ConfluenceMarkupBuilder.java +++ b/src/main/java/io/github/swagger2markup/markup/builder/internal/confluenceMarkup/ConfluenceMarkupBuilder.java @@ -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); diff --git a/src/main/java/io/github/swagger2markup/markup/builder/internal/markdown/MarkdownBuilder.java b/src/main/java/io/github/swagger2markup/markup/builder/internal/markdown/MarkdownBuilder.java index 6aa37581..6b5c94ba 100644 --- a/src/main/java/io/github/swagger2markup/markup/builder/internal/markdown/MarkdownBuilder.java +++ b/src/main/java/io/github/swagger2markup/markup/builder/internal/markdown/MarkdownBuilder.java @@ -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) diff --git a/src/test/resources/expected/asciidoc/test.adoc b/src/test/resources/expected/asciidoc/test.adoc index c76ff92b..c3b741eb 100644 --- a/src/test/resources/expected/asciidoc/test.adoc +++ b/src/test/resources/expected/asciidoc/test.adoc @@ -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 diff --git a/src/test/resources/expected/confluenceMarkup/test.txt b/src/test/resources/expected/confluenceMarkup/test.txt index 2cb772e4..675885b0 100644 --- a/src/test/resources/expected/confluenceMarkup/test.txt +++ b/src/test/resources/expected/confluenceMarkup/test.txt @@ -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 diff --git a/src/test/resources/expected/markdown/test.md b/src/test/resources/expected/markdown/test.md index 3ad47b82..d1215b2c 100644 --- a/src/test/resources/expected/markdown/test.md +++ b/src/test/resources/expected/markdown/test.md @@ -6,6 +6,7 @@ ## Section with anchor Level 1a + ## Section with anchor Level 1a ### Section Level 2a @@ -13,6 +14,7 @@ ### Section with anchor Level 2a + ### Section with anchor Level 2a #### Section Level 3a @@ -20,6 +22,7 @@ #### Section with anchor Level 3a + #### Section with anchor Level 3a ##### Section Level 4a @@ -27,6 +30,7 @@ ##### Section with anchor Level 4a + ##### Section with anchor Level 4a ###### Section Level 5a @@ -34,6 +38,7 @@ ###### Section with anchor Level 5a + ###### Section with anchor Level 5a Paragraph with long text bla bla bla bla bla