From: Jean-Baptiste Lievremont Date: Wed, 7 May 2014 15:09:00 +0000 (+0200) Subject: SONAR-4681 Add support for ordered lists in SQ Markdown X-Git-Tag: 4.4-RC1~1170 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=06cd07f0685459b11c9478aa729bea4a8ddbdf5f;p=sonarqube.git SONAR-4681 Add support for ordered lists in SQ Markdown --- diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlListChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlListChannel.java index 7267db63b26..c8842f0a1c8 100644 --- a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlListChannel.java +++ b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlListChannel.java @@ -23,21 +23,61 @@ import org.sonar.channel.Channel; import org.sonar.channel.CodeReader; import org.sonar.channel.RegexChannel; +/** + * Lists come in two flavors: + * + + * E.g., the input: + *
+ * * One
+ * * Two
+ * * Three
+ * 
+ * will produce: + * {@literal} + * + * Whereas the input: + *
+ * 1. One
+ * 1. Two
+ * 1. Three
+ * 
+ * will produce: + * {@literal
    }{@literal
  1. }One{@literal
  2. } + * {@literal
  3. }Two{@literal
  4. } + * {@literal
  5. }Three{@literal
  6. }{@literal
} + * + * @since 2.10.1 + */ class HtmlListChannel extends Channel { - private ListElementChannel listElement = new ListElementChannel(); + private OrderedListElementChannel orderedListElement = new OrderedListElementChannel(); + private UnorderedListElementChannel unorderedListElement = new UnorderedListElementChannel(); private EndOfLine endOfLine = new EndOfLine(); private boolean pendingListConstruction; @Override public boolean consume(CodeReader code, MarkdownOutput output) { try { - if (code.getColumnPosition() == 0 && listElement.consume(code, output)) { - while (endOfLine.consume(code, output) && listElement.consume(code, output)) { - // consume input + ListElementChannel currentChannel = null; + if (code.getColumnPosition() == 0) { + if (orderedListElement.consume(code, output)) { + currentChannel = orderedListElement; + } else if (unorderedListElement.consume(code, output)) { + currentChannel = unorderedListElement; + } + if (currentChannel != null) { + while (endOfLine.consume(code, output) && currentChannel.consume(code, output)) { + // consume input + } + output.append(""); + return true; } - output.append(""); - return true; } return false; } finally { @@ -45,16 +85,31 @@ class HtmlListChannel extends Channel { } } - private class ListElementChannel extends RegexChannel { + private class OrderedListElementChannel extends ListElementChannel { + public OrderedListElementChannel() { + super("\\d\\.", "ol"); + } + } + + private class UnorderedListElementChannel extends ListElementChannel { + public UnorderedListElementChannel() { + super("\\*", "ul"); + } + } - public ListElementChannel() { - super("\\s*+\\*\\s[^\r\n]*+"); + private abstract class ListElementChannel extends RegexChannel { + + private String listElement; + + protected ListElementChannel(String markerRegexp, String listElement) { + super("\\s*+" + markerRegexp + "\\s[^\r\n]*+"); + this.listElement = listElement; } @Override protected void consume(CharSequence token, MarkdownOutput output) { if (!pendingListConstruction) { - output.append("
    "); + output.append("<" + listElement + ">"); pendingListConstruction = true; } output.append("
  • "); @@ -64,8 +119,12 @@ class HtmlListChannel extends Channel { private int searchIndexOfFirstCharacter(CharSequence token) { for (int index = 0; index < token.length(); index++) { - if (token.charAt(index) == '*') { - while (++index
  • one
  • \r
  • two
  • \r\n
  • three
  • \n
  • \n
*five"); assertThat(Markdown.convertToHtml(" * one\r* two")).isEqualTo("
  • one
  • \r
  • two
"); + assertThat(Markdown.convertToHtml("* \r*")).isEqualTo("
  • \r
*"); + } + + @Test + public void shouldDecorateOrderedList() { + assertThat(Markdown.convertToHtml(" 1. one\r1. two\r\n1. three\n 1. \n 1.five")) + .isEqualTo("
  1. one
  2. \r
  3. two
  4. \r\n
  5. three
  6. \n
  7. \n
1.five"); + assertThat(Markdown.convertToHtml(" 1. one\r1. two")).isEqualTo("
  1. one
  2. \r
  3. two
"); + assertThat(Markdown.convertToHtml("1. \r1.")).isEqualTo("
  1. \r
1."); + } + + @Test + public void shouldDecorateMixedOrderedAndUnorderedList() { + assertThat(Markdown.convertToHtml(" 1. one\r* two\r\n1. three\n * \n 1.five")) + .isEqualTo("
  1. one
  2. \r
  • two
  • \r\n
  1. three
  2. \n
  • \n
1.five"); } @Test